|
| 1 | +import "module-alias/register"; |
| 2 | + |
| 3 | +import { BigNumber } from "@ethersproject/bignumber"; |
| 4 | +import { defaultAbiCoder } from "ethers/lib/utils"; |
| 5 | + |
| 6 | +import { Address, Bytes } from "@utils/types"; |
| 7 | +import { Account } from "@utils/test/types"; |
| 8 | +import { |
| 9 | + EMPTY_BYTES, |
| 10 | + ZERO, |
| 11 | +} from "@utils/constants"; |
| 12 | +import { UniswapV2TransferFeeExchangeAdapter } from "@utils/contracts"; |
| 13 | +import DeployHelper from "@utils/deploys"; |
| 14 | +import { |
| 15 | + ether, |
| 16 | +} from "@utils/index"; |
| 17 | +import { |
| 18 | + addSnapshotBeforeRestoreAfterEach, |
| 19 | + getAccounts, |
| 20 | + getSystemFixture, |
| 21 | + getUniswapFixture, |
| 22 | + getWaffleExpect, |
| 23 | + getLastBlockTimestamp |
| 24 | +} from "@utils/test/index"; |
| 25 | + |
| 26 | +import { SystemFixture, UniswapFixture } from "@utils/fixtures"; |
| 27 | + |
| 28 | +const expect = getWaffleExpect(); |
| 29 | + |
| 30 | +describe("UniswapV2TransferFeeExchangeAdapter", () => { |
| 31 | + let owner: Account; |
| 32 | + let mockSetToken: Account; |
| 33 | + let deployer: DeployHelper; |
| 34 | + let setup: SystemFixture; |
| 35 | + let uniswapSetup: UniswapFixture; |
| 36 | + |
| 37 | + let uniswapV2TransferFeeExchangeAdapter: UniswapV2TransferFeeExchangeAdapter; |
| 38 | + |
| 39 | + before(async () => { |
| 40 | + [ |
| 41 | + owner, |
| 42 | + mockSetToken, |
| 43 | + ] = await getAccounts(); |
| 44 | + |
| 45 | + deployer = new DeployHelper(owner.wallet); |
| 46 | + setup = getSystemFixture(owner.address); |
| 47 | + await setup.initialize(); |
| 48 | + |
| 49 | + uniswapSetup = getUniswapFixture(owner.address); |
| 50 | + await uniswapSetup.initialize( |
| 51 | + owner, |
| 52 | + setup.weth.address, |
| 53 | + setup.wbtc.address, |
| 54 | + setup.dai.address |
| 55 | + ); |
| 56 | + |
| 57 | + uniswapV2TransferFeeExchangeAdapter = await deployer.adapters.deployUniswapV2TransferFeeExchangeAdapter(uniswapSetup.router.address); |
| 58 | + }); |
| 59 | + |
| 60 | + addSnapshotBeforeRestoreAfterEach(); |
| 61 | + |
| 62 | + describe("constructor", async () => { |
| 63 | + let subjectUniswapRouter: Address; |
| 64 | + |
| 65 | + beforeEach(async () => { |
| 66 | + subjectUniswapRouter = uniswapSetup.router.address; |
| 67 | + }); |
| 68 | + |
| 69 | + async function subject(): Promise<any> { |
| 70 | + return await deployer.adapters.deployUniswapV2TransferFeeExchangeAdapter(subjectUniswapRouter); |
| 71 | + } |
| 72 | + |
| 73 | + it("should have the correct router address", async () => { |
| 74 | + const deployedUniswapV2TransferFeeExchangeAdapter = await subject(); |
| 75 | + |
| 76 | + const actualRouterAddress = await deployedUniswapV2TransferFeeExchangeAdapter.router(); |
| 77 | + expect(actualRouterAddress).to.eq(uniswapSetup.router.address); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe("getSpender", async () => { |
| 82 | + async function subject(): Promise<any> { |
| 83 | + return await uniswapV2TransferFeeExchangeAdapter.getSpender(); |
| 84 | + } |
| 85 | + |
| 86 | + it("should return the correct spender address", async () => { |
| 87 | + const spender = await subject(); |
| 88 | + |
| 89 | + expect(spender).to.eq(uniswapSetup.router.address); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe("getTradeCalldata", async () => { |
| 94 | + let sourceAddress: Address; |
| 95 | + let destinationAddress: Address; |
| 96 | + let sourceQuantity: BigNumber; |
| 97 | + let destinationQuantity: BigNumber; |
| 98 | + |
| 99 | + let subjectMockSetToken: Address; |
| 100 | + let subjectSourceToken: Address; |
| 101 | + let subjectDestinationToken: Address; |
| 102 | + let subjectSourceQuantity: BigNumber; |
| 103 | + let subjectMinDestinationQuantity: BigNumber; |
| 104 | + let subjectData: Bytes; |
| 105 | + |
| 106 | + beforeEach(async () => { |
| 107 | + sourceAddress = setup.wbtc.address; // WBTC Address |
| 108 | + sourceQuantity = BigNumber.from(100000000); // Trade 1 WBTC |
| 109 | + destinationAddress = setup.dai.address; // DAI Address |
| 110 | + destinationQuantity = ether(30000); // Receive at least 30k DAI |
| 111 | + |
| 112 | + subjectSourceToken = sourceAddress; |
| 113 | + subjectDestinationToken = destinationAddress; |
| 114 | + subjectMockSetToken = mockSetToken.address; |
| 115 | + subjectSourceQuantity = sourceQuantity; |
| 116 | + subjectMinDestinationQuantity = destinationQuantity; |
| 117 | + subjectData = EMPTY_BYTES; |
| 118 | + }); |
| 119 | + |
| 120 | + async function subject(): Promise<any> { |
| 121 | + return await uniswapV2TransferFeeExchangeAdapter.getTradeCalldata( |
| 122 | + subjectSourceToken, |
| 123 | + subjectDestinationToken, |
| 124 | + subjectMockSetToken, |
| 125 | + subjectSourceQuantity, |
| 126 | + subjectMinDestinationQuantity, |
| 127 | + subjectData, |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + it("should return the correct trade calldata", async () => { |
| 132 | + const calldata = await subject(); |
| 133 | + const callTimestamp = await getLastBlockTimestamp(); |
| 134 | + const expectedCallData = uniswapSetup.router.interface.encodeFunctionData("swapExactTokensForTokensSupportingFeeOnTransferTokens", [ |
| 135 | + sourceQuantity, |
| 136 | + destinationQuantity, |
| 137 | + [sourceAddress, destinationAddress], |
| 138 | + subjectMockSetToken, |
| 139 | + callTimestamp, |
| 140 | + ]); |
| 141 | + expect(JSON.stringify(calldata)).to.eq(JSON.stringify([uniswapSetup.router.address, ZERO, expectedCallData])); |
| 142 | + }); |
| 143 | + |
| 144 | + describe("when passed in custom path to trade data", async () => { |
| 145 | + beforeEach(async () => { |
| 146 | + const path = [sourceAddress, setup.weth.address, destinationAddress]; |
| 147 | + subjectData = defaultAbiCoder.encode( |
| 148 | + ["address[]"], |
| 149 | + [path] |
| 150 | + ); |
| 151 | + }); |
| 152 | + |
| 153 | + it("should return the correct trade calldata", async () => { |
| 154 | + const calldata = await subject(); |
| 155 | + const callTimestamp = await getLastBlockTimestamp(); |
| 156 | + const expectedCallData = uniswapSetup.router.interface.encodeFunctionData("swapExactTokensForTokensSupportingFeeOnTransferTokens", [ |
| 157 | + sourceQuantity, |
| 158 | + destinationQuantity, |
| 159 | + [sourceAddress, setup.weth.address, destinationAddress], |
| 160 | + subjectMockSetToken, |
| 161 | + callTimestamp, |
| 162 | + ]); |
| 163 | + expect(JSON.stringify(calldata)).to.eq(JSON.stringify([uniswapSetup.router.address, ZERO, expectedCallData])); |
| 164 | + }); |
| 165 | + }); |
| 166 | + }); |
| 167 | +}); |
0 commit comments