/
sol-verifier.js
339 lines (316 loc) · 12.8 KB
/
sol-verifier.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
'use strict';
const fs = require('fs');
const parser = require('solidity-parser-antlr');
const Verifier = require('../index');
const { deployContract } = require('./utils/deploy');
const solReleases = require('../lib/solReleases');
const mockMap = require('./utils/mock_mapping.json');
const { getPragma, processFile } = require('./utils/import');
require('chai').should();
function sleep (ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const ETHERSCAN_KEY = process.env.KEY;
describe('sol-verifier', () => {
describe('Deploying & Verifying Sample.sol', () => {
let contractAddress;
let contractName;
let network;
let sampleData;
let path;
before('Deploy Sample.sol', async () => {
try{
contractName = 'Sample';
network = 'rinkeby';
path = __dirname + '/contracts/'+ contractName +'.sol';
const contractSource = fs.readFileSync(path, 'UTF-8');
const parsedData = parser.parse(contractSource).children;
const compiler = await solReleases.getCompilerVersion(parsedData, mockMap);
contractAddress = await deployContract(contractName, network, compiler);
await sleep(30000); // To make sure that contractCode is stored
}catch(err){
throw err;
}
});
it('Verifies Sample.sol contract successfully', async () => {
sampleData = {
key: ETHERSCAN_KEY,
path : path,
contractAddress: contractAddress,
network : network,
};
const response = await Verifier.verifyContract(sampleData);
await sleep(30000);
response.status.should.equal('1');
});
it('Trying to verify already verified contract (should fail)', async () => {
await sleep(30000); // To make sure that etherscan gets sufficient time to verify the contract above
try {
await Verifier.verifyContract(sampleData);
}catch(error) {
error.message.should.equal('Contract source code already verified');
}
});
it('Trying to verify contract without passing Etherscan API key (should fail)', async () => {
try{
const temp = {
key: '',
path : path,
contractAddress: contractAddress,
network : network };
await Verifier.verifyContract(temp);
}catch(error) {
error.message.should.equal('Missing or invalid ApiKey');
}
});
it('Trying to verify contract by passing non-existing Ethereum network (should fail)', async () => {
const temp = {
key: ETHERSCAN_KEY,
path : path,
contractAddress: contractAddress,
network : 'random',
};
try{
await Verifier.verifyContract(temp);
}catch(err){
err.message.should.equal('Invalid Network/Network Not Supported!!!');
}
});
it('Trying to verify contract by passing invalid contract address (should fail)', async () => {
try{
sampleData.contractAddress = '0x1234567890';
await Verifier.verifyContract(sampleData);
} catch (error) {
error.message.should.equal('Missing or invalid contractAddress (should start with 0x)');
}
});
});
describe('Compiling & Verifying Sample.sol with all explicit options', () => {
let contractAddress;
let contractName;
let network;
let sampleData;
let path;
before('Compile & Deploy Sample.sol', async () => {
try{
contractName = 'Sample';
network = 'rinkeby';
path = __dirname + '/contracts/'+ contractName +'.sol';
const contractSource = fs.readFileSync(path, 'UTF-8');
const parsedData = parser.parse(contractSource).children;
const compiler = await solReleases.getCompilerVersion(parsedData, mockMap);
contractAddress = await deployContract(contractName, network, compiler, null, [], true, 2000);
await sleep(30000); // To make sure that contractCode is stored
}catch(err){
throw err;
}
});
it('Verifies Sample.sol contract successfully by enabling optimization', async () => {
sampleData = {
key: ETHERSCAN_KEY,
path : path,
contractAddress: contractAddress,
network : network,
runs : 2000,
evmVersion: 'byzantium',
licenseType: 3,
optimizationFlag: true, // Optimization Enabled
};
const response = await Verifier.verifyContract(sampleData);
response.status.should.equal('1');
});
});
describe('Compiling & Verifying SampleOld.sol with version 0.4.18', () => {
let contractAddress;
let contractName;
let network;
let sampleData;
let path;
before('Compile & Deploy SampleOld.sol', async () => {
try{
contractName = 'SampleOld';
network = 'rinkeby';
path = __dirname + '/contracts/'+ contractName +'.sol';
const contractSource = fs.readFileSync(path, 'UTF-8');
const parsedData = parser.parse(contractSource).children;
const compiler = await solReleases.getCompilerVersion(parsedData, mockMap);
contractAddress = await deployContract(contractName, network, compiler, null, [], false);
await sleep(30000); // To make sure that contractCode is stored
}catch(err){
throw err;
}
});
it('Verifies SampleOld.sol contract successfully with old way of defining constructor', async () => {
sampleData = {
key: ETHERSCAN_KEY,
path : path,
contractAddress: contractAddress,
network : network,
};
const response = await Verifier.verifyContract(sampleData);
response.status.should.equal('1');
});
});
describe('Deploying & Verifying SampleWithConstructor.sol', () => {
let contractAddress;
let contractName;
let network;
let sampleData;
const constructParams = [];
let path;
before('Deploy SampleWithConstructor.sol', async () => {
try{
contractName = 'SampleWithConstructor';
network = 'rinkeby';
constructParams.push(['0x48656c6c6f20576f726c64210000000000000000000000000000000000000000', '0x48656c6c6f20576f726c64220000000000000000000000000000000000000000']); // eslint-disable-line max-len
constructParams.push([250, 251, 252]); // Fix size array
constructParams.push('nickname'); // string
path = __dirname + '/contracts/'+ contractName +'.sol';
const contractSource = fs.readFileSync(path, 'UTF-8');
const parsedData = parser.parse(contractSource).children;
const compiler = await solReleases.getCompilerVersion(parsedData, mockMap);
contractAddress = await deployContract(contractName, network, compiler, null, constructParams);
await sleep(40000); // To make sure that contractCode is stored
}catch(err){
throw err;
}
});
it('Verifies SampleWithConstructor.sol contract with array params in constructor successfully', async () => {
sampleData = {
key : ETHERSCAN_KEY,
path : path,
contractAddress: contractAddress,
network : network,
cvalues : constructParams,
};
const response = await Verifier.verifyContract(sampleData);
response.status.should.equal('1');
});
it('Trying to verify SampleWithConstructor contract without passing constructor values (should fail)', async () => {
await sleep(10000);
sampleData.cvalues = null;
try{
await Verifier.verifyContract(sampleData);
}catch(err){
err.message.should.equal('Constructor Found!!! Please Provide --constructParams Option');
}
});
});
describe('Deploying & Verifying MultiContractSample.sol', () => {
let contractAddress;
let contractName;
let network;
let sampleData;
const constructParams = [];
let path;
before('Deploy MultiContractSample.sol', async () => {
try{
contractName = 'MultiContractSample';
network = 'rinkeby';
constructParams.push(40);
path = __dirname + '/contracts/'+ contractName +'.sol';
const contractSource = fs.readFileSync(path, 'UTF-8');
const parsedData = parser.parse(contractSource).children;
const compiler = await solReleases.getCompilerVersion(parsedData, mockMap);
contractAddress = await deployContract(contractName, network, compiler, null, constructParams);
await sleep(30000); // To make sure that contractCode is stored
}catch(err){
throw err;
}
});
it('Verifies MultiContractSample.sol contract successfully', async () => {
sampleData = {
key : ETHERSCAN_KEY,
path : path,
contractAddress: contractAddress,
network : network,
cvalues : constructParams,
contractName : contractName,
};
const response = await Verifier.verifyContract(sampleData);
response.status.should.equal('1');
});
it('Trying to verify MultiContractSample contract with passing contractName(should fail)', async () => {
await sleep(10000);
sampleData.contractName = null;
try{
await Verifier.verifyContract(sampleData);
}catch(err){
err.message.should.equal('More Than One Contracts in File!!! Please Provide --contractName Option');
}
});
});
describe('Deploying & Verifying contract with import', () => {
let contractAddress;
let contractName;
let network;
let sampleData;
it('Deploys & verifies contract with relative file import successfully', async () => {
contractName = 'SampleWithImport';
network = 'rinkeby';
const pathToDeploy = __dirname + '/contracts/'+ 'SampleWithImport_merged' +'.sol'; // Pre merged contract
const pathToVerify = __dirname + '/contracts/'+ contractName +'.sol';
const pragma = await getPragma(pathToVerify);
const contractSource = await processFile(pathToVerify, true);
const parsedData = parser.parse(pragma + '\n\n' + contractSource).children;
const compiler = await solReleases.getCompilerVersion(parsedData, mockMap);
contractAddress = await deployContract(contractName, network, compiler, pathToDeploy);
await sleep(40000); // To make sure that contractCode is stored
sampleData = {
key : ETHERSCAN_KEY,
path : pathToVerify,
contractAddress: contractAddress,
network : network,
contractName : contractName,
};
const response = await Verifier.verifyContract(sampleData);
response.status.should.equal('1');
});
it('Deploys & verifies contract with node_modules file import successfully', async () => {
contractName = 'SampleWithNodeModulesImport';
network = 'rinkeby';
const pathToDeploy = __dirname + '/contracts/'+ 'SampleWithNodeModulesImport_merged' +'.sol';
const pathToVerify = __dirname + '/contracts/'+ contractName +'.sol';
const pragma = await getPragma(pathToVerify);
const contractSource = await processFile(pathToVerify, true);
const parsedData = parser.parse(pragma + '\n\n' + contractSource).children;
const compiler = await solReleases.getCompilerVersion(parsedData, mockMap);
contractAddress = await deployContract(contractName, network, compiler, pathToDeploy);
await sleep(40000); // To make sure that contractCode is stored
sampleData = {
key : ETHERSCAN_KEY,
path : pathToVerify,
contractAddress: contractAddress,
network : network,
contractName : contractName,
};
const response = await Verifier.verifyContract(sampleData);
response.status.should.equal('1');
});
it('Deploys & verifies contract with user defined type in construtor successfully', async () => {
contractName = 'SampleWithUserDefinedType';
network = 'rinkeby';
const constructParams = [];
constructParams.push('0x0000000000000000000000000000000000000000');
constructParams.push(12345);
const pathToDeploy = __dirname + '/contracts/'+ 'SampleWithUserDefinedType_merged' +'.sol';
const pathToVerify = __dirname + '/contracts/'+ contractName +'.sol';
const pragma = await getPragma(pathToVerify);
const contractSource = await processFile(pathToVerify, true);
const parsedData = parser.parse(pragma + '\n\n' + contractSource).children;
const compiler = await solReleases.getCompilerVersion(parsedData, mockMap);
contractAddress = await deployContract(contractName, network, compiler, pathToDeploy, constructParams);
await sleep(40000); // To make sure that contractCode is stored
sampleData = {
key : ETHERSCAN_KEY,
path : pathToVerify,
contractAddress: contractAddress,
network : network,
cvalues : constructParams,
contractName : contractName,
};
const response = await Verifier.verifyContract(sampleData);
response.status.should.equal('1');
});
});
});