|
| 1 | +import "module-alias/register"; |
| 2 | + |
| 3 | +import { BigNumber, BigNumberish } from "@ethersproject/bignumber"; |
| 4 | +import { solidityPack } from "ethers/lib/utils"; |
| 5 | + |
| 6 | +import { Address, Bytes } from "@utils/types"; |
| 7 | +import { Account } from "@utils/test/types"; |
| 8 | +import { ZERO } from "@utils/constants"; |
| 9 | +import { UniswapV3ExchangeAdapter } from "@utils/contracts"; |
| 10 | +import DeployHelper from "@utils/deploys"; |
| 11 | +import { ether } from "@utils/index"; |
| 12 | +import { |
| 13 | + addSnapshotBeforeRestoreAfterEach, |
| 14 | + getAccounts, |
| 15 | + getSystemFixture, |
| 16 | + getWaffleExpect, |
| 17 | + getLastBlockTimestamp, |
| 18 | + getUniswapV3Fixture, |
| 19 | + getRandomAddress |
| 20 | +} from "@utils/test/index"; |
| 21 | + |
| 22 | +import { SystemFixture, UniswapV3Fixture } from "@utils/fixtures"; |
| 23 | + |
| 24 | +const expect = getWaffleExpect(); |
| 25 | + |
| 26 | +describe("UniswapV3ExchangeAdapter", () => { |
| 27 | + let owner: Account; |
| 28 | + let mockSetToken: Account; |
| 29 | + let deployer: DeployHelper; |
| 30 | + let setup: SystemFixture; |
| 31 | + let uniswapV3Fixture: UniswapV3Fixture; |
| 32 | + |
| 33 | + let uniswapV3ExchangeAdapter: UniswapV3ExchangeAdapter; |
| 34 | + |
| 35 | + before(async () => { |
| 36 | + [ |
| 37 | + owner, |
| 38 | + mockSetToken, |
| 39 | + ] = await getAccounts(); |
| 40 | + |
| 41 | + deployer = new DeployHelper(owner.wallet); |
| 42 | + setup = getSystemFixture(owner.address); |
| 43 | + await setup.initialize(); |
| 44 | + |
| 45 | + uniswapV3Fixture = getUniswapV3Fixture(owner.address); |
| 46 | + await uniswapV3Fixture.initialize( |
| 47 | + owner, |
| 48 | + setup.weth, |
| 49 | + 2500, |
| 50 | + setup.wbtc, |
| 51 | + 35000, |
| 52 | + setup.dai |
| 53 | + ); |
| 54 | + |
| 55 | + uniswapV3ExchangeAdapter = await deployer.adapters.deployUniswapV3ExchangeAdapter(uniswapV3Fixture.swapRouter.address); |
| 56 | + }); |
| 57 | + |
| 58 | + addSnapshotBeforeRestoreAfterEach(); |
| 59 | + |
| 60 | + describe("#constructor", async () => { |
| 61 | + let subjectSwapRouter: Address; |
| 62 | + |
| 63 | + beforeEach(async () => { |
| 64 | + subjectSwapRouter = uniswapV3Fixture.swapRouter.address; |
| 65 | + }); |
| 66 | + |
| 67 | + async function subject(): Promise<any> { |
| 68 | + return await deployer.adapters.deployUniswapV3ExchangeAdapter(subjectSwapRouter); |
| 69 | + } |
| 70 | + |
| 71 | + it("should have the correct SwapRouter address", async () => { |
| 72 | + const deployedUniswapV3ExchangeAdapter = await subject(); |
| 73 | + |
| 74 | + const actualRouterAddress = await deployedUniswapV3ExchangeAdapter.swapRouter(); |
| 75 | + expect(actualRouterAddress).to.eq(uniswapV3Fixture.swapRouter.address); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe("#getSpender", async () => { |
| 80 | + async function subject(): Promise<any> { |
| 81 | + return await uniswapV3ExchangeAdapter.getSpender(); |
| 82 | + } |
| 83 | + |
| 84 | + it("should return the correct spender address", async () => { |
| 85 | + const spender = await subject(); |
| 86 | + |
| 87 | + expect(spender).to.eq(uniswapV3Fixture.swapRouter.address); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe("#getTradeCalldata", async () => { |
| 92 | + |
| 93 | + let subjectMockSetToken: Address; |
| 94 | + let subjectSourceToken: Address; |
| 95 | + let subjectDestinationToken: Address; |
| 96 | + let subjectSourceQuantity: BigNumber; |
| 97 | + let subjectMinDestinationQuantity: BigNumber; |
| 98 | + let subjectPath: Bytes; |
| 99 | + |
| 100 | + beforeEach(async () => { |
| 101 | + subjectSourceToken = setup.wbtc.address; |
| 102 | + subjectSourceQuantity = BigNumber.from(100000000); |
| 103 | + subjectDestinationToken = setup.weth.address; |
| 104 | + subjectMinDestinationQuantity = ether(25); |
| 105 | + |
| 106 | + subjectMockSetToken = mockSetToken.address; |
| 107 | + |
| 108 | + subjectPath = solidityPack(["address", "uint24", "address"], [subjectSourceToken, BigNumber.from(3000), subjectDestinationToken]); |
| 109 | + }); |
| 110 | + |
| 111 | + async function subject(): Promise<any> { |
| 112 | + return await uniswapV3ExchangeAdapter.getTradeCalldata( |
| 113 | + subjectSourceToken, |
| 114 | + subjectDestinationToken, |
| 115 | + subjectMockSetToken, |
| 116 | + subjectSourceQuantity, |
| 117 | + subjectMinDestinationQuantity, |
| 118 | + subjectPath, |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + it("should return the correct trade calldata", async () => { |
| 123 | + const calldata = await subject(); |
| 124 | + const callTimestamp = await getLastBlockTimestamp(); |
| 125 | + |
| 126 | + const expectedCallData = uniswapV3Fixture.swapRouter.interface.encodeFunctionData("exactInput", [{ |
| 127 | + path: subjectPath, |
| 128 | + recipient: mockSetToken.address, |
| 129 | + deadline: callTimestamp, |
| 130 | + amountIn: subjectSourceQuantity, |
| 131 | + amountOutMinimum: subjectMinDestinationQuantity, |
| 132 | + }]); |
| 133 | + |
| 134 | + expect(JSON.stringify(calldata)).to.eq(JSON.stringify([uniswapV3Fixture.swapRouter.address, ZERO, expectedCallData])); |
| 135 | + }); |
| 136 | + |
| 137 | + context("when source token does not match path", async () => { |
| 138 | + beforeEach(async () => { |
| 139 | + subjectSourceToken = await getRandomAddress(); |
| 140 | + }); |
| 141 | + |
| 142 | + it("should revert", async () => { |
| 143 | + await expect(subject()).to.be.revertedWith("UniswapV3ExchangeAdapter: source token path mismatch"); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + context("when destination token does not match path", async () => { |
| 148 | + beforeEach(async () => { |
| 149 | + subjectDestinationToken = await getRandomAddress(); |
| 150 | + }); |
| 151 | + |
| 152 | + it("should revert", async () => { |
| 153 | + await expect(subject()).to.be.revertedWith("UniswapV3ExchangeAdapter: destination token path mismatch"); |
| 154 | + }); |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + describe("#generateDataParam", async () => { |
| 159 | + |
| 160 | + let subjectToken1: Address; |
| 161 | + let subjectFee1: BigNumberish; |
| 162 | + let subjectToken2: Address; |
| 163 | + let subjectFee2: BigNumberish; |
| 164 | + let subjectToken3: Address; |
| 165 | + |
| 166 | + beforeEach(async () => { |
| 167 | + subjectToken1 = setup.wbtc.address; |
| 168 | + subjectFee1 = 3000; |
| 169 | + subjectToken2 = setup.weth.address; |
| 170 | + subjectFee2 = 500; |
| 171 | + subjectToken3 = setup.weth.address; |
| 172 | + }); |
| 173 | + |
| 174 | + async function subject(): Promise<string> { |
| 175 | + return await uniswapV3ExchangeAdapter.generateDataParam([subjectToken1, subjectToken2, subjectToken3], [subjectFee1, subjectFee2]); |
| 176 | + } |
| 177 | + |
| 178 | + it("should create the correct path data", async () => { |
| 179 | + const data = await subject(); |
| 180 | + |
| 181 | + const expectedData = solidityPack( |
| 182 | + ["address", "uint24", "address", "uint24", "address"], |
| 183 | + [subjectToken1, subjectFee1, subjectToken2, subjectFee2, subjectToken3] |
| 184 | + ); |
| 185 | + |
| 186 | + expect(data).to.eq(expectedData); |
| 187 | + }); |
| 188 | + }); |
| 189 | +}); |
0 commit comments