0

I have deployed a contract on the Mumbai Testnet, and it includes a function called "transfer". I would like to call this function automatically on every new block mined. Essentially, I want to implement this functionality in a test script. How can I achieve this?

Here are the details:

  1. The contract is already deployed on the Mumbai Testnet.
  2. The contract includes a function called "transfer". I want to call the "transfer"
  3. function on every new block mined.

I need to implement this functionality in a test script.

Could you please provide guidance on how to accomplish this? Any code examples or step-by-step instructions would be greatly appreciated. Thank you!

2 Answers 2

0

In a blockchain, the block author has the right to include a transaction or purposefully exclude it. Unless it is programmed in the rules that a certain operation should happen on every block. But if you want to do it on a public chain then it's not practical. Though you can keep shooting transactions but still no 100% guarantee you'll be included in every block.

You have to accept that you will miss some blocks. E.g. if your first transaction was in block 1000 and then next few block authors didn't purposefully include your tx and it missed some. Next block author picked it so it's now in 1005. Now, the question is: can you now do all the work required for previous missed blocks + current one i.e. 1001, 1002, 1003, 1004, 1005?

There are certain smart contracts that reward tokens every block (e.g. MasterChef). It sounds like they require a transaction every block to rewards their users. However, the mechanism they have implemented is novel and it does not require a transaction every block to achieve it. Whenever their users withdraw their reward, it calculates the rewards for last missed blocks and allocates it and finally process the user rewards.

If your mechanism requires tx in every block and there's no way to redesign it to account for missed blocks, you have to consider modifying the blockchain rules (which means a different chain).

2
  • I only gave a example. i wants to call a function only on every new block. Jun 29, 2023 at 10:59
  • In the OP it's mentioned you want to call the transfer function which is usually state modifying and requires a transaction. Is your goal to call a view method on every block? Jun 29, 2023 at 11:57
0

you can use provider.on from ethers.js to subscribe to event when a new block is produced and then do whatever you are trying to do

below is the sample code to help you out

In this code I have tried to print the totalsupply of USDT everytime a new block is published/produced

const { ethers } = require('ethers');

async function fetchTotalSupply() {
    // Connect to an Ethereum node
    const provider = new ethers.JsonRpcProvider('https://eth-mainnet.g.alchemy.com/v2/your-url');

    // USDT contract address
    const usdtContractAddress = '0xdac17f958d2ee523a2206206994597c13d831ec7';

    // USDT contract ABI (Application Binary Interface)
    const usdtContractABI = [
        {
            "constant": true,
            "inputs": [],
            "name": "totalSupply",
            "outputs": [
                {
                    "name": "",
                    "type": "uint256"
                }
            ],
            "payable": false,
            "stateMutability": "view",
            "type": "function"
        }
    ];


    // Create an instance of the USDT contract
    const usdtContract = new ethers.Contract(usdtContractAddress, usdtContractABI, provider);

    // Get the total supply of USDT
    let totalSupply = await usdtContract.totalSupply();
    console.log(`Total supply of USDT: ${totalSupply}`);

    // Listen for new blocks
    provider.on('block', async (blockNumber) => {
        // Get the total supply of USDT
        totalSupply = await usdtContract.totalSupply();
        console.log(`New block produced - Total supply of USDT: ${totalSupply}`);
    });
}

fetchTotalSupply();
4
  • though it might not be possible that the call will be included in every block as , it is up to the block producer to pick the transactions, you can try to increase the tip but you cannot be certain that it will be included in every block Jun 29, 2023 at 11:02
  • its not about the include in block. i want to call the function only on the newly mined blocks. Jun 29, 2023 at 11:06
  • Then the above approach might work I haven't tested it personally but it should work as far as I know Jun 29, 2023 at 11:11
  • @Vijayrajpanchal updated the example to a more relevant one for your convenience Jun 29, 2023 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.