Skip to content

Commit

Permalink
add native currency routing cached routes unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
jsy1218 committed Sep 24, 2024
1 parent b906b14 commit a752555
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
SupportedRoutes,
} from '@uniswap/smart-order-router'
import { AWSError, DynamoDB, Lambda } from 'aws-sdk'
import { ChainId, Currency, CurrencyAmount, Fraction, Token, TradeType } from '@uniswap/sdk-core'
import { ChainId, Currency, CurrencyAmount, Fraction, TradeType } from '@uniswap/sdk-core'
import { Protocol } from '@uniswap/router-sdk'
import { PairTradeTypeChainId } from './model/pair-trade-type-chain-id'
import { CachedRoutesMarshaller } from '../../marshalling/cached-routes-marshaller'
Expand Down Expand Up @@ -449,14 +449,14 @@ export class DynamoRouteCachingProvider extends IRouteCachingProvider {
*
* @param _chainId
* @param _amount
* @param _quoteToken
* @param _quoteCurrency
* @param _tradeType
* @param _protocols
*/
public async getCacheMode(
_chainId: ChainId,
_amount: CurrencyAmount<Currency>,
_quoteToken: Token,
_quoteCurrency: Currency,
_tradeType: TradeType,
_protocols: Protocol[]
): Promise<CacheMode> {
Expand All @@ -481,7 +481,7 @@ export class DynamoRouteCachingProvider extends IRouteCachingProvider {
}

/**
* Helper function to determine the tokenIn and tokenOut given the tradeType, quoteToken and amount.currency
* Helper function to determine the tokenIn and tokenOut given the tradeType, quoteCurrency and amount.currency
*
* @param amount
* @param quoteCurrency
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import chaiAsPromised from 'chai-as-promised'
import 'reflect-metadata'
import { setupTables } from '../../../../dbSetup'
import { DynamoRouteCachingProvider } from '../../../../../../lib/handlers/router-entities/route-caching'
import { Protocol } from '@uniswap/router-sdk'
import { ChainId, CurrencyAmount, TradeType } from '@uniswap/sdk-core'
import { ADDRESS_ZERO, Protocol } from '@uniswap/router-sdk'
import { ChainId, CurrencyAmount, Ether, TradeType } from '@uniswap/sdk-core'
import JSBI from 'jsbi'
import { FeeAmount, Pool } from '@uniswap/v3-sdk'
import { Pool as V4Pool } from '@uniswap/v4-sdk'
import { WNATIVE_ON } from '../../../../../utils/tokens'
import { CacheMode, CachedRoute, CachedRoutes, UNI_MAINNET, USDC_MAINNET, V3Route } from '@uniswap/smart-order-router'
import { DynamoDBTableProps } from '../../../../../../bin/stacks/routing-database-stack'
import { V4Route } from '@uniswap/smart-order-router/build/main/routers'
import { TICK_SPACINGS } from '../../../../../utils/ticks'

chai.use(chaiAsPromised)

Expand Down Expand Up @@ -80,6 +83,17 @@ const TEST_WETH_USDC_POOL = new Pool(
/* tickCurrent */ -69633
)

const TEST_ETH_USDC_POOL = new V4Pool(
Ether.onChain(ChainId.MAINNET),
USDC_MAINNET,
FeeAmount.HIGH,
TICK_SPACINGS[FeeAmount.HIGH],
ADDRESS_ZERO,
/* sqrtRatio */ '2437312313659959819381354528',
/* liquidity */ '10272714736694327408',
/* tickCurrent */ -69633
)

const TEST_UNI_USDC_POOL = new Pool(
UNI_MAINNET,
USDC_MAINNET,
Expand All @@ -90,9 +104,11 @@ const TEST_UNI_USDC_POOL = new Pool(
)

const TEST_WETH_USDC_V3_ROUTE = new V3Route([TEST_WETH_USDC_POOL], WETH, USDC_MAINNET)
const TEST_ETH_USDC_V4_ROUTE = new V4Route([TEST_ETH_USDC_POOL], Ether.onChain(ChainId.MAINNET), USDC_MAINNET)
const TEST_UNI_USDC_ROUTE = new V3Route([TEST_UNI_USDC_POOL], UNI_MAINNET, USDC_MAINNET)

const TEST_CACHED_ROUTE = new CachedRoute({ route: TEST_WETH_USDC_V3_ROUTE, percent: 100 })
const TEST_CACHED_ROUTE_V4 = new CachedRoute({ route: TEST_ETH_USDC_V4_ROUTE, percent: 100 })
const TEST_CACHED_ROUTES = new CachedRoutes({
routes: [TEST_CACHED_ROUTE],
chainId: TEST_CACHED_ROUTE.route.chainId,
Expand All @@ -104,6 +120,17 @@ const TEST_CACHED_ROUTES = new CachedRoutes({
originalAmount: '1',
blocksToLive: 5,
})
const TEST_CACHED_NATIVE_CURRENCY_ROUTES = new CachedRoutes({
routes: [TEST_CACHED_ROUTE_V4],
chainId: TEST_CACHED_ROUTE_V4.route.chainId,
currencyIn: Ether.onChain(ChainId.MAINNET),
currencyOut: USDC_MAINNET,
protocolsCovered: [TEST_CACHED_ROUTE_V4.protocol],
blockNumber: 0,
tradeType: TradeType.EXACT_INPUT,
originalAmount: '1',
blocksToLive: 5,
})

const TEST_UNCACHED_ROUTE = new CachedRoute({ route: TEST_UNI_USDC_ROUTE, percent: 100 })
const TEST_UNCACHED_ROUTES = new CachedRoutes({
Expand Down Expand Up @@ -158,6 +185,76 @@ describe('DynamoRouteCachingProvider', async () => {
expect(route).to.not.be.undefined
})

it('Caches routes for a native currency input without v4 protocol', async () => {
const currencyAmount = CurrencyAmount.fromRawAmount(
Ether.onChain(ChainId.MAINNET),
JSBI.BigInt(1 * 10 ** WETH.decimals)
)
const cacheMode = await dynamoRouteCache.getCacheMode(
ChainId.MAINNET,
currencyAmount,
USDC_MAINNET,
TradeType.EXACT_INPUT,
[Protocol.V3]
)
expect(cacheMode).to.equal(CacheMode.Livemode)

const insertedIntoCache = await dynamoRouteCache.setCachedRoute(TEST_CACHED_ROUTES, currencyAmount)
expect(insertedIntoCache).to.be.true

const cacheModeFromCachedRoutes = await dynamoRouteCache.getCacheModeFromCachedRoutes(
TEST_CACHED_ROUTES,
currencyAmount
)
expect(cacheModeFromCachedRoutes).to.equal(CacheMode.Livemode)

// Fetches route successfully from cache when it has been cached.
const route = await dynamoRouteCache.getCachedRoute(
ChainId.MAINNET,
currencyAmount,
USDC_MAINNET,
TradeType.EXACT_INPUT,
[Protocol.V3],
TEST_CACHED_ROUTES.blockNumber
)
expect(route).to.not.be.undefined
})

it('Caches routes for a native currency input with v4 protocol', async () => {
const currencyAmount = CurrencyAmount.fromRawAmount(
Ether.onChain(ChainId.MAINNET),
JSBI.BigInt(1 * 10 ** WETH.decimals)
)
const cacheMode = await dynamoRouteCache.getCacheMode(
ChainId.MAINNET,
currencyAmount,
USDC_MAINNET,
TradeType.EXACT_INPUT,
[Protocol.V4]
)
expect(cacheMode).to.equal(CacheMode.Livemode)

const insertedIntoCache = await dynamoRouteCache.setCachedRoute(TEST_CACHED_NATIVE_CURRENCY_ROUTES, currencyAmount)
expect(insertedIntoCache).to.be.true

const cacheModeFromCachedRoutes = await dynamoRouteCache.getCacheModeFromCachedRoutes(
TEST_CACHED_NATIVE_CURRENCY_ROUTES,
currencyAmount
)
expect(cacheModeFromCachedRoutes).to.equal(CacheMode.Livemode)

// Fetches route successfully from cache when it has been cached.
const route = await dynamoRouteCache.getCachedRoute(
ChainId.MAINNET,
currencyAmount,
USDC_MAINNET,
TradeType.EXACT_INPUT,
[Protocol.V4],
TEST_CACHED_NATIVE_CURRENCY_ROUTES.blockNumber
)
expect(route).to.not.be.undefined
})

it('Still uses RoutesDB Table for the default configuration', async () => {
const currencyAmount = CurrencyAmount.fromRawAmount(UNI_MAINNET, JSBI.BigInt(1 * 10 ** UNI_MAINNET.decimals))
const cacheMode = await dynamoRouteCache.getCacheMode(
Expand Down

0 comments on commit a752555

Please sign in to comment.