Skip to content

Commit

Permalink
Port Combine modify whitelist commands (ref PR PolymathNetwork#667)
Browse files Browse the repository at this point in the history
  • Loading branch information
F-OBrien committed May 12, 2019
1 parent 95fcd0a commit a396060
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 83 deletions.
131 changes: 84 additions & 47 deletions CLI/commands/common/common_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,45 @@ 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 canSendAfter = readlineSync.questionInt(`Enter the time (Unix Epoch time) when the sale lockup period ends and the investor can freely transfer his tokens (now = ${now}): `, { defaultInput: now });
let canReceiveAfter = readlineSync.questionInt(`Enter the time (Unix Epoch time) when the purchase lockup period ends and the investor can freely receive 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 modifyWhitelistAction = currentTransferManager.methods.modifyKYCData(investor, canSendAfter, canReceiveAfter, expiryTime);
let modifyWhitelistReceipt = await sendTransaction(modifyWhitelistAction);
let moduleVersion = await getModuleVersion(currentTransferManager);
if (moduleVersion != '1.0.0') {
let modifyWhitelistEvent = getEventFromLogs(currentTransferManager._jsonInterface, modifyWhitelistReceipt.logs, 'ModifyKYCData');
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,6 +95,54 @@ async function checkPermissions(action) {
return
}

async function sendTransaction(action, options) {
await checkPermissions(action);

options = getFinalOptions(options);
let gasLimit = await getGasLimit(options, action);

console.log(chalk.black.bgYellowBright(`---- Transaction executed: ${action._method.name} - Gas limit provided: ${gasLimit} ----`));

let nonce = await web3.eth.getTransactionCount(options.from.address);
if (nonce < options.minNonce) {
nonce = minNonce;
}
let abi = action.encodeABI();
let parameter = {
from: options.from.address,
to: action._parent._address,
data: abi,
gasLimit: gasLimit,
gasPrice: options.gasPrice,
nonce: nonce,
value: web3.utils.toHex(options.value)
};

const transaction = new Tx(parameter);
transaction.sign(Buffer.from(options.from.privateKey.replace('0x', ''), 'hex'));
return await web3.eth.sendSignedTransaction('0x' + transaction.serialize().toString('hex'))
.on('transactionHash', function (hash) {
console.log(`
Your transaction is being processed. Please wait...
TxHash: ${hash}`
);
})
.on('receipt', function (receipt) {
console.log(`
Congratulations! The transaction was successfully completed.
Gas used: ${receipt.gasUsed} - Gas spent: ${web3.utils.fromWei((new web3.utils.BN(options.gasPrice)).mul(new web3.utils.BN(receipt.gasUsed)))} Ether
Review it on Etherscan.
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);
Expand Down Expand Up @@ -102,52 +182,6 @@ module.exports = {
getNonce: async function (from) {
return (await web3.eth.getTransactionCount(from.address, "pending"));
},
sendTransaction: async function (action, options) {
await checkPermissions(action);

options = getFinalOptions(options);
let gasLimit = await getGasLimit(options, action);

console.log(chalk.black.bgYellowBright(`---- Transaction executed: ${action._method.name} - Gas limit provided: ${gasLimit} ----`));

let nonce = await web3.eth.getTransactionCount(options.from.address);
if (nonce < options.minNonce) {
nonce = minNonce;
}
let abi = action.encodeABI();
let parameter = {
from: options.from.address,
to: action._parent._address,
data: abi,
gasLimit: gasLimit,
gasPrice: options.gasPrice,
nonce: nonce,
value: web3.utils.toHex(options.value)
};

const transaction = new Tx(parameter);
transaction.sign(Buffer.from(options.from.privateKey.replace('0x', ''), 'hex'));
return await web3.eth.sendSignedTransaction('0x' + transaction.serialize().toString('hex'))
.on('transactionHash', function (hash) {
console.log(`
Your transaction is being processed. Please wait...
TxHash: ${hash}`
);
})
.on('receipt', function (receipt) {
console.log(`
Congratulations! The transaction was successfully completed.
Gas used: ${receipt.gasUsed} - Gas spent: ${web3.utils.fromWei((new web3.utils.BN(options.gasPrice)).mul(new web3.utils.BN(receipt.gasUsed)))} Ether
Review it on Etherscan.
TxHash: ${receipt.transactionHash}\n`
);
});
},
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));
},
getMultipleEventsFromLogs: function (jsonInterface, logs, eventName) {
let eventJsonInterface = jsonInterface.find(o => o.name === eventName && o.type === 'event');
let filteredLogs = logs.filter(l => l.topics.includes(eventJsonInterface.signature));
Expand All @@ -172,5 +206,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 @@ -296,16 +296,8 @@ async function issueTokens() {
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): ');
await modifyKYCData(investor, fromTime, toTime, expiryTime);
let generalTransferManager = await getGeneralTransferManager();
await common.queryModifyWhiteList(generalTransferManager);
break;
case 'Issue tokens to a single address':
console.log(chalk.yellow(`Investor should be previously whitelisted.`));
Expand All @@ -321,17 +313,13 @@ async function issueTokens() {
}

/// Issue actions
async function modifyKYCData(investor, fromTime, toTime, expiryTime) {
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 modifyKYCDataAction = generalTransferManager.methods.modifyKYCData(investor, fromTime, toTime, expiryTime);
let modifyKYCDataReceipt = await common.sendTransaction(modifyKYCDataAction);
let modifyKYCDataEvent = common.getEventFromLogs(generalTransferManager._jsonInterface, modifyKYCDataReceipt.logs, 'ModifyKYCData');

console.log(chalk.green(`${modifyKYCDataEvent._investor} has been whitelisted sucessfully!`));
generalTransferManager.setProvider(web3.currentProvider);
return generalTransferManager;
}

async function issueToSingleAddress(_investor, _amount) {
Expand Down
20 changes: 1 addition & 19 deletions CLI/commands/transfer_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,25 +398,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 canSendAfter = readlineSync.questionInt(`Enter the time (Unix Epoch time) when the sale lockup period ends and the investor can freely transfer his tokens (now = ${now}): `, { defaultInput: now });
let canReceiveAfter = readlineSync.questionInt(`Enter the time (Unix Epoch time) when the purchase lockup period ends and the investor can freely receive 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 modifyWhitelistAction = currentTransferManager.methods.modifyKYCData(investor, canSendAfter, canReceiveAfter, expiryTime);
let modifyWhitelistReceipt = await common.sendTransaction(modifyWhitelistAction);
if (moduleVersion != '1.0.0') {
let modifyWhitelistEvent = common.getEventFromLogs(currentTransferManager._jsonInterface, modifyWhitelistReceipt.logs, 'ModifyKYCData');
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 a396060

Please sign in to comment.