Skip to content

Commit

Permalink
other tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fourlen committed Jun 7, 2024
1 parent af7c93a commit b6b48cc
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 32 deletions.
7 changes: 4 additions & 3 deletions src/periphery/contracts/SwapRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion src/periphery/contracts/libraries/Path.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/periphery/test/Quoter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

Expand Down
43 changes: 23 additions & 20 deletions src/periphery/test/SwapRouter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -782,7 +785,7 @@ describe('SwapRouter', function () {
const traderBefore = await getBalances(trader.address);

await exactOutput(
tokens.map((token) => token.address),
path,
1,
5
);
Expand All @@ -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);

Expand All @@ -807,7 +810,7 @@ describe('SwapRouter', function () {
it('events', async () => {
await expect(
exactOutput(
tokens.map((token) => token.address),
path,
1,
5
)
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/periphery/test/__snapshots__/SwapRouter.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -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`;
2 changes: 1 addition & 1 deletion src/periphery/test/__snapshots__/V3Migrator.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -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`;
10 changes: 5 additions & 5 deletions src/periphery/test/shared/completeFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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', {
Expand Down

0 comments on commit b6b48cc

Please sign in to comment.