forked from thundercore/tt20
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy-contracts
executable file
·156 lines (143 loc) · 5.49 KB
/
deploy-contracts
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
#!/usr/bin/env node
// module imports
const child_process = require('child_process');
const fs = require('fs');
const path = require('path');
const JSON5 = require('json5');
const program = require('commander');
const Web3 = require('web3');
const viewblock = require('./viewblock');
const flattenContracts = require('./flattenContracts');
const truffleConfig = require('./truffle-config');
const BN = Web3.utils.BN;
// command line parsing
program.usage('[options]')
.option('--reset', 'call "truffle migrate --reset"')
.option('--dry-run', 'do not actually call "truffle migrate"')
.option('--network [network]', 'use the named network specified in `truffle-config.js`')
.option('--verbose-rpc', 'pass --verbose-rpc to "truffle migrate"')
.parse(process.argv);
// call "truffle migrate"
const truffleMigrateArgs = [ 'migrate' ];
if (program.reset) {
truffleMigrateArgs.push('--reset');
}
if (program.network) {
truffleMigrateArgs.push(...['--network', program.network]);
}
if (program.verboseRpc) {
truffleMigrateArgs.push('verbose-rpc');
}
console.log('Command:', [ 'truffle' ].concat(truffleMigrateArgs));
if (!program.dryRun) {
// will throw an Error on non-zero subprocess exit
child_process.execFileSync('truffle', truffleMigrateArgs, { stdio: 'inherit' });
}
function readDir(dirPath) {
return new Promise((resolve, reject) => {
fs.readdir(dirPath, function(err, dirContents) {
if (err) {
reject(err);
return;
}
resolve(dirContents);
});
});
}
/*
// call "truffle run save-per-network-deployment-record"
const cmdArgs = [ 'run', 'save-per-network-deployment-record' ];
if (program.network) {
cmdArgs.push(...['--network', program.network]);
}
console.log('Command:', [ 'truffle' ].concat(cmdArgs));
if (!program.dryRun) {
child_process.execFileSync('truffle', cmdArgs, { stdio: 'inherit' });
}
(async function() {
if (viewblock.supportedNetworkName(program.network)) {
// read config files
let localConfStr;
try {
localConfStr = fs.readFileSync('local.jsonc', { encoding: 'utf8' });
} catch (err) {
console.error('please create a "local.jsonc" file with a "viewblockApiKey" key');
return;
}
const localConf = JSON5.parse(localConfStr);
// concat Solidity sources and their transitive dependencies in `flattened/`
await flattenContracts.flatten();
const dirPath = 'flattened';
const dirContents = await readDir(dirPath);
for (let i = 0; i < dirContents.length; i++) {
const n = dirContents[i];
const p = path.join(dirPath, n);
const sourceCode = fs.readFileSync(p, {encoding: 'utf-8'});
if (sourceCode === undefined || sourceCode === null || sourceCode.length === 0) {
console.error('failed to read contract source code');
return;
}
const contractName = path.basename(n, '.sol');
const contractDataStr = fs.readFileSync(path.join('build', 'contracts', contractName + '.json'));
const contractData = JSON5.parse(contractDataStr);
const solcParams = truffleConfig['compilers']['solc'];
// FIXME: easily access the eth provider for both truffleConfig[network].provider and truffleConfig[network].{host,port}
const networkId = truffleConfig['networks'][program.network]['network_id'];
try {
if (contractData['networks'][networkId] === undefined) {
console.error(`contract "${contractName}" not deployed on network ${networkId}, skipping.`);
continue;
}
await viewblock.verifyContract(localConf.viewblockApiKey,
'thundercore' // chain,
sourceCode // code ,
contractData['networks'][networkId]['address'] // address,
contractName // name,
viewblock.solcVersionMap(solcParams['version']) // solcVersion,
solcParams['settings']['optimizer']['enabled'] // optimized,
solcParams['settings']['optimizer']['runs'] // runs,
solcParams['settings']['evmVersion'] // evmVersion,
null // libraries,
);
} catch(error) {
console.error('viewblock.verifyContract failed:', error);
}
}
}
})();
const transfer = async(to, amount) => {
const t = new BN(amount);
const cmdArgs = [];
if (program.network) {
cmdArgs.push(...['--network', program.network]);
}
cmdArgs.push(...[ 'exec', 'transfer.js', to, t.toString() ]);
console.log('Command:', [ 'truffle' ].concat(cmdArgs));
if (!program.dryRun) {
child_process.execFileSync('truffle', cmdArgs, { stdio: 'inherit' });
}
};
const issueToken = async(to, amount) => {
const t = new BN(amount);
const cmdArgs = [];
if (program.network) {
cmdArgs.push(...['--network', program.network]);
}
cmdArgs.push(...[ 'exec', 'issue.js', to, t.toString() ]);
console.log('Command:', [ 'truffle' ].concat(cmdArgs));
//if (!program.dryRun) {
child_process.execFileSync('truffle', cmdArgs, { stdio: 'inherit' });
//}
};
(async () => {
const contractDataStr = fs.readFileSync(path.join('build', 'contracts', 'Token.json'));
const contractData = JSON5.parse(contractDataStr);
// FIXME: easily access the eth provider for both truffleConfig[network].provider and truffleConfig[network].{host,port}
const networkId = truffleConfig['networks'][program.network]['network_id'];
const tokenAddress = contractData['networks'][networkId]['address']
// transfer 1000 into token contract instance
await transfer(tokenAddress , 1000);
// NOTE:
//await issueToken('ADDRESS', (new BN(AMOUNT*1000*1000)));
})();
*/