Skip to content

Instantly share code, notes, and snippets.

@bitcoinbrisbane
Forked from AndyWatt83/truffleTestHelper.js
Last active October 26, 2018 11:35
Show Gist options
  • Save bitcoinbrisbane/63d89c1d381609d919096f066890231b to your computer and use it in GitHub Desktop.
Save bitcoinbrisbane/63d89c1d381609d919096f066890231b 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.
advanceTimeAndBlock = async (time) => {
await advanceTime(time);
await advanceBlock();
return Promise.resolve(web3.eth.getBlock('latest'));
}
advanceTime = (time) => {
return new Promise((resolve, reject) => {
web3.currentProvider.send({
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.send({
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