|
| 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 | + THREE, |
| 11 | + ZERO, |
| 12 | +} from "@utils/constants"; |
| 13 | +import { BalancerV1ExchangeAdapter } from "@utils/contracts"; |
| 14 | +import DeployHelper from "@utils/deploys"; |
| 15 | +import { |
| 16 | + ether, |
| 17 | +} from "@utils/index"; |
| 18 | +import { |
| 19 | + addSnapshotBeforeRestoreAfterEach, |
| 20 | + getAccounts, |
| 21 | + getSystemFixture, |
| 22 | + getBalancerFixture, |
| 23 | + getWaffleExpect |
| 24 | +} from "@utils/test/index"; |
| 25 | + |
| 26 | +import { SystemFixture, BalancerFixture } from "@utils/fixtures"; |
| 27 | + |
| 28 | +const expect = getWaffleExpect(); |
| 29 | + |
| 30 | +describe("BalancerV1ExchangeAdapter", () => { |
| 31 | + let owner: Account; |
| 32 | + let mockSetToken: Account; |
| 33 | + let deployer: DeployHelper; |
| 34 | + let setup: SystemFixture; |
| 35 | + let balancerSetup: BalancerFixture; |
| 36 | + |
| 37 | + let balancerV1ExchangeAdapter: BalancerV1ExchangeAdapter; |
| 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 | + balancerSetup = getBalancerFixture(owner.address); |
| 50 | + await balancerSetup.initialize( |
| 51 | + owner, |
| 52 | + setup.weth, |
| 53 | + setup.wbtc, |
| 54 | + setup.dai |
| 55 | + ); |
| 56 | + |
| 57 | + balancerV1ExchangeAdapter = await deployer.adapters.deployBalancerV1ExchangeAdapter(balancerSetup.exchange.address); |
| 58 | + }); |
| 59 | + |
| 60 | + addSnapshotBeforeRestoreAfterEach(); |
| 61 | + |
| 62 | + describe("constructor", async () => { |
| 63 | + let subjectBalancerProxyAddress: Address; |
| 64 | + |
| 65 | + beforeEach(async () => { |
| 66 | + subjectBalancerProxyAddress = balancerSetup.exchange.address; |
| 67 | + }); |
| 68 | + |
| 69 | + async function subject(): Promise<any> { |
| 70 | + return await deployer.adapters.deployBalancerV1ExchangeAdapter(subjectBalancerProxyAddress); |
| 71 | + } |
| 72 | + |
| 73 | + it("should have the correct proxy address", async () => { |
| 74 | + const deployedBalancerV1ExchangeAdapter = await subject(); |
| 75 | + |
| 76 | + const actualProxyAddress = await deployedBalancerV1ExchangeAdapter.balancerProxy(); |
| 77 | + expect(actualProxyAddress).to.eq(subjectBalancerProxyAddress); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe("getSpender", async () => { |
| 82 | + async function subject(): Promise<any> { |
| 83 | + return await balancerV1ExchangeAdapter.getSpender(); |
| 84 | + } |
| 85 | + |
| 86 | + it("should return the correct spender address", async () => { |
| 87 | + const spender = await subject(); |
| 88 | + |
| 89 | + expect(spender).to.eq(balancerSetup.exchange.address); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe("getBalancerExchangeData", async () => { |
| 94 | + let subjectShouldSwapFixedInputAmount: boolean; |
| 95 | + |
| 96 | + beforeEach(async () => { |
| 97 | + subjectShouldSwapFixedInputAmount = true; |
| 98 | + }); |
| 99 | + |
| 100 | + async function subject(): Promise<any> { |
| 101 | + return await balancerV1ExchangeAdapter.getBalancerExchangeData(subjectShouldSwapFixedInputAmount); |
| 102 | + } |
| 103 | + |
| 104 | + it("should return the correct data", async () => { |
| 105 | + const balancerData = await subject(); |
| 106 | + const expectedData = defaultAbiCoder.encode( |
| 107 | + ["bool"], |
| 108 | + [subjectShouldSwapFixedInputAmount] |
| 109 | + ); |
| 110 | + |
| 111 | + expect(balancerData).to.eq(expectedData); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + describe("generateDataParam", async () => { |
| 116 | + let sourceToken: Address; |
| 117 | + let destinationToken: Address; |
| 118 | + |
| 119 | + let subjectSourceToken: Address; |
| 120 | + let subjectDestinationToken: Address; |
| 121 | + let subjectFixIn: boolean; |
| 122 | + |
| 123 | + beforeEach(async () => { |
| 124 | + sourceToken = setup.wbtc.address; |
| 125 | + destinationToken = setup.dai.address; |
| 126 | + |
| 127 | + subjectSourceToken = sourceToken; |
| 128 | + subjectDestinationToken = destinationToken; |
| 129 | + }); |
| 130 | + |
| 131 | + async function subject(): Promise<any> { |
| 132 | + return await balancerV1ExchangeAdapter.generateDataParam( |
| 133 | + subjectSourceToken, |
| 134 | + subjectDestinationToken, |
| 135 | + subjectFixIn |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + describe("when boolean fixed input amount is true", async () => { |
| 140 | + beforeEach(async () => { |
| 141 | + subjectFixIn = true; |
| 142 | + }); |
| 143 | + |
| 144 | + it("should return the correct trade calldata", async () => { |
| 145 | + const dataParam = await subject(); |
| 146 | + |
| 147 | + const expectedDataParam = defaultAbiCoder.encode( |
| 148 | + ["bool"], |
| 149 | + [subjectFixIn] |
| 150 | + ); |
| 151 | + expect(JSON.stringify(dataParam)).to.eq(JSON.stringify(expectedDataParam)); |
| 152 | + }); |
| 153 | + }); |
| 154 | + |
| 155 | + describe("when boolean fixed input amount is false", async () => { |
| 156 | + beforeEach(async () => { |
| 157 | + subjectFixIn = false; |
| 158 | + }); |
| 159 | + |
| 160 | + it("should return the correct trade calldata", async () => { |
| 161 | + const dataParam = await subject(); |
| 162 | + |
| 163 | + const expectedDataParam = defaultAbiCoder.encode( |
| 164 | + ["bool"], |
| 165 | + [subjectFixIn] |
| 166 | + ); |
| 167 | + expect(JSON.stringify(dataParam)).to.eq(JSON.stringify(expectedDataParam)); |
| 168 | + }); |
| 169 | + }); |
| 170 | + }); |
| 171 | + |
| 172 | + describe("getTradeCalldata", async () => { |
| 173 | + let sourceToken: Address; |
| 174 | + let destinationToken: Address; |
| 175 | + let sourceQuantity: BigNumber; |
| 176 | + let destinationQuantity: BigNumber; |
| 177 | + |
| 178 | + let subjectMockSetToken: Address; |
| 179 | + let subjectSourceToken: Address; |
| 180 | + let subjectDestinationToken: Address; |
| 181 | + let subjectSourceQuantity: BigNumber; |
| 182 | + let subjectMinDestinationQuantity: BigNumber; |
| 183 | + let subjectData: Bytes; |
| 184 | + |
| 185 | + beforeEach(async () => { |
| 186 | + sourceToken = setup.wbtc.address; // WBTC Address |
| 187 | + sourceQuantity = BigNumber.from(100000000); // Trade 1 WBTC |
| 188 | + destinationToken = setup.dai.address; // DAI Address |
| 189 | + destinationQuantity = ether(30000); // Receive at least 30k DAI |
| 190 | + |
| 191 | + subjectSourceToken = sourceToken; |
| 192 | + subjectDestinationToken = destinationToken; |
| 193 | + subjectMockSetToken = mockSetToken.address; |
| 194 | + subjectSourceQuantity = sourceQuantity; |
| 195 | + subjectMinDestinationQuantity = destinationQuantity; |
| 196 | + subjectData = EMPTY_BYTES; |
| 197 | + }); |
| 198 | + |
| 199 | + async function subject(): Promise<any> { |
| 200 | + return await balancerV1ExchangeAdapter.getTradeCalldata( |
| 201 | + subjectSourceToken, |
| 202 | + subjectDestinationToken, |
| 203 | + subjectMockSetToken, |
| 204 | + subjectSourceQuantity, |
| 205 | + subjectMinDestinationQuantity, |
| 206 | + subjectData, |
| 207 | + ); |
| 208 | + } |
| 209 | + |
| 210 | + describe("when boolean fixed input amount is true", async () => { |
| 211 | + beforeEach(async () => { |
| 212 | + const shouldSwapFixedInputAmount = true; |
| 213 | + subjectData = defaultAbiCoder.encode(["bool"], [shouldSwapFixedInputAmount]); |
| 214 | + }); |
| 215 | + |
| 216 | + it("should return the correct trade calldata", async () => { |
| 217 | + const calldata = await subject(); |
| 218 | + |
| 219 | + const expectedCallData = balancerSetup.exchange.interface.encodeFunctionData("smartSwapExactIn", [ |
| 220 | + sourceToken, |
| 221 | + destinationToken, |
| 222 | + sourceQuantity, |
| 223 | + destinationQuantity, |
| 224 | + THREE, |
| 225 | + ]); |
| 226 | + expect(JSON.stringify(calldata)).to.eq(JSON.stringify([balancerSetup.exchange.address, ZERO, expectedCallData])); |
| 227 | + }); |
| 228 | + }); |
| 229 | + |
| 230 | + describe("when boolean fixed input amount is false", async () => { |
| 231 | + beforeEach(async () => { |
| 232 | + const shouldSwapFixedInputAmount = false; |
| 233 | + subjectData = defaultAbiCoder.encode(["bool"], [shouldSwapFixedInputAmount]); |
| 234 | + }); |
| 235 | + |
| 236 | + it("should return the correct trade calldata", async () => { |
| 237 | + const calldata = await subject(); |
| 238 | + |
| 239 | + const expectedCallData = balancerSetup.exchange.interface.encodeFunctionData("smartSwapExactOut", [ |
| 240 | + sourceToken, |
| 241 | + destinationToken, |
| 242 | + destinationQuantity, // Source and destination quantity are flipped for smartSwapExactOut |
| 243 | + sourceQuantity, |
| 244 | + THREE, |
| 245 | + ]); |
| 246 | + expect(JSON.stringify(calldata)).to.eq(JSON.stringify([balancerSetup.exchange.address, ZERO, expectedCallData])); |
| 247 | + }); |
| 248 | + }); |
| 249 | + }); |
| 250 | +}); |
0 commit comments