Skip to content

Instantly share code, notes, and snippets.

@AndyWatt83
Created July 21, 2018 23:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save AndyWatt83/dae87c8c2c4bf5a6096d1950d0b8964e to your computer and use it in GitHub Desktop.
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.
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