blockchainsolidityremixopenzeppelin

How to modify PaymentSplitter.sol to autopay?


I've done little tests with the paymentSplitter code from OpenZeppelin and I don't seem to find a proper way to make it pay automatically. Gotta say that I'm a rookie at this and probably theres a stupid thing I'm missing

The PaymentSplitter contract uses the function release() so the wallets can pay the gas fee and claim their payment, but this is not worth for me as I want the received Ether to go to liquidity on 2 other coins-- And call a function right after

So my doubts may be pretty dumb but, is there a way to use the contract balance for gas fee? Then, create an event that executes the release function when a deposit is made?

Since I've read lots of documentation and I'm still nowhere near, I'd love some simple examples!

EDIT:

Today I found this video: https://www.youtube.com/watch?v=IVq3gR2L5Iw

This should work properly right? Is there any tip on setting the right gas price for 3 transactions?


Solution

  • is there a way to use the contract balance for gas fee?

    No. The gas fee is always paid by the transaction sender. You need a private key to the sender address to be able to send a transaction. And the contract address private key is unknown.


    You can create an off-chain app (in JS, Python, etc.), that will send a transaction to the PaymentSplitter in predefined situations (cron, reaction to some other event, ...). Example using web3js:

    const Web3 = require("web3");
    const web3 = new Web3(providerUrl);
    web3.eth.accounts.wallet.add(senderPrivateKey);
    const contract = new web3.eth.Contract(abiJson, contractAddress);
    
    const executeRelease = async () => {
        await contract.methods.release(contractFunctionParams).send({
            from: senderAddress
        });
    }
    
    // invoke when needed
    executeRelease();
    

    Or you can make use of already existing implementations such as Chainlink Cron Jobs that will execute the transaction for you (for a payment made in LINK, token of the Chainlink service).