Skip to content

Instantly share code, notes, and snippets.

@danielyaa5
Created April 28, 2017 06:12
Show Gist options
  • Save danielyaa5/d138148de74271ec471ec706f0413520 to your computer and use it in GitHub Desktop.
Save danielyaa5/d138148de74271ec471ec706f0413520 to your computer and use it in GitHub Desktop.
testRpc.increaseTime = function(secondsToJump, cb) {
function send(method, params, callback) {
if (typeof params == "function") {
callback = params;
params = [];
}
provider.sendAsync({
jsonrpc: "2.0",
method: method,
params: params || [],
id: new Date().getTime()
}, callback);
}
var timestampBeforeJump;
web3.eth.getBlock('latest', function(err, block){
if(err) return cb(err)
timestampBeforeJump = block.timestamp
send("evm_increaseTime", [secondsToJump], function(err, result) {
if (err) return cb(err);
// Mine a block so new time is recorded.
send("evm_mine", function(err, result) {
if (err) return cb(err);
web3.eth.getBlock('latest', function(err, block){
if(err) return cb(err)
console.log('block', block.timestamp)
var secondsJumped = block.timestamp - timestampBeforeJump;
// Somehow it jumps an extra 18 seconds, ish, when run inside the whole
// test suite. It might have something to do with when the before block
// runs and when the test runs. Likely the last block didn't occur for
// awhile.
console.log({secondsToJump, secondsJumped});
if(secondsJumped < secondsToJump) cb(Error('Testrpc time jump failed'));
cb()
})
})
})
})
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment