Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jsy1218 committed Nov 15, 2024
1 parent 34f7e38 commit 70c4df2
Show file tree
Hide file tree
Showing 6 changed files with 687 additions and 59 deletions.
5 changes: 3 additions & 2 deletions lib/cron/cache-pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
V2SubgraphProvider,
V3SubgraphPool,
V3SubgraphProvider,
V4SubgraphPool,
} from '@uniswap/smart-order-router'
import { EventBridgeEvent, ScheduledHandler } from 'aws-lambda'
import { S3 } from 'aws-sdk'
Expand All @@ -23,7 +24,7 @@ import { AWSMetricsLogger } from '../handlers/router-entities/aws-metrics-logger
import { metricScope } from 'aws-embedded-metrics'
import * as zlib from 'zlib'
import dotenv from 'dotenv'
import { v4HooksPoolsFiltering } from '../../test/utils/v4HooksPoolsFiltering'
import { v4HooksPoolsFiltering } from '../util/v4HooksPoolsFiltering'

// Needed for local stack dev, not needed for staging or prod
// But it still doesn't work on the local cdk stack update,
Expand Down Expand Up @@ -223,7 +224,7 @@ const handler: ScheduledHandler = metricScope((metrics) => async (event: EventBr
}

if (protocol === Protocol.V4) {
pools = v4HooksPoolsFiltering(chainId, pools)
pools = v4HooksPoolsFiltering(chainId, pools as Array<V4SubgraphPool>)
}

metric.putMetric(`${metricPrefix}.getPools.latency`, Date.now() - beforeGetPool)
Expand Down
62 changes: 62 additions & 0 deletions lib/util/v4HooksPoolsFiltering.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { V4SubgraphPool } from '@uniswap/smart-order-router'
import { Hook } from '@uniswap/v4-sdk'
import { HOOKS_ADDRESSES_ALLOWLIST } from './hooksAddressesAllowlist'
import { ChainId } from '@uniswap/sdk-core'
import { PriorityQueue } from '@datastructures-js/priority-queue'

type V4PoolGroupingKey = string

function convertV4PoolToGroupingKey(pool: V4SubgraphPool): V4PoolGroupingKey {
return pool.token0.id.concat(pool.token1.id).concat(pool.feeTier)
}

function isHooksPoolRoutable(pool: V4SubgraphPool): boolean {
return (
!Hook.hasSwapPermissions(pool.hooks) &&
// If the fee tier is smaller than or equal to 100%, it means the pool is not dynamic fee pool.
// Swap fee in total can be 100% (https://github.com/Uniswap/v4-core/blob/b619b6718e31aa5b4fa0286520c455ceb950276d/src/libraries/SwapMath.sol#L12)
// Dynamic fee is at 0x800000 or 838.8608% fee tier.
// Since pool manager doesn;t check the fee at 100% max during pool initialization (https://github.com/Uniswap/v4-core/blob/main/src/PoolManager.sol#L128)
// it's more defensive programming to ensure the fee tier is less than or equal to 100%
Number(pool.feeTier) <= 1000000
)
}

// it has to be a min heap in order to preserve the top eth tvl v4 pools
const V4SubgraphPoolComparator = (a: V4SubgraphPool, b: V4SubgraphPool) => {
return a.tvlETH > b.tvlETH ? 1 : -1
}

export function v4HooksPoolsFiltering(chainId: ChainId, pools: Array<V4SubgraphPool>): Array<V4SubgraphPool> {
const v4PoolsByTokenPairsAndFees: Record<V4PoolGroupingKey, PriorityQueue<V4SubgraphPool>> = {}

pools.forEach((pool: V4SubgraphPool) => {
if (isHooksPoolRoutable(pool)) {
const v4Pools =
v4PoolsByTokenPairsAndFees[convertV4PoolToGroupingKey(pool)] ??
new PriorityQueue<V4SubgraphPool>(V4SubgraphPoolComparator)

v4Pools.push(pool)

if (v4Pools.size() > 10) {
v4Pools.dequeue()
}

v4PoolsByTokenPairsAndFees[pool.token0.id.concat(pool.token1.id).concat(pool.feeTier)] = v4Pools
}
})

const topTvlPools: Array<V4SubgraphPool> = []
Object.values(v4PoolsByTokenPairsAndFees).forEach((pq: PriorityQueue<V4SubgraphPool>) => {
topTvlPools.push(...pq.toArray())
})

const allowlistedHooksPools = pools.filter((pool: V4SubgraphPool) => {
return (
HOOKS_ADDRESSES_ALLOWLIST[chainId].includes(pool.hooks.toLowerCase()) &&
!topTvlPools.find((topPool: V4SubgraphPool) => topPool.id.toLowerCase() === pool.id.toLowerCase())
)
})

return topTvlPools.concat(allowlistedHooksPools)
}
29 changes: 23 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"typescript": "^4.2.3"
},
"dependencies": {
"@datastructures-js/priority-queue": "^6.3.1",
"@hapi/joi": "^17.1.1",
"@middy/core": "^2.4.1",
"@middy/http-error-handler": "^2.4.1",
Expand All @@ -80,13 +81,13 @@
"@types/async-retry": "^1.4.2",
"@types/chai-subset": "^1.3.3",
"@types/qs": "^6.9.7",
"@types/semver": "^7.5.8",
"@types/sinon": "^10.0.6",
"@types/stats-lite": "^2.2.0",
"@uniswap/default-token-list": "^11.13.0",
"@uniswap/permit2-sdk": "^1.3.0",
"@uniswap/router-sdk": "^1.14.0",
"@uniswap/sdk-core": "^5.9.0",
"@types/semver": "^7.5.8",
"@uniswap/smart-order-router": "4.8.0",
"@uniswap/token-lists": "^1.0.0-beta.33",
"@uniswap/universal-router-sdk": "^4.6.1",
Expand Down
Loading

0 comments on commit 70c4df2

Please sign in to comment.