Skip to content

[CLI] Some improvements #464

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

Merged
merged 6 commits into from
Dec 10, 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
49 changes: 42 additions & 7 deletions CLI/commands/helpers/time.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
const moment = require('moment');
const chalk = require('chalk');

function increaseTime(duration) {
const id = Date.now();

return new Promise((resolve, reject) => {
web3.currentProvider.send(
{
jsonrpc: "2.0",
method: "evm_increaseTime",
params: [duration],
id: id
},
err1 => {
if (err1) return reject(err1);

web3.currentProvider.send(
{
jsonrpc: "2.0",
method: "evm_mine",
id: id + 1
},
(err2, res) => {
return err2 ? reject(err2) : resolve(res);
}
);
}
);
});
}

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

Expand All @@ -26,7 +55,7 @@ async function increaseTimeByDate(toTime) {
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")}`));
console.log(chalk.green(`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`));
}
Expand All @@ -38,10 +67,18 @@ async function increaseTimeByDate(toTime) {
}
}

function increaseTimeByDuration(duration) {
let currentTime = moment().unix();
let toTime = currentTime.add(duration, "seconds");
return increaseTimeByDate(toTime);
async function increaseTimeByDuration(duration) {
if (await web3.eth.net.getId() === 15) {
if (duration > 0) {
await increaseTime(duration);
currentTime = (await web3.eth.getBlock('latest')).timestamp;
console.log(chalk.green(`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(`Time traveling is only possible over develpment network`));
}
}

function increaseTimeToDate(date) {
Expand All @@ -55,5 +92,3 @@ function increaseTimeToEpochDate(epochDate) {
}

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


13 changes: 12 additions & 1 deletion CLI/commands/sto_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ let tokenSymbol;
////////////////////////
// Artifacts
let securityTokenRegistry;
let moduleRegistry;
let polyToken;
let usdToken;
let securityToken;
Expand Down Expand Up @@ -118,7 +119,12 @@ async function addSTOModule(stoConfig) {

let optionSelected;
if (typeof stoConfig === 'undefined') {
let options = ['CappedSTO', 'USDTieredSTO'];
let availableModules = await moduleRegistry.methods.getModulesByTypeAndToken(gbl.constants.MODULES_TYPES.STO, securityToken.options.address).call();
let options = await Promise.all(availableModules.map(async function (m) {
let moduleFactoryABI = abis.moduleFactory();
let moduleFactory = new web3.eth.Contract(moduleFactoryABI, m);
return web3.utils.hexToUtf8(await moduleFactory.methods.name().call());
}));
let index = readlineSync.keyInSelect(options, 'What type of STO do you want?', { cancel: 'Return' });
optionSelected = index != -1 ? options[index] : 'Return';
} else {
Expand Down Expand Up @@ -899,6 +905,11 @@ async function setup() {
securityTokenRegistry = new web3.eth.Contract(securityTokenRegistryABI, securityTokenRegistryAddress);
securityTokenRegistry.setProvider(web3.currentProvider);

let moduleRegistryAddress = await contracts.moduleRegistry();
let moduleRegistryABI = abis.moduleRegistry();
moduleRegistry = new web3.eth.Contract(moduleRegistryABI, moduleRegistryAddress);
moduleRegistry.setProvider(web3.currentProvider);

let polytokenAddress = await contracts.polyToken();
let polytokenABI = abis.polyToken();
polyToken = new web3.eth.Contract(polytokenABI, polytokenAddress);
Expand Down
52 changes: 34 additions & 18 deletions CLI/commands/transfer_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const PERCENTAGE_WHITELIST_DATA_CSV = './CLI/data/Transfer/PercentageTM/whitelis
let tokenSymbol;
let securityToken;
let securityTokenRegistry;
let moduleRegistry;
let currentTransferManager;

async function executeApp() {
Expand Down Expand Up @@ -48,23 +49,23 @@ async function executeApp() {
console.log('Selected:', optionSelected, '\n');
switch (optionSelected) {
case 'Verify transfer':
let verifyTotalSupply = web3.utils.fromWei(await securityToken.methods.totalSupply().call());
await logTotalInvestors();
let verifyTransferFrom = readlineSync.question(`Enter the sender account (${Issuer.address}): `, {
limit: function (input) {
return web3.utils.isAddress(input);
},
limitMessage: "Must be a valid address",
defaultInput: Issuer.address
});
let verifyFromBalance = web3.utils.fromWei(await securityToken.methods.balanceOf(verifyTransferFrom).call());
console.log(chalk.yellow(`Balance of ${verifyTransferFrom}: ${verifyFromBalance} ${tokenSymbol}`));
await logBalance(verifyTransferFrom, verifyTotalSupply);
let verifyTransferTo = readlineSync.question('Enter the receiver account: ', {
limit: function (input) {
return web3.utils.isAddress(input);
},
limitMessage: "Must be a valid address",
});
let verifyToBalance = web3.utils.fromWei(await securityToken.methods.balanceOf(verifyTransferTo).call());
console.log(chalk.yellow(`Balance of ${verifyTransferTo}: ${verifyToBalance} ${tokenSymbol}`));
await logBalance(verifyTransferTo, verifyTotalSupply);
let verifyTransferAmount = readlineSync.question('Enter amount of tokens to verify: ');
let isVerified = await securityToken.methods.verifyTransfer(verifyTransferFrom, verifyTransferTo, web3.utils.toWei(verifyTransferAmount), web3.utils.fromAscii("")).call();
if (isVerified) {
Expand All @@ -74,25 +75,26 @@ async function executeApp() {
}
break;
case 'Transfer':
let fromBalance = web3.utils.fromWei(await securityToken.methods.balanceOf(Issuer.address).call());
console.log(chalk.yellow(`Balance of ${Issuer.address}: ${fromBalance} ${tokenSymbol}`));
let totalSupply = web3.utils.fromWei(await securityToken.methods.totalSupply().call());
await logTotalInvestors();
await logBalance(Issuer.address, totalSupply);
let transferTo = readlineSync.question('Enter beneficiary of tranfer: ', {
limit: function (input) {
return web3.utils.isAddress(input);
},
limitMessage: "Must be a valid address"
});
let toBalance = web3.utils.fromWei(await securityToken.methods.balanceOf(transferTo).call());
console.log(chalk.yellow(`Balance of ${transferTo}: ${toBalance} ${tokenSymbol}`));
await logBalance(transferTo, totalSupply);
let transferAmount = readlineSync.question('Enter amount of tokens to transfer: ');
let isTranferVerified = await securityToken.methods.verifyTransfer(Issuer.address, transferTo, web3.utils.toWei(transferAmount), web3.utils.fromAscii("")).call();
if (isTranferVerified) {
let transferAction = securityToken.methods.transfer(transferTo, web3.utils.toWei(transferAmount));
let receipt = await common.sendTransaction(transferAction);
let event = common.getEventFromLogs(securityToken._jsonInterface, receipt.logs, 'Transfer');
console.log(chalk.green(`${event.from} transferred ${web3.utils.fromWei(event.value)} ${tokenSymbol} to ${event.to} successfully!`));
console.log(`Balance of ${Issuer.address} after transfer: ${web3.utils.fromWei(await securityToken.methods.balanceOf(Issuer.address).call())} ${tokenSymbol}`);
console.log(`Balance of ${transferTo} after transfer: ${web3.utils.fromWei(await securityToken.methods.balanceOf(transferTo).call())} ${tokenSymbol}`);
await logTotalInvestors();
await logBalance(Issuer.address, totalSupply);
await logBalance(transferTo, totalSupply);
} else {
console.log(chalk.red(`Transfer failed at verification. Please review the transfer restrictions.`));
}
Expand Down Expand Up @@ -231,14 +233,12 @@ async function configExistingModules(tmModules) {
}

async function addTransferManagerModule() {
let options = [
'GeneralTransferManager',
'ManualApprovalTransferManager',
'CountTransferManager',
'PercentageTransferManager',
//'SingleTradeVolumeRestrictionTM',
//'LookupVolumeRestrictionTM'*/
];
let availableModules = await moduleRegistry.methods.getModulesByTypeAndToken(gbl.constants.MODULES_TYPES.TRANSFER, securityToken.options.address).call();
let options = await Promise.all(availableModules.map(async function (m) {
let moduleFactoryABI = abis.moduleFactory();
let moduleFactory = new web3.eth.Contract(moduleFactoryABI, m);
return web3.utils.hexToUtf8(await moduleFactory.methods.name().call());
}));

let index = readlineSync.keyInSelect(options, 'Which Transfer Manager module do you want to add? ', { cancel: 'Return' });
if (index != -1 && readlineSync.keyInYNStrict(`Are you sure you want to add ${options[index]} module?`)) {
Expand Down Expand Up @@ -1134,6 +1134,11 @@ async function setup() {
let securityTokenRegistryABI = abis.securityTokenRegistry();
securityTokenRegistry = new web3.eth.Contract(securityTokenRegistryABI, securityTokenRegistryAddress);
securityTokenRegistry.setProvider(web3.currentProvider);

let moduleRegistryAddress = await contracts.moduleRegistry();
let moduleRegistryABI = abis.moduleRegistry();
moduleRegistry = new web3.eth.Contract(moduleRegistryABI, moduleRegistryAddress);
moduleRegistry.setProvider(web3.currentProvider);
} catch (err) {
console.log(err)
console.log('\x1b[31m%s\x1b[0m', "There was a problem getting the contracts. Make sure they are deployed to the selected network.");
Expand Down Expand Up @@ -1171,6 +1176,17 @@ async function selectToken() {
return result;
}

async function logTotalInvestors() {
let investorsCount = await securityToken.methods.getInvestorCount().call();
console.log(chalk.yellow(`Total investors at the moment: ${investorsCount}`));
}

async function logBalance(from, totalSupply) {
let fromBalance = web3.utils.fromWei(await securityToken.methods.balanceOf(from).call());
let percentage = totalSupply != '0' ? ` - ${parseFloat(fromBalance) / parseFloat(totalSupply) * 100}% of total supply` : '';
console.log(chalk.yellow(`Balance of ${from}: ${fromBalance} ${tokenSymbol}${percentage}`));
}

module.exports = {
executeApp: async function (_tokenSymbol) {
await initialize(_tokenSymbol);
Expand Down