Skip to content

Commit

Permalink
gas tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilz committed Sep 14, 2023
1 parent efef4ac commit 242d110
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 66 deletions.
128 changes: 64 additions & 64 deletions test/integration-tests/Uniswap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,70 +457,6 @@ describe('Uniswap V2 and V3 Tests:', () => {
}
}

describe.only('STETH', () => {
const STETH_DUST = 1;

it('completes a V3 exactIn with STETH --> WETH', async () => {
const { stethContract } = await setupStethContext()

const amountInSTETH: number = expandTo18DecimalsBN(0.1)
const amountInWSTETH: number = await stethContract.getSharesByPooledEth(amountInSTETH.sub(STETH_DUST))
const amountOutMin: number = expandTo6DecimalsBN(0)

planner.addCommand(CommandType.PERMIT2_TRANSFER_FROM, [STETH.address, ADDRESS_THIS, amountInSTETH])
planner.addCommand(CommandType.WRAP_STETH, [ADDRESS_THIS, CONTRACT_BALANCE])
addV3ExactInTrades(
planner,
1,
amountOutMin,
MSG_SENDER,
[WSTETH.address, WETH.address],
SOURCE_ROUTER,
amountInWSTETH
)

const stethBalanceBefore = await stethContract.balanceOf(bob.address)
const {
wethBalanceBefore,
wethBalanceAfter,
} = await executeRouter(planner)
const stethBalanceAfter = await stethContract.balanceOf(bob.address)

expect(stethBalanceBefore.sub(stethBalanceAfter)).to.eq(amountInSTETH.sub(STETH_DUST))
expect(wethBalanceAfter.sub(wethBalanceBefore).toString()).to.eq('89416039046452189')
})

it('completes a V3 exactIn with WETH --> STETH', async () => {
const { stethContract, wstethContract, wethContract } = await setupStethContext()

const amountInWETH: number = expandTo18DecimalsBN(0.001)
const amountOutMin: number = expandTo6DecimalsBN(0)

addV3ExactInTrades(
planner,
1,
amountOutMin,
ADDRESS_THIS,
[WETH.address, WSTETH.address],
SOURCE_MSG_SENDER,
amountInWETH
)
planner.addCommand(CommandType.UNWRAP_STETH, [MSG_SENDER, 0])

const wstethBalanceBefore = await wstethContract.balanceOf(bob.address)
const stethBalanceBefore = await stethContract.balanceOf(bob.address)
const {
wethBalanceBefore,
wethBalanceAfter,
} = await executeRouter(planner)
const wstethBalanceAfter = await wstethContract.balanceOf(bob.address)
const stethBalanceAfter = await stethContract.balanceOf(bob.address)

expect(stethBalanceAfter.sub(stethBalanceBefore)).to.eq('825732662352157')
expect(wethBalanceBefore.sub(wethBalanceAfter)).to.eq(amountInWETH)
})
})

describe('ERC20 --> ERC20', () => {
it('completes a V3 exactIn swap', async () => {
const amountOutMin: BigNumber = expandTo18DecimalsBN(0.0005)
Expand Down Expand Up @@ -653,6 +589,70 @@ describe('Uniswap V2 and V3 Tests:', () => {
expect(ethBalanceBefore.sub(ethBalanceAfter)).to.eq(wethTraded.add(gasSpent))
})
})

describe('STETH', () => {
const STETH_DUST = 1;

it('completes a V3 exactIn with STETH --> WETH', async () => {
const { stethContract } = await setupStethContext()

const amountInSTETH: number = expandTo18DecimalsBN(0.1)
const amountInWSTETH: number = await stethContract.getSharesByPooledEth(amountInSTETH.sub(STETH_DUST))
const amountOutMin: number = expandTo6DecimalsBN(0)

planner.addCommand(CommandType.PERMIT2_TRANSFER_FROM, [STETH.address, ADDRESS_THIS, amountInSTETH])
planner.addCommand(CommandType.WRAP_STETH, [ADDRESS_THIS, CONTRACT_BALANCE])
addV3ExactInTrades(
planner,
1,
amountOutMin,
MSG_SENDER,
[WSTETH.address, WETH.address],
SOURCE_ROUTER,
amountInWSTETH
)

const stethBalanceBefore = await stethContract.balanceOf(bob.address)
const {
wethBalanceBefore,
wethBalanceAfter,
} = await executeRouter(planner)
const stethBalanceAfter = await stethContract.balanceOf(bob.address)

expect(stethBalanceBefore.sub(stethBalanceAfter)).to.eq(amountInSTETH.sub(STETH_DUST))
expect(wethBalanceAfter.sub(wethBalanceBefore).toString()).to.eq('89416039046452189')
})

it('completes a V3 exactIn with WETH --> STETH', async () => {
const { stethContract, wstethContract, wethContract } = await setupStethContext()

const amountInWETH: number = expandTo18DecimalsBN(0.001)
const amountOutMin: number = expandTo6DecimalsBN(0)

addV3ExactInTrades(
planner,
1,
amountOutMin,
ADDRESS_THIS,
[WETH.address, WSTETH.address],
SOURCE_MSG_SENDER,
amountInWETH
)
planner.addCommand(CommandType.UNWRAP_STETH, [MSG_SENDER, 0])

const wstethBalanceBefore = await wstethContract.balanceOf(bob.address)
const stethBalanceBefore = await stethContract.balanceOf(bob.address)
const {
wethBalanceBefore,
wethBalanceAfter,
} = await executeRouter(planner)
const wstethBalanceAfter = await wstethContract.balanceOf(bob.address)
const stethBalanceAfter = await stethContract.balanceOf(bob.address)

expect(stethBalanceAfter.sub(stethBalanceBefore)).to.eq('825732662352157')
expect(wethBalanceBefore.sub(wethBalanceAfter)).to.eq(amountInWETH)
})
})
})

