Skip to content

Commit 331eb83

Browse files
Merge pull request #408 from PolymathNetwork/private/fede/cli/csvImprovements
[CLI] Improvements for CSV methods
2 parents eb8787c + 4974b13 commit 331eb83

File tree

12 files changed

+423
-630
lines changed

12 files changed

+423
-630
lines changed

CLI/commands/ST20Generator.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ var readlineSync = require('readline-sync');
22
var BigNumber = require('bignumber.js');
33
var moment = require('moment');
44
var chalk = require('chalk');
5-
const shell = require('shelljs');
65
var contracts = require('./helpers/contract_addresses');
76
var abis = require('./helpers/contract_abis');
87
var common = require('./common/common_functions');
98
var gbl = require('./common/global');
9+
var whitelist = require('./whitelist');
10+
var multi_mint = require('./multi_mint');
11+
var accredit = require('./accredit');
12+
var changeNonAccreditedLimit = require('./changeNonAccreditedLimit');
1013

1114
let securityTokenRegistryAddress;
1215

@@ -241,12 +244,11 @@ async function step_Wallet_Issuance(){
241244
}
242245

243246
async function multi_mint_tokens() {
244-
//await whitelist.startWhitelisting(tokenSymbol);
245-
shell.exec(`${__dirname}/scripts/script.sh Whitelist ${tokenSymbol} 75 ${remoteNetwork}`);
247+
await whitelist.executeApp(tokenSymbol, 75);
246248
console.log(chalk.green(`\nCongrats! All the affiliates get succssfully whitelisted, Now its time to Mint the tokens\n`));
247249
console.log(chalk.red(`WARNING: `) + `Please make sure all the addresses that get whitelisted are only eligible to hold or get Security token\n`);
248250

249-
shell.exec(`${__dirname}/scripts//script.sh Multimint ${tokenSymbol} 75 ${remoteNetwork}`);
251+
await multi_mint.executeApp(tokenSymbol, 75);
250252
console.log(chalk.green(`\nHurray!! Tokens get successfully Minted and transferred to token holders`));
251253
}
252254

