7

I am creating smart contracts with truffle and testrpc. When writing unit tests I wish to change the block time to simulate different times that smart contracts may be called. How does one set evm_increaseTime inside truffle console?

RFV
  • 633
  • 7
  • 16
  • 1
    It's you again! https://stackoverflow.com/questions/43341622/how-to-use-the-testrpc-evm-increasetime-parameter-from-truffle-console – 0xcaff Jun 15 '17 at 22:59

1 Answers1

11

You need to send JSON-RPC request to testrpc. Here is a helper function to call it from console:

truffle(development)> 
const increaseTime = addSeconds => {
    web3.currentProvider.send({
        jsonrpc: "2.0", 
        method: "evm_increaseTime", 
        params: [addSeconds], id: 0
    })
}

Now you can use it to adjust time of the next block:

truffle(development)> web3.eth.getBlock(web3.eth.blockNumber).timestamp
1500300000
truffle(development)> increaseTime(12345)
{ id: 0, jsonrpc: '2.0', result: 1500312345 }
truffle(development)> web3.eth.sendTransaction({from: web3.eth.accounts[0]})
'0x3d3fb06adbb5cb1d5e36423b8867b8f0364b451b8ee11189ae0f7a2fa593484d'
truffle(development)> web3.eth.getBlock(web3.eth.blockNumber).timestamp
1500312345
OneChillDude
  • 342
  • 1
  • 12
max taldykin
  • 2,966
  • 19
  • 27
  • 2
    This doesn't work anymore. Using truffle 5.0.30 and web3 1.2.0, I'm getting Uncaught TypeError: callback is not a function – Paul Razvan Berg Aug 07 '19 at 13:45
  • I'm also seeing this failure. – Alex Coventry Dec 02 '19 at 23:35
  • 2
    As Paul and Alex said, this answer should be updated. See this question/answer pair in order to make it work: https://ethereum.stackexchange.com/questions/67354/in-truffle5-how-to-manipulate-time-with-evm-increasetime – viarnes Jan 28 '20 at 15:00