Skip to content

Commit

Permalink
feat(release): Update contracts to not use hardcoded pool bytecode ha…
Browse files Browse the repository at this point in the history
…sh (#5)

* feat(release): Update contracts and compile configs

* feat(release): Remove brittle gas tests
  • Loading branch information
weiyuan95 authored Sep 28, 2022
1 parent 6936bc2 commit 52c601e
Show file tree
Hide file tree
Showing 17 changed files with 543 additions and 1,564 deletions.
5 changes: 3 additions & 2 deletions contracts/libraries/PoolAddress.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity =0.7.6;

import { UniswapV3Pool } from '@birthdayresearch/uniswap-v3-core/contracts/UniswapV3Pool.sol';

/// @title Provides functions for deriving a pool address from the factory, tokens, and the fee
library PoolAddress {
bytes32 internal constant POOL_INIT_CODE_HASH = 0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54;

/// @notice The identifying key of the pool
struct PoolKey {
Expand Down Expand Up @@ -39,7 +40,7 @@ library PoolAddress {
hex'ff',
factory,
keccak256(abi.encode(key.token0, key.token1, key.fee)),
POOL_INIT_CODE_HASH
keccak256(type(UniswapV3Pool).creationCode)
)
)
)
Expand Down
3 changes: 2 additions & 1 deletion contracts/test/PoolAddressTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
pragma solidity =0.7.6;

import '../libraries/PoolAddress.sol';
import { UniswapV3Pool } from '@birthdayresearch/uniswap-v3-core/contracts/UniswapV3Pool.sol';

contract PoolAddressTest {
function POOL_INIT_CODE_HASH() external pure returns (bytes32) {
return PoolAddress.POOL_INIT_CODE_HASH;
return keccak256(type(UniswapV3Pool).creationCode);
}

function computeAddress(
Expand Down
41 changes: 4 additions & 37 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,14 @@ import '@nomiclabs/hardhat-waffle'
import 'hardhat-typechain'
import 'hardhat-watcher'

const LOW_OPTIMIZER_COMPILER_SETTINGS = {
version: '0.7.6',
settings: {
evmVersion: 'istanbul',
optimizer: {
enabled: true,
runs: 2_000,
},
metadata: {
bytecodeHash: 'none',
},
},
}

const LOWEST_OPTIMIZER_COMPILER_SETTINGS = {
version: '0.7.6',
settings: {
evmVersion: 'istanbul',
optimizer: {
enabled: true,
runs: 1_000,
},
metadata: {
bytecodeHash: 'none',
},
},
}

// Compiler settings is in line with V3-Core
const DEFAULT_COMPILER_SETTINGS = {
version: '0.7.6',
settings: {
evmVersion: 'istanbul',
optimizer: {
enabled: true,
runs: 1_000_000,
runs: 800,
},
metadata: {
bytecodeHash: 'none',
Expand All @@ -49,7 +22,8 @@ const DEFAULT_COMPILER_SETTINGS = {
export default {
networks: {
hardhat: {
allowUnlimitedContractSize: false,
// Since we only need to deploy the contracts for testing, we don't care about the contract size
allowUnlimitedContractSize: true,
},
mainnet: {
url: `https://mainnet.infura.io/v3/${process.env.INFURA_API_KEY}`,
Expand Down Expand Up @@ -86,13 +60,6 @@ export default {
},
solidity: {
compilers: [DEFAULT_COMPILER_SETTINGS],
overrides: {
'contracts/NonfungiblePositionManager.sol': LOW_OPTIMIZER_COMPILER_SETTINGS,
'contracts/test/MockTimeNonfungiblePositionManager.sol': LOW_OPTIMIZER_COMPILER_SETTINGS,
'contracts/test/NFTDescriptorTest.sol': LOWEST_OPTIMIZER_COMPILER_SETTINGS,
'contracts/NonfungibleTokenPositionDescriptor.sol': LOWEST_OPTIMIZER_COMPILER_SETTINGS,
'contracts/libraries/NFTDescriptor.sol': LOWEST_OPTIMIZER_COMPILER_SETTINGS,
},
},
watcher: {
test: {
Expand Down
8 changes: 0 additions & 8 deletions test/Base64.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { base64Encode } from './shared/base64'
import { expect } from './shared/expect'
import { Base64Test } from '../typechain'
import { randomBytes } from 'crypto'
import snapshotGasCost from './shared/snapshotGasCost'

function stringToHex(str: string): string {
return `0x${Buffer.from(str, 'utf8').toString('hex')}`
Expand Down Expand Up @@ -39,10 +38,6 @@ describe('Base64', () => {
it(`works for "${example}"`, async () => {
expect(await base64.encode(stringToHex(example))).to.eq(base64Encode(example))
})

it(`gas cost of encode(${example})`, async () => {
await snapshotGasCost(base64.getGasCostOfEncode(stringToHex(example)))
})
}

describe('max size string (24kB)', () => {
Expand All @@ -56,9 +51,6 @@ describe('Base64', () => {
it('correctness', async () => {
expect(await base64.encode(stringToHex(str))).to.eq(base64Encode(str))
})
it('gas cost', async () => {
await snapshotGasCost(base64.getGasCostOfEncode(stringToHex(str)))
})
})

it('tiny fuzzing', async () => {
Expand Down
83 changes: 0 additions & 83 deletions test/LiquidityAmounts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { LiquidityAmountsTest } from '../typechain/LiquidityAmountsTest'
import { encodePriceSqrt } from './shared/encodePriceSqrt'
import { expect } from './shared/expect'

import snapshotGasCost from './shared/snapshotGasCost'

describe('LiquidityAmounts', async () => {
let liquidityFromAmounts: LiquidityAmountsTest
Expand All @@ -13,22 +12,6 @@ describe('LiquidityAmounts', async () => {
liquidityFromAmounts = (await liquidityFromAmountsTestFactory.deploy()) as LiquidityAmountsTest
})

describe('#getLiquidityForAmount0', () => {
it('gas', async () => {
const sqrtPriceAX96 = encodePriceSqrt(100, 110)
const sqrtPriceBX96 = encodePriceSqrt(110, 100)
await snapshotGasCost(liquidityFromAmounts.getGasCostOfGetLiquidityForAmount0(sqrtPriceAX96, sqrtPriceBX96, 100))
})
})

describe('#getLiquidityForAmount1', () => {
it('gas', async () => {
const sqrtPriceAX96 = encodePriceSqrt(100, 110)
const sqrtPriceBX96 = encodePriceSqrt(110, 100)
await snapshotGasCost(liquidityFromAmounts.getGasCostOfGetLiquidityForAmount1(sqrtPriceAX96, sqrtPriceBX96, 100))
})
})

describe('#getLiquidityForAmounts', () => {
it('amounts for price inside', async () => {
const sqrtPriceX96 = encodePriceSqrt(1, 1)
Expand Down Expand Up @@ -99,47 +82,6 @@ describe('LiquidityAmounts', async () => {
)
expect(liquidity).to.eq(2097)
})

it('gas for price below', async () => {
const sqrtPriceX96 = encodePriceSqrt(99, 110)
const sqrtPriceAX96 = encodePriceSqrt(100, 110)
const sqrtPriceBX96 = encodePriceSqrt(110, 100)
await snapshotGasCost(
liquidityFromAmounts.getGasCostOfGetLiquidityForAmounts(sqrtPriceX96, sqrtPriceAX96, sqrtPriceBX96, 100, 200)
)
})
it('gas for price above', async () => {
const sqrtPriceX96 = encodePriceSqrt(111, 100)
const sqrtPriceAX96 = encodePriceSqrt(100, 110)
const sqrtPriceBX96 = encodePriceSqrt(110, 100)
await snapshotGasCost(
liquidityFromAmounts.getGasCostOfGetLiquidityForAmounts(sqrtPriceX96, sqrtPriceAX96, sqrtPriceBX96, 100, 200)
)
})
it('gas for price inside', async () => {
const sqrtPriceX96 = encodePriceSqrt(1, 1)
const sqrtPriceAX96 = encodePriceSqrt(100, 110)
const sqrtPriceBX96 = encodePriceSqrt(110, 100)
await snapshotGasCost(
liquidityFromAmounts.getGasCostOfGetLiquidityForAmounts(sqrtPriceX96, sqrtPriceAX96, sqrtPriceBX96, 100, 200)
)
})
})

describe('#getAmount0ForLiquidity', () => {
it('gas', async () => {
const sqrtPriceAX96 = encodePriceSqrt(100, 110)
const sqrtPriceBX96 = encodePriceSqrt(110, 100)
await snapshotGasCost(liquidityFromAmounts.getGasCostOfGetAmount0ForLiquidity(sqrtPriceAX96, sqrtPriceBX96, 100))
})
})

describe('#getLiquidityForAmount1', () => {
it('gas', async () => {
const sqrtPriceAX96 = encodePriceSqrt(100, 110)
const sqrtPriceBX96 = encodePriceSqrt(110, 100)
await snapshotGasCost(liquidityFromAmounts.getGasCostOfGetAmount1ForLiquidity(sqrtPriceAX96, sqrtPriceBX96, 100))
})
})

describe('#getAmountsForLiquidity', () => {
Expand Down Expand Up @@ -212,30 +154,5 @@ describe('LiquidityAmounts', async () => {
expect(amount0).to.eq(0)
expect(amount1).to.eq(199)
})

it('gas for price below', async () => {
const sqrtPriceX96 = encodePriceSqrt(99, 110)
const sqrtPriceAX96 = encodePriceSqrt(100, 110)
const sqrtPriceBX96 = encodePriceSqrt(110, 100)
await snapshotGasCost(
liquidityFromAmounts.getGasCostOfGetAmountsForLiquidity(sqrtPriceX96, sqrtPriceAX96, sqrtPriceBX96, 2148)
)
})
it('gas for price above', async () => {
const sqrtPriceX96 = encodePriceSqrt(111, 100)
const sqrtPriceAX96 = encodePriceSqrt(100, 110)
const sqrtPriceBX96 = encodePriceSqrt(110, 100)
await snapshotGasCost(
liquidityFromAmounts.getGasCostOfGetAmountsForLiquidity(sqrtPriceX96, sqrtPriceAX96, sqrtPriceBX96, 1048)
)
})
it('gas for price inside', async () => {
const sqrtPriceX96 = encodePriceSqrt(1, 1)
const sqrtPriceAX96 = encodePriceSqrt(100, 110)
const sqrtPriceBX96 = encodePriceSqrt(110, 100)
await snapshotGasCost(
liquidityFromAmounts.getGasCostOfGetAmountsForLiquidity(sqrtPriceX96, sqrtPriceAX96, sqrtPriceBX96, 2097)
)
})
})
})
10 changes: 0 additions & 10 deletions test/Multicall.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { ethers, waffle } from 'hardhat'
import { TestMulticall } from '../typechain/TestMulticall'
import { expect } from './shared/expect'

import snapshotGasCost from './shared/snapshotGasCost'

describe('Multicall', async () => {
let wallets: Wallet[]

Expand Down Expand Up @@ -54,12 +52,4 @@ describe('Multicall', async () => {
expect(await multicall.returnSender()).to.eq(wallets[0].address)
})
})

it('gas cost of pay w/o multicall', async () => {
await snapshotGasCost(multicall.pays({ value: 3 }))
})

it('gas cost of pay w/ multicall', async () => {
await snapshotGasCost(multicall.multicall([multicall.interface.encodeFunctionData('pays')], { value: 3 }))
})
})
22 changes: 0 additions & 22 deletions test/NFTDescriptor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { expect } from './shared/expect'
import { TestERC20Metadata, NFTDescriptorTest } from '../typechain'
import { Fixture } from 'ethereum-waffle'
import { FeeAmount, TICK_SPACINGS } from './shared/constants'
import snapshotGasCost from './shared/snapshotGasCost'
import { formatSqrtRatioX96 } from './shared/formatSqrtRatioX96'
import { getMaxTick, getMinTick } from './shared/ticks'
import { randomBytes } from 'crypto'
Expand Down Expand Up @@ -305,27 +304,6 @@ describe('NFTDescriptor', () => {
})
})

it('gas', async () => {
await snapshotGasCost(
nftDescriptor.getGasCostOfConstructTokenURI({
tokenId,
baseTokenAddress,
quoteTokenAddress,
baseTokenSymbol,
quoteTokenSymbol,
baseTokenDecimals,
quoteTokenDecimals,
flipRatio,
tickLower,
tickUpper,
tickCurrent,
tickSpacing,
fee,
poolAddress,
})
)
})

it('snapshot matches', async () => {
// get snapshot with super rare special sparkle
tokenId = 1
Expand Down
Loading

0 comments on commit 52c601e

Please sign in to comment.