@@ -9,9 +9,11 @@ import {
99 log ,
1010 metric ,
1111 MetricLoggerUnit ,
12+ NATIVE_CURRENCY ,
1213 routeToString ,
1314 SupportedRoutes ,
1415 SwapOptionsUniversalRouter ,
16+ WRAPPED_NATIVE_CURRENCY ,
1517} from '@uniswap/smart-order-router'
1618import { AWSError , DynamoDB , Lambda } from 'aws-sdk'
1719import { ChainId , Currency , CurrencyAmount , Fraction , Token , TradeType } from '@uniswap/sdk-core'
@@ -24,6 +26,7 @@ import { DEFAULT_BLOCKS_TO_LIVE_ROUTES_DB } from '../../../util/defaultBlocksToL
2426import { serializeRouteIds } from '@uniswap/smart-order-router/build/main/util/serializeRouteIds'
2527import { UniversalRouterVersion } from '@uniswap/universal-router-sdk'
2628import { computeProtocolsInvolvedIfMixed } from '../../../util/computeProtocolsInvolvedIfMixed'
29+ import { QueryInput } from 'aws-sdk/clients/dynamodb'
2730
2831interface ConstructorParams {
2932 /**
@@ -141,7 +144,22 @@ export class DynamoRouteCachingProvider extends IRouteCachingProvider {
141144 } ,
142145 }
143146
144- const result = await this . ddbClient . query ( queryParams ) . promise ( )
147+ const additionalQueryParams = DynamoRouteCachingProvider . getAdditionalQueryParams ( currencyIn , currencyOut , tradeType , chainId , includesV4Pool , this . routesTableName )
148+
149+ let result = undefined
150+ if ( additionalQueryParams === undefined ) {
151+ result = await this . ddbClient . query ( queryParams ) . promise ( )
152+ } else {
153+ const [ directResult , additionalResult ] = await Promise . all ( [
154+ this . ddbClient . query ( queryParams ) . promise ( ) ,
155+ this . ddbClient . query ( additionalQueryParams ) . promise ( ) ,
156+ ] )
157+ result = {
158+ Items : [ ...directResult . Items ?? [ ] , ...additionalResult . Items ?? [ ] ] ,
159+ }
160+ }
161+
162+ result = await this . ddbClient . query ( queryParams ) . promise ( )
145163 if ( result . Items && result . Items . length > 0 ) {
146164 metric . putMetric ( 'RoutesDbPreFilterEntriesFound' , result . Items . length , MetricLoggerUnit . Count )
147165
@@ -217,6 +235,78 @@ export class DynamoRouteCachingProvider extends IRouteCachingProvider {
217235 return undefined
218236 }
219237
238+ static getAdditionalQueryParams ( currencyIn : Currency , currencyOut : Currency , tradeType : TradeType , chainId : ChainId , includesV4Pool : boolean , tableName : string ) {
239+ let additionalQueryParams = undefined
240+
241+ if ( currencyIn . isNative ) {
242+ additionalQueryParams = {
243+ TableName : tableName ,
244+ KeyConditionExpression : '#pk = :pk' ,
245+ ExpressionAttributeNames : {
246+ '#pk' : 'pairTradeTypeChainId' ,
247+ } ,
248+ ExpressionAttributeValues : {
249+ ':pk' : new PairTradeTypeChainId ( {
250+ currencyIn : WRAPPED_NATIVE_CURRENCY [ chainId ] . address ,
251+ currencyOut : PairTradeTypeChainId . deriveCurrencyAddress ( includesV4Pool , currencyOut ) ,
252+ tradeType,
253+ chainId,
254+ } ) ,
255+ } ,
256+ }
257+ } else if ( currencyIn . address === WRAPPED_NATIVE_CURRENCY [ chainId ] . address ) {
258+ additionalQueryParams = {
259+ TableName : tableName ,
260+ KeyConditionExpression : '#pk = :pk' ,
261+ ExpressionAttributeNames : {
262+ '#pk' : 'pairTradeTypeChainId' ,
263+ } ,
264+ ExpressionAttributeValues : {
265+ ':pk' : new PairTradeTypeChainId ( {
266+ currencyIn : NATIVE_CURRENCY [ chainId ] . toString ( ) ,
267+ currencyOut : PairTradeTypeChainId . deriveCurrencyAddress ( includesV4Pool , currencyOut ) ,
268+ tradeType,
269+ chainId,
270+ } ) ,
271+ } ,
272+ }
273+ } else if ( currencyOut . isNative ) {
274+ additionalQueryParams = {
275+ TableName : tableName ,
276+ KeyConditionExpression : '#pk = :pk' ,
277+ ExpressionAttributeNames : {
278+ '#pk' : 'pairTradeTypeChainId' ,
279+ } ,
280+ ExpressionAttributeValues : {
281+ ':pk' : new PairTradeTypeChainId ( {
282+ currencyIn : PairTradeTypeChainId . deriveCurrencyAddress ( includesV4Pool , currencyIn ) ,
283+ currencyOut : WRAPPED_NATIVE_CURRENCY [ chainId ] . address ,
284+ tradeType,
285+ chainId,
286+ } ) ,
287+ } ,
288+ }
289+ } else if ( currencyOut . address === WRAPPED_NATIVE_CURRENCY [ chainId ] . address ) {
290+ additionalQueryParams = {
291+ TableName : tableName ,
292+ KeyConditionExpression : '#pk = :pk' ,
293+ ExpressionAttributeNames : {
294+ '#pk' : 'pairTradeTypeChainId' ,
295+ } ,
296+ ExpressionAttributeValues : {
297+ ':pk' : new PairTradeTypeChainId ( {
298+ currencyIn : PairTradeTypeChainId . deriveCurrencyAddress ( includesV4Pool , currencyIn ) ,
299+ currencyOut : NATIVE_CURRENCY [ chainId ] . toString ( ) ,
300+ tradeType,
301+ chainId,
302+ } ) ,
303+ } ,
304+ }
305+ }
306+
307+ return additionalQueryParams
308+ }
309+
220310 private parseCachedRoutes (
221311 result : PromiseResult < DynamoDB . DocumentClient . QueryOutput , AWSError > ,
222312 chainId : ChainId ,
0 commit comments