How to advance block number when developing on RSK Regtest?

Viewed 133

I have a smart contract that uses block number, and I need to increment the block number without actually waiting for time to pass.

Is this possible when running an RSK node in Regtest? How can I do this with Javascript?

1 Answers

In Regtest, yes it is indeed possible: Use the evm_mine JSON-RPC method to mine blocks.

const asyncMine = async () => {
    return new Promise((resolve, reject) => {
        web3.currentProvider.send({
            jsonrpc: "2.0",
            method: "evm_mine",
            id: new Date().getTime()
            }, (error, result) => {
                if (error) {
                    return reject(error);
                }
                return resolve(result);
            });
    });
};

Note that this is consistent with the approach used in Ethereum developer tools, e.g. Ganache. Alternatively, use evm_increaseTime to increase time of the block:

function increaseTimestamp(web3, increase) {
    return new Promise((resolve, reject) => {
        web3.currentProvider.send({
            method: "evm_increaseTime",
            params: [increase],
            jsonrpc: "2.0",
            id: new Date().getTime()
          }, (error, result) => {
            if (error) {
                return reject(error);
            }
            return asyncMine().then( ()=> resolve(result));
          });
    }); 
}
Related