forked from WingsDao/eth-peg-zone
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbridge.js
531 lines (424 loc) · 16.8 KB
/
bridge.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
/**
* Deploy and test bridge functional.
*
* @module test/bridge
*/
'use strict';
/*global web3,artifacts,assert*/
require('chai').should();
const BSInterface = artifacts.require('BankStorage');
const BridgeInterface = artifacts.require('Bridge');
const PoAInterface = artifacts.require('PoAGovernment');
const ERC20Interface = require('openzeppelin-solidity/build/contracts/ERC20Mintable');
const {getValidators, ZERO_ADDRESS, DESTINATION} = require('./helpers/accounts');
const poaTxs = require('./helpers/poaTxs');
const MAX_FEE = web3.utils.toBN('9999');
function getFee(amount, feePercent) {
return web3.utils.toBN(amount)
.mul(web3.utils.toBN(feePercent))
.div(MAX_FEE.addn(1))
.toString();
}
describe('Bridge', () => {
const ETH_CAPACITY = web3.utils.toWei('2', 'ether');
const ETH_MIN_AMOUNT = web3.utils.toWei('0.01', 'ether');
const TO_EXCHANGE = web3.utils.toWei('1', 'ether');
const TO_WITHDRAW = web3.utils.toWei('0.5', 'ether');
const TO_EXCHANGE_CAPACITY = web3.utils.toWei('5', 'ether');
const TO_EXCHANGE_LESS_MIN = '1';
const ETH_FEE_PERCENTAGE = '100';
const ETH_DECIMALS = '18';
const ERC_BALANCE = web3.utils.toWei('100000000', 'ether');
const ERC_SYMBOL = 'WINGS';
const ERC_DECIMALS = '18';
const ERC_INDEX = '1';
let withdrawIds = 0;
let owner, validators, recipient;
let bridge;
let poa;
let erc20;
let bs;
let ethIndex;
before(async () => {
const accounts = await getValidators();
recipient = accounts.wbAddresses.shift();
owner = accounts.validators.shift();
validators = accounts.validators;
const ERC20 = new web3.eth.Contract(ERC20Interface.abi, null, {
data: ERC20Interface.bytecode
});
erc20 = await ERC20.deploy().send({
from: owner,
gas: 2000000
});
await erc20.methods.mint(owner, ERC_BALANCE).send({
from: owner,
gas: 120000
});
const BS = new web3.eth.Contract(BSInterface.abi, null, {
data: BSInterface.bytecode
});
bs = await BS.deploy({
arguments: []
}).send({
from: owner,
gas: 6000000
});
const Bridge = new web3.eth.Contract(BridgeInterface.abi, null, {
data: BridgeInterface.bytecode
});
bridge = await Bridge.deploy({
arguments: [
ETH_CAPACITY,
ETH_MIN_AMOUNT,
ETH_FEE_PERCENTAGE,
bs.options.address
]
}).send({
from: owner,
gas: 6000000
});
const PoA = new web3.eth.Contract(PoAInterface.abi, null, {
data: PoAInterface.bytecode
});
poa = await PoA.deploy({
arguments: [
bridge.options.address,
bs.options.address
]
}).send({
from: owner,
gas: 6000000
});
poaTxs.setPoA(poa);
const ethTokenAddress = await bridge.methods.getEthTokenAddress().call();
await bs.methods.setup(poa.options.address, ethTokenAddress).send({
from: owner,
gas: 200000
});
await bs.methods.transferOwnership(bridge.options.address).send({
from: owner,
gas: 100000
});
await poa.methods.setup(validators, accounts.wbAddresses).send({
from: owner,
gas: 2000000
});
await bridge.methods.transferOwnership(poa.options.address).send({
from: owner,
gas: 100000
});
ethIndex = await bridge.methods.ethIndex().call();
});
it('should has correct fee for eth', async () => {
const {feePercentage} = await bridge.methods.currencies(ethIndex).call();
feePercentage.should.equal(ETH_FEE_PERCENTAGE);
});
it('should has correct min exchange', async () => {
const {minExchange} = await bridge.methods.currencies(ethIndex).call();
minExchange.should.equal(ETH_MIN_AMOUNT);
});
it('should has correct capacity', async () => {
const {capacity} = await bridge.methods.currencies(ethIndex).call();
capacity.should.equal(ETH_CAPACITY);
});
it('should has correct decimals', async () => {
const {decimals} = await bridge.methods.currencies(ethIndex).call();
decimals.should.equal(ETH_DECIMALS);
});
it('should pause bridge', async () => {
const data = bridge.methods.pause().encodeABI();
const isExecuted = await poaTxs.sendAndConfirm(DESTINATION.TARGET, data, {
from: validators[0],
gas: 600000
}, validators.slice(1));
isExecuted.should.equal(true);
const isPaused = await bridge.methods.paused().call();
isPaused.should.equal(true);
});
it('should resume bridge', async () => {
const data = bridge.methods.resume().encodeABI();
const isExecuted = await poaTxs.sendAndConfirm(DESTINATION.TARGET, data, {
from: validators[0],
gas: 6000000
}, validators.slice(1));
isExecuted.should.equal(true);
const isPaused = await bridge.methods.paused().call();
isPaused.should.equal(false);
});
it('should deposit bridge', async () => {
const {feePercentage} = await bridge.methods.currencies(ethIndex).call();
const exchangeId = await bridge.methods.exchangeId().call();
const receipt = await bridge.methods.exchange(
ethIndex,
recipient,
TO_EXCHANGE
).send({
value: TO_EXCHANGE,
from: owner,
gas: 1000000
});
const realAmount = web3.utils.toBN(TO_EXCHANGE)
.sub(web3.utils.toBN(getFee(TO_EXCHANGE, feePercentage)))
.toString();
receipt.events.should.have.any.keys('CURRENCY_EXCHANGED');
const values = receipt.events.CURRENCY_EXCHANGED.returnValues;
values._currencyId.should.equal(ethIndex);
values._spender.should.equal(owner);
values._recipient.should.equal(recipient);
values._id.should.equal(exchangeId);
values._amount.should.equal(realAmount);
const brBalance = await web3.eth.getBalance(bridge.options.address);
brBalance.should.be.equal('0');
const {balance} = await bridge.methods.currencies(ethIndex).call();
balance.should.equal(realAmount);
});
/**
uint256 _currencyId,
uint256 _withdrawId,
bytes32 _sender,
address payable _recipient,
uint256 _amount,
uint256 _gas
**/
it('should withdraw from bridge', async () => {
const balance = await web3.eth.getBalance(owner);
const data = bridge.methods.withdraw(
ethIndex,
withdrawIds.toString(),
web3.utils.randomHex(32),
owner,
TO_WITHDRAW,
'6000000'
).encodeABI();
const isExecuted = await poaTxs.sendAndConfirm(DESTINATION.TARGET, data, {
from: validators[0],
gas: 6000000
}, validators.slice(1));
isExecuted.should.equal(true);
const newBalance = await web3.eth.getBalance(owner);
const realBalance = web3.utils.toBN(balance).add(web3.utils.toBN(TO_WITHDRAW));
realBalance.toString().should.be.equal(newBalance);
withdrawIds++;
});
it('should update balance after withdraw', async () => {
const {feePercentage} = await bridge.methods.currencies(ethIndex).call();
const realAmount = web3.utils.toBN(TO_EXCHANGE)
.sub(web3.utils.toBN(getFee(TO_EXCHANGE, feePercentage)))
.toString();
const ethBalance = (await bridge.methods.currencies(ethIndex).call()).balance;
const diff = web3.utils.toBN(realAmount).sub(web3.utils.toBN(TO_WITHDRAW)).toString();
ethBalance.should.equal(diff);
});
it('should return correct ETH pseudo token address', async () => {
const ethTokenAddress = await bridge.methods.getEthTokenAddress().call();
ethTokenAddress.should.equal(ZERO_ADDRESS);
});
it('should return correct fee', async () => {
const {feePercentage} = await bridge.methods.currencies(ethIndex).call();
const fee = getFee(TO_EXCHANGE, feePercentage);
const contractFee = await bridge.methods.getFee(ethIndex, TO_EXCHANGE).call();
fee.should.equal(contractFee);
});
it('should change capacity', async () => {
const newCapacity = web3.utils.toBN(ETH_CAPACITY).muln(2).toString();
const data = bridge.methods.changeCapacity(ethIndex, newCapacity).encodeABI();
const isExecuted = await poaTxs.sendAndConfirm(DESTINATION.TARGET, data, {
from: validators[0],
gas: 6000000
}, validators.slice(1));
isExecuted.should.equal(true);
const {capacity} = await bridge.methods.currencies(ethIndex).call();
capacity.should.be.equal(newCapacity);
});
it('should prevent change capacity less then min exchange', async () => {
const newCapacity = '1'; // 1 wei
const data = bridge.methods.changeCapacity(ethIndex, newCapacity).encodeABI();
const isExecuted = await poaTxs.sendAndConfirm(DESTINATION.TARGET,data, {
from: validators[0],
gas: 6000000
}, validators.slice(1));
isExecuted.should.equal(false);
});
it('should change min exchange', async () => {
const newMinExchange = '1';
const data = bridge.methods.changeMinExchange(ethIndex, newMinExchange).encodeABI();
const isExecuted = await poaTxs.sendAndConfirm(DESTINATION.TARGET, data, {
from: validators[0],
gas: 6000000
}, validators.slice(1));
isExecuted.should.equal(true);
const {minExchange} = await bridge.methods.currencies(ethIndex).call();
minExchange.should.equal(newMinExchange);
});
it('should prevent change capacity less then balance', async () => {
const newCapacity = '2';
const data = bridge.methods.changeCapacity(ethIndex, newCapacity).encodeABI();
const isExecuted = await poaTxs.sendAndConfirm(DESTINATION.TARGET, data, {
from: validators[0],
gas: 6000000
}, validators.slice(1));
isExecuted.should.equal(false);
});
it('should prevent exchange because of min exchange', async () => {
const data = bridge.methods.changeMinExchange(ethIndex, ETH_MIN_AMOUNT).encodeABI();
const isExecuted = await poaTxs.sendAndConfirm(DESTINATION.TARGET, data, {
from: validators[0],
gas: 6000000
}, validators.slice(1));
isExecuted.should.equal(true);
return bridge.methods.exchange(
ethIndex,
recipient,
TO_EXCHANGE_LESS_MIN
).send({
value: TO_EXCHANGE_LESS_MIN,
from: owner,
gas: 6000000
}).then(() => {
throw new Error('Expected reject');
}).catch(e => e.message.should.contains('Amount should be great or equal then min exchage'));
});
it('should prevent exchange because of capacity', async () => {
return bridge.methods.exchange(
ethIndex,
recipient,
TO_EXCHANGE_CAPACITY
).send({
value: TO_EXCHANGE_CAPACITY,
from: owner,
gas: 1000000
}).then(() => {
throw new Error('Expected reject');
}).catch(e => e.message.should.contains('Cant convert ETH/tokens because of capacity'));
});
it('should prevent withdraw because of min exchange', async () => {
const data = bridge.methods.withdraw(
ethIndex,
withdrawIds.toString(),
web3.utils.randomHex(32),
owner,
'1',
'6000000'
).encodeABI();
const isExecuted = await poaTxs.sendAndConfirm(DESTINATION.TARGET, data, {
from: validators[0],
gas: 6000000
}, validators.slice(1));
isExecuted.should.equal(false);
});
it('should change fee', async () => {
const newFee = web3.utils.toBN(ETH_FEE_PERCENTAGE).muln(2).toString();
const data = bridge.methods.changeFee(ethIndex, newFee).encodeABI();
const isExecuted = await poaTxs.sendAndConfirm(DESTINATION.TARGET, data, {
from: validators[0],
gas: 6000000
}, validators.slice(1));
isExecuted.should.equal(true);
const {feePercentage} = await bridge.methods.currencies(ethIndex).call();
feePercentage.should.equal(newFee);
});
it('should add new currency as ERC20', async () => {
const data = bridge.methods.addCurrency(
erc20.options.address,
ERC_SYMBOL,
ERC_DECIMALS,
ETH_CAPACITY,
ETH_MIN_AMOUNT,
ETH_FEE_PERCENTAGE
).encodeABI();
const isExecuted = await poaTxs.sendAndConfirm(
DESTINATION.TARGET,
data,
{
from: validators[0],
gas: 6000000
},
validators.slice(1)
);
isExecuted.should.equal(true);
const token = await bridge.methods.currencies(ERC_INDEX).call();
token.symbol.should.equal(ERC_SYMBOL);
token.decimals.should.equal(ERC_DECIMALS);
token.tokenContract.should.equal(erc20.options.address);
token.balance.should.equal('0');
token.capacity.should.equal(ETH_CAPACITY);
token.minExchange.should.equal(ETH_MIN_AMOUNT);
token.feePercentage.should.equal(ETH_FEE_PERCENTAGE);
});
it('should deposit ERC20', async () => {
const {feePercentage} = await bridge.methods.currencies(ERC_INDEX).call();
const accBalance = await erc20.methods.balanceOf(owner).call();
await erc20.methods.approve(bridge.options.address, TO_EXCHANGE).send({
from: owner,
gas: 1000000
});
await bridge.methods.exchange(
ERC_INDEX,
recipient,
TO_EXCHANGE
).send({
value: TO_EXCHANGE,
from: owner,
gas: 1000000
});
const newAccBalance = await erc20.methods.balanceOf(owner).call();
const diff = web3.utils.toBN(accBalance).sub(web3.utils.toBN(TO_EXCHANGE)).toString();
diff.should.equal(newAccBalance);
const realAmount = web3.utils.toBN(TO_EXCHANGE)
.sub(web3.utils.toBN(getFee(TO_EXCHANGE, feePercentage)))
.toString();
const {balance} = await bridge.methods.currencies(ERC_INDEX).call();
balance.should.equal(realAmount);
});
it('should withdraw ERC20', async () => {
const balance = await erc20.methods.balanceOf(owner).call();
const data = bridge.methods.withdraw(
ERC_INDEX,
withdrawIds.toString(),
web3.utils.randomHex(32),
owner,
TO_WITHDRAW,
'6000000'
).encodeABI();
const isExecuted = await poaTxs.sendAndConfirm(DESTINATION.TARGET, data, {
from: validators[0],
gas: 6000000
}, validators.slice(1));
isExecuted.should.equal(true);
const newBalance = await erc20.methods.balanceOf(owner).call();
const diff = web3.utils
.toBN(balance)
.add(web3.utils.toBN(TO_WITHDRAW))
.toString();
diff.should.equal(newBalance);
});
it('should migrate bank storage owner to new one', async () => {
const data = bridge.methods.migration(owner).encodeABI();
const isExecuted = await poaTxs.sendAndConfirm(DESTINATION.TARGET, data, {
from: validators[0],
gas: 6000000
}, validators.slice(1));
isExecuted.should.equal(true);
const bsOwner = await bs.methods.owner().call();
bsOwner.should.equal(owner);
});
it('should withdraw fee', async () => {
for (let validator of validators) {
const balance = await bs.methods.getValidatorFee(validator, ZERO_ADDRESS).call();
assert(parseInt(balance) > 0, 'fees should be great then zero');
await bs.methods.withdrawFee(ZERO_ADDRESS, balance, '500000').send({
from: validator,
gas: 500000
});
}
for (let validator of validators) {
const balance = await bs.methods.getValidatorFee(validator, erc20.options.address).call();
assert(parseInt(balance) > 0, 'token fees should be great then zero');
await bs.methods.withdrawFee(erc20.options.address, balance, '500000').send({
from: validator,
gas: 500000
});
}
});
});