From b6b48cc2703299ca14ac37585507e3f49ef1a2ef Mon Sep 17 00:00:00 2001 From: fourlen Date: Fri, 7 Jun 2024 19:09:56 +0300 Subject: [PATCH] other tests --- src/periphery/contracts/SwapRouter.sol | 7 +-- src/periphery/contracts/libraries/Path.sol | 1 - src/periphery/test/Quoter.spec.ts | 2 +- src/periphery/test/SwapRouter.spec.ts | 43 ++++++++++--------- .../__snapshots__/SwapRouter.spec.ts.snap | 2 +- .../__snapshots__/V3Migrator.spec.ts.snap | 2 +- src/periphery/test/shared/completeFixture.ts | 10 ++--- 7 files changed, 35 insertions(+), 32 deletions(-) diff --git a/src/periphery/contracts/SwapRouter.sol b/src/periphery/contracts/SwapRouter.sol index 18a0338b..d58bb2eb 100644 --- a/src/periphery/contracts/SwapRouter.sol +++ b/src/periphery/contracts/SwapRouter.sol @@ -127,9 +127,6 @@ contract SwapRouter is ) external payable override checkDeadline(params.deadline) returns (uint256 amountOut) { address payer = msg.sender; // msg.sender pays for the first hop - // console.log('path'); - // console.logBytes(params.path); - while (true) { bool hasMultiplePools = params.path.hasMultiplePools(); @@ -239,6 +236,10 @@ contract SwapRouter is ) external payable override checkDeadline(params.deadline) returns (uint256 amountIn) { // it's okay that the payer is fixed to msg.sender here, as they're only paying for the "final" exact output // swap, which happens first, and subsequent swaps are paid for within nested callback frames + + console.log('path'); + console.logBytes(params.path); + exactOutputInternal( params.amountOut, params.recipient, diff --git a/src/periphery/contracts/libraries/Path.sol b/src/periphery/contracts/libraries/Path.sol index 6864500e..f697931d 100644 --- a/src/periphery/contracts/libraries/Path.sol +++ b/src/periphery/contracts/libraries/Path.sol @@ -44,7 +44,6 @@ library Path { /// @return deployer The address of the custom pool deployer /// @return tokenB The second token of the given pool function decodeFirstPool(bytes memory path) internal pure returns (address tokenA, address deployer, address tokenB) { - console.logBytes(path); tokenA = path.toAddress(0); deployer = path.toAddress(DEPLOYER_OFFSET); tokenB = path.toAddress(NEXT_OFFSET); diff --git a/src/periphery/test/Quoter.spec.ts b/src/periphery/test/Quoter.spec.ts index 69d34efb..37f4f02e 100644 --- a/src/periphery/test/Quoter.spec.ts +++ b/src/periphery/test/Quoter.spec.ts @@ -170,7 +170,7 @@ describe('Quoter', () => { it('2 -> 1 -> 0', async () => { const { amountOut, fees } = await quoter.quoteExactInput.staticCall( - encodePath(path.reverse()), + encodePath(path.slice().reverse()), 5 ); diff --git a/src/periphery/test/SwapRouter.spec.ts b/src/periphery/test/SwapRouter.spec.ts index ff4f520d..9b870fe7 100644 --- a/src/periphery/test/SwapRouter.spec.ts +++ b/src/periphery/test/SwapRouter.spec.ts @@ -74,6 +74,7 @@ describe('SwapRouter', function () { }> = async () => { const { wnative, factory, router, tokens, path, nft } = await loadFixture(completeFixture); let _tokens = tokens as [TestERC20WithAddress, TestERC20WithAddress, TestERC20WithAddress]; + // approve & fund wallets for (const token of _tokens) { await token.approve(router, MaxUint256); @@ -223,7 +224,7 @@ describe('SwapRouter', function () { // get balances before const poolBefore = await getBalances(pool); const traderBefore = await getBalances(trader.address); - console.log(path.slice(0, 3)); + await exactInput(path.slice(0, 3)); // get balances after @@ -279,7 +280,7 @@ describe('SwapRouter', function () { it('2 -> 1 -> 0', async () => { const traderBefore = await getBalances(trader.address); - await exactInput(tokens.map((token) => token.address).reverse(), 5, 1); + await exactInput(path.slice().reverse(), 5, 1); const traderAfter = await getBalances(trader.address); @@ -351,7 +352,7 @@ describe('SwapRouter', function () { it('WNativeToken -> 0 -> 1', async () => { const traderBefore = await getBalances(trader.address); - await expect(exactInput([await wnative.getAddress(), tokens[0].address, tokens[1].address], 5)) + await expect(exactInput([await wnative.getAddress(), ZERO_ADDRESS, tokens[0].address, ZERO_ADDRESS, tokens[1].address], 5)) .to.emit(wnative, 'Deposit') .withArgs(await router.getAddress(), 5); @@ -376,7 +377,7 @@ describe('SwapRouter', function () { const poolBefore = await getBalances(pool); const traderBefore = await getBalances(trader.address); - await expect(exactInput([tokens[0].address, await wnative.getAddress()])) + await expect(exactInput([tokens[0].address, ZERO_ADDRESS, await wnative.getAddress()])) .to.emit(wnative, 'Withdrawal') .withArgs(await router.getAddress(), 1); @@ -393,7 +394,7 @@ describe('SwapRouter', function () { // get balances before const traderBefore = await getBalances(trader.address); - await expect(exactInput([tokens[0].address, tokens[1].address, await wnative.getAddress()], 5)) + await expect(exactInput([tokens[0].address, ZERO_ADDRESS, tokens[1].address, ZERO_ADDRESS, await wnative.getAddress()], 5)) .to.emit(wnative, 'Withdrawal') .withArgs(await router.getAddress(), 1); @@ -714,6 +715,7 @@ describe('SwapRouter', function () { // ensure that the swap fails if the limit is any tighter params.amountInMaximum -= 1; + // router.connect(trader).exactOutput(params, { value }) await expect(router.connect(trader).exactOutput(params, { value })).to.be.revertedWith('Too much requested'); params.amountInMaximum += 1; @@ -724,7 +726,7 @@ describe('SwapRouter', function () { it('reverts if deadline passed', async () => { await expect( exactOutput( - tokens.slice(0, 2).map((token) => token.address), + path.slice(0, 3), 1, 3, 2 @@ -739,8 +741,10 @@ describe('SwapRouter', function () { // get balances before const poolBefore = await getBalances(pool); const traderBefore = await getBalances(trader.address); - - await exactOutput(tokens.slice(0, 2).map((token) => token.address)); + + console.log('path: ', path); + + await exactOutput(path.slice(0, 3)); // get balances after const poolAfter = await getBalances(pool); @@ -760,10 +764,9 @@ describe('SwapRouter', function () { const traderBefore = await getBalances(trader.address); await exactOutput( - tokens - .slice(0, 2) + path + .slice(0, 3) .reverse() - .map((token) => token.address) ); // get balances after @@ -782,7 +785,7 @@ describe('SwapRouter', function () { const traderBefore = await getBalances(trader.address); await exactOutput( - tokens.map((token) => token.address), + path, 1, 5 ); @@ -796,7 +799,7 @@ describe('SwapRouter', function () { it('2 -> 1 -> 0', async () => { const traderBefore = await getBalances(trader.address); - await exactOutput(tokens.map((token) => token.address).reverse(), 1, 5); + await exactOutput(path.slice().reverse(), 1, 5); const traderAfter = await getBalances(trader.address); @@ -807,7 +810,7 @@ describe('SwapRouter', function () { it('events', async () => { await expect( exactOutput( - tokens.map((token) => token.address), + path, 1, 5 ) @@ -846,7 +849,7 @@ describe('SwapRouter', function () { const poolBefore = await getBalances(pool); const traderBefore = await getBalances(trader.address); - await expect(exactOutput([await wnative.getAddress(), tokens[0].address])) + await expect(exactOutput([await wnative.getAddress(), ZERO_ADDRESS, tokens[0].address])) .to.emit(wnative, 'Deposit') .withArgs(await router.getAddress(), 3); @@ -862,7 +865,7 @@ describe('SwapRouter', function () { it('WNativeToken -> 0 -> 1', async () => { const traderBefore = await getBalances(trader.address); - await expect(exactOutput([await wnative.getAddress(), tokens[0].address, tokens[1].address], 1, 5)) + await expect(exactOutput([await wnative.getAddress(), ZERO_ADDRESS, tokens[0].address, ZERO_ADDRESS, tokens[1].address], 1, 5)) .to.emit(wnative, 'Deposit') .withArgs(await router.getAddress(), 5); @@ -887,7 +890,7 @@ describe('SwapRouter', function () { const poolBefore = await getBalances(pool); const traderBefore = await getBalances(trader.address); - await expect(exactOutput([tokens[0].address, await wnative.getAddress()])) + await expect(exactOutput([tokens[0].address, ZERO_ADDRESS, await wnative.getAddress()])) .to.emit(wnative, 'Withdrawal') .withArgs(await router.getAddress(), 1); @@ -904,7 +907,7 @@ describe('SwapRouter', function () { // get balances before const traderBefore = await getBalances(trader.address); - await expect(exactOutput([tokens[0].address, tokens[1].address, await wnative.getAddress()], 1, 5)) + await expect(exactOutput([tokens[0].address, ZERO_ADDRESS, tokens[1].address, ZERO_ADDRESS, await wnative.getAddress()], 1, 5)) .to.emit(wnative, 'Withdrawal') .withArgs(await router.getAddress(), 1); @@ -1071,7 +1074,7 @@ describe('SwapRouter', function () { it('#sweepTokenWithFee', async () => { const amountOutMinimum = 100; const params = { - path: encodePath([tokens[0].address, tokens[1].address]), + path: encodePath([tokens[0].address, ZERO_ADDRESS, tokens[1].address]), recipient: await router.getAddress(), deadline: 1, amountIn: 102, @@ -1101,7 +1104,7 @@ describe('SwapRouter', function () { const amountOutMinimum = 100; const params = { - path: encodePath([tokens[0].address, await wnative.getAddress()]), + path: encodePath([tokens[0].address, ZERO_ADDRESS, await wnative.getAddress()]), recipient: await router.getAddress(), deadline: 1, amountIn: 102, diff --git a/src/periphery/test/__snapshots__/SwapRouter.spec.ts.snap b/src/periphery/test/__snapshots__/SwapRouter.spec.ts.snap index 30c61f4f..503f2085 100644 --- a/src/periphery/test/__snapshots__/SwapRouter.spec.ts.snap +++ b/src/periphery/test/__snapshots__/SwapRouter.spec.ts.snap @@ -1,3 +1,3 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`SwapRouter bytecode size [ @skip-on-coverage ] 1`] = `13181`; +exports[`SwapRouter bytecode size [ @skip-on-coverage ] 1`] = `13667`; diff --git a/src/periphery/test/__snapshots__/V3Migrator.spec.ts.snap b/src/periphery/test/__snapshots__/V3Migrator.spec.ts.snap index 4f8984ee..d0899762 100644 --- a/src/periphery/test/__snapshots__/V3Migrator.spec.ts.snap +++ b/src/periphery/test/__snapshots__/V3Migrator.spec.ts.snap @@ -1,3 +1,3 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`V3Migrator #migrate gas [ @skip-on-coverage ] 1`] = `750201`; +exports[`V3Migrator #migrate gas [ @skip-on-coverage ] 1`] = `750213`; diff --git a/src/periphery/test/shared/completeFixture.ts b/src/periphery/test/shared/completeFixture.ts index d5ceae22..5aebb532 100644 --- a/src/periphery/test/shared/completeFixture.ts +++ b/src/periphery/test/shared/completeFixture.ts @@ -64,6 +64,11 @@ const completeFixture: () => Promise<{ tokens[1].address_ = await tokens[1].getAddress(); tokens[2].address_ = await tokens[2].getAddress(); + tokens.sort((tokenA: TestERC20WithAddress, tokenB: TestERC20WithAddress) => { + if (!tokenA.address_ || !tokenB.address_) return 0; + return tokenA.address_.toLowerCase() < tokenB.address_.toLowerCase() ? -1 : 1; + }); + const path: [string, string, string, string, string] = [ tokens[0].address_, ZERO_ADDRESS, // deployer @@ -72,11 +77,6 @@ const completeFixture: () => Promise<{ tokens[2].address_ ] - tokens.sort((tokenA: TestERC20WithAddress, tokenB: TestERC20WithAddress) => { - if (!tokenA.address_ || !tokenB.address_) return 0; - return tokenA.address_.toLowerCase() < tokenB.address_.toLowerCase() ? -1 : 1; - }); - const nftDescriptorLibraryFactory = await ethers.getContractFactory('NFTDescriptor'); const nftDescriptorLibrary = await nftDescriptorLibraryFactory.deploy(); const positionDescriptorFactory = await ethers.getContractFactory('NonfungibleTokenPositionDescriptor', {