-
Notifications
You must be signed in to change notification settings - Fork 17
/
verify.ts
281 lines (235 loc) · 9.54 KB
/
verify.ts
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
const commandLineArgs = require('command-line-args');
const commandLineUsage = require('command-line-usage');
const ora = require('ora');
require('dotenv').config();
const axios = require('axios');
const glob = require('glob');
const path = require('path');
import * as truffleFlattener from 'truffle-flattener';
// tslint:disable-next-line: variable-name
const Web3 = require('web3');
const fs = require('fs');
const querystring = require('querystring');
const abi = require('ethereumjs-abi');
import { promisify } from 'es6-promisify';
const sleep = (milliseconds: number): Promise<any> => {
return new Promise((resolve: () => void): any => setTimeout(resolve, milliseconds));
};
// tslint:disable: max-line-length
const optionDefinitions = [
{ name: 'help', alias: 'h', type: Boolean, description: 'show these command line options' },
{ name: 'version', alias: 'v', type: String, description: 'Optional Arc package version number. Default is the last version listed in migration.json.' },
{ name: 'contractAddress', alias: 'a', type: String, description: 'Optional address of a single contract to verify, instead of the default. Must be given with \'contractName\'.' },
{ name: 'contractName', alias: 'c', type: String, description: 'Optional name of a single contract to verify, instead of verifying all of them.' },
{ name: 'network', alias: 'n', type: String, description: 'Required name of the network, such as kovan, rinkeby or mainnet.' },
{ name: 'provider', alias: 'p', type: String, description: 'Required url for web3 network provider (not needed for -g).' },
{ name: 'check', alias: 'g', type: String, description: 'Given verify GUID, check contract verification status. Ignores other options.' },
{ name: 'outputFlattened', alias: 'f', type: String, description: 'When verifying, absolute path where to save the flattened .sol file to flattened.["contractName"].sol.' },
];
// tslint:enable: max-line-length
const options = commandLineArgs(optionDefinitions);
const usage = (): void => {
const sections = [
{
content: 'Verify migrated Arc contracts to EtherScan.io',
header: 'Verify Contracts',
},
{
header: 'Options',
optionList: optionDefinitions,
},
];
console.log(commandLineUsage(sections));
};
const error = (message: string, showUsage: boolean = false) => {
console.error(message);
if (showUsage) {
usage();
}
process.exit(1);
};
if (options.help) {
usage();
process.exit(0);
}
if (!options.network) {
error(`'network' is required. Run 'npm run help' for cli information.`);
}
if (options.contractAddress && !options.contractName) {
error(`contractName is required when contractAddress is given. Run 'npm run help' for cli information.`);
}
const APIKEY = process.env.APIKEY;
if (!APIKEY) {
error(`APIKEY is not found. See README.md for more information.`);
}
let addresses = {} as any;
let allAddresses = {} as any;
let web3: any;
if (!options.check) {
if (!options.provider) {
error(`'provider' is required. Run 'npm run help' for cli information.`);
}
/**
* Enumerate contract names (each with its migrated address) and
* fetch the .sol file for it.
*/
const migratedContracts = require('./migration.json');
options.version = options.version ||
migratedContracts[options.network] &&
// tslint:disable-next-line: max-line-length
(Object.keys(migratedContracts[options.network].base)[Object.keys(migratedContracts[options.network].base).length - 1]);
allAddresses = (migratedContracts[options.network] &&
migratedContracts[options.network].base[options.version]) || undefined;
if (!allAddresses) {
error(`contracts were not deployed to ${options.network}, or invalid network name`);
}
if (options.contractName && options.contractAddress) {
addresses[options.contractName] = options.contractAddress;
} else {
if (options.contractName) {
if (!allAddresses[options.contractName]) {
error(
`contract ${options.contractName} has not been not deployed to ${options.network}, or invalid contract name`);
}
addresses[options.contractName] = allAddresses[options.contractName];
} else {
addresses = allAddresses;
}
}
web3 = new Web3(options.provider);
}
const processContracts = async (): Promise<any> => {
let exitCode = 1;
let anySucceeded = false;
let spinner: any;
try {
const etherscanUrl =
`https://api${options.network === 'mainnet' ? '' : ('-' + options.network)}.etherscan.io/api`;
if (options.check) {
// check is the GUID
const result = await validateResult(options.check, etherscanUrl);
console.log(`Verification status: ${result.result}`);
exitCode = 0;
} else {
console.log(`Verifying contract(s) on ${options.network}, Arc version ${options.version}...`);
// tslint:disable-next-line: forin
for (const contractName in addresses) {
let constructorArguments: string;
const address = addresses[contractName];
switch (contractName) {
case 'GEN':
case 'ControllerCreator':
// ignore these
continue;
case 'Redeemer':
constructorArguments = await getConstructorParams(address, 128);
break;
case 'GenesisProtocol':
case 'DaoCreator':
case 'DAORegistry':
constructorArguments = await getConstructorParams(address, 64);
break;
}
spinner = ora();
spinner.start(`${contractName} at ${addresses[contractName]}`);
let foundIn = './node_modules/@daostack/arc';
process.chdir(path.join(__dirname, foundIn));
let solFilePath = glob.sync(`./contracts/**/${contractName}.sol`);
if (!solFilePath.length) {
foundIn = './node_modules/@daostack/infra';
process.chdir(path.join(__dirname, foundIn));
solFilePath = glob.sync(`./contracts/**/${contractName}.sol`);
}
if (!solFilePath.length) {
foundIn = './node_modules/@daostack/arc-hive';
process.chdir(path.join(__dirname, foundIn));
solFilePath = glob.sync(`./contracts/**/${contractName}.sol`);
}
if (!solFilePath.length) {
spinner.fail(`contract ${contractName}.sol not found`);
continue;
}
const flattened = await truffleFlattener([solFilePath[0]]);
if (options.outputFlattened) {
fs.writeFileSync(path.join(options.outputFlattened, `flattened.${contractName}.sol`), flattened);
}
const version = 'v0.5.17+commit.d19bba13';
const apiConfig = {
action: 'verifysourcecode',
apikey: APIKEY,
compilerVersion: version,
constructorArguements: constructorArguments,
// constructorArguements: constructorArguments ? constructorArguments.toString('hex') : '',
contractaddress: addresses[contractName],
contractname: contractName,
module: 'contract',
optimizationUsed: '1',
runs: '200',
sourceCode: flattened,
};
const encodedPostData = querystring.stringify(apiConfig).replace(/%20/g, '+');
const response = await axios.post(etherscanUrl, encodedPostData,
{
headers: {
'Content-Length': encodedPostData.length,
'Content-Type': 'application/x-www-form-urlencoded',
},
}
);
const result = response.data;
if (result.status === '1') {
// 1 = submission success, use the guid returned (result.result) to check the status of the submission.
anySucceeded = true;
const verifyResult = await validateResult(result.result, etherscanUrl);
if (verifyResult.result !== 'Pending in queue') {
spinner.fail(
`${contractName} at ${addresses[contractName]} ${verifyResult.result}, GUID: ${result.result}`);
} else {
spinner.succeed(
`${contractName} at ${addresses[contractName]} ${verifyResult.result}, GUID: ${result.result}`);
}
} else {
if (result.result.indexOf('already verified') !== -1) {
spinner.info(`${contractName} at ${addresses[contractName]} ${result.result}`);
} else {
spinner.fail(`${contractName} at ${addresses[contractName]} ${result.result}`);
}
}
// await sleep(500);
// spinner.succeed(`${ contractName } at ${ addresses[contractName] } `);
}
exitCode = 0;
}
} catch (ex) {
if (spinner) {
spinner.fail();
}
console.error(ex.message);
} finally {
if (anySucceeded) {
// tslint:disable-next-line: max-line-length
console.log(`In-queue validation(s) may or may succeed. To confirm, use -g option with GUID or check on https://${options.network === 'mainnet' ? '' : (options.network + '.')}etherscan.io/contractsVerified`);
}
}
process.exit(exitCode);
};
/**
* Given a GUID, confirms whether the validation really did succeed
* @param guid
* @param url
*/
const validateResult = async (guid: string, url: string): Promise<any> => {
const response = await axios.get(url,
{
params:
{ module: 'contract', action: 'checkverifystatus', guid },
});
return response.data;
};
// const contructorParamsPrefix = 'a165627a7a72305820';
const getConstructorParams = async (address: string, bytes: number): Promise<string> => {
const code = await promisify((callback: any): any =>
web3.eth.getCode(address, callback))() as string;
return code.substr(code.length - bytes);
};
processContracts();