Skip to content

Commit f2a1cce

Browse files
authored
Merge pull request #464 from PolymathNetwork/CLI-improvements
[CLI] Some improvements - Fixed timetravel
2 parents b92f693 + 41e3d7b commit f2a1cce

File tree

3 files changed

+88
-26
lines changed

3 files changed

+88
-26
lines changed

CLI/commands/helpers/time.js

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,35 @@
11
const moment = require('moment');
22
const chalk = require('chalk');
33

4+
function increaseTime(duration) {
5+
const id = Date.now();
6+
7+
return new Promise((resolve, reject) => {
8+
web3.currentProvider.send(
9+
{
10+
jsonrpc: "2.0",
11+
method: "evm_increaseTime",
12+
params: [duration],
13+
id: id
14+
},
15+
err1 => {
16+
if (err1) return reject(err1);
17+
18+
web3.currentProvider.send(
19+
{
20+
jsonrpc: "2.0",
21+
method: "evm_mine",
22+
id: id + 1
23+
},
24+
(err2, res) => {
25+
return err2 ? reject(err2) : resolve(res);
26+
}
27+
);
28+
}
29+
);
30+
});
31+
}
32+
433
function jumpToTime(timestamp) {
534
const id = Date.now();
635

@@ -26,7 +55,7 @@ async function increaseTimeByDate(toTime) {
2655
if (toTime.unix() > currentTime) {
2756
await jumpToTime(toTime.unix());
2857
currentTime = (await web3.eth.getBlock('latest')).timestamp;
29-
console.log(chalk.gree(`Current datetime is ${currentTime} or ${moment.unix(currentTime).format("MM/DD/YYYY HH:mm:ss")}`));
58+
console.log(chalk.green(`Current datetime is ${currentTime} or ${moment.unix(currentTime).format("MM/DD/YYYY HH:mm:ss")}`));
3059
} else {
3160
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`));
3261
}
@@ -38,10 +67,18 @@ async function increaseTimeByDate(toTime) {
3867
}
3968
}
4069

41-
function increaseTimeByDuration(duration) {
42-
let currentTime = moment().unix();
43-
let toTime = currentTime.add(duration, "seconds");
44-
return increaseTimeByDate(toTime);
70+
async function increaseTimeByDuration(duration) {
71+
if (await web3.eth.net.getId() === 15) {
72+
if (duration > 0) {
73+
await increaseTime(duration);
74+
currentTime = (await web3.eth.getBlock('latest')).timestamp;
75+
console.log(chalk.green(`Current datetime is ${currentTime} or ${moment.unix(currentTime).format("MM/DD/YYYY HH:mm:ss")}`));
76+
} else {
77+
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`));
78+
}
79+
} else {
80+
console.log(chalk.red(`Time traveling is only possible over develpment network`));
81+
}
4582
}
4683

