-
-
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.
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.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