This repository has been archived by the owner on Feb 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
/
test.js
377 lines (349 loc) · 13 KB
/
test.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
var TestRPC = require("ethereumjs-testrpc");
var Web3 = require('web3');
var solc = require('solc');
var fs = require('fs');
var async = require('async');
var assert = require('assert');
var BigNumber = require('bignumber.js');
var sha256 = require('js-sha256').sha256;
//Config
var solidityFile = './smart_contract/FirstBloodToken_test.sol';
var contractName = 'FirstBloodToken';
var startBlock = 2326762; //9-26-2016 midnight UTC assuming 14 second blocks
var endBlock = 2499819; //10-23-2016 midnight UTC assuming 14 second blocks
function sign(web3, address, value, callback) {
web3.eth.sign(address, value, function(err, sig) {
if (!err) {
try {
var r = sig.slice(0, 66);
var s = '0x' + sig.slice(66, 130);
var v = parseInt('0x' + sig.slice(130, 132), 16);
if (sig.length<132) {
//web3.eth.sign shouldn't return a signature of length<132, but if it does...
sig = sig.slice(2);
r = '0x' + sig.slice(0, 64);
s = '0x00' + sig.slice(64, 126);
v = parseInt('0x' + sig.slice(126, 128), 16);
}
if (v!=27 && v!=28) v+=27;
callback(undefined, {r: r, s: s, v: v});
} catch (err) {
callback(err, undefined);
}
} else {
callback(err, undefined);
}
});
}
describe('Smart contract token test ', function() {
this.timeout(240*1000);
//globals
var web3 = new Web3();
var port = 12345;
var contract;
var contractAddress;
var accounts;
var testCases;
var unit = new BigNumber(Math.pow(10,18));
var founder;
var signer;
before("Initialize TestRPC server", function(done) {
web3.setProvider(new Web3.providers.HttpProvider("http://localhost:"+port));
done();
});
before('Get accounts', function(done) {
web3.eth.getAccounts(function(err, accs) {
if(err) {
done(err);
return;
}
accounts = accs;
assert.equal(accounts.length, 10);
done();
});
});
it('Deploy smart contract', function(done) {
fs.readFile(solidityFile, function(err, result){
var source = result.toString();
var output = solc.compile(source, 1); // 1 activates the optimiser
var abi = JSON.parse(output.contracts[contractName].interface);
var bytecode = output.contracts[contractName].bytecode;
contract = web3.eth.contract(abi);
//Put constructor arguments here:
founder = accounts[0];
signer = accounts[1];
doneCalled = false;
var contractInstance = contract.new(founder, signer, startBlock, endBlock, {from: accounts[0], gas: 3000000, data: bytecode}, function(err, myContract){
assert.equal(err, null);
web3.eth.getTransactionReceipt(myContract.transactionHash, function(err, result){
assert.equal(err, null);
assert.equal(result!=undefined, true);
contractAddress = result.contractAddress;
contract = web3.eth.contract(abi).at(contractAddress);
if (!doneCalled) done();
doneCalled = true;
});
});
});
});
it('Set up test cases', function(done){
var blockNumber = startBlock;
testCases = [];
var numBlocks = 8;
for (i=0; i<numBlocks; i++) {
var blockNumber = Math.round(startBlock + (endBlock-startBlock)*i/(numBlocks-1));
var expectedPrice;
if (blockNumber>=startBlock && blockNumber<startBlock+250) {
expectedPrice = 170;
} else if (blockNumber>endBlock || blockNumber<startBlock) {
expectedPrice = 100;
} else {
//must use Math.floor to simulate Solidity's integer division
expectedPrice = 100 + Math.floor(Math.floor(4*(endBlock - blockNumber)/(endBlock - startBlock + 1))*67/4);
}
var accountNum = Math.max(1,Math.min(i+1, accounts.length-1));
var account = accounts[accountNum];
expectedPrice = Math.round(expectedPrice);
testCases.push(
{
accountNum: accountNum,
blockNumber: blockNumber,
expectedPrice: expectedPrice,
account: account,
}
);
}
done();
});
it('Should sign test cases', function(done) {
async.mapSeries(testCases,
function(testCase, callbackMap) {
var hash = sha256(new Buffer(testCase.account.slice(2),'hex'));
sign(web3, signer, hash, function(err, sig) {
testCase.v = sig.v;
testCase.r = sig.r;
testCase.s = sig.s;
callbackMap(null, testCase);
});
},
function(err, newTestCases) {
testCases = newTestCases;
done();
}
);
});
it('Test price', function(done) {
async.eachSeries(testCases,
function(testCase, callbackEach) {
contract.testPrice(testCase.blockNumber, function(err, result){
assert.equal(err, null);
assert.equal(result.toNumber(), testCase.expectedPrice);
callbackEach();
});
},
function(err) {
done();
}
);
});
it('Test buy', function(done) {
var amountToBuy = 3;
var amountBought = 0;
web3.eth.getBalance(founder, function(err, result){
var initialBalance = result;
async.eachSeries(testCases,
function(testCase, callbackEach) {
contract.setBlockNumber(testCase.blockNumber, {from: testCase.account, value: 0}, function(err, result){
assert.equal(err, null);
contract.buy(testCase.v, testCase.r, testCase.s, {from: testCase.account, value: web3.toWei(amountToBuy, "ether")}, function(err, result){
assert.equal(err, null);
amountBought += amountToBuy;
contract.balanceOf(testCase.account, function(err, result){
assert.equal(err, null);
assert.equal(result.equals(unit.times(new BigNumber(testCase.expectedPrice)).times(new BigNumber(amountToBuy))), true);
callbackEach();
});
});
});
},
function(err) {
web3.eth.getBalance(founder, function(err, result){
var finalBalance = result;
assert.equal(finalBalance.minus(initialBalance).equals(unit.times(new BigNumber(amountBought))), true);
done();
});
}
);
});
});
it('Test buying on behalf of a recipient', function(done) {
var amountToBuy = web3.toWei(1, "ether");
contract.setBlockNumber(endBlock-10, {from: accounts[0], value: 0}, function(err, result){
assert.equal(err, null);
contract.balanceOf(accounts[2], function(err, result){
var initialBalance = result;
var hash = sha256(new Buffer(accounts[1].slice(2),'hex'));
sign(web3, signer, hash, function(err, sig) {
contract.buyRecipient(accounts[2], sig.v, sig.r, sig.s, {from: accounts[1], value: amountToBuy}, function(err, result){
assert.equal(err, null);
contract.price(function(err, result){
var price = result;
contract.balanceOf(accounts[2], function(err, result){
var finalBalance = result;
assert.equal(finalBalance.sub(initialBalance).equals((new BigNumber(amountToBuy)).times(price)), true);
done();
});
});
});
});
});
});
});
it('Test halting, buying, and failing', function(done) {
contract.halt({from: founder, value: 0}, function(err, result){
assert.equal(err, null);
var hash = sha256(new Buffer(accounts[1].slice(2),'hex'));
sign(web3, signer, hash, function(err, sig) {
contract.buy(sig.v, sig.r, sig.s, {from: accounts[1], value: web3.toWei(1, "ether")}, function(err, result){
assert.equal(!err, false);
done();
});
});
});
});
it('Test unhalting, buying, and succeeding', function(done) {
contract.unhalt({from: founder, value: 0}, function(err, result){
assert.equal(err, null);
var hash = sha256(new Buffer(accounts[1].slice(2),'hex'));
sign(web3, signer, hash, function(err, sig) {
contract.buy(sig.v, sig.r, sig.s, {from: accounts[1], value: web3.toWei(1, "ether")}, function(err, result){
assert.equal(!err, true);
done();
});
});
});
});
it('Test buying after the sale ends', function(done) {
contract.setBlockNumber(endBlock+1, {from: accounts[0], value: 0}, function(err, result){
assert.equal(err, null);
var hash = sha256(new Buffer(accounts[1].slice(2),'hex'));
sign(web3, signer, hash, function(err, sig) {
contract.buy(sig.v, sig.r, sig.s, {from: accounts[1], value: web3.toWei(1, "ether")}, function(err, result){
assert.equal(!err, false);
done();
});
});
});
});
it('Test contract balance is zero', function(done){
web3.eth.getBalance(contractAddress, function(err, balance){
assert.equal(balance.equals(new BigNumber(0)), true);
done();
});
});
it('Test bounty and ecosystem allocation', function(done) {
contract.totalSupply(function(err, result){
var totalSupply = result;
var expectedChange = new BigNumber(totalSupply).div(20).add((new BigNumber(2500000)).times(unit));
var blockNumber = endBlock + 1;
contract.balanceOf(founder, function(err, balance){
var initialFounderBalance = balance;
contract.setBlockNumber(blockNumber, {from: founder, value: 0}, function(err, result){
assert.equal(err, null);
contract.allocateBountyAndEcosystemTokens({from: founder, value: 0}, function(err, result){
contract.balanceOf(founder, function(err, balance){
var finalFounderBalance = balance;
assert.equal(err, null);
assert.equal(finalFounderBalance.minus(initialFounderBalance).equals(new BigNumber(expectedChange)), true);
done();
});
});
});
});
});
});
it('Test bounty and ecosystem allocation twice', function(done) {
contract.allocateBountyAndEcosystemTokens({from: founder, value: 0}, function(err, result){
assert.equal(!err, false);
done();
});
});
it('Test founder token allocation too early', function(done) {
var blockNumber = endBlock + 86400/14 * 366;
contract.allocateFounderTokens({from: founder, value: 0}, function(err, result){
assert.equal(!err, false);
done();
});
});
it('Test founder token allocation on time', function(done) {
contract.presaleTokenSupply(function(err, result){
var totalSupply = result;
var expectedFounderAllocation = new BigNumber(totalSupply).div(10);
var blockNumber = endBlock + 86400/14 * 366;
contract.balanceOf(founder, function(err, balance){
var initialFounderBalance = balance;
contract.setBlockNumber(blockNumber, {from: founder, value: 0}, function(err, result){
assert.equal(err, null);
contract.allocateFounderTokens({from: founder, value: 0}, function(err, result){
contract.balanceOf(founder, function(err, balance){
var finalFounderBalance = balance;
assert.equal(err, null);
assert.equal(finalFounderBalance.minus(initialFounderBalance).equals(expectedFounderAllocation), true);
done();
});
});
});
});
});
});
it('Test founder token allocation twice', function(done) {
contract.allocateFounderTokens({from: founder, value: 0}, function(err, result){
assert.equal(!err, false);
done();
});
});
it('Test founder change by hacker', function(done) {
var newFounder = accounts[1];
var hacker = accounts[1];
contract.changeFounder(newFounder, {from: hacker, value: 0}, function(err, result){
assert.equal(!err, false);
done();
});
});
it('Test founder change', function(done) {
var newFounder = accounts[1];
contract.changeFounder(newFounder, {from: founder, value: 0}, function(err, result){
assert.equal(err, null);
contract.founder(function(err, result){
assert.equal(err, null);
assert.equal(result, newFounder);
done();
});
});
});
it('Test restricted early transfer', function(done) {
var account3 = accounts[3];
var account4 = accounts[4];
var amount = web3.toWei(1, "ether");
var blockNumber = endBlock + 100;
contract.setBlockNumber(blockNumber, {from: founder, value: 0}, function(err, result){
contract.transfer(account3, amount, {from: account4, value: 0}, function(err, result){
assert.equal(!err, false);
done();
});
});
});
it('Test transfer after restricted period', function(done) {
var account3 = accounts[3];
var account4 = accounts[4];
var amount = web3.toWei(1, "ether");
var blockNumber = Math.round(endBlock + 61*86400/14);
contract.setBlockNumber(blockNumber, {from: founder, value: 0}, function(err, result){
assert.equal(err, null);
contract.transfer(account3, amount, {from: account4, value: 0}, function(err, result){
assert.equal(err, null);
done();
});
});
});
});