From 72cfbc4580f3f1c3896e0555c0047ff806ba2aae Mon Sep 17 00:00:00 2001 From: Joseph C Wang Date: Tue, 7 May 2019 02:23:10 +0800 Subject: [PATCH] combine modify white list commands --- CLI/commands/common/common_functions.js | 128 +++++++++++++++--------- CLI/commands/token_manager.js | 22 +--- CLI/commands/transfer_manager.js | 21 +--- 3 files changed, 89 insertions(+), 82 deletions(-) diff --git a/CLI/commands/common/common_functions.js b/CLI/commands/common/common_functions.js index 40665d514..e549271f3 100644 --- a/CLI/commands/common/common_functions.js +++ b/CLI/commands/common/common_functions.js @@ -2,6 +2,7 @@ const chalk = require('chalk'); const Tx = require('ethereumjs-tx'); const permissionsList = require('./permissions_list'); const abis = require('../helpers/contract_abis'); +const readlineSync = require('readline-sync'); function connect(abi, address) { contractRegistry = new web3.eth.Contract(abi, address); @@ -9,6 +10,38 @@ function connect(abi, address) { return contractRegistry }; +async function queryModifyWhiteList(currentTransferManager) { + let investor = readlineSync.question('Enter the address to whitelist: ', { + limit: function (input) { + return web3.utils.isAddress(input); + }, + limitMessage: "Must be a valid address" + }); + let now = Math.floor(Date.now() / 1000); + let fromTime = readlineSync.questionInt(`Enter the time (Unix Epoch time) when the sale lockup period ends and the investor can freely sell his tokens (now = ${now}): `, { defaultInput: now }); + let toTime = readlineSync.questionInt(`Enter the time (Unix Epoch time) when the purchase lockup period ends and the investor can freely purchase tokens from others (now = ${now}): `, { defaultInput: now }); + let oneYearFromNow = Math.floor(Date.now() / 1000 + (60 * 60 * 24 * 365)); + let expiryTime = readlineSync.questionInt(`Enter the time until the investors KYC will be valid (after this time expires, the investor must re-do KYC) (1 year from now = ${oneYearFromNow}): `, { defaultInput: oneYearFromNow }); + let canBuyFromSTO = readlineSync.keyInYNStrict('Can the investor buy from security token offerings?'); + let modifyWhitelistAction = currentTransferManager.methods.modifyWhitelist(investor, fromTime, toTime, expiryTime, canBuyFromSTO); + let modifyWhitelistReceipt = await sendTransaction(modifyWhitelistAction); + let moduleVersion = await getModuleVersion(currentTransferManager); + if (moduleVersion != '1.0.0') { + let modifyWhitelistEvent = getEventFromLogs(currentTransferManager._jsonInterface, modifyWhitelistReceipt.logs, 'ModifyWhitelist'); + console.log(chalk.green(`${modifyWhitelistEvent._investor} has been whitelisted sucessfully!`)); + } else { + console.log(chalk.green(`${investor} has been whitelisted sucessfully!`)); + } +} + +async function getModuleVersion(currentTransferManager) { + let moduleFactoryABI = abis.moduleFactory(); + let factoryAddress = await currentTransferManager.methods.factory().call(); + let moduleFactory = new web3.eth.Contract(moduleFactoryABI, factoryAddress); + let moduleVersion = await moduleFactory.methods.version().call(); + return moduleVersion +} + async function checkPermission(contractName, functionName, contractRegistry) { let permission = permissionsList.verifyPermission(contractName, functionName); if (permission === undefined) { @@ -63,46 +96,7 @@ async function checkPermissions(action) { return } -module.exports = { - convertToDaysRemaining: function (timeRemaining) { - var seconds = parseInt(timeRemaining, 10); - - var days = Math.floor(seconds / (3600 * 24)); - seconds -= days * 3600 * 24; - var hrs = Math.floor(seconds / 3600); - seconds -= hrs * 3600; - var mnts = Math.floor(seconds / 60); - seconds -= mnts * 60; - return (days + " days, " + hrs + " Hrs, " + mnts + " Minutes, " + seconds + " Seconds"); - }, - logAsciiBull: function () { - console.log(` - /######%%, /#( - ##########%%%%%, ,%%%. % - *#############%%%%%##%%%%%%# ## - (################%%%%#####%%%%//###%, - .####################%%%%#########/ - (#########%%############%%%%%%%%%#%%% - ,(%#%%%%%%%%%%%%############%%%%%%%###%%%. - (######%%###%%%%%%%%##############%%%%%####%%%* - /#######%%%%######%%%%##########%###,.%######%%%( - #%%%%%#######%%%%%%###########%%%%%*###### /####%%%# - #. ,%%####%%%%%%%(/#%%%%%%%%( #%#### ,#%/ - *#%( .%%%##%%%%%% .%%%#* - .%%%%#%%%% .%%%###( - %%%#####% (%%. - #%###(, - *#%# - %%# - * - &% - %%%. -`); - }, - getNonce: async function (from) { - return (await web3.eth.getTransactionCount(from.address, "pending")); - }, - sendTransaction: async function (action, options) { +async function sendTransaction(action, options) { await checkPermissions(action); options = getFinalOptions(options); @@ -142,11 +136,52 @@ module.exports = { TxHash: ${receipt.transactionHash}\n` ); }); + }; + +function getEventFromLogs(jsonInterface, logs, eventName) { + let eventJsonInterface = jsonInterface.find(o => o.name === eventName && o.type === 'event'); + let log = logs.find(l => l.topics.includes(eventJsonInterface.signature)); + return web3.eth.abi.decodeLog(eventJsonInterface.inputs, log.data, log.topics.slice(1)); +} + +module.exports = { + convertToDaysRemaining: function (timeRemaining) { + var seconds = parseInt(timeRemaining, 10); + + var days = Math.floor(seconds / (3600 * 24)); + seconds -= days * 3600 * 24; + var hrs = Math.floor(seconds / 3600); + seconds -= hrs * 3600; + var mnts = Math.floor(seconds / 60); + seconds -= mnts * 60; + return (days + " days, " + hrs + " Hrs, " + mnts + " Minutes, " + seconds + " Seconds"); }, - getEventFromLogs: function (jsonInterface, logs, eventName) { - let eventJsonInterface = jsonInterface.find(o => o.name === eventName && o.type === 'event'); - let log = logs.find(l => l.topics.includes(eventJsonInterface.signature)); - return web3.eth.abi.decodeLog(eventJsonInterface.inputs, log.data, log.topics.slice(1)); + logAsciiBull: function () { + console.log(` + /######%%, /#( + ##########%%%%%, ,%%%. % + *#############%%%%%##%%%%%%# ## + (################%%%%#####%%%%//###%, + .####################%%%%#########/ + (#########%%############%%%%%%%%%#%%% + ,(%#%%%%%%%%%%%%############%%%%%%%###%%%. + (######%%###%%%%%%%%##############%%%%%####%%%* + /#######%%%%######%%%%##########%###,.%######%%%( + #%%%%%#######%%%%%%###########%%%%%*###### /####%%%# + #. ,%%####%%%%%%%(/#%%%%%%%%( #%#### ,#%/ + *#%( .%%%##%%%%%% .%%%#* + .%%%%#%%%% .%%%###( + %%%#####% (%%. + #%###(, + *#%# + %%# + * + &% + %%%. +`); + }, + getNonce: async function (from) { + return (await web3.eth.getTransactionCount(from.address, "pending")); }, getMultipleEventsFromLogs: function (jsonInterface, logs, eventName) { let eventJsonInterface = jsonInterface.find(o => o.name === eventName && o.type === 'event'); @@ -172,5 +207,8 @@ module.exports = { } } return result; - } + }, + sendTransaction, + getEventFromLogs, + queryModifyWhiteList }; diff --git a/CLI/commands/token_manager.js b/CLI/commands/token_manager.js index a908a4277..28e69e06b 100644 --- a/CLI/commands/token_manager.js +++ b/CLI/commands/token_manager.js @@ -288,17 +288,8 @@ async function mintTokens() { console.log('Selected:', selected); switch (selected) { case 'Modify whitelist': - let investor = readlineSync.question('Enter the address to whitelist: ', { - limit: function (input) { - return web3.utils.isAddress(input); - }, - limitMessage: "Must be a valid address" - }); - let fromTime = readlineSync.questionInt('Enter the time (Unix Epoch time) when the sale lockup period ends and the investor can freely sell his tokens: '); - let toTime = readlineSync.questionInt('Enter the time (Unix Epoch time) when the purchase lockup period ends and the investor can freely purchase tokens from others: '); - let expiryTime = readlineSync.questionInt('Enter the time till investors KYC will be validated (after that investor need to do re-KYC): '); - let canBuyFromSTO = readlineSync.keyInYNStrict('Can the investor buy from security token offerings?'); - await modifyWhitelist(investor, fromTime, toTime, expiryTime, canBuyFromSTO); + let generalTransferManager = await getGeneralTransferManager(); + await common.queryModifyWhiteList(generalTransferManager); break; case 'Mint tokens to a single address': console.log(chalk.yellow(`Investor should be previously whitelisted.`)); @@ -314,16 +305,13 @@ async function mintTokens() { } /// Mint actions -async function modifyWhitelist(investor, fromTime, toTime, expiryTime, canBuyFromSTO) { +async function getGeneralTransferManager() { let gmtModules = await securityToken.methods.getModulesByName(web3.utils.toHex('GeneralTransferManager')).call(); let generalTransferManagerAddress = gmtModules[0]; let generalTransferManagerABI = abis.generalTransferManager(); let generalTransferManager = new web3.eth.Contract(generalTransferManagerABI, generalTransferManagerAddress); - - let modifyWhitelistAction = generalTransferManager.methods.modifyWhitelist(investor, fromTime, toTime, expiryTime, canBuyFromSTO); - let modifyWhitelistReceipt = await common.sendTransaction(modifyWhitelistAction); - let modifyWhitelistEvent = common.getEventFromLogs(generalTransferManager._jsonInterface, modifyWhitelistReceipt.logs, 'ModifyWhitelist'); - console.log(chalk.green(`${modifyWhitelistEvent._investor} has been whitelisted sucessfully!`)); + generalTransferManager.setProvider(web3.currentProvider); + return generalTransferManager; } async function mintToSingleAddress(_investor, _amount) { diff --git a/CLI/commands/transfer_manager.js b/CLI/commands/transfer_manager.js index 13f59b128..2c7bc46d2 100644 --- a/CLI/commands/transfer_manager.js +++ b/CLI/commands/transfer_manager.js @@ -402,26 +402,7 @@ async function generalTransferManager() { console.log(chalk.green(`Default times have been updated successfully!`)); break; case 'Modify whitelist': - let investor = readlineSync.question('Enter the address to whitelist: ', { - limit: function (input) { - return web3.utils.isAddress(input); - }, - limitMessage: "Must be a valid address" - }); - let now = Math.floor(Date.now() / 1000); - let fromTime = readlineSync.questionInt(`Enter the time (Unix Epoch time) when the sale lockup period ends and the investor can freely sell his tokens (now = ${now}): `, { defaultInput: now }); - let toTime = readlineSync.questionInt(`Enter the time (Unix Epoch time) when the purchase lockup period ends and the investor can freely purchase tokens from others (now = ${now}): `, { defaultInput: now }); - let oneYearFromNow = Math.floor(Date.now() / 1000 + (60 * 60 * 24 * 365)); - let expiryTime = readlineSync.questionInt(`Enter the time until the investors KYC will be valid (after this time expires, the investor must re-do KYC) (1 year from now = ${oneYearFromNow}): `, { defaultInput: oneYearFromNow }); - let canBuyFromSTO = readlineSync.keyInYNStrict('Can the investor buy from security token offerings?'); - let modifyWhitelistAction = currentTransferManager.methods.modifyWhitelist(investor, fromTime, toTime, expiryTime, canBuyFromSTO); - let modifyWhitelistReceipt = await common.sendTransaction(modifyWhitelistAction); - if (moduleVersion != '1.0.0') { - let modifyWhitelistEvent = common.getEventFromLogs(currentTransferManager._jsonInterface, modifyWhitelistReceipt.logs, 'ModifyWhitelist'); - console.log(chalk.green(`${modifyWhitelistEvent._investor} has been whitelisted sucessfully!`)); - } else { - console.log(chalk.green(`${investor} has been whitelisted sucessfully!`)); - } + await common.queryModifyWhiteList(currentTransferManager); break; case 'Modify whitelist from CSV': await modifyWhitelistInBatch();