current position:Home>How do contracts simulate the passage of time?

How do contracts simulate the passage of time?

2022-02-03 20:51:25 Q & A of Denglian community

I have a contract , The pledged funds need to be unlocked after a period of time , How to conduct simulation test .

Take the answer 1:

Two way :

  1. One is when testing , Shorten the contract time in equal proportion , such as 1 Shorten the day to one second .
  2. Deploy the contract to ganane, ganane Provides a way to advance time evm_increaseTime.

Usage method : After deploying the contract , use truffle console Access control , If you want to move time forward by one day (86400 second ) Use the following command :

//  Adjust the time  86400 = 1 day > web3.currentProvider.send({ jsonrpc: '2.0', method: 'evm_increaseTime', params: [86400], id: new Date().getTime() }, (err, result) => {})//  Block out  > web3.currentProvider.send({ jsonrpc: '2.0', method: 'evm_mine', id: new Date().getTime() }, (err, result) => {})//  Call contract > let mc = await MyContract.deployed();> mc.MyMethod()

Reference resources This article

copyright notice
author[Q & A of Denglian community],Please bring the original link to reprint, thank you.

Random recommended