@@ -1003,7 +1005,7 @@ async function usdTieredSTO_configure() {
10031005
await common.sendTransaction(changeAccreditedAction);
10041006
break;
10051007
case 2:
1006-
shell.exec(`${__dirname}/scripts/script.sh Accredit ${tokenSymbol} 75 ${remoteNetwork}`);
1008+
await accredit.executeApp(tokenSymbol, 75);
10071009
break;
10081010
case 3:
10091011
let account = readlineSync.question('Enter the address to change non accredited limit: ');
@@ -1015,7 +1017,7 @@ async function usdTieredSTO_configure() {
10151017
await common.sendTransaction(changeNonAccreditedLimitAction);
10161018
break;
10171019
case 4:
1018-
shell.exec(`${__dirname}/scripts/script.sh NonAccreditedLimit ${tokenSymbol} 75 ${remoteNetwork}`);
1020+
await changeNonAccreditedLimit.executeApp(tokenSymbol, 75);
10191021
break;
10201022
case 5:
10211023
await modfifyTimes();

CLI/commands/accredit.js

Lines changed: 61 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,149 +1,94 @@
1-
var fs = require('fs');
2-
var csv = require('fast-csv');
3-
var BigNumber = require('bignumber.js');
4-
var chalk = require('chalk');
51
var common = require('./common/common_functions');
6-
var global = require('./common/global');
7-
var contracts = require('./helpers/contract_addresses');
8-
var abis = require('./helpers/contract_abis')
9-
10-
/////////////////////////////ARTIFACTS//////////////////////////////////////////
11-
let securityTokenRegistry;
12-
let securityToken;
13-
let usdTieredSTO;
14-
15-
////////////////////////////USER INPUTS//////////////////////////////////////////
16-
let tokenSymbol = process.argv.slice(2)[0]; //token symbol
17-
let BATCH_SIZE = process.argv.slice(2)[1]; //batch size
18-
if (!BATCH_SIZE) BATCH_SIZE = 75;
19-
let remoteNetwork = process.argv.slice(2)[2];
2+
var csv_shared = require('./common/csv_shared');
3+
var abis = require('./helpers/contract_abis');
4+
var BigNumber = require('bignumber.js');
205

21-
/////////////////////////GLOBAL VARS//////////////////////////////////////////
22-
//distribData is an array of batches. i.e. if there are 200 entries, with batch sizes of 75, we get [[75],[75],[50]]
236
let distribData = new Array();
24-
//allocData is a temporary array that stores up to the batch size,
25-
//then gets push into distribData, then gets set to 0 to start another batch
26-
let allocData = new Array();
27-
//full file data is a single array that contains all arrays. i.e. if there are 200 entries we get [[200]]
287
let fullFileData = new Array();
29-
//baa data is an array that contains invalid entries
308
let badData = new Array();
319

32-
//////////////////////////////////////////ENTRY INTO SCRIPT//////////////////////////////////////////
33-
startScript();
34-
35-
async function startScript() {
36-
if (remoteNetwork == 'undefined') remoteNetwork = undefined;
37-
await global.initialize(remoteNetwork);
38-
39-
try {
40-
let securityTokenRegistryAddress = await contracts.securityTokenRegistry();
41-
let securityTokenRegistryABI = abis.securityTokenRegistry();
42-
securityTokenRegistry = new web3.eth.Contract(securityTokenRegistryABI, securityTokenRegistryAddress);
43-
securityTokenRegistry.setProvider(web3.currentProvider);
44-
console.log("Processing investor CSV upload. Batch size is " + BATCH_SIZE + " accounts per transaction");
45-
readFile();
46-
} catch (err) {
47-
console.log(err)
48-
console.log('\x1b[31m%s\x1b[0m', "There was a problem getting the contracts. Make sure they are deployed to the selected network.");
49-
return;
50-
}
51-
}
10+
let securityToken;
5211

53-
///////////////////////////FUNCTION READING THE CSV FILE
54-
function readFile() {
55-
var stream = fs.createReadStream("./CLI/data/accredited_data.csv");
12+
async function startScript(tokenSymbol, batchSize) {
13+
securityToken = await csv_shared.start(tokenSymbol, batchSize);
5614

57-
let index = 0;
58-
console.log(`
59-
--------------------------------------------
60-
----------- Parsing the csv file -----------
61-
--------------------------------------------
62-
`);
15+
let result_processing = await csv_shared.read('./CLI/data/accredited_data.csv', accredit_processing);
16+
distribData = result_processing.distribData;
17+
fullFileData = result_processing.fullFileData;
18+
badData = result_processing.badData;
19+
20+
await saveInBlockchain();
21+
};
6322

64-
var csvStream = csv()
65-
.on("data", function (data) {
66-
let isAddress = web3.utils.isAddress(data[0]);
67-
let isAccredited = (typeof JSON.parse(data[1].toLowerCase())) == "boolean" ? JSON.parse(data[1].toLowerCase()) : "not-valid";
23+
function accredit_processing(csv_line) {
24+
let isAddress = web3.utils.isAddress(csv_line[0]);
25+
let isAccredited = (typeof JSON.parse(csv_line[1].toLowerCase())) == "boolean" ? JSON.parse(csv_line[1].toLowerCase()) : "not-valid";
6826

69-
if (isAddress && (isAccredited != "not-valid") ) {
70-
let userArray = new Array()
71-
let checksummedAddress = web3.utils.toChecksumAddress(data[0]);
27+
if (isAddress &&
28+
(isAccredited != "not-valid")) {
29+
return [true, new Array(web3.utils.toChecksumAddress(csv_line[0]), isAccredited)]
30+
} else {
31+
return [false, new Array(csv_line[0], isAccredited)]
32+
}
33+
}
7234

73-
userArray.push(checksummedAddress)
74-
userArray.push(isAccredited)
35+
async function saveInBlockchain() {
36+
let gtmModules;
37+
try {
38+
gtmModules = await securityToken.methods.getModulesByName(web3.utils.toHex('USDTieredSTO')).call();
39+
} catch (e) {
40+
console.log("Please attach USDTieredSTO module before launch this action.", e)
41+
process.exit(0)
42+
}
7543

76-
allocData.push(userArray);
77-
fullFileData.push(userArray);
78-
79-
index++;
80-
if (index >= BATCH_SIZE) {
81-
distribData.push(allocData);
82-
allocData = [];
83-
index = 0;
84-
}
85-
} else {
86-
let userArray = new Array()
87-
userArray.push(data[0])
88-
userArray.push(isAccredited);
44+
if (!gtmModules.length) {
45+
console.log("Please attach USDTieredSTO module before launch this action.")
46+
process.exit(0)
47+
}
8948

90-
badData.push(userArray);
91-
fullFileData.push(userArray)
92-
}
93-
})
94-
.on("end", function () {
95-
//Add last remainder batch
96-
distribData.push(allocData);
97-
allocData = [];
49+
let usdTieredSTO = new web3.eth.Contract(abis.usdTieredSTO(), gtmModules[0]);
9850

99-
changeAccredited();
100-
});
51+
console.log(`
52+
--------------------------------------------------------
53+
----- Sending accreditation changes to blockchain -----
54+
--------------------------------------------------------
55+
`);
10156

102-
stream.pipe(csvStream);
103-
}
57+
for (let i = 0; i < distribData.length; i++) {
58+
try {
10459

105-
// MAIN FUNCTION COMMUNICATING TO BLOCKCHAIN
106-
async function changeAccredited() {
107-
// Let's check if token has already been deployed, if it has, skip to STO
108-
let tokenDeployedAddress = await securityTokenRegistry.methods.getSecurityTokenAddress(tokenSymbol).call();
109-
if (tokenDeployedAddress != "0x0000000000000000000000000000000000000000") {
110-
let securityTokenABI = abis.securityToken();
111-
securityToken = new web3.eth.Contract(securityTokenABI, tokenDeployedAddress);
112-
let result = await securityToken.methods.getModulesByName(web3.utils.toHex('USDTieredSTO')).call();
113-
if (result.length > 0) {
114-
let usdTieredSTOABI = abis.usdTieredSTO();
115-
usdTieredSTO = new web3.eth.Contract(usdTieredSTOABI, result[0]);
116-
console.log(`
117-
-------------------------------------------------------
118-
----- Sending accreditation changes to blockchain -----
119-
-------------------------------------------------------
120-
`);
121-
//this for loop will do the batches, so it should run 75, 75, 50 with 200
60+
// Splitting the user arrays to be organized by input
12261
for (let i = 0; i < distribData.length; i++) {
12362
try {
124-
let investorArray = [];
125-
let isAccreditedArray = [];
63+
let investorArray = [], isAccreditedArray = [];
12664

127-
//splitting the user arrays to be organized by input
12865
for (let j = 0; j < distribData[i].length; j++) {
12966
investorArray.push(distribData[i][j][0])
13067
isAccreditedArray.push(distribData[i][j][1])
13168
}
13269

133-
let changeAccreditedAction = usdTieredSTO.methods.changeAccredited(investorArray, isAccreditedArray);
134-
let r = await common.sendTransaction(changeAccreditedAction);
70+
let changeAccreditedAction = await usdTieredSTO.methods.changeAccredited(investorArray, isAccreditedArray);
71+
let tx = await common.sendTransaction(changeAccreditedAction);
13572
console.log(`Batch ${i} - Attempting to change accredited accounts:\n\n`, investorArray, "\n\n");
13673
console.log("---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------");
137-
console.log("Change accredited transaction was successful.", r.gasUsed, "gas used. Spent:", web3.utils.fromWei(BigNumber(r.gasUsed * defaultGasPrice).toString(), "ether"), "Ether");
74+
console.log("Change accredited transaction was successful.", tx.gasUsed, "gas used. Spent:", web3.utils.fromWei(BigNumber(tx.gasUsed * defaultGasPrice).toString(), "ether"), "Ether");
13875
console.log("---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------\n\n");
13976
} catch (err) {
14077
console.log("ERROR:", err);
14178
}
14279
}
143-
} else {
144-
console.log(chalk.red(`There is no USDTieredSTO module attached to the ${tokenSymbol.toUpperCase()} Token. No further actions can be taken.`));
145-
}
146-
} else {
147-
console.log(chalk.red(`Token symbol provided is not a registered Security Token.`));
80+
81+
} catch (err) {
82+
console.log("ERROR", err)
83+
process.exit(0)
84+
}
14885
}
86+
87+
return;
14988
}
89+
90+
module.exports = {
91+
executeApp: async (tokenSymbol, batchSize) => {
92+
return startScript(tokenSymbol, batchSize);
93+
}
94+
}

0 commit comments

Comments
 (0)