Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sor 3.16.24 #370

Merged
merged 3 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion lib/handlers/injector-sor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import {
V3PoolProvider,
IRouteCachingProvider,
CachingV2PoolProvider,
TokenValidatorProvider,
TokenPropertiesProvider,
} from '@uniswap/smart-order-router'
import { TokenList } from '@uniswap/token-lists'
import { default as bunyan, default as Logger } from 'bunyan'
Expand All @@ -48,6 +50,7 @@ import { DefaultEVMClient } from './evm/EVMClient'
import { InstrumentedEVMProvider } from './evm/provider/InstrumentedEVMProvider'
import { deriveProviderName } from './evm/provider/ProviderName'
import { V2DynamoCache } from './pools/pool-caching/v2/v2-dynamo-cache'
import { OnChainTokenFeeFetcher } from '@uniswap/smart-order-router/build/main/providers/token-fee-fetcher'

export const SUPPORTED_CHAINS: ChainId[] = [
ChainId.MAINNET,
Expand Down Expand Up @@ -190,7 +193,19 @@ export abstract class InjectorSOR<Router, QueryParams> extends Injector<
sourceOfTruthPoolProvider: noCacheV3PoolProvider,
})

const underlyingV2PoolProvider = new V2PoolProvider(chainId, multicall2Provider)
const tokenFeeFetcher = new OnChainTokenFeeFetcher(chainId, provider)
const tokenValidatorProvider = new TokenValidatorProvider(
chainId,
multicall2Provider,
new NodeJSCache(new NodeCache({ stdTTL: 30000, useClones: false }))
)
const tokenPropertiesProvider = new TokenPropertiesProvider(
chainId,
tokenValidatorProvider,
new NodeJSCache(new NodeCache({ stdTTL: 30000, useClones: false })),
tokenFeeFetcher
)
const underlyingV2PoolProvider = new V2PoolProvider(chainId, multicall2Provider, tokenPropertiesProvider)
const v2PoolProvider = new CachingV2PoolProvider(
chainId,
underlyingV2PoolProvider,
Expand Down
57 changes: 54 additions & 3 deletions lib/handlers/pools/pool-caching/v2/v2-dynamo-cache.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ICache } from '@uniswap/smart-order-router/build/main/providers/cache'
import { Pair } from '@uniswap/v2-sdk'
import { DocumentClient } from 'aws-sdk/clients/dynamodb'
import { log } from '@uniswap/smart-order-router'
import { PairMarshaller } from '../../../marshalling'
import { BatchGetItemInput, DocumentClient } from 'aws-sdk/clients/dynamodb'
import { log, metric, MetricLoggerUnit } from '@uniswap/smart-order-router'
import { MarshalledPair, PairMarshaller } from '../../../marshalling'

export class V2DynamoCache implements ICache<{ pair: Pair; block?: number }> {
private readonly ddbClient: DocumentClient
Expand All @@ -18,6 +18,57 @@ export class V2DynamoCache implements ICache<{ pair: Pair; block?: number }> {
},
})
}

// TODO: ROUTE-81 & ROUTE-84 - once smart-order-router updates the ICache.batchGet API to take in
// composite key as part of ROUTE-83, then we can leverage the batchGet Dynamo call
// for both caching-pool-provider and token-properties-provider
// Prior to completion of ROUTE-81 & ROUTE-84, this function is not being called anywhere.
async batchGet(keys: Set<string>): Promise<Record<string, { pair: Pair; block?: number | undefined } | undefined>> {
const records: Record<string, { pair: Pair; block?: number | undefined } | undefined> = {}
const batchGetParams: BatchGetItemInput = {
RequestItems: {
[this.tableName]: {
Keys: Array.from(keys).map((key) => {
// TODO: ROUTE-83 fix the ICache.batchGet to allow passing in composite key type
// instead of a simple string type
// then fix the key destructuring here
const [cacheKey, block] = key.split(':', 2)
return {
cacheKey: { S: cacheKey },
block: { N: block },
}
}),
},
},
}

const result = await this.ddbClient.batchGet(batchGetParams).promise()
const unprocessedKeys = result?.UnprocessedKeys?.[this.tableName]?.Keys

if (unprocessedKeys && unprocessedKeys.length > 0) {
metric.putMetric('V2_PAIRS_DYNAMO_CACHING_UNPROCESSED_KEYS', unprocessedKeys.length, MetricLoggerUnit.None)
}

return (
result.Responses?.[this.tableName]
?.map((item) => {
const key = item.cacheKey.S!
const block = parseInt(item.block.N!)
const itemBinary = item.item.B!
const pairBuffer = Buffer.from(itemBinary)
const pairJson: MarshalledPair = JSON.parse(pairBuffer.toString())

return {
[key]: {
pair: PairMarshaller.unmarshal(pairJson),
block,
},
}
})
?.reduce((accumulatedRecords, currentRecord) => ({ ...accumulatedRecords, ...currentRecord }), records) ??
records
)
}
async get(key: string): Promise<{ pair: Pair; block?: number } | undefined> {
try {
const queryParams = {
Expand Down
50 changes: 25 additions & 25 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"@uniswap/permit2-sdk": "^1.2.0",
"@uniswap/router-sdk": "^1.6.0",
"@uniswap/sdk-core": "^4.0.3",
"@uniswap/smart-order-router": "3.16.21",
"@uniswap/smart-order-router": "3.16.24",
"@uniswap/token-lists": "^1.0.0-beta.33",
"@uniswap/universal-router-sdk": "^1.5.7",
"@uniswap/v2-sdk": "^3.2.0",
Expand Down