2

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?

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Owans
  • 1,037
  • 5
  • 14
  • 1
    Please visit the [help], take the [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output using the `[<>]` snippet editor. – mplungjan Jan 12 '21 at 08:55

1 Answers1

5

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));
          });
    }); 
}