Skip to content

Commit

Permalink
add test to assert against hitting new caching lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
jsy1218 committed Oct 30, 2024
1 parent 437755a commit 4aa9d41
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,11 @@ export class DynamoRouteCachingProvider extends IRouteCachingProvider {
}

log.info(`[DynamoRouteCachingProvider] Sending async caching request to lambda ${JSON.stringify(params)}`)
metric.putMetric(
`CachingQuoteForRoutesDbRequestSentToLambda${this.cachingQuoteLambdaName}`,
1,
MetricLoggerUnit.Count
)

this.lambdaClient.invoke(params).promise()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ import {
USDC_MAINNET,
V3Route,
nativeOnChain,
MetricLoggerUnit,
} from '@uniswap/smart-order-router'
import { DynamoDBTableProps } from '../../../../../../bin/stacks/routing-database-stack'
import { V4Route } from '@uniswap/smart-order-router/build/main/routers'
import { NEW_CACHED_ROUTES_ROLLOUT_PERCENT } from '../../../../../../lib/util/newCachedRoutesRolloutPercent'
import sinon, { SinonSpy } from 'sinon'
import { metric } from '@uniswap/smart-order-router/build/main/util/metric'

chai.use(chaiAsPromised)

Expand Down Expand Up @@ -152,13 +156,63 @@ const TEST_UNCACHED_ROUTES = new CachedRoutes({
})

describe('DynamoRouteCachingProvider', async () => {
let spy: SinonSpy

beforeEach(() => {
spy = sinon.spy(metric, 'putMetric')
})

afterEach(() => {
spy.restore()
})

setupTables(TEST_ROUTE_CACHING_TABLE, TEST_ROUTE_DB_TABLE)
const dynamoRouteCache = new DynamoRouteCachingProvider({
routesTableName: DynamoDBTableProps.RoutesDbTable.Name,
routesCachingRequestFlagTableName: DynamoDBTableProps.RoutesDbCachingRequestFlagTable.Name,
cachingQuoteLambdaName: 'test',
})

it('Cached routes hits new cached routes lambda', async () => {
spy.withArgs('CachingQuoteForRoutesDbRequestSentToLambdanewcachinglambda', 1, MetricLoggerUnit.Count)

// testnet rolls out at 100%
const newCachedRoutesRolloutPercent = NEW_CACHED_ROUTES_ROLLOUT_PERCENT[ChainId.SEPOLIA]

const dynamoRouteCache = new DynamoRouteCachingProvider({
routesTableName: DynamoDBTableProps.RoutesDbTable.Name,
routesCachingRequestFlagTableName: DynamoDBTableProps.RoutesDbCachingRequestFlagTable.Name,
cachingQuoteLambdaName: Math.random() * 100 < (newCachedRoutesRolloutPercent ?? 0) ? 'newcachinglambda' : 'test',
})

const currencyAmount = CurrencyAmount.fromRawAmount(WETH, JSBI.BigInt(1 * 10 ** WETH.decimals))

const cacheMode = await dynamoRouteCache.getCacheMode(
ChainId.MAINNET,
currencyAmount,
USDC_MAINNET,
TradeType.EXACT_INPUT,
[Protocol.V3, Protocol.V4]
)
expect(cacheMode).to.equal(CacheMode.Livemode)

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

// 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

sinon.assert.called(spy)
})

it('Caches routes properly for a token pair that has its cache configured to Livemode', async () => {
const currencyAmount = CurrencyAmount.fromRawAmount(WETH, JSBI.BigInt(1 * 10 ** WETH.decimals))
const currencyAmountETH = CurrencyAmount.fromRawAmount(
Expand Down

0 comments on commit 4aa9d41

Please sign in to comment.