Skip to content

Commit

Permalink
chore: bump sor to 4.0.0 - feat(breaking): top-level alpha router nat…
Browse files Browse the repository at this point in the history
…ive currencyIn, currencyIn, quoteCurrency support
  • Loading branch information
jsy1218 committed Sep 24, 2024
1 parent dc27299 commit b9949f8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 22 deletions.
3 changes: 3 additions & 0 deletions lib/cron/cache-config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Protocol } from '@uniswap/router-sdk'
import { V2SubgraphProvider, V3SubgraphProvider, V4SubgraphProvider } from '@uniswap/smart-order-router'
import { ChainId } from '@uniswap/sdk-core'
import dotenv from 'dotenv'

dotenv.config()

// during local cdk stack update, the env vars are not populated
// make sure to fill in the env vars below
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import {
CachedRoute,
CachedRoutes,
CacheMode,
getAddress,
ID_TO_NETWORK_NAME,
IRouteCachingProvider,
log,
metric,
MetricLoggerUnit,
routeToString,
SupportedRoutes,
SupportedRoutes
} from '@uniswap/smart-order-router'
import { AWSError, DynamoDB, Lambda } from 'aws-sdk'
import { ChainId, Currency, CurrencyAmount, Fraction, Token, TradeType } from '@uniswap/sdk-core'
Expand Down Expand Up @@ -123,25 +124,30 @@ export class DynamoRouteCachingProvider extends IRouteCachingProvider {
*
* @param chainId
* @param amount
* @param quoteToken
* @param quoteCurrency
* @param tradeType
* @param _protocols
* @protected
*/
protected async _getCachedRoute(
chainId: ChainId,
amount: CurrencyAmount<Currency>,
quoteToken: Token,
quoteCurrency: Currency,
tradeType: TradeType,
protocols: Protocol[],
currentBlockNumber: number,
optimistic: boolean
): Promise<CachedRoutes | undefined> {
const { tokenIn, tokenOut } = this.determineTokenInOut(amount, quoteToken, tradeType)
const { currencyIn, currencyOut } = this.determineCurrencyInOut(amount, quoteCurrency, tradeType)
// This is for backward compatibility with the existing cached routes, that doesn't include V4, i.e.. doesn't support native currency routing
// If we are changing to use native currency address that doesn't have V4, we will have a temporary period of cached routes miss
// because frontend can still send in native currency as tokenIn/tokenOut, and we are not caching the wrapped addresses.
const currencyInAddress = protocols.includes(Protocol.V4) ? getAddress(currencyIn) : currencyIn.wrapped.address
const currencyOutAddress = protocols.includes(Protocol.V4) ? getAddress(currencyOut) : currencyOut.wrapped.address

const partitionKey = new PairTradeTypeChainId({
currencyIn: tokenIn.address,
currencyOut: tokenOut.address,
currencyIn: currencyInAddress,
currencyOut: currencyOutAddress,
tradeType,
chainId,
})
Expand Down Expand Up @@ -478,19 +484,19 @@ export class DynamoRouteCachingProvider extends IRouteCachingProvider {
* Helper function to determine the tokenIn and tokenOut given the tradeType, quoteToken and amount.currency
*
* @param amount
* @param quoteToken
* @param quoteCurrency
* @param tradeType
* @private
*/
private determineTokenInOut(
private determineCurrencyInOut(
amount: CurrencyAmount<Currency>,
quoteToken: Token,
quoteCurrency: Currency,
tradeType: TradeType
): { tokenIn: Token; tokenOut: Token } {
): { currencyIn: Currency; currencyOut: Currency } {
if (tradeType == TradeType.EXACT_INPUT) {
return { tokenIn: amount.currency.wrapped, tokenOut: quoteToken }
return { currencyIn: amount.currency, currencyOut: quoteCurrency }
} else {
return { tokenIn: quoteToken, tokenOut: amount.currency.wrapped }
return { currencyIn: quoteCurrency, currencyOut: amount.currency }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ChainId, TradeType } from '@uniswap/sdk-core'
import { CachedRoutes, getAddress } from '@uniswap/smart-order-router'
import { Protocol } from '@uniswap/router-sdk'

interface PairTradeTypeChainIdArgs {
currencyIn: string
Expand Down Expand Up @@ -29,9 +30,15 @@ export class PairTradeTypeChainId {
}

public static fromCachedRoutes(cachedRoutes: CachedRoutes): PairTradeTypeChainId {
// This is for backward compatibility with the existing cached routes, that doesn't include V4, i.e.. doesn't support native currency routing
// If we are changing to use native currency address that doesn't have V4, we will have a temporary period of cached routes miss
// because frontend can still send in native currency as tokenIn/tokenOut, and we are not caching the wrapped addresses.
const currencyInAddress = cachedRoutes.protocolsCovered.includes(Protocol.V4) ? getAddress(cachedRoutes.currencyIn) : cachedRoutes.currencyIn.wrapped.address
const currencyOutAddress = cachedRoutes.protocolsCovered.includes(Protocol.V4) ? getAddress(cachedRoutes.currencyIn) : cachedRoutes.currencyIn.wrapped.address

return new PairTradeTypeChainId({
currencyIn: getAddress(cachedRoutes.currencyIn),
currencyOut: getAddress(cachedRoutes.currencyOut),
currencyIn: currencyInAddress,
currencyOut: currencyOutAddress,
tradeType: cachedRoutes.tradeType,
chainId: cachedRoutes.chainId,
})
Expand Down
14 changes: 7 additions & 7 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 @@ -87,7 +87,7 @@
"@uniswap/router-sdk": "^1.11.2",
"@uniswap/sdk-core": "^5.4.0",
"@types/semver": "^7.5.8",
"@uniswap/smart-order-router": "3.58.0",
"@uniswap/smart-order-router": "/tmp/uniswap-smart-order-router-3.59.0-native-1.tgz",
"@uniswap/token-lists": "^1.0.0-beta.33",
"@uniswap/universal-router-sdk": "^3.0.2",
"@uniswap/v2-sdk": "^4.3.2",
Expand Down

0 comments on commit b9949f8

Please sign in to comment.