-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
229 lines (202 loc) · 9.24 KB
/
index.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
import chai, { expect, assert } from 'chai';
import sinonChai from 'sinon-chai';
import sinon from 'sinon';
import { it, describe, afterEach } from 'mocha';
import BigNumber from 'bignumber.js';
import { Tx, Input, Output, Outpoint, Block, Period, Exit } from 'leap-core';
import ExitManager from './src/exitMarket/exitManager';
import Erc20 from './src/erc20Contract';
import ExitHandlerContract from './src/exitHandlerContract';
chai.use(sinonChai);
const alice = '0x83B3525e17F9eAA92dAE3f9924cc333c94C7E98a';
const alicePriv = '0xbd54b17c48ac1fc91d5ef2ef02e9911337f8758e93c801b619e5d178094486cc';
const exitHandler = '0x791186143a8fe5f0287f0DC35df3A71354f607b6';
const DEFAULT_EXIT_STAKE = new BigNumber(1000000000000000000);
const contract = {
startBoughtExit: {
sendTransaction() {},
},
balanceOf: { call() {} },
allowance: { call() {} },
getTokenAddr: { call() {} },
exitStake: { call() {} },
};
const web3 = { eth: {
contract() {},
at() {},
} };
sinon.stub(web3.eth, 'contract').returns(web3.eth);
sinon.stub(web3.eth, 'at').returns(contract);
describe('Exit Manager', () => {
it('should throw if rate to low', async () => {
// prepare state
const deposit = Tx.deposit(0, 100, alice);
let transfer = Tx.transfer(
[new Input(new Outpoint(deposit.hash(), 0))],
[new Output(50, exitHandler), new Output(50, alice)],
);
transfer = transfer.sign([alicePriv]);
const block = new Block(33);
block.addTx(deposit).addTx(transfer);
const prevPeriodRoot = '0x32C220482C68413FBF8290E3B1E49B0A85901CFCD62AB0738761568A2A6E8A57';
const period = new Period(prevPeriodRoot, [block]);
period.setValidatorData(0, alice);
const transferProof = period.proof(transfer);
// sign exit receipt
const sellPrice = 49;
const utxoId = (new Outpoint(transfer.hash(), 0)).getUtxoId();
const signedData = Exit.signOverExit(utxoId, sellPrice, alicePriv);
const signedDataBytes32 = Exit.bufferToBytes32Array(signedData);
const manager = new ExitManager(1000, null);
try {
await manager.sellExit([], transferProof, 0, 0, signedDataBytes32);
} catch (err) {
expect(err.message).to.contain('too low for utxo size');
}
});
it('should throw if ERC 721', async () => {
// prepare state
const deposit = Tx.deposit(0, 100, alice, 35000);
let transfer = Tx.transfer(
[new Input(new Outpoint(deposit.hash(), 0))],
[new Output(50, exitHandler, 35000), new Output(50, alice, 35000)],
);
transfer = transfer.sign([alicePriv]);
const block = new Block(33);
block.addTx(deposit).addTx(transfer);
const prevPeriodRoot = '0x32C220482C68413FBF8290E3B1E49B0A85901CFCD62AB0738761568A2A6E8A57';
const period = new Period(prevPeriodRoot, [block]);
period.setValidatorData(0, alice);
const transferProof = period.proof(transfer);
// sign exit receipt
const sellPrice = 49;
const utxoId = (new Outpoint(transfer.hash(), 0)).getUtxoId();
const signedData = Exit.signOverExit(utxoId, sellPrice, alicePriv);
const signedDataBytes32 = Exit.bufferToBytes32Array(signedData);
// mock blockchain
sinon.stub(contract.getTokenAddr, 'call').yields(null, alice);
sinon.stub(contract.exitStake, 'call').yields(null, DEFAULT_EXIT_STAKE);
const exitHandlerContract = new ExitHandlerContract(web3, alice, exitHandler);
const rate = 900;
// rate, exitContract, erc20Contract, senderAddr, exitHandlerAddr
const manager = new ExitManager(rate, exitHandlerContract, null, alice, exitHandler);
try {
await manager.sellExit([], transferProof, 0, 0, signedDataBytes32);
} catch (err) {
expect(err.message).to.contain('bad color');
}
});
it('should throw if balance too low', async () => {
// prepare state
const deposit = Tx.deposit(0, 100, alice);
let transfer = Tx.transfer(
[new Input(new Outpoint(deposit.hash(), 0))],
[new Output(50, exitHandler), new Output(50, alice)],
);
transfer = transfer.sign([alicePriv]);
const block = new Block(33);
block.addTx(deposit).addTx(transfer);
const prevPeriodRoot = '0x32C220482C68413FBF8290E3B1E49B0A85901CFCD62AB0738761568A2A6E8A57';
const period = new Period(prevPeriodRoot, [block]);
period.setValidatorData(0, alice);
const transferProof = period.proof(transfer);
// sign exit receipt
const sellPrice = 49;
const utxoId = (new Outpoint(transfer.hash(), 0)).getUtxoId();
const signedData = Exit.signOverExit(utxoId, sellPrice, alicePriv);
const signedDataBytes32 = Exit.bufferToBytes32Array(signedData);
// mock blockchain
sinon.stub(contract.balanceOf, 'call').yields(null, new BigNumber(30));
sinon.stub(contract.allowance, 'call').yields(null, new BigNumber(100));
sinon.stub(contract.getTokenAddr, 'call').yields(null, alice);
sinon.stub(contract.exitStake, 'call').yields(null, DEFAULT_EXIT_STAKE);
const rate = 900;
const exitHandlerContract = new ExitHandlerContract(web3, alice, exitHandler);
const erc20 = new Erc20(web3);
// rate, exitContract, erc20Contract, senderAddr, exitHandlerAddr
const manager = new ExitManager(rate, exitHandlerContract, erc20, alice, exitHandler);
try {
await manager.sellExit([], transferProof, 0, 0, signedDataBytes32);
} catch (err) {
expect(err.message).to.contain('insufficient to handle exit size');
}
});
it('should throw if allowance too low', async () => {
// prepare state
const deposit = Tx.deposit(0, 100, alice);
let transfer = Tx.transfer(
[new Input(new Outpoint(deposit.hash(), 0))],
[new Output(50, exitHandler), new Output(50, alice)],
);
transfer = transfer.sign([alicePriv]);
const block = new Block(33);
block.addTx(deposit).addTx(transfer);
const prevPeriodRoot = '0x32C220482C68413FBF8290E3B1E49B0A85901CFCD62AB0738761568A2A6E8A57';
const period = new Period(prevPeriodRoot, [block]);
period.setValidatorData(0, alice);
const transferProof = period.proof(transfer);
// sign exit receipt
const sellPrice = 49;
const utxoId = (new Outpoint(transfer.hash(), 0)).getUtxoId();
const signedData = Exit.signOverExit(utxoId, sellPrice, alicePriv);
const signedDataBytes32 = Exit.bufferToBytes32Array(signedData);
// mock blockchain
sinon.stub(contract.balanceOf, 'call').yields(null, new BigNumber(100));
sinon.stub(contract.allowance, 'call').yields(null, new BigNumber(30));
sinon.stub(contract.getTokenAddr, 'call').yields(null, alice);
sinon.stub(contract.exitStake, 'call').yields(null, DEFAULT_EXIT_STAKE);
const rate = 900;
const exitHandlerContract = new ExitHandlerContract(web3, alice, exitHandler);
const erc20 = new Erc20(web3);
// rate, exitContract, erc20Contract, senderAddr, exitHandlerAddr
const manager = new ExitManager(rate, exitHandlerContract, erc20, alice, exitHandler);
try {
await manager.sellExit([], transferProof, 0, 0, signedDataBytes32);
} catch (err) {
expect(err.message).to.contain('allowance');
}
});
it('should allow to sell', async () => {
// prepare state
const deposit = Tx.deposit(0, 100, alice);
let transfer = Tx.transfer(
[new Input(new Outpoint(deposit.hash(), 0))],
[new Output(50, exitHandler), new Output(50, alice)],
);
transfer = transfer.sign([alicePriv]);
const block = new Block(33);
block.addTx(deposit).addTx(transfer);
const prevPeriodRoot = '0x32C220482C68413FBF8290E3B1E49B0A85901CFCD62AB0738761568A2A6E8A57';
const period = new Period(prevPeriodRoot, [block]);
period.setValidatorData(0, alice);
const transferProof = period.proof(transfer);
const depositProof = period.proof(deposit);
const outputIndex = 0;
const inputIndex = 0;
// sign exit receipt
const sellPrice = 49;
const utxoId = (new Outpoint(transfer.hash(), 0)).getUtxoId();
const signedData = Exit.signOverExit(utxoId, sellPrice, alicePriv);
const signedDataBytes32 = Exit.bufferToBytes32Array(signedData);
// mock blockchain
sinon.stub(contract.balanceOf, 'call').yields(null, new BigNumber(100));
sinon.stub(contract.allowance, 'call').yields(null, new BigNumber(100));
sinon.stub(contract.getTokenAddr, 'call').yields(null, alice);
sinon.stub(contract.exitStake, 'call').yields(null, DEFAULT_EXIT_STAKE);
sinon.stub(contract.startBoughtExit, 'sendTransaction').yields(null, '0x112233');
const exitHandlerContract = new ExitHandlerContract(web3, alice, exitHandler);
const erc20 = new Erc20(web3);
const rate = 900;
// rate, exitContract, erc20Contract, senderAddr, exitHandlerAddr
const manager = new ExitManager(rate, exitHandlerContract, erc20, alice, exitHandler);
const rsp = await manager.sellExit(depositProof, transferProof, inputIndex, outputIndex, signedDataBytes32);
assert.equal(rsp, JSON.stringify({ txHash: '0x112233' }));
});
afterEach(() => {
if (contract.balanceOf.call.restore) contract.balanceOf.call.restore();
if (contract.allowance.call.restore) contract.allowance.call.restore();
if (contract.getTokenAddr.call.restore) contract.getTokenAddr.call.restore();
if (contract.exitStake.call.restore) contract.exitStake.call.restore();
if (contract.startBoughtExit.sendTransaction.restore) contract.startBoughtExit.sendTransaction.restore();
});
});