Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLI] Time travel on development network #461

Merged
merged 3 commits into from
Dec 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions CLI/commands/helpers/time.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const moment = require('moment');
const chalk = require('chalk');

function jumpToTime(timestamp) {
const id = Date.now();

return new Promise((resolve, reject) => {
web3.currentProvider.send(
{
jsonrpc: "2.0",
method: "evm_mine",
params: [timestamp],
id: id
},
(err, res) => {
return err ? reject(err) : resolve(res);
}
);
});
}

async function increaseTimeByDate(toTime) {
if (await web3.eth.net.getId() === 15) {
if (toTime.isValid()) {
let currentTime = (await web3.eth.getBlock('latest')).timestamp;
if (toTime.unix() > currentTime) {
await jumpToTime(toTime.unix());
currentTime = (await web3.eth.getBlock('latest')).timestamp;
console.log(chalk.gree(`Current datetime is ${currentTime} or ${moment.unix(currentTime).format("MM/DD/YYYY HH:mm:ss")}`));
} else {
console.log(chalk.red(`It is not possible to go back in time. Please try again with a time in the future to travel to`));
}
} else {
console.log(chalk.red(`Date format is not valid. Please use a valid Unix epoch time`));
}
} else {
console.log(chalk.red(`Time traveling is only possible over develpment network`));
}
}

function increaseTimeByDuration(duration) {
let currentTime = moment().unix();
let toTime = currentTime.add(duration, "seconds");
return increaseTimeByDate(toTime);
}

function increaseTimeToDate(date) {
var toDate = moment(date, ['MM-DD-YYYY', 'MM-DD-YYYY HH:mm:ss']);
return increaseTimeByDate(toDate);
}

function increaseTimeToEpochDate(epochDate) {
var toTime = moment.unix(epochDate);
return increaseTimeByDate(toTime);
}

module.exports = { increaseTimeByDuration, increaseTimeToDate, increaseTimeToEpochDate };


48 changes: 34 additions & 14 deletions CLI/polymath-cli.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
#!/usr/bin/env node

var faucet = require('./commands/faucet');
var investor_portal = require('./commands/investor_portal');
var token_manager = require('./commands/token_manager');
var st20generator = require('./commands/ST20Generator');
var sto_manager = require('./commands/sto_manager');
var transfer = require('./commands/transfer');
var transfer_ownership = require('./commands/transfer_ownership');
var dividends_manager = require('./commands/dividends_manager');
var transfer_manager = require('./commands/transfer_manager');
var contract_manager = require('./commands/contract_manager');
var strMigrator = require('./commands/strMigrator');
var permission_manager = require('./commands/permission_manager');
var program = require('commander');
var gbl = require('./commands/common/global');
const faucet = require('./commands/faucet');
const investor_portal = require('./commands/investor_portal');
const token_manager = require('./commands/token_manager');
const st20generator = require('./commands/ST20Generator');
const sto_manager = require('./commands/sto_manager');
const transfer = require('./commands/transfer');
const transfer_ownership = require('./commands/transfer_ownership');
const dividends_manager = require('./commands/dividends_manager');
const transfer_manager = require('./commands/transfer_manager');
const contract_manager = require('./commands/contract_manager');
const strMigrator = require('./commands/strMigrator');
const permission_manager = require('./commands/permission_manager');
const time = require('./commands/helpers/time')
const gbl = require('./commands/common/global');
const program = require('commander');
const moment = require('moment');
const yaml = require('js-yaml');
const fs = require('fs');

Expand Down Expand Up @@ -151,6 +153,24 @@ program
await permission_manager.executeApp();
});

program
.command('time_travel')
.alias('tt')
.option('-p, --period <seconds>', 'Period of time in seconds to increase')
.option('-d, --toDate <date>', 'Human readable date ("MM/DD/YY [HH:mm:ss]") to travel to')
.option('-e, --toEpochTime <epochTime>', 'Unix Epoch time to travel to')
.description('Increases time on EVM according to given value.')
.action(async function (cmd) {
await gbl.initialize(program.remoteNode);
if (cmd.period) {
await time.increaseTimeByDuration(parseInt(cmd.period));
} else if (cmd.toDate) {
await time.increaseTimeToDate(cmd.toDate);
} else if (cmd.toEpochTime) {
await time.increaseTimeToEpochDate(cmd.toEpochTime);
}
});

program.parse(process.argv);

if (typeof program.commands.length == 0) {
Expand Down