Skip to content

Commit c8a4f56

Browse files
Merge pull request #446 from PolymathNetwork/private/fede/cli/moveMultiMint
[CLI] CSV refactoring and csv scripts are moved to their corresponding module script
2 parents cb079cb + 2dca646 commit c8a4f56

21 files changed

+316
-689
lines changed

CLI/commands/accredit.js

Lines changed: 0 additions & 94 deletions
This file was deleted.

CLI/commands/changeNonAccreditedLimit.js

Lines changed: 0 additions & 94 deletions
This file was deleted.

CLI/commands/common/common_functions.js

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function getFinalOptions(options) {
4242
async function getGasLimit(options, action) {
4343
let block = await web3.eth.getBlock("latest");
4444
let networkGasLimit = block.gasLimit;
45-
let gas = Math.round(options.factor * (await action.estimateGas({ from: options.from.address, value: options.value})));
45+
let gas = Math.round(options.factor * (await action.estimateGas({ from: options.from.address, value: options.value })));
4646
return (gas > networkGasLimit) ? networkGasLimit : gas;
4747
}
4848

@@ -65,16 +65,16 @@ async function checkPermissions(action) {
6565
module.exports = {
6666
convertToDaysRemaining: function (timeRemaining) {
6767
var seconds = parseInt(timeRemaining, 10);
68-
68+
6969
var days = Math.floor(seconds / (3600 * 24));
70-
seconds -= days * 3600 * 24;
71-
var hrs = Math.floor(seconds / 3600);
72-
seconds -= hrs * 3600;
70+
seconds -= days * 3600 * 24;
71+
var hrs = Math.floor(seconds / 3600);
72+
seconds -= hrs * 3600;
7373
var mnts = Math.floor(seconds / 60);
74-
seconds -= mnts * 60;
74+
seconds -= mnts * 60;
7575
return (days + " days, " + hrs + " Hrs, " + mnts + " Minutes, " + seconds + " Seconds");
7676
},
77-
logAsciiBull: function() {
77+
logAsciiBull: function () {
7878
console.log(`
7979
/######%%, /#(
8080
##########%%%%%, ,%%%. %
@@ -103,8 +103,8 @@ module.exports = {
103103

104104
options = getFinalOptions(options);
105105
let gasLimit = await getGasLimit(options, action);
106-
107-
console.log(chalk.black.bgYellowBright(`---- Transaction executed: ${action._method.name} - Gas limit provided: ${gasLimit} ----`));
106+
107+
console.log(chalk.black.bgYellowBright(`---- Transaction executed: ${action._method.name} - Gas limit provided: ${gasLimit} ----`));
108108

109109
let nonce = await web3.eth.getTransactionCount(options.from.address);
110110
let abi = action.encodeABI();
@@ -117,24 +117,24 @@ module.exports = {
117117
nonce: nonce,
118118
value: web3.utils.toHex(options.value)
119119
};
120-
120+
121121
const transaction = new Tx(parameter);
122122
transaction.sign(Buffer.from(options.from.privateKey.replace('0x', ''), 'hex'));
123123
return await web3.eth.sendSignedTransaction('0x' + transaction.serialize().toString('hex'))
124-
.on('transactionHash', function(hash){
125-
console.log(`
124+
.on('transactionHash', function (hash) {
125+
console.log(`
126126
Your transaction is being processed. Please wait...
127127
TxHash: ${hash}`
128-
);
129-
})
130-
.on('receipt', function(receipt){
131-
console.log(`
128+
);
129+
})
130+
.on('receipt', function (receipt) {
131+
console.log(`
132132
Congratulations! The transaction was successfully completed.
133133
Gas used: ${receipt.gasUsed} - Gas spent: ${web3.utils.fromWei((new web3.utils.BN(options.gasPrice)).mul(new web3.utils.BN(receipt.gasUsed)))} Ether
134134
Review it on Etherscan.
135135
TxHash: ${receipt.transactionHash}\n`
136-
);
137-
});
136+
);
137+
});
138138
},
139139
getEventFromLogs: function (jsonInterface, logs, eventName) {
140140
let eventJsonInterface = jsonInterface.find(o => o.name === eventName && o.type === 'event');
@@ -145,5 +145,22 @@ module.exports = {
145145
let eventJsonInterface = jsonInterface.find(o => o.name === eventName && o.type === 'event');
146146
let filteredLogs = logs.filter(l => l.topics.includes(eventJsonInterface.signature));
147147
return filteredLogs.map(l => web3.eth.abi.decodeLog(eventJsonInterface.inputs, l.data, l.topics.slice(1)));
148+
},
149+
splitIntoBatches: function (data, batchSize) {
150+
let allBatches = [];
151+
for (let index = 0; index < data.length; index += batchSize) {
152+
allBatches.push(data.slice(index, index + batchSize));
153+
}
154+
return allBatches;
155+
},
156+
transposeBatches: function (batches) {
157+
let result = [];
158+
if (batches.length > 0 && batches[0].length > 0) {
159+
let columns = batches[0][0].length;
160+
for (let index = 0; index < columns; index++) {
161+
result[index] = batches.map(batch => batch.map(record => record[index]));
162+
}
163+
}
164+
return result;
148165
}
149166
};

CLI/commands/common/constants.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,30 @@ module.exports = Object.freeze({
77
BURN: 5
88
},
99
DURATION: {
10-
seconds: function(val) {
10+
seconds: function (val) {
1111
return val
1212
},
13-
minutes: function(val) {
13+
minutes: function (val) {
1414
return val * this.seconds(60)
1515
},
16-
hours: function(val) {
16+
hours: function (val) {
1717
return val * this.minutes(60)
1818
},
19-
days: function(val) {
19+
days: function (val) {
2020
return val * this.hours(24)
2121
},
22-
weeks: function(val) {
22+
weeks: function (val) {
2323
return val * this.days(7)
2424
},
25-
years: function(val) {
25+
years: function (val) {
2626
return val * this.days(365)
2727
}
2828
},
2929
FUND_RAISE_TYPES: {
3030
ETH: 0,
3131
POLY: 1,
3232
DAI: 2
33-
}
33+
},
34+
DEFAULT_BATCH_SIZE: 75,
35+
ADDRESS_ZERO: '0x0000000000000000000000000000000000000000'
3436
});

CLI/commands/helpers/csv.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const csvParse = require('csv-parse/lib/sync');
2+
const fs = require('fs');
3+
const web3Utils = require('web3-utils');
4+
5+
function _cast(obj) {
6+
if (/^(\-|\+)?([1-9]+[0-9]*)$/.test(obj)) { // Int
7+
obj = parseInt(obj);
8+
}
9+
else if (/^[+-]?([0-9]*[.])?[0-9]+$/.test(obj)) { // Float
10+
obj = parseFloat(obj);
11+
}
12+
else if (/^(\d{1,2})[-\/](\d{1,2})[-\/](\d{4})$/.test(obj)) { // Datetime
13+
var matches = /^(\d{1,2})[-\/](\d{1,2})[-\/](\d{4})$/.exec(obj);
14+
var composedDate = new Date(matches[3], matches[1] - 1, matches[2]);
15+
var timestampDate = composedDate.getTime();
16+
obj = timestampDate / 1000;
17+
}
18+
else if (obj.toLowerCase() === "true" || obj.toLowerCase() === "false") { // Boolean
19+
obj = JSON.parse(obj.toLowerCase());
20+
} else if (web3Utils.isAddress(obj)) {
21+
obj = web3Utils.toChecksumAddress(obj);
22+
}
23+
return obj;
24+
}
25+
26+
function parse(_csvFilePath, _batchSize) {
27+
// Read file
28+
let input = fs.readFileSync(_csvFilePath);
29+
// Parse csv
30+
let data = csvParse(input, { cast: _cast });
31+
32+
return data;
33+
}
34+
35+
module.exports = parse;

0 commit comments

Comments
 (0)