4784
function increaseTimeToDate(date) {
@@ -55,5 +92,3 @@ function increaseTimeToEpochDate(epochDate) {
5592
}
5693

5794
module.exports = { increaseTimeByDuration, increaseTimeToDate, increaseTimeToEpochDate };
58-
59-

CLI/commands/sto_manager.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ let tokenSymbol;
1818
////////////////////////
1919
// Artifacts
2020
let securityTokenRegistry;
21+
let moduleRegistry;
2122
let polyToken;
2223
let usdToken;
2324
let securityToken;
@@ -118,7 +119,12 @@ async function addSTOModule(stoConfig) {
118119

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

908+
let moduleRegistryAddress = await contracts.moduleRegistry();
909+
let moduleRegistryABI = abis.moduleRegistry();
910+
moduleRegistry = new web3.eth.Contract(moduleRegistryABI, moduleRegistryAddress);
911+
moduleRegistry.setProvider(web3.currentProvider);
912+
902913
let polytokenAddress = await contracts.polyToken();
903914
let polytokenABI = abis.polyToken();
904915
polyToken = new web3.eth.Contract(polytokenABI, polytokenAddress);

CLI/commands/transfer_manager.js

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const PERCENTAGE_WHITELIST_DATA_CSV = './CLI/data/Transfer/PercentageTM/whitelis
1717
let tokenSymbol;
1818
let securityToken;
1919
let securityTokenRegistry;
20+
let moduleRegistry;
2021
let currentTransferManager;
2122

2223
async function executeApp() {
@@ -48,23 +49,23 @@ async function executeApp() {
4849
console.log('Selected:', optionSelected, '\n');
4950
switch (optionSelected) {
5051
case 'Verify transfer':
52+
let verifyTotalSupply = web3.utils.fromWei(await securityToken.methods.totalSupply().call());
53+
await logTotalInvestors();
5154
let verifyTransferFrom = readlineSync.question(`Enter the sender account (${Issuer.address}): `, {
5255
limit: function (input) {
5356
return web3.utils.isAddress(input);
5457
},
5558
limitMessage: "Must be a valid address",
5659
defaultInput: Issuer.address
5760
});
58-
let verifyFromBalance = web3.utils.fromWei(await securityToken.methods.balanceOf(verifyTransferFrom).call());
59-
console.log(chalk.yellow(`Balance of ${verifyTransferFrom}: ${verifyFromBalance} ${tokenSymbol}`));
61+
await logBalance(verifyTransferFrom, verifyTotalSupply);
6062
let verifyTransferTo = readlineSync.question('Enter the receiver account: ', {
6163
limit: function (input) {
6264
return web3.utils.isAddress(input);
6365
},
6466
limitMessage: "Must be a valid address",
6567
});
66-
let verifyToBalance = web3.utils.fromWei(await securityToken.methods.balanceOf(verifyTransferTo).call());
67-
console.log(chalk.yellow(`Balance of ${verifyTransferTo}: ${verifyToBalance} ${tokenSymbol}`));
68+
await logBalance(verifyTransferTo, verifyTotalSupply);
6869
let verifyTransferAmount = readlineSync.question('Enter amount of tokens to verify: ');
6970
let isVerified = await securityToken.methods.verifyTransfer(verifyTransferFrom, verifyTransferTo, web3.utils.toWei(verifyTransferAmount), web3.utils.fromAscii("")).call();
7071
if (isVerified) {
@@ -74,25 +75,26 @@ async function executeApp() {
7475
}
7576
break;
7677
case 'Transfer':
77-
let fromBalance = web3.utils.fromWei(await securityToken.methods.balanceOf(Issuer.address).call());
78-
console.log(chalk.yellow(`Balance of ${Issuer.address}: ${fromBalance} ${tokenSymbol}`));
78+
let totalSupply = web3.utils.fromWei(await securityToken.methods.totalSupply().call());
79+
await logTotalInvestors();
80+
await logBalance(Issuer.address, totalSupply);
7981
let transferTo = readlineSync.question('Enter beneficiary of tranfer: ', {
8082
limit: function (input) {
8183
return web3.utils.isAddress(input);
8284
},
8385
limitMessage: "Must be a valid address"
8486
});
85-
let toBalance = web3.utils.fromWei(await securityToken.methods.balanceOf(transferTo).call());
86-
console.log(chalk.yellow(`Balance of ${transferTo}: ${toBalance} ${tokenSymbol}`));
87+
await logBalance(transferTo, totalSupply);
8788
let transferAmount = readlineSync.question('Enter amount of tokens to transfer: ');
8889
let isTranferVerified = await securityToken.methods.verifyTransfer(Issuer.address, transferTo, web3.utils.toWei(transferAmount), web3.utils.fromAscii("")).call();
8990
if (isTranferVerified) {
9091
let transferAction = securityToken.methods.transfer(transferTo, web3.utils.toWei(transferAmount));
9192
let receipt = await common.sendTransaction(transferAction);
9293
let event = common.getEventFromLogs(securityToken._jsonInterface, receipt.logs, 'Transfer');
9394
console.log(chalk.green(`${event.from} transferred ${web3.utils.fromWei(event.value)} ${tokenSymbol} to ${event.to} successfully!`));
94-
console.log(`Balance of ${Issuer.address} after transfer: ${web3.utils.fromWei(await securityToken.methods.balanceOf(Issuer.address).call())} ${tokenSymbol}`);
95-
console.log(`Balance of ${transferTo} after transfer: ${web3.utils.fromWei(await securityToken.methods.balanceOf(transferTo).call())} ${tokenSymbol}`);
95+
await logTotalInvestors();
96+
await logBalance(Issuer.address, totalSupply);
97+
await logBalance(transferTo, totalSupply);
9698
} else {
9799
console.log(chalk.red(`Transfer failed at verification. Please review the transfer restrictions.`));
98100
}
@@ -231,14 +233,12 @@ async function configExistingModules(tmModules) {
231233
}
232234

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

243243
let index = readlineSync.keyInSelect(options, 'Which Transfer Manager module do you want to add? ', { cancel: 'Return' });
244244
if (index != -1 && readlineSync.keyInYNStrict(`Are you sure you want to add ${options[index]} module?`)) {
@@ -1134,6 +1134,11 @@ async function setup() {
11341134
let securityTokenRegistryABI = abis.securityTokenRegistry();
11351135
securityTokenRegistry = new web3.eth.Contract(securityTokenRegistryABI, securityTokenRegistryAddress);
11361136
securityTokenRegistry.setProvider(web3.currentProvider);
1137+
1138+
let moduleRegistryAddress = await contracts.moduleRegistry();
1139+
let moduleRegistryABI = abis.moduleRegistry();
1140+
moduleRegistry = new web3.eth.Contract(moduleRegistryABI, moduleRegistryAddress);
1141+
moduleRegistry.setProvider(web3.currentProvider);
11371142
} catch (err) {
11381143
console.log(err)
11391144
console.log('\x1b[31m%s\x1b[0m', "There was a problem getting the contracts. Make sure they are deployed to the selected network.");
@@ -1171,6 +1176,17 @@ async function selectToken() {
11711176
return result;
11721177
}
11731178

1179+
async function logTotalInvestors() {
1180+
let investorsCount = await securityToken.methods.getInvestorCount().call();
1181+
console.log(chalk.yellow(`Total investors at the moment: ${investorsCount}`));
1182+
}
1183+
1184+
async function logBalance(from, totalSupply) {
1185+
let fromBalance = web3.utils.fromWei(await securityToken.methods.balanceOf(from).call());
1186+
let percentage = totalSupply != '0' ? ` - ${parseFloat(fromBalance) / parseFloat(totalSupply) * 100}% of total supply` : '';
1187+
console.log(chalk.yellow(`Balance of ${from}: ${fromBalance} ${tokenSymbol}${percentage}`));
1188+
}
1189+
11741190
module.exports = {
11751191
executeApp: async function (_tokenSymbol) {
11761192
await initialize(_tokenSymbol);

0 commit comments

Comments
 (0)