-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
871 lines (667 loc) · 27.3 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
// Load library
const path = require('path');
const fs = require('fs');
const solc = require('solc');
const Web3 = require('web3');
const browserify = require('browserify');
const Tx = require('ethereumjs-tx');
const SolidityFunction = require('web3/lib/web3/function');
const appConfig = require('./appconfig');
const _ = require('lodash');
const md5File = require('md5-file');
var accounts = appConfig['accounts'];
var selectedHost = appConfig['networks'][ appConfig['settings']['selectedHost'] ];
var selectedAccountIndex = appConfig['settings']['selectedAccountIndex'];
var contractAddress = '';
// Retrieve the command line arguments
var argv = require('minimist')(process.argv.slice(2), { string: ['checkminter'] });
var solcInput = {
language: "Solidity",
sources: { },
settings: {
optimizer: {
enabled: true
},
evmVersion: "byzantium",
outputSelection: {
"*": {
"": [
"legacyAST",
"ast"
],
"*": [
"abi",
"evm.bytecode.object",
"evm.bytecode.sourceMap",
"evm.deployedBytecode.object",
"evm.deployedBytecode.sourceMap",
"evm.gasEstimates"
]
},
}
}
};
var web3 = null;
if (typeof argv.ropsten !== 'undefined') {
selectedHost = appConfig['networks']['ropstenHost'];
} else if (typeof argv.kovan !== 'undefined') {
selectedHost = appConfig['networks']['kovanHost'];
} else if (typeof argv.rinkeby !== 'undefined') {
selectedHost = appConfig['networks']['rinkebyHost'];
} else if (typeof argv.mainnet !== 'undefined') {
selectedHost = appConfig['networks']['mainnetHost'];;
}
if (typeof argv.account !== 'undefined') {
selectedAccountIndex = argv.account;
}
web3 = new Web3(new Web3.providers.HttpProvider(selectedHost));
var gasPrice = web3.eth.gasPrice;
var gasPriceHex = web3.toHex(gasPrice);
var gasLimitHex = web3.toHex(6000000);
var block = web3.eth.getBlock("latest");
var nonce = web3.eth.getTransactionCount(accounts[selectedAccountIndex].address, "pending");
var nonceHex = web3.toHex(nonce);
function findImports(importFile) {
console.log("Import File:" + importFile);
try {
// Find in contracts folder first
result = fs.readFileSync("contracts/" + importFile, 'utf8');
return { contents: result };
} catch (error) {
// Try to look into node_modules
try {
result = fs.readFileSync("node_modules/" + importFile, 'utf8');
return { contents: result };
} catch (error) {
console.log(error.message);
return { error: 'File not found' };
}
}
}
function waitForTransactionReceipt(hash) {
console.log('Waiting for contract to be mined');
const receipt = web3.eth.getTransactionReceipt(hash);
// If no receipt, try again in 1s
if (receipt == null) {
setTimeout(() => {
waitForTransactionReceipt(hash);
}, 1000);
} else {
// The transaction was mined, we can retrieve the contract address
console.log('Contract address: ' + receipt.contractAddress);
//testContract(receipt.contractAddress);
}
}
function buildContract(contract) {
let contractFile = 'contracts/' + contract;
let jsonOutputName = path.parse(contract).name + '.json';
let jsonOutputFile = './build/contracts/' + jsonOutputName;
let result = false;
try {
result = fs.statSync(contractFile);
} catch (error) {
console.log(error.message);
return false;
}
let contractFileChecksum = md5File.sync(contractFile);
try {
fs.statSync(jsonOutputFile);
let jsonContent = fs.readFileSync(jsonOutputFile, 'utf8');
let jsonObject = JSON.parse(jsonContent);
let buildChecksum = '';
if (typeof jsonObject['contracts'][contract]['checksum'] != 'undefined') {
buildChecksum = jsonObject['contracts'][contract]['checksum'];
console.log('File Checksum: ' + contractFileChecksum);
console.log('Build Checksum: ' + buildChecksum);
if (contractFileChecksum === buildChecksum) {
console.log('No build is required due no change in file.');
console.log('==============================');
return true;
}
}
} catch (error) {
// Any file not found, will continue build
}
let contractContent = fs.readFileSync(contractFile, 'utf8');
console.log('Contract File: ' + contract);
solcInput.sources[contract] = {
"content": contractContent
};
let solcInputString = JSON.stringify(solcInput);
let output = solc.compileStandardWrapper(solcInputString, findImports);
let jsonOutput = JSON.parse(output);
let isError = false;
if (jsonOutput.errors) {
jsonOutput.errors.forEach(error => {
console.log(error.severity + ': ' + error.component + ': ' + error.formattedMessage);
if (error.severity == 'error') {
isError = true;
}
});
}
if (isError) {
// Compilation errors
console.log('Compile error!');
return false;
}
// Update the sol file checksum
jsonOutput['contracts'][contract]['checksum'] = contractFileChecksum;
let formattedJson = JSON.stringify(jsonOutput, null, 4);
//console.log(formattedJson);
fs.writeFileSync('./build/contracts/' + jsonOutputName, formattedJson);
console.log('==============================');
return true;
}
function deployContract(contract) {
let jsonOutputName = path.parse(contract).name + '.json';
let jsonFile = './build/contracts/' + jsonOutputName;
let webJsonFile = './www/assets/contracts/' + jsonOutputName;
let result = false;
try {
result = fs.statSync(jsonFile);
} catch (error) {
console.log(error.message);
return false;
}
let contractJsonContent = fs.readFileSync(jsonFile, 'utf8');
let jsonOutput = JSON.parse(contractJsonContent);
var abi = jsonOutput['contracts'][contract][path.parse(contract).name]['abi'];
var bytecode = jsonOutput['contracts'][contract][path.parse(contract).name]['evm']['bytecode']['object'];
//console.log(bytecode);
var tokenContract = web3.eth.contract(abi);
var contractData = null;
if (contract == 'FrancisToken.sol') {
// Mintable ERC20 Token has constructor parameters
contractData = tokenContract.new.getData('Unwallz Token', 'UWT', 18, 10000000000 * 1e18, {
data: '0x' + bytecode
});
} else {
contractData = tokenContract.new.getData({
data: '0x' + bytecode
});
}
var rawTx = {
nonce: nonceHex,
gasPrice: gasPriceHex,
gasLimit: gasLimitHex,
data: contractData,
from: accounts[selectedAccountIndex].address
};
console.log(rawTx);
var privateKey = new Buffer(accounts[selectedAccountIndex].key, 'hex')
var tx = new Tx(rawTx);
tx.sign(privateKey);
var serializedTx = tx.serialize();
var receipt = null;
web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), (err, hash) => {
if (err) {
console.log(err); return;
}
// Log the tx, you can explore status manually with eth.getTransaction()
console.log('Contract creation tx: ' + hash);
// Wait for the transaction to be mined
while (receipt == null) {
receipt = web3.eth.getTransactionReceipt(hash);
//sleep.sleep(1);
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 1000);
}
console.log('Contract address: ' + receipt.contractAddress);
console.log('Contract File: ' + contract);
// Update JSON
jsonOutput['contracts'][contract]['contractAddress'] = receipt.contractAddress;
let webJsonOutput = {
'abi': abi,
'contractAddress': receipt.contractAddress
};
let formattedJson = JSON.stringify(jsonOutput, null, 4);
let formattedWebJson = JSON.stringify(webJsonOutput);
//console.log(formattedJson);
fs.writeFileSync(jsonFile, formattedJson);
fs.writeFileSync(webJsonFile, formattedWebJson);
console.log('==============================');
});
return true;
}
function contractInfo(contract) {
let jsonOutputName = path.parse(contract).name + '.json';
let jsonFile = './build/contracts/' + jsonOutputName;
let result = false;
try {
result = fs.statSync(jsonFile);
} catch (error) {
console.log(error.message);
return false;
}
let contractJsonContent = fs.readFileSync(jsonFile, 'utf8');
let jsonOutput = JSON.parse(contractJsonContent);
let abi = jsonOutput['contracts'][contract][path.parse(contract).name]['abi'];
let contractAddress = jsonOutput['contracts'][contract]['contractAddress'];
// Francis Token Contract
let tokenContract = web3.eth.contract(abi).at(contractAddress);
let tokenDecimals = tokenContract.decimals();
console.log('My wallet address: ' + accounts[selectedAccountIndex].address);
console.log('Token Name: ' + tokenContract.name.call());
console.log('Token Symbol: ' + tokenContract.symbol());
console.log('Token Decimal: ' + tokenContract.decimals());
console.log('Token Total Supply: ' + (tokenContract.totalSupply()));
console.log('Token Capped: ' + (tokenContract.cap() / (Math.pow(10, tokenDecimals))));
console.log('Is Minter: ' + tokenContract.isMinter.call(accounts[selectedAccountIndex].address));
console.log('My Token Balance: ' + (tokenContract.balanceOf.call(accounts[selectedAccountIndex].address) / Math.pow(10, tokenDecimals) ) );
return true;
}
// Display some client information
if (typeof argv.info !== 'undefined') {
console.log('==============================');
console.log('Gas Price: ' + gasPrice);
console.log('Gas Limit: ' + gasLimitHex);
console.log("Latest Block Gas Limit: " + block.gasLimit);
console.log('Nonce: ' + nonce + '(' + nonceHex + ')');
console.log('==============================');
}
if (typeof argv.build !== 'undefined') {
// Build contract
let contracts = appConfig['build']['contracts'];
contracts.forEach(contract => {
let result = buildContract(contract);
if (!result) {
return;
}
});
return;
}
if (typeof argv.deploy !== 'undefined') {
// Build contract
let contracts = appConfig['build']['contracts'];
contracts.forEach(contract => {
if (argv.deploy == '*' || argv.deploy == contract) {
let result = deployContract(contract);
if (!result) {
return;
}
// Update new nonce
//nonce = web3.eth.getTransactionCount(accounts[selectedAccountIndex].address);
nonce++;
nonceHex = web3.toHex(nonce);
}
});
return;
}
if (typeof argv.contractinfo !== 'undefined') {
if (typeof argv.account !== 'undefined') {
selectedAccountIndex = argv.account;
}
let result = contractInfo('FrancisToken.sol');
/*
let contracts = appConfig['build']['contracts'];
contracts.forEach(contract => {
let result = contractInfo(contract);
if (!result) {
return;
}
});
*/
return;
}
if (typeof argv.mint !== 'undefined') {
// Mint Francis Token
let contractSource = 'FrancisToken.sol';
let jsonOutputName = path.parse(contractSource).name + '.json';
let jsonFile = './build/contracts/' + jsonOutputName;
let contractJsonContent = fs.readFileSync(jsonFile, 'utf8');
let jsonOutput = JSON.parse(contractJsonContent);
let abi = jsonOutput['contracts'][contractSource][path.parse(contractSource).name]['abi'];
let contractAddress = jsonOutput['contracts'][contractSource]['contractAddress'];
// Francis Token Contract
let tokenContract = web3.eth.contract(abi).at(contractAddress);
let tokenDecimals = tokenContract.decimals();
let tokenToMint = argv.mint * Math.pow(10, tokenDecimals);
// Mint to my wallet address
let privateKey = new Buffer(accounts[selectedAccountIndex].key, 'hex');
console.log('Mint ' + argv.mint + ' token to address ' + accounts[selectedAccountIndex].address);
let solidityFunction = new SolidityFunction('', _.find(abi, { name: 'mint' }), '');
let payloadData = solidityFunction.toPayload([accounts[selectedAccountIndex].address, tokenToMint]).data;
let rawTx = {
nonce: nonceHex,
gasPrice: gasPriceHex,
gasLimit: gasLimitHex,
to: contractAddress,
from: accounts[selectedAccountIndex].address,
data: payloadData
};
let tx = new Tx(rawTx);
tx.sign(privateKey);
var serializedTx = tx.serialize();
web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function (err, hash) {
if (err) {
console.log('Error:');
console.log(err);
}
else {
console.log('Transaction receipt hash pending');
console.log(hash);
}
});
return;
}
if (typeof argv.tokensale !== 'undefined') {
// Francis Token Contract
let tokenSource = 'FrancisToken.sol';
let jsonTokenFilename = path.parse(tokenSource).name + '.json';
let jsonTokenFile = './build/contracts/' + jsonTokenFilename;
let tokenJsonContent = fs.readFileSync(jsonTokenFile, 'utf8');
let tokenJsonOutput = JSON.parse(tokenJsonContent);
let tokenAbi = tokenJsonOutput['contracts'][tokenSource][path.parse(tokenSource).name]['abi'];
let tokenAddress = tokenJsonOutput['contracts'][tokenSource]['contractAddress'];
let tokenContract = web3.eth.contract(tokenAbi).at(tokenAddress);
// Francis Token Sale Contract
let tokenSaleSource = 'FrancisTokenSale.sol';
let jsonTokenSaleFilename = path.parse(tokenSaleSource).name + '.json';
let jsonTokenSaleFile = './build/contracts/' + jsonTokenSaleFilename;
let tokenSaleJsonContent = fs.readFileSync(jsonTokenSaleFile, 'utf8');
let tokenSaleJsonOutput = JSON.parse(tokenSaleJsonContent);
let tokenSaleAbi = tokenSaleJsonOutput['contracts'][tokenSaleSource][path.parse(tokenSaleSource).name]['abi'];
let tokenSaleAddress = tokenSaleJsonOutput['contracts'][tokenSaleSource]['contractAddress'];
let tokenSaleContract = web3.eth.contract(tokenSaleAbi).at(tokenSaleAddress);
let privateKey = new Buffer(accounts[selectedAccountIndex].key, 'hex');
let result = false;
console.log('Token Contract Address: ' + tokenAddress);
console.log('Token Sale Contract Address: ' + tokenSaleAddress);
function addPriceTier(tierTokenPrice, tierMaxSupply, tierTimestamp) {
let solidityFunction = new SolidityFunction('', _.find(tokenSaleAbi, { name: 'addPriceTier' }), '');
let payloadData = solidityFunction.toPayload([tierTokenPrice, tierMaxSupply, tierTimestamp]).data;
let rawTx = {
nonce: nonceHex,
gasPrice: gasPriceHex,
gasLimit: gasLimitHex,
to: tokenSaleAddress,
from: accounts[selectedAccountIndex].address,
data: payloadData
};
let tx = new Tx(rawTx);
tx.sign(privateKey);
let serializedTx = tx.serialize();
/*
web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function (err, hash) {
if (err) {
console.log('Error:');
console.log(err);
}
else {
console.log('Transaction receipt hash pending');
console.log(hash);
}
});
*/
let receipt = web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'));
console.log ('Transaction Receipt: ' + receipt);
return receipt;
}
if (typeof argv.isminter !== 'undefined') {
// Check the token sale contract allow to mint token or not?
tokenSaleContract.isMinter( ( error, status) => {
if(!error) {
console.log(JSON.stringify(status));
} else {
console.error(error);
}
});
//console.log('IsMinter() exit.');
return;
}
if (typeof argv.updatetokenaddress !== 'undefined') {
let solidityFunction = new SolidityFunction('', _.find(tokenSaleAbi, { name: 'setTokenAddress' }), '');
let payloadData = solidityFunction.toPayload([tokenAddress]).data;
let rawTx = {
nonce: nonceHex,
gasPrice: gasPriceHex,
gasLimit: gasLimitHex,
to: tokenSaleAddress,
from: accounts[selectedAccountIndex].address,
data: payloadData
};
let tx = new Tx(rawTx);
tx.sign(privateKey);
let serializedTx = tx.serialize();
web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function (err, hash) {
if (err) {
console.log('Error:');
console.log(err);
}
else {
console.log('Transaction receipt hash pending');
console.log(hash);
}
});
return;
}
if (typeof argv.enableminter !== 'undefined') {
// Add Token Sale Contract as Minter
let solidityFunction = new SolidityFunction('', _.find(tokenAbi, { name: 'addMinter' }), '');
let payloadData = solidityFunction.toPayload([tokenSaleAddress]).data;
let rawTx = {
nonce: nonceHex,
gasPrice: gasPriceHex,
gasLimit: gasLimitHex,
to: tokenAddress,
from: accounts[selectedAccountIndex].address,
data: payloadData
};
let tx = new Tx(rawTx);
tx.sign(privateKey);
let serializedTx = tx.serialize();
web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function (err, hash) {
if (err) {
console.log('Error:');
console.log(err);
}
else {
console.log('Transaction receipt hash pending');
console.log(hash);
}
});
return;
}
if (typeof argv.checkminter !== 'undefined') {
console.log ('Check Address: ' + argv.checkminter);
tokenContract.isMinter( argv.checkminter, ( error, status) => {
if(!error) {
console.log(JSON.stringify(status));
} else {
console.error(error);
}
});
return;
}
if (typeof argv.addpricetier !== 'undefined') {
console.log('Add Price Tier -->');
let tierTimestamp = 0;
let tierTokenPrice = 0;
let tierMaxSupply = 0;
let tokenDecimals = tokenContract.decimals();
console.log('Token Decimals: ' + tokenDecimals);
if (typeof argv.date !== 'undefined') {
tierTimestamp = new Date(argv.date).getTime() / 1000;
console.log('Timestamp: ' + tierTimestamp);
}
if (typeof argv.tokenprice !== 'undefined') {
tierTokenPrice = argv.tokenprice;
console.log('Token Price: ' + tierTokenPrice);
}
if (typeof argv.maxsupply !== 'undefined') {
tierMaxSupply = argv.maxsupply * Math.pow(10, tokenDecimals);
console.log('Max Supply: ' + tierMaxSupply);
}
addPriceTier(tierTokenPrice, tierMaxSupply, tierTimestamp);
return;
}
if (typeof argv.generatepricetiers !== 'undefined') {
console.log('Generate Price Tier -->');
let tokenDecimals = tokenContract.decimals();
let tierTokenPrice = 1000;
let tierTimestamp = new Date('2018-09-26').getTime() / 1000;
let tierMaxSupply = 100000 * Math.pow(10, tokenDecimals);
addPriceTier(tierTokenPrice, tierMaxSupply, tierTimestamp);
nonce++;
nonceHex = web3.toHex(nonce);
tierTokenPrice = 900;
tierTimestamp = new Date('2018-09-28').getTime() / 1000;
tierMaxSupply = 200000 * Math.pow(10, tokenDecimals);
addPriceTier(tierTokenPrice, tierMaxSupply, tierTimestamp);
nonce++;
nonceHex = web3.toHex(nonce);
tierTokenPrice = 800;
tierTimestamp = new Date('2018-09-30').getTime() / 1000;
tierMaxSupply = 300000 * Math.pow(10, tokenDecimals);
addPriceTier(tierTokenPrice, tierMaxSupply, tierTimestamp);
nonce++;
nonceHex = web3.toHex(nonce);
tierTokenPrice = 700;
tierTimestamp = new Date('2018-10-02').getTime() / 1000;
tierMaxSupply = 400000 * Math.pow(10, tokenDecimals);
addPriceTier(tierTokenPrice, tierMaxSupply, tierTimestamp);
nonce++;
nonceHex = web3.toHex(nonce);
tierTokenPrice = 600;
tierTimestamp = new Date('2018-10-04').getTime() / 1000;
tierMaxSupply = 0;
addPriceTier(tierTokenPrice, tierMaxSupply, tierTimestamp);
}
if (typeof argv.pricetierscount !== 'undefined') {
tokenSaleContract.priceTiersCount( ( error, status) => {
if(!error) {
console.log ('Price Tier Count: ' + status);
} else {
console.error(error);
}
});
return;
}
if (typeof argv.getpricetier !== 'undefined') {
console.log('Get Price Tier -> ');
tokenSaleContract.getPriceTier( argv.getpricetier, ( error, status) => {
if(!error) {
console.log(JSON.stringify(status));
let tierDate = new Date(status[3] * 1000);
console.log ('Tier Date: ' + tierDate.getUTCFullYear() + '-'
+ (tierDate.getUTCMonth()+1) + '-' + tierDate.getUTCDate() + ' '
+ tierDate.getUTCHours() + ':' + tierDate.getUTCMinutes() + ':' + tierDate.getUTCSeconds());
} else {
console.error(error);
}
});
return;
}
if (typeof argv.getcurrentpricetierindex !== 'undefined') {
console.log('Get Current Price Tier Index -> ');
tokenSaleContract.getCurrentPriceTierIndex( ( error, status) => {
if(!error) {
console.log(JSON.stringify(status));
} else {
console.error(error);
}
});
return;
}
if (typeof argv.buytoken !== 'undefined') {
let accountIndex = selectedAccountIndex;
if (typeof argv.account !== 'undefined') {
accountIndex = argv.account;
}
let amountInWei = web3.toWei(argv.buytoken);
let amountInWeiHex = web3.toHex(amountInWei);
console.log('From Account: ' + accounts[accountIndex].address);
console.log('Buy Ether (Wei): ' + amountInWei);
nonce = web3.eth.getTransactionCount(accounts[accountIndex].address);
nonceHex = web3.toHex(nonce);
privateKey = new Buffer(accounts[accountIndex].key, 'hex');
let rawTx = {
nonce: nonceHex,
gasPrice: gasPriceHex,
gasLimit: gasLimitHex,
to: tokenSaleAddress,
from: accounts[accountIndex].address,
value: amountInWeiHex
};
let tx = new Tx(rawTx);
tx.sign(privateKey);
let serializedTx = tx.serialize();
let receipt = web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'));
console.log('Transaction Receipt: ' + receipt);
return;
}
if (typeof argv.balance !== 'undefined') {
console.log('Token Sale Ether Balance: ' + (web3.eth.getBalance(tokenSaleAddress) / 1e18).toFixed(18));
return;
}
if (typeof argv.withdraw !== 'undefined') {
let accountIndex = selectedAccountIndex;
if (typeof argv.account !== 'undefined') {
accountIndex = argv.account;
}
nonce = web3.eth.getTransactionCount(accounts[accountIndex].address);
nonceHex = web3.toHex(nonce);
privateKey = new Buffer(accounts[accountIndex].key, 'hex');
console.log('Withdraw To Account: ' + accounts[accountIndex].address);
console.log('Withdraw Ether: ' + (web3.eth.getBalance(tokenSaleAddress) / 1e18).toFixed(18));
console.log('Nonce: ' + nonce);
let solidityFunction = new SolidityFunction('', _.find(tokenSaleAbi, { name: 'withdraw' }), '');
let payloadData = solidityFunction.toPayload([]).data;
let rawTx = {
nonce: nonceHex,
gasPrice: gasPriceHex,
gasLimit: gasLimitHex,
to: tokenSaleAddress,
from: accounts[accountIndex].address,
data: payloadData
};
let tx = new Tx(rawTx);
tx.sign(privateKey);
let serializedTx = tx.serialize();
let receipt = web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'));
console.log ('Transaction Receipt: ' + receipt);
return;
}
return;
}
if (typeof argv.bundle !== 'undefined') {
var bundler = browserify('ipfs-src.js');
bundler.transform('uglifyify', { global: true })
bundler.bundle().pipe(fs.createWriteStream('www/assets/js/bundle.js'));
console.log('JS bundle done.');
return;
}
/*
if (typeof argv.capturetheether !== 'undefined') {
if (typeof argv.callmechallenge !== 'undefined') {
let callMeChallengeSource = 'CallMeChallenge.sol';
let jsonCallMeChallengeFilename = path.parse(callMeChallengeSource).name + '.json';
let jsonCallMeChallengeFile = './build/contracts/' + jsonCallMeChallengeFilename;
let callMeChallengeJsonContent = fs.readFileSync(jsonCallMeChallengeFile, 'utf8');
let callMeChallengeJsonOutput = JSON.parse(callMeChallengeJsonContent);
let callMeChallengeAbi = callMeChallengeJsonOutput['contracts'][callMeChallengeSource][path.parse(callMeChallengeSource).name]['abi'];
let callMeChallengeAddress = '0x96Cf79D162b0e2BcaefB4BD75236D4531596262e';
let callMeChallengeContract = web3.eth.contract(callMeChallengeAbi).at(callMeChallengeAddress);
nonce = web3.eth.getTransactionCount(accounts[selectedAccountIndex].address);
nonceHex = web3.toHex(nonce);
privateKey = new Buffer(accounts[selectedAccountIndex].key, 'hex');
let solidityFunction = new SolidityFunction('', _.find(callMeChallengeAbi, { name: 'callme' }), '');
let payloadData = solidityFunction.toPayload([]).data;
let rawTx = {
nonce: nonceHex,
gasPrice: gasPriceHex,
gasLimit: gasLimitHex,
to: callMeChallengeAddress,
from: accounts[selectedAccountIndex].address,
data: payloadData
};
let tx = new Tx(rawTx);
tx.sign(privateKey);
let serializedTx = tx.serialize();
let receipt = web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'));
console.log ('Transaction Receipt: ' + receipt);
return;
}
return;
}
*/
console.log('End here.');