1- var fs = require ( 'fs' ) ;
2- var csv = require ( 'fast-csv' ) ;
3- var BigNumber = require ( 'bignumber.js' ) ;
4- var chalk = require ( 'chalk' ) ;
51var 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]]
236let 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]]
287let fullFileData = new Array ( ) ;
29- //baa data is an array that contains invalid entries
308let 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