|
1 | 1 | import { expect } from "chai"; |
2 | 2 | import { Signer } from "ethers"; |
3 | | -import { parseUnits } from "ethers/lib/utils"; |
4 | | -import { ethers } from "hardhat"; |
| 3 | +import { _TypedDataEncoder, parseUnits } from "ethers/lib/utils"; |
| 4 | +import { ethers, network } from "hardhat"; |
5 | 5 |
|
6 | | -import { SwapHelper, WBNB } from "../../../typechain"; |
| 6 | +import { FaucetToken, SwapHelper, WBNB } from "../../../typechain"; |
7 | 7 |
|
8 | 8 | describe("SwapHelper", () => { |
| 9 | + const maxUint256 = ethers.constants.MaxUint256; |
| 10 | + let owner: Signer; |
9 | 11 | let user1: Signer; |
| 12 | + let user2: Signer; |
10 | 13 | let userAddress: string; |
| 14 | + let user2Address: string; |
11 | 15 | let swapHelper: SwapHelper; |
12 | 16 | let wBNB: WBNB; |
| 17 | + let erc20: FaucetToken; |
13 | 18 |
|
14 | 19 | beforeEach(async () => { |
15 | | - [user1] = await ethers.getSigners(); |
| 20 | + [owner, user1, user2] = await ethers.getSigners(); |
16 | 21 | userAddress = await user1.getAddress(); |
| 22 | + user2Address = await user2.getAddress(); |
17 | 23 |
|
18 | 24 | const WBNBFactory = await ethers.getContractFactory("WBNB"); |
19 | 25 | wBNB = await WBNBFactory.deploy(); |
20 | 26 |
|
| 27 | + const ERC20Factory = await ethers.getContractFactory("FaucetToken"); |
| 28 | + erc20 = await ERC20Factory.deploy(parseUnits("10000", 18), "Test Token", 18, "TEST"); |
| 29 | + |
21 | 30 | const SwapHelperFactory = await ethers.getContractFactory("SwapHelper"); |
22 | | - swapHelper = await SwapHelperFactory.deploy(wBNB.address); |
| 31 | + swapHelper = await SwapHelperFactory.deploy(wBNB.address, await owner.getAddress()); |
| 32 | + }); |
| 33 | + |
| 34 | + describe("wrap", () => { |
| 35 | + it("should only work within multicall", async () => { |
| 36 | + const amount = parseUnits("1", 18); |
| 37 | + expect(await wBNB.balanceOf(userAddress)).to.equal(0); |
| 38 | + const wrapData = await swapHelper.populateTransaction.wrap(amount); |
| 39 | + await swapHelper.connect(user1).multicall([wrapData.data!], maxUint256, "0x", { value: amount }); |
| 40 | + expect(await wBNB.balanceOf(swapHelper.address)).to.equal(amount); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe("sweep", () => { |
| 45 | + it("should sweep ERC20 tokens to specified address", async () => { |
| 46 | + const amount = parseUnits("1000", 18); |
| 47 | + await erc20.connect(owner).transfer(swapHelper.address, amount); |
| 48 | + expect(await erc20.balanceOf(swapHelper.address)).to.equal(amount); |
| 49 | + expect(await erc20.balanceOf(userAddress)).to.equal(0); |
| 50 | + |
| 51 | + await swapHelper.connect(user1).sweep(erc20.address, userAddress); |
| 52 | + expect(await erc20.balanceOf(swapHelper.address)).to.equal(0); |
| 53 | + expect(await erc20.balanceOf(userAddress)).to.equal(amount); |
| 54 | + }); |
| 55 | + |
| 56 | + it("should work within multicall", async () => { |
| 57 | + const amount = parseUnits("1000", 18); |
| 58 | + await erc20.connect(owner).transfer(swapHelper.address, amount); |
| 59 | + const sweepData = await swapHelper.populateTransaction.sweep(erc20.address, userAddress); |
| 60 | + await swapHelper.connect(user1).multicall([sweepData.data!], maxUint256, "0x"); |
| 61 | + expect(await erc20.balanceOf(swapHelper.address)).to.equal(0); |
| 62 | + expect(await erc20.balanceOf(userAddress)).to.equal(amount); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should wrap and transfer all in a single call", async () => { |
| 66 | + const amount = parseUnits("1", 18); |
| 67 | + expect(await wBNB.balanceOf(userAddress)).to.equal(0); |
| 68 | + const wrapData = await swapHelper.populateTransaction.wrap(amount); |
| 69 | + const sweepData = await swapHelper.populateTransaction.sweep(wBNB.address, userAddress); |
| 70 | + await swapHelper.connect(user1).multicall([wrapData.data!, sweepData.data!], maxUint256, "0x", { value: amount }); |
| 71 | + expect(await wBNB.balanceOf(swapHelper.address)).to.equal(0); |
| 72 | + expect(await wBNB.balanceOf(userAddress)).to.equal(amount); |
| 73 | + }); |
23 | 74 | }); |
24 | 75 |
|
25 | | - it("should wrap native BNB into WBNB", async () => { |
26 | | - const amount = parseUnits("1", 18); |
27 | | - expect(await wBNB.balanceOf(userAddress)).to.equal(0); |
28 | | - const wrapData = await swapHelper.populateTransaction.wrap(amount); |
29 | | - expect(await swapHelper.connect(user1).multicall([wrapData.data!], { value: amount })); |
30 | | - expect(await wBNB.balanceOf(swapHelper.address)).to.equal(amount); |
| 76 | + describe("approveMax", () => { |
| 77 | + it("should approve maximum amount to a spender", async () => { |
| 78 | + const spender = user2Address; |
| 79 | + expect(await erc20.allowance(swapHelper.address, spender)).to.equal(0); |
| 80 | + await swapHelper.connect(user1).approveMax(erc20.address, spender); |
| 81 | + expect(await erc20.allowance(swapHelper.address, spender)).to.equal(maxUint256); |
| 82 | + }); |
| 83 | + |
| 84 | + it("should work within multicall", async () => { |
| 85 | + const spender = user2Address; |
| 86 | + const approveData = await swapHelper.populateTransaction.approveMax(erc20.address, spender); |
| 87 | + await swapHelper.connect(user1).multicall([approveData.data!], maxUint256, "0x"); |
| 88 | + expect(await erc20.allowance(swapHelper.address, spender)).to.equal(maxUint256); |
| 89 | + }); |
31 | 90 | }); |
32 | 91 |
|
33 | | - it("should wrap and transfer all in a single call", async () => { |
34 | | - const amount = parseUnits("1", 18); |
35 | | - expect(await wBNB.balanceOf(userAddress)).to.equal(0); |
36 | | - const wrapData = await swapHelper.populateTransaction.wrap(amount); |
37 | | - const sweepData = await swapHelper.populateTransaction.sweep(wBNB.address, userAddress); |
38 | | - expect(await swapHelper.connect(user1).multicall([wrapData.data!, sweepData.data!], { value: amount })); |
39 | | - expect(await wBNB.balanceOf(swapHelper.address)).to.equal(0); |
40 | | - expect(await wBNB.balanceOf(userAddress)).to.equal(amount); |
| 92 | + describe("multicall", () => { |
| 93 | + const types = { |
| 94 | + Multicall: [ |
| 95 | + { name: "calls", type: "bytes[]" }, |
| 96 | + { name: "deadline", type: "uint256" }, |
| 97 | + ], |
| 98 | + }; |
| 99 | + |
| 100 | + it("should revert if deadline is in the past", async () => { |
| 101 | + const amount = parseUnits("1", 18); |
| 102 | + const wrapData = await swapHelper.populateTransaction.wrap(amount); |
| 103 | + await expect( |
| 104 | + swapHelper.connect(user1).multicall([wrapData.data!], 1234, "0x", { value: amount }), |
| 105 | + ).to.be.revertedWithCustomError(swapHelper, "DeadlineReached"); |
| 106 | + }); |
| 107 | + |
| 108 | + it("should check signature if provided", async () => { |
| 109 | + const domain = { |
| 110 | + chainId: network.config.chainId, |
| 111 | + name: "VenusSwap", |
| 112 | + verifyingContract: swapHelper.address, |
| 113 | + version: "1", |
| 114 | + }; |
| 115 | + const singleAmount = parseUnits("1", 18); |
| 116 | + const totalAmount = singleAmount.mul(3); |
| 117 | + const wrapData = await swapHelper.populateTransaction.wrap(singleAmount); |
| 118 | + const calls = [wrapData.data!, wrapData.data!, wrapData.data!]; |
| 119 | + const deadline = maxUint256; |
| 120 | + const signature = await owner._signTypedData(domain, types, { calls, deadline }); |
| 121 | + await swapHelper.connect(user1).multicall(calls, deadline, signature, { value: totalAmount }); |
| 122 | + expect(await wBNB.balanceOf(swapHelper.address)).to.equal(totalAmount); |
| 123 | + }); |
| 124 | + |
| 125 | + it("should revert if the signature is invalid", async () => { |
| 126 | + const domain = { |
| 127 | + chainId: network.config.chainId, |
| 128 | + name: "VenusSwap", |
| 129 | + verifyingContract: swapHelper.address, |
| 130 | + version: "1", |
| 131 | + }; |
| 132 | + const singleAmount = parseUnits("1", 18); |
| 133 | + const totalAmount = singleAmount.mul(3); |
| 134 | + const wrapData = await swapHelper.populateTransaction.wrap(singleAmount); |
| 135 | + const deadline = maxUint256; |
| 136 | + const signature = await owner._signTypedData(domain, types, { |
| 137 | + // authorizing just one call, not 3 |
| 138 | + calls: [wrapData.data!], |
| 139 | + deadline, |
| 140 | + }); |
| 141 | + await expect( |
| 142 | + swapHelper.connect(user1).multicall( |
| 143 | + // trying to execute 3 txs, but only 1 is authorized |
| 144 | + [wrapData.data!, wrapData.data!, wrapData.data!], |
| 145 | + deadline, |
| 146 | + signature, |
| 147 | + { value: totalAmount }, |
| 148 | + ), |
| 149 | + ).to.be.revertedWithCustomError(swapHelper, "Unauthorized"); |
| 150 | + }); |
41 | 151 | }); |
42 | 152 | }); |
0 commit comments