Skip to content

Commit

Permalink
Merge pull request #667 from joequant/dev/refactor-modify-whitelist
Browse files Browse the repository at this point in the history
[CLI] Combine modify white list commands
  • Loading branch information
VictorVicente authored May 8, 2019
2 parents 9f6bd95 + 6c502b8 commit aa635df
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 82 deletions.
128 changes: 83 additions & 45 deletions CLI/commands/common/common_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,46 @@ 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);
contractRegistry.setProvider(web3.currentProvider);
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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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');
Expand All @@ -172,5 +207,8 @@ module.exports = {
}
}
return result;
}
},
sendTransaction,
getEventFromLogs,
queryModifyWhiteList
};
22 changes: 5 additions & 17 deletions CLI/commands/token_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.`));
Expand All @@ -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) {
Expand Down
21 changes: 1 addition & 20 deletions CLI/commands/transfer_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit aa635df

Please sign in to comment.