describe('Mixing V2 and V3', () => {
Expand Down
86 changes: 84 additions & 2 deletions test/integration-tests/gas-tests/Uniswap.gas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import {
import { BigNumber, BigNumberish } from 'ethers'
import { UniversalRouter, Permit2 } from '../../../typechain'
import { abi as TOKEN_ABI } from '../../../artifacts/solmate/src/tokens/ERC20.sol/ERC20.json'
import { approveAndExecuteSwapRouter02, resetFork, WETH, DAI, USDC, USDT } from '../shared/mainnetForkHelpers'
import { abi as STETH_ABI } from '../../../artifacts/contracts/interfaces/external/ISTETH.sol/ISTETH.json'
import { approveAndExecuteSwapRouter02, resetFork, WETH, DAI, USDC, USDT, STETH, WSTETH } from '../shared/mainnetForkHelpers'
import {
ADDRESS_THIS,
ALICE_ADDRESS,
Expand Down Expand Up @@ -518,7 +519,8 @@ describe('Uniswap Gas Tests', () => {
amountOutMin: BigNumberish,
recipient?: string,
tokens: string[] = [DAI.address, WETH.address],
sourceOfTokens: boolean = SOURCE_MSG_SENDER
sourceOfTokens: boolean = SOURCE_MSG_SENDER,
amountIn: BigNumber = expandTo18DecimalsBN(500)
) => {
const path = encodePathExactInput(tokens)
for (let i = 0; i < numTrades; i++) {
Expand Down Expand Up @@ -676,6 +678,58 @@ describe('Uniswap Gas Tests', () => {
)
})
})

describe('STETH', () => {
const STETH_DUST = 1;

it('gas: completes a V3 exactIn with STETH --> WETH', async () => {
const { stethContract, wstethContract, wethContract } = await setupStethContext()

const amountInSTETH: number = expandTo18DecimalsBN(0.1)
const amountInWSTETH: number = await stethContract.getSharesByPooledEth(amountInSTETH.sub(STETH_DUST))
const amountOutMin: number = expandTo6DecimalsBN(0)

planner.addCommand(CommandType.PERMIT2_TRANSFER_FROM, [STETH.address, ADDRESS_THIS, amountInSTETH])
planner.addCommand(CommandType.WRAP_STETH, [ADDRESS_THIS, CONTRACT_BALANCE])
addV3ExactInTrades(
planner,
1,
amountOutMin,
MSG_SENDER,
[WSTETH.address, WETH.address],
SOURCE_ROUTER,
amountInWSTETH
)

const { commands, inputs } = planner
await snapshotGasCost(
router['execute(bytes,bytes[],uint256)'](commands, inputs, DEADLINE, { value: amountIn })
)
})

it('gas: completes a V3 exactIn with WETH --> STETH', async () => {
const { stethContract, wstethContract, wethContract } = await setupStethContext()

const amountInWETH: number = expandTo18DecimalsBN(0.001)
const amountOutMin: number = expandTo6DecimalsBN(0)

addV3ExactInTrades(
planner,
1,
amountOutMin,
ADDRESS_THIS,
[WETH.address, WSTETH.address],
SOURCE_MSG_SENDER,
amountInWETH
)
planner.addCommand(CommandType.UNWRAP_STETH, [MSG_SENDER, 0])

const { commands, inputs } = planner
await snapshotGasCost(
router['execute(bytes,bytes[],uint256)'](commands, inputs, DEADLINE, { value: amountIn })
)
})
})
})
})

Expand Down Expand Up @@ -1197,4 +1251,32 @@ describe('Uniswap Gas Tests', () => {
})
})
})
async function setupStethContext(): Promise<StethContextParams> {
const FORK_WITH_STETH_POOL = 18135610
await resetFork(FORK_WITH_STETH_POOL)
await hre.network.provider.request({
method: 'hardhat_impersonateAccount',
params: [ALICE_ADDRESS],
})
permit2 = (await deployPermit2()).connect(bob) as Permit2
router = (await deployUniversalRouter(permit2)).connect(bob) as UniversalRouter

let stethContract = new ethers.Contract(STETH.address, STETH_ABI, bob)
let wstethContract = new ethers.Contract(WSTETH.address, TOKEN_ABI, bob)
let wethContract = new ethers.Contract(WETH.address, TOKEN_ABI, bob)


// alice gives bob some tokens
await wethContract.connect(alice).transfer(bob.address, expandTo18DecimalsBN(100))
await stethContract.connect(alice).transfer(bob.address, expandTo18DecimalsBN(0.3))

// Bob max-approves the permit2 contract to access his STETH and WETH
await wethContract.connect(bob).approve(permit2.address, MAX_UINT)
await stethContract.connect(bob).approve(permit2.address, MAX_UINT)
await permit2.approve(STETH.address, router.address, MAX_UINT160, DEADLINE)
await permit2.approve(WETH.address, router.address, MAX_UINT160, DEADLINE)

return { stethContract, wstethContract, wethContract }
}

})
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,17 @@ Object {
"gasUsed": 129549,
}
`;

exports[`Uniswap Gas Tests Trade on UniswapV3 with Universal Router. STETH gas: completes a V3 exactIn with STETH --> WETH 1`] = `
Object {
"calldataByteLength": 804,
"gasUsed": 313473,
}
`;

exports[`Uniswap Gas Tests Trade on UniswapV3 with Universal Router. STETH gas: completes a V3 exactIn with WETH --> STETH 1`] = `
Object {
"calldataByteLength": 644,
"gasUsed": 262236,
}
`;

0 comments on commit 242d110

Please sign in to comment.