0

I'm trying to integrate Pricefeeds in the chain keepers to automate a sell order when the price goes under certain value. Separately both work but when adding the Price feed function inside the checkUpkeep() it doesn't trigger the performUpkeep().

Does checkUpkeep() function can call the priceFeed function or is not even praepared for this?

how should I implement it then?

Here is the code:

function checkUpkeep(bytes memory /* checkData */) public view override returns (//,bytes memory value
        bool upkeepNeeded, 
        bytes memory num
        ){

        uint256 EthPrice = 0;
        // uint256 i = 2;
        EthPrice = getPrice();
        num = abi.encodePacked(EthPrice);
        if (EthPrice > 0){
            upkeepNeeded = true;
        }

        return (upkeepNeeded, num);//, value
    }

    function performUpkeep(bytes calldata num) external override {//, bytes calldata value
        (bool upkeepNeeded, ) = checkUpkeep("");
        
        if (!upkeepNeeded) {
            revert Order__UpkeepNotNeeded(
                address(this).balance,
                s_Wallets.length
            );
        }
        //Byte conversion to uint
        uint256 number;
        number = abi.decode(num, (uint256));

        // for(uint i=0;i<num.length;i++){
        //     number = number + uint(uint8(num[i]))*(2**(8*(num.length-(i+1))));
        // }
        s_nombre = number;
    }
Oriok
  • 63
  • 7
  • 1
    Hi Oriok, in your last post, I noticed you are using Rinkeby testnet. Can you try the same code with Chainlink keepers and price feed in Goerli? – Frank Kong Sep 16 '22 at 07:42
  • Yes, that's true realized few hours ago. Just integrated to Goerli and Keepers work but when integrating Pricefeeds it stop working. I'm trying to solve this now. When I call PriceFeed in a separate function it works normally so I don't know. Also I'm using Goerli address for priceFeed provided by the ChainLink doc. – Oriok Sep 16 '22 at 09:48
  • I use the same code as you in `checkUpKeep` in my smart contract and the `performUpKeep` can be called by Chainlink keepers. Just one difference, why you use uint256 for price data? Price cannot be a negative number. – Frank Kong Sep 16 '22 at 14:50
  • When you revalidate conditions in your `performUpKeep`, it seems that you are calling function incorrectly in `(bool upkeepNeeded, ) = checkUpkeep("")`, the datatype is string while the bytes are required for `checkUpKeep()` – Frank Kong Sep 16 '22 at 15:14
  • I'm using Uint256 for unsigned number so only positive numbers. I think I don't understand what is your point on this. Also `(bool upkeepNeeded, ) = checkUpkeep("")` in the patrick collins tutorial it says ("") calls the function without sending data. Anyway Frank the code finally worked thank you very much for your help!! – Oriok Sep 16 '22 at 17:55
  • yw, what did you do to make the code work? – Frank Kong Sep 17 '22 at 09:13
  • I was deploying the contract and keepers with an already PerformUpkeep execution situation. I think this was the mistake, once I deployed and the condition to execute the performUpkeep was reached then everythink started working. Also had some sign mistakes in the code that make me expect some different outcome. I uploaded the code in github: https://github.com/obc92/MarketOrder. Now i want to implement a uniswapV3 sell function inside the performUpkeep so the code sells once a Stop loss is reached. – Oriok Sep 18 '22 at 12:51

0 Answers0