automationchainlink

How to use UpKeep Automation Payable function In Chainlink


I want to send native ETH to another smart contract address using a function. But when I try to send using payable(toaddress).send function or call value function it throws error. And say that I have to use use Payable function to send to another address.

`solidity

function performUpkeep(bytes calldata /* performData */) external payable override {
    //call
      (bool sent, ) = forgreenAdd.call{value: 10000000000}("");
}

`

image

I have tried with some functions with payable. All of them worked perfectly. But when I try to use payable for performUpkeep. It does not work.


Solution

  • The contract that you should automate is the one that is going to send the money not the one that is going to receive, so think of that I create 2 possibilities that you could do:

    Receiver.sol

    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.9;
    
    contract Receiver {
        receive() external payable  { 
        }
    
        function giveMeMoney() external payable {
        }
    }
    

    Sender.sol (Automate this contract using Chainlink)

    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.9;
    
    interface IReceiver {
        function giveMeMoney() external payable;
    }
    
    contract Sender {
        address receiverAddress;
        IReceiver receiverContract;
    
        constructor(address _receiverAddress) {
            receiverAddress = _receiverAddress;
            receiverContract = IReceiver(_receiverAddress);
        }
    
        receive() external payable  { 
        }
    
        function sendEth(uint256 amount) public {
            if(address(this).balance > amount) {
                payable(receiverAddress).transfer(amount);
            }
        }
    
        function sendEthWithMethod(uint256 amount) public {
            receiverContract.giveMeMoney{value: amount}();
        }
    }