0

I'm integrating a code where a ChainLink pricefeed is called inside a checkUpkeep() function transferring data to a performUpkeep(). The code checks if a Stop Loss price previously registered and refered to a wallet is reached and then calls the performUpkeep() to modify the wallet data in a mapping and pull the wallet out from the list (Sell the token to a DEX pending for implementation).

This is the code, the pricefeed works and is called in EthPrice = getPrice(); but the data transferred between keepers functions using code and decode functions are not working and I don't know why.

function checkUpkeep(bytes memory /* checkData */) public view override returns (//,bytes memory value
        bool upkeepNeeded, 
        bytes memory num
        ){
        
        bool sellTime; 
        bool Quant;
        uint256 EthPrice;
        // bytes memory Wallet;
        // bytes memory num; 

        EthPrice = getPrice();

        address[] memory id = new address[](s_Wallets.length);

        for (uint i = 0; i < s_Wallets.length; i++) {           //Search in loop which Stop should be triggered
          id[i] = s_Wallets[i];
          Dades memory Data = s_Registre[id[i]];
          uint256 SL = Data.Stop;
          uint256 Q = Data.QuantityETH;
          sellTime = (SL <= EthPrice); 
          Quant = (Q > 0);
          if (sellTime && Quant){
            num = abi.encode(i);
            // Wallet = abi.encode(id[i]);
            //value = abi.encodePacked(Q);
            upkeepNeeded = (sellTime && Quant);
            break;
          }
          //upkeepNeeded = (sellTime && Quant); //All conditions must be True
        }  
        //upkeepNeeded = true;
        return (upkeepNeeded, num);//, value
    }

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

        // RESET DATA FROM WALLET 
        // Reseteja les dades
        Dades storage dades = s_Registre[s_Wallets[number]];
        dades.QuantityETH = 0;
        dades.Stop = 0;
        //Delets wallet from the list
        s_Wallets = Remove(number);
    }

Also when testing with js sends me this error and I don't know why when executing the performupkeep. await MarketOrder.performUpkeep([])

Error: Transaction reverted and Hardhat couldn't infer the reason.
      at MarketOrder.performUpkeep (contracts/MarketOrder.sol:176)
      at MarketOrder.performUpkeep (contracts/MarketOrder.sol:176)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at runNextTicks (node:internal/process/task_queues:65:3)
      at listOnTimeout (node:internal/timers:528:9)
      at processTimers (node:internal/timers:502:7)
      at HardhatNode._mineBlockWithPendingTxs (node_modules/hardhat/src/internal/hardhat-network/provider/node.ts:1773:23)
      at HardhatNode.mineBlock (node_modules/hardhat/src/internal/hardhat-network/provider/node.ts:466:16)
      at EthModule._sendTransactionAndReturnHash (node_modules/hardhat/src/internal/hardhat-network/provider/modules/eth.ts:1504:18)

Does anyone have any advice on what it is wrong in the code?

Oriok
  • 63
  • 7
  • Hi Oriok, codes look good, can you add the failed transaction hash in your question? You said "keepers functions using code and decode functions are not working", I am wondering how they are not working? The performUpkeep does not called by keepers? Or the number is not correct? – Frank Kong Sep 14 '22 at 09:35
  • There is no failed tx, the main problem is that the num varible num = abi.encode(i) encoded in the checkUpkeep() is not decoding the value correctly to the performUpkeep() and I don't understand why. When trying the same code with normal functions, not using keepers, the code works correctly. – Oriok Sep 14 '22 at 11:30
  • Can you check add your keepers ID in your question? The encoded bytes data can be found in the event log on etherscan so we can check if the encoded data is correct or not. – Frank Kong Sep 14 '22 at 12:54
  • I deployed in the rinkeby testnet Keepers ID: 27678792749027342351926112223550934568668022026296532557639271165604649030468 Registry add: 0x02777053d6764996e594c3E88AF1D58D5363a2e6 – Oriok Sep 14 '22 at 13:18
  • How can I check this with the keeper ID? I haven't added events in the contract yet. Thank you Frank – Oriok Sep 14 '22 at 13:47
  • Keepers node calls function `performUpkeep` in contract `KeeperRegistry` and the function calls function `_performUpkeepWithParams` in order to call function `performUpKeep` in user contract. In function `_performUpkeepWithParams`, there is an event `emit UpkeepPerformed(params.id, success, params.from, payment, params.performData);`, so you can check if your upkeep run successfully through this event. You can check the contract KeeperRegistry at https://rinkeby.etherscan.io/address/0x02777053d6764996e594c3e88af1d58d5363a2e6#code – Frank Kong Sep 15 '22 at 00:29
  • 1
    I check your upkeep at https://keepers.chain.link/rinkeby/27678792749027342351926112223550934568668022026296532557639271165604649030468, it seems the upkeep does not call performUpkeep at all, so I guess `upkeepNeeded` in your `checkUpkeep` function does not turn to true as you expect. – Frank Kong Sep 15 '22 at 00:35
  • I really don't know why this is not working, during testing upkeepNeeded it's activated as true but check data is not transmitted properly and in the rinkeby network nothing works. – Oriok Sep 15 '22 at 20:56
  • Thanks for your answer Frank, simplifyed the code in this other thread [https://stackoverflow.com/questions/73730943/solidity-keepers-data-transfer] to make it easier to answer but still is not working. – Oriok Sep 15 '22 at 20:57
  • Hi Oriok, the problem is not the data transferred properly though Chainlink Keepers. The problem is the Keeper cannot get the `needUpKeep` returned as true when calling `checkUpKeep`. And your link to new question is broken. – Frank Kong Sep 16 '22 at 02:44
  • You were right here – Oriok Sep 16 '22 at 09:50

0 Answers0