Created
July 21, 2018 23:53
-
-
Save AndyWatt83/dae87c8c2c4bf5a6096d1950d0b8964e to your computer and use it in GitHub Desktop.
A helper file with a couple of functions for advancing the blockchain in time, and blocks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
advanceTimeAndBlock = async (time) => { | |
await advanceTime(time); | |
await advanceBlock(); | |
return Promise.resolve(web3.eth.getBlock('latest')); | |
} | |
advanceTime = (time) => { | |
return new Promise((resolve, reject) => { | |
web3.currentProvider.sendAsync({ | |
jsonrpc: "2.0", | |
method: "evm_increaseTime", | |
params: [time], | |
id: new Date().getTime() | |
}, (err, result) => { | |
if (err) { return reject(err); } | |
return resolve(result); | |
}); | |
}); | |
} | |
advanceBlock = () => { | |
return new Promise((resolve, reject) => { | |
web3.currentProvider.sendAsync({ | |
jsonrpc: "2.0", | |
method: "evm_mine", | |
id: new Date().getTime() | |
}, (err, result) => { | |
if (err) { return reject(err); } | |
const newBlockHash = web3.eth.getBlock('latest').hash; | |
return resolve(newBlockHash) | |
}); | |
}); | |
} | |
module.exports = { | |
advanceTime, | |
advanceBlock, | |
advanceTimeAndBlock | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment