From 265ef4a7cddae71c0bee68eabb640b397168cfb7 Mon Sep 17 00:00:00 2001 From: Victor Date: Thu, 6 Dec 2018 18:40:33 -0300 Subject: [PATCH 1/2] Time travel on development network --- CLI/commands/helpers/time.js | 59 ++++++++++++++++++++++++++++++++++++ CLI/polymath-cli.js | 48 ++++++++++++++++++++--------- 2 files changed, 93 insertions(+), 14 deletions(-) create mode 100644 CLI/commands/helpers/time.js diff --git a/CLI/commands/helpers/time.js b/CLI/commands/helpers/time.js new file mode 100644 index 000000000..3c5e2ce3c --- /dev/null +++ b/CLI/commands/helpers/time.js @@ -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`)); + } +} + +async function increaseTimeByDuration(duration) { + let blockTimestamp = moment.unix((await web3.eth.getBlock('latest')).timestamp); + let toTime = blockTimestamp.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 }; + + diff --git a/CLI/polymath-cli.js b/CLI/polymath-cli.js index 2ce101a4d..1a423385b 100644 --- a/CLI/polymath-cli.js +++ b/CLI/polymath-cli.js @@ -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'); @@ -151,6 +153,24 @@ program await permission_manager.executeApp(); }); +program + .command('time_travel') + .alias('tt') + .option('-p, --period ', 'Period of time in seconds to increase') + .option('-d, --toDate ', 'Human readable date ("MM/DD/YY [HH:mm:ss]") to travel to') + .option('-e, --toEpochTime ', '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) { From c9fae08b6e276a15d1a3c5c8e2b51892a3fe7747 Mon Sep 17 00:00:00 2001 From: Victor Date: Thu, 6 Dec 2018 18:49:41 -0300 Subject: [PATCH 2/2] Fix jump a period of time --- CLI/commands/helpers/time.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CLI/commands/helpers/time.js b/CLI/commands/helpers/time.js index 3c5e2ce3c..dd244d04e 100644 --- a/CLI/commands/helpers/time.js +++ b/CLI/commands/helpers/time.js @@ -38,9 +38,9 @@ async function increaseTimeByDate(toTime) { } } -async function increaseTimeByDuration(duration) { - let blockTimestamp = moment.unix((await web3.eth.getBlock('latest')).timestamp); - let toTime = blockTimestamp.add(duration, "seconds"); +function increaseTimeByDuration(duration) { + let currentTime = moment().unix(); + let toTime = currentTime.add(duration, "seconds"); return increaseTimeByDate(toTime); }