Oriok
Oriok

Reputation: 55

Solidity Chainlink keepers

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?

Upvotes: 0

Views: 88

Answers (0)

Related Questions