2

I am using Remix to write a smart contract. One of the functions checks the block number before paying out a user:

contract MyContract {
  uint lastBlockNumber;

  // ...

  function setLastBlockNumber(uint number) {
    lastBlockNumber = number;
  }

  function claim() public {
    if (block.number >= lastBlockNumber + 10) {
      msg.sender.transfer(this.balance);
    }
  }
}

I don't see a way to set the block number in Remix so that I can test that if 10 blocks have passed it will actually pay out the balance. Is this possible, and if so how?

(Background: I'm coming to Solidity from JavaScript. I have noticed a block number field in the Debugger tab, but as far as I can tell that's a way of selecting a transaction to debug. I don't see anything in the Remix documentation about incrementing or setting the block number.)

  • I don't think you can do this with the JavaScript VM embedded in Remix, but you can do it with ganache. – smarx Mar 5 '18 at 4:34
2

I wouldn`t use Remix as a way of actual development. Instead, try using truffle which has ganache (testrpc) integrated. To check the blocknumber however use block.number. Which can be found: http://solidity.readthedocs.io/en/develop/units-and-global-variables.html.

0

truffle and ganache enable you to move forward in block creation.

in your it test block use :

await increaseTime(your duration in time not in block);

of course ganache accept specifing the block time creation exp

ganache-cli -b 3 //3 seconds

then define the increaseTime function as follow :

module.exports.increaseTime = function increaseTime(duration) {
  const id = Date.now();

  return new Promise((resolve, reject) => {
    web3.currentProvider.sendAsync(
      {
        jsonrpc: "2.0",
        method: "evm_increaseTime",
        params: [duration],
        id: id
      },
      err1 => {
        if (err1) return reject(err1);

        web3.currentProvider.sendAsync(
          {
            jsonrpc: "2.0",
            method: "evm_mine",
            id: id + 1
          },
          (err2, res) => {
            return err2 ? reject(err2) : resolve(res);
          });
      });
  });
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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