This repository was archived by the owner on Mar 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcollectFeeERC20EVM.js
338 lines (303 loc) · 11 KB
/
collectFeeERC20EVM.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
// The Licensed Work is (c) 2022 Sygma
// SPDX-License-Identifier: LGPL-3.0-only
const TruffleAssert = require("truffle-assertions");
const Ethers = require("ethers");
const Helpers = require("../test/helpers");
const ERC20MintableContract = artifacts.require("ERC20PresetMinterPauser");
const DefaultMessageReceiverContract = artifacts.require("DefaultMessageReceiver");
const ERC20HandlerContract = artifacts.require("ERC20Handler");
const DynamicFeeHandlerContract = artifacts.require("TwapNativeTokenFeeHandler");
const FeeHandlerRouterContract = artifacts.require("FeeHandlerRouter");
const TwapOracleContract = artifacts.require("TwapOracle");
const FACTORY_ABI = require("@uniswap/v3-core/artifacts/contracts/UniswapV3Factory.sol/UniswapV3Factory.json").abi;
const FACTORY_BYTECODE = require(
"@uniswap/v3-core/artifacts/contracts/UniswapV3Factory.sol/UniswapV3Factory.json"
).bytecode;
const POOL_ABI = require("@uniswap/v3-core/artifacts/contracts/UniswapV3Pool.sol/UniswapV3Pool.json").abi;
const POOL_BYTECODE = require("@uniswap/v3-core/artifacts/contracts/UniswapV3Pool.sol/UniswapV3Pool.json").bytecode;
const QUOTER_ABI = require("@uniswap/v3-periphery/artifacts/contracts/lens/Quoter.sol/Quoter.json").abi;
const QUOTER_BYTECODE = require("@uniswap/v3-periphery/artifacts/contracts/lens/Quoter.sol/Quoter.json").bytecode;
contract("TwapNativeTokenFeeHandler - [collectFee]", async (accounts) => {
const recipientAddress = accounts[2];
const tokenAmount = Ethers.utils.parseEther("1");
// const fee = Ethers.utils.parseEther("0.05");
const depositorAddress = accounts[1];
const emptySetResourceData = "0x";
const originDomainID = 1;
const destinationDomainID = 3;
const gasUsed = 100000;
const gasPrice = 200000000000;
const ProtocolFeeType = {
None: "0",
Fixed: "1",
Percentage: "2"
}
const fixedProtocolFee = Ethers.utils.parseEther("0.001");
const sender = accounts[0];
const UNISWAP_V3_FACTORY_ADDRESS = "0x1F98431c8aD98523631AE4a59f267346ea31F984";
const WETH_ADDRESS = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
const MATIC_ADDRESS = "0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0";
let UniswapFactoryInstance;
let TwapOracleInstance;
let BridgeInstance;
let FeeHandlerRouterInstance;
let pool_500;
let pool_3000;
let pool_10000;
let QuoterInstance;
let DynamicFeeHandlerInstance;
let resourceID;
let DefaultMessageReceiverInstance;
let ERC20HandlerInstance;
let ERC20MintableInstance;
let depositData;
beforeEach(async () => {
await Promise.all([
(BridgeInstance = await Helpers.deployBridge(
originDomainID,
accounts[0]
)),
(ERC20MintableInstance = ERC20MintableContract.new(
"ERC20Token",
"ERC20TOK"
).then((instance) => (ERC20MintableInstance = instance))),
]);
DefaultMessageReceiverInstance = await DefaultMessageReceiverContract.new([], 100000);
ERC20HandlerInstance = await ERC20HandlerContract.new(
BridgeInstance.address,
DefaultMessageReceiverInstance.address
);
FeeHandlerRouterInstance = await FeeHandlerRouterContract.new(
BridgeInstance.address
);
DynamicFeeHandlerInstance = await DynamicFeeHandlerContract.new(
BridgeInstance.address,
FeeHandlerRouterInstance.address,
0
);
resourceID = Helpers.createResourceID(
ERC20MintableInstance.address,
originDomainID
);
const provider = new Ethers.providers.JsonRpcProvider();
const signer = provider.getSigner();
UniswapFactoryInstance = new Ethers.ethers.ContractFactory(
new Ethers.ethers.utils.Interface(FACTORY_ABI), FACTORY_BYTECODE, signer
);
UniswapFactoryInstance = await UniswapFactoryInstance.attach(UNISWAP_V3_FACTORY_ADDRESS);
QuoterInstance = new Ethers.ethers.ContractFactory(
new Ethers.ethers.utils.Interface(QUOTER_ABI), QUOTER_BYTECODE, signer
);
QuoterInstance = await QuoterInstance.deploy(UniswapFactoryInstance.address, WETH_ADDRESS);
const poolFactory = new Ethers.ethers.ContractFactory(
new Ethers.ethers.utils.Interface(POOL_ABI), POOL_BYTECODE, signer
);
pool_500 = await UniswapFactoryInstance.getPool(WETH_ADDRESS, MATIC_ADDRESS, 500);
pool_500 = await poolFactory.attach(pool_500);
pool_3000 = await UniswapFactoryInstance.getPool(WETH_ADDRESS, MATIC_ADDRESS, 3000);
pool_3000 = await poolFactory.attach(pool_3000);
pool_10000 = await UniswapFactoryInstance.getPool(WETH_ADDRESS, MATIC_ADDRESS, 10000);
pool_10000 = await poolFactory.attach(pool_10000);
TwapOracleInstance = await TwapOracleContract.new(UniswapFactoryInstance.address, WETH_ADDRESS);
await TwapOracleInstance.setPool(MATIC_ADDRESS, 500, 100);
await FeeHandlerRouterInstance.adminSetResourceHandler(
destinationDomainID,
resourceID,
DynamicFeeHandlerInstance.address
);
await DynamicFeeHandlerInstance.setFeeOracle(TwapOracleInstance.address);
await DynamicFeeHandlerInstance.setGasPrice(
destinationDomainID,
gasPrice, // Polygon gas price is 200 Gwei
ProtocolFeeType.Fixed,
fixedProtocolFee
);
await DynamicFeeHandlerInstance.setWrapTokenAddress(destinationDomainID, MATIC_ADDRESS);
await DynamicFeeHandlerInstance.setFeeProperties(gasUsed);
await BridgeInstance.adminSetResource(
ERC20HandlerInstance.address,
resourceID,
ERC20MintableInstance.address,
emptySetResourceData
);
await ERC20MintableInstance.mint(depositorAddress, tokenAmount);
await ERC20MintableInstance.approve(ERC20HandlerInstance.address, tokenAmount, {
from: depositorAddress,
});
await BridgeInstance.adminChangeFeeHandler(FeeHandlerRouterInstance.address);
await FeeHandlerRouterInstance.adminSetResourceHandler(
destinationDomainID,
resourceID,
DynamicFeeHandlerInstance.address
);
depositData = Helpers.createERCDepositData(
tokenAmount,
20,
recipientAddress
);
// set MPC address to unpause the Bridge
await BridgeInstance.endKeygen(Helpers.mpcAddress);
});
it("should collect fee in native coins", async () => {
const feeData = "0x00";
const balanceBefore = await web3.eth.getBalance(
DynamicFeeHandlerInstance.address
);
const res = await FeeHandlerRouterInstance.calculateFee.call(
sender,
originDomainID,
destinationDomainID,
resourceID,
depositData,
"0x00"
);
const fee = res.fee;
const depositTx = await BridgeInstance.deposit(
destinationDomainID,
resourceID,
depositData,
feeData,
{
from: depositorAddress,
value: fee,
}
);
TruffleAssert.eventEmitted(depositTx, "Deposit", (event) => {
return (
event.destinationDomainID.toNumber() === destinationDomainID &&
event.resourceID === resourceID.toLowerCase()
);
});
const internalTx = await TruffleAssert.createTransactionResult(
DynamicFeeHandlerInstance,
depositTx.tx
);
TruffleAssert.eventEmitted(internalTx, "FeeCollected", (event) => {
return (
event.sender === depositorAddress &&
event.fromDomainID.toNumber() === originDomainID &&
event.destinationDomainID.toNumber() === destinationDomainID &&
event.resourceID === resourceID.toLowerCase() &&
event.fee.toString() === fee.toString() &&
event.tokenAddress === Ethers.constants.AddressZero
);
});
const balanceAfter = await web3.eth.getBalance(
DynamicFeeHandlerInstance.address
);
assert.equal(balanceAfter, Ethers.BigNumber.from(fee.toString()).add(balanceBefore));
});
it("deposit should revert if invalid fee (msg.value) amount supplied", async () => {
const res = await FeeHandlerRouterInstance.calculateFee.call(
sender,
originDomainID,
destinationDomainID,
resourceID,
depositData,
"0x00"
);
const expectedFee = res.fee;
const fee = Ethers.BigNumber.from(expectedFee.toString()).div(2);
const errorValues = await Helpers.expectToRevertWithCustomError(
BridgeInstance.deposit(
destinationDomainID,
resourceID,
depositData,
"0x00",
{
from: depositorAddress,
value: fee,
}
),
"IncorrectFeeSupplied(uint256)"
);
assert.equal(errorValues[0].toString(), fee.toString());
});
it("deposit should revert if the destination coin's price is 0", async () => {
const fee = Ethers.utils.parseEther("1.0");
await TwapOracleInstance.setPrice(MATIC_ADDRESS, 0);
await Helpers.expectToRevertWithCustomError(
BridgeInstance.deposit(
destinationDomainID,
resourceID,
depositData,
"0x00",
{
from: depositorAddress,
value: fee,
}
),
"IncorrectPrice()"
);
});
it("deposit should not revert if exceed fee (msg.value) amount supplied", async () => {
const exceedFee = Ethers.utils.parseEther("1.0");
const depositTx = await BridgeInstance.deposit(
destinationDomainID,
resourceID,
depositData,
"0x00",
{
from: depositorAddress,
value: exceedFee,
}
);
TruffleAssert.eventEmitted(depositTx, "Deposit", (event) => {
return (
event.destinationDomainID.toNumber() === destinationDomainID &&
event.resourceID === resourceID.toLowerCase()
);
});
});
it("should successfully change fee handler from FeeRouter to DynamicFeeHandler and collect fee", async () => {
await BridgeInstance.adminChangeFeeHandler(
DynamicFeeHandlerInstance.address
);
const balanceBefore = await web3.eth.getBalance(
DynamicFeeHandlerInstance.address
);
const res = await FeeHandlerRouterInstance.calculateFee.call(
sender,
originDomainID,
destinationDomainID,
resourceID,
depositData,
"0x00"
);
const fee = res.fee;
const depositTx = await BridgeInstance.deposit(
destinationDomainID,
resourceID,
depositData,
"0x00",
{
from: depositorAddress,
value: fee,
}
);
TruffleAssert.eventEmitted(depositTx, "Deposit", (event) => {
return (
event.destinationDomainID.toNumber() === destinationDomainID &&
event.resourceID === resourceID.toLowerCase()
);
});
const internalTx = await TruffleAssert.createTransactionResult(
DynamicFeeHandlerInstance,
depositTx.tx
);
TruffleAssert.eventEmitted(internalTx, "FeeCollected", (event) => {
return (
event.sender === depositorAddress &&
event.fromDomainID.toNumber() === originDomainID &&
event.destinationDomainID.toNumber() === destinationDomainID &&
event.resourceID === resourceID.toLowerCase() &&
event.fee.toString() === fee.toString() &&
event.tokenAddress === Ethers.constants.AddressZero
);
});
const balanceAfter = await web3.eth.getBalance(
DynamicFeeHandlerInstance.address
);
assert.equal(balanceAfter, Ethers.BigNumber.from(fee.toString()).add(balanceBefore));
});
});