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

chore: bump sor to 3.53.0 - feat: cached routes to support native currencies #835

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
14 changes: 7 additions & 7 deletions lib/handlers/marshalling/cached-routes-marshaller.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { CachedRoutes } from '@uniswap/smart-order-router'
import { ChainId, TradeType } from '@uniswap/sdk-core'
import { Protocol } from '@uniswap/router-sdk'
import { MarshalledToken, TokenMarshaller } from './token-marshaller'
import { MarshalledCurrency, TokenMarshaller } from './token-marshaller'
import { CachedRouteMarshaller, MarshalledCachedRoute } from './cached-route-marshaller'

export interface MarshalledCachedRoutes {
routes: MarshalledCachedRoute[]
chainId: ChainId
tokenIn: MarshalledToken
tokenOut: MarshalledToken
tokenIn: MarshalledCurrency
tokenOut: MarshalledCurrency
protocolsCovered: Protocol[]
blockNumber: number
tradeType: TradeType
Expand All @@ -21,8 +21,8 @@ export class CachedRoutesMarshaller {
return {
routes: cachedRoutes.routes.map((route) => CachedRouteMarshaller.marshal(route)),
chainId: cachedRoutes.chainId,
tokenIn: TokenMarshaller.marshal(cachedRoutes.tokenIn),
tokenOut: TokenMarshaller.marshal(cachedRoutes.tokenOut),
tokenIn: TokenMarshaller.marshal(cachedRoutes.currencyIn),
tokenOut: TokenMarshaller.marshal(cachedRoutes.currencyOut),
protocolsCovered: cachedRoutes.protocolsCovered,
blockNumber: cachedRoutes.blockNumber,
tradeType: cachedRoutes.tradeType,
Expand All @@ -35,8 +35,8 @@ export class CachedRoutesMarshaller {
return new CachedRoutes({
routes: marshalledCachedRoutes.routes.map((route) => CachedRouteMarshaller.unmarshal(route)),
chainId: marshalledCachedRoutes.chainId,
tokenIn: TokenMarshaller.unmarshal(marshalledCachedRoutes.tokenIn),
tokenOut: TokenMarshaller.unmarshal(marshalledCachedRoutes.tokenOut),
currencyIn: TokenMarshaller.unmarshal(marshalledCachedRoutes.tokenIn),
currencyOut: TokenMarshaller.unmarshal(marshalledCachedRoutes.tokenOut),
protocolsCovered: marshalledCachedRoutes.protocolsCovered,
blockNumber: marshalledCachedRoutes.blockNumber,
tradeType: marshalledCachedRoutes.tradeType,
Expand Down
4 changes: 2 additions & 2 deletions lib/handlers/marshalling/currency-amount-marshaller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { CurrencyAmount, Token } from '@uniswap/sdk-core'
import { MarshalledToken, TokenMarshaller } from './token-marshaller'
import { MarshalledCurrency, TokenMarshaller } from './token-marshaller'

export interface MarshalledCurrencyAmount {
currency: MarshalledToken
currency: MarshalledCurrency
numerator: string
denominator: string
}
Expand Down
18 changes: 9 additions & 9 deletions lib/handlers/marshalling/route-marshaller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MixedRoute, V2Route, V3Route, V4Route } from '@uniswap/smart-order-router/build/main/routers'
import { Protocol } from '@uniswap/router-sdk'
import { MarshalledToken, TokenMarshaller } from './token-marshaller'
import { MarshalledCurrency, TokenMarshaller } from './token-marshaller'
import { MarshalledPair, PairMarshaller } from './pair-marshaller'
import { MarshalledPool as V3MarshalledPool, PoolMarshaller as V3PoolMarshaller } from './v3/pool-marshaller'
import { MarshalledPool as V4MarshalledPool, PoolMarshaller as V4PoolMarshaller } from './v4/pool-marshaller'
Expand All @@ -11,29 +11,29 @@ import { Pair } from '@uniswap/v2-sdk'

export interface MarshalledV2Route {
protocol: Protocol
input: MarshalledToken
output: MarshalledToken
input: MarshalledCurrency
output: MarshalledCurrency
pairs: MarshalledPair[]
}

export interface MarshalledV3Route {
protocol: Protocol
input: MarshalledToken
output: MarshalledToken
input: MarshalledCurrency
output: MarshalledCurrency
pools: V3MarshalledPool[]
}

export interface MarshalledV4Route {
protocol: Protocol
input: MarshalledToken
output: MarshalledToken
input: MarshalledCurrency
output: MarshalledCurrency
pools: V4MarshalledPool[]
}

export interface MarshalledMixedRoute {
protocol: Protocol
input: MarshalledToken
output: MarshalledToken
input: MarshalledCurrency
output: MarshalledCurrency
pools: (V4MarshalledPool | V3MarshalledPool | MarshalledPair)[]
}

Expand Down
37 changes: 19 additions & 18 deletions lib/handlers/marshalling/token-marshaller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Token } from '@uniswap/sdk-core'
import { Currency, Token } from '@uniswap/sdk-core'
import { BigNumber } from 'ethers'
import { getAddress } from '@uniswap/smart-order-router'

export interface MarshalledToken {
export interface MarshalledCurrency {
chainId: number
address: string
decimals: number
Expand All @@ -12,28 +13,28 @@ export interface MarshalledToken {
}

export class TokenMarshaller {
public static marshal(token: Token): MarshalledToken {
public static marshal(currency: Currency): MarshalledCurrency {
return {
chainId: token.chainId,
address: token.address,
decimals: token.decimals,
symbol: token.symbol,
name: token.name,
buyFeeBps: token.buyFeeBps?.toString(),
sellFeeBps: token.sellFeeBps?.toString(),
chainId: currency.chainId,
address: getAddress(currency),
decimals: currency.decimals,
symbol: currency.symbol,
name: currency.name,
buyFeeBps: currency.isToken ? currency.buyFeeBps?.toString() : undefined,
sellFeeBps: currency.isToken ? currency.sellFeeBps?.toString() : undefined,
}
}

public static unmarshal(marshalledToken: MarshalledToken): Token {
public static unmarshal(marshalledCurrency: MarshalledCurrency): Token {
return new Token(
marshalledToken.chainId,
marshalledToken.address,
marshalledToken.decimals,
marshalledToken.symbol,
marshalledToken.name,
marshalledCurrency.chainId,
marshalledCurrency.address,
marshalledCurrency.decimals,
marshalledCurrency.symbol,
marshalledCurrency.name,
true, // at this point we know it's valid token address
marshalledToken.buyFeeBps ? BigNumber.from(marshalledToken.buyFeeBps) : undefined,
marshalledToken.sellFeeBps ? BigNumber.from(marshalledToken.sellFeeBps) : undefined
marshalledCurrency.buyFeeBps ? BigNumber.from(marshalledCurrency.buyFeeBps) : undefined,
marshalledCurrency.sellFeeBps ? BigNumber.from(marshalledCurrency.sellFeeBps) : undefined
)
}
}
6 changes: 3 additions & 3 deletions lib/handlers/marshalling/v3/pool-marshaller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Pool } from '@uniswap/v3-sdk'
import { FeeAmount } from '@uniswap/v3-sdk/dist/constants'
import { MarshalledToken, TokenMarshaller } from '../token-marshaller'
import { MarshalledCurrency, TokenMarshaller } from '../token-marshaller'
import { Protocol } from '@uniswap/router-sdk'

export interface MarshalledPool {
protocol: Protocol
token0: MarshalledToken
token1: MarshalledToken
token0: MarshalledCurrency
token1: MarshalledCurrency
fee: FeeAmount
sqrtRatioX96: string
liquidity: string
Expand Down
6 changes: 3 additions & 3 deletions lib/handlers/marshalling/v4/pool-marshaller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Pool } from '@uniswap/v4-sdk'
import { FeeAmount } from '@uniswap/v3-sdk/dist/constants'
import { MarshalledToken, TokenMarshaller } from '../token-marshaller'
import { MarshalledCurrency, TokenMarshaller } from '../token-marshaller'
import { Protocol } from '@uniswap/router-sdk'

export interface MarshalledPool {
protocol: Protocol
token0: MarshalledToken
token1: MarshalledToken
token0: MarshalledCurrency
token1: MarshalledCurrency
fee: FeeAmount
tickSpacing: number
hooks: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ export class DynamoRouteCachingProvider extends IRouteCachingProvider {
const { tokenIn, tokenOut } = this.determineTokenInOut(amount, quoteToken, tradeType)

const partitionKey = new PairTradeTypeChainId({
tokenIn: tokenIn.address,
tokenOut: tokenOut.address,
currencyIn: tokenIn.address,
currencyOut: tokenOut.address,
tradeType,
chainId,
})
Expand Down Expand Up @@ -239,8 +239,8 @@ export class DynamoRouteCachingProvider extends IRouteCachingProvider {
const cachedRoutes = new CachedRoutes({
routes: Array.from(routesMap.values()),
chainId: first.chainId,
tokenIn: first.tokenIn,
tokenOut: first.tokenOut,
currencyIn: first.currencyIn,
currencyOut: first.currencyOut,
protocolsCovered: first.protocolsCovered,
blockNumber,
tradeType: first.tradeType,
Expand Down Expand Up @@ -331,9 +331,9 @@ export class DynamoRouteCachingProvider extends IRouteCachingProvider {
): void {
const payload = {
queryStringParameters: {
tokenInAddress: partitionKey.tokenIn,
tokenInAddress: partitionKey.currencyIn,
tokenInChainId: partitionKey.chainId.toString(),
tokenOutAddress: partitionKey.tokenOut,
tokenOutAddress: partitionKey.currencyOut,
tokenOutChainId: partitionKey.chainId.toString(),
amount: amount.quotient.toString(),
type: partitionKey.tradeType === 0 ? 'exactIn' : 'exactOut',
Expand Down Expand Up @@ -384,8 +384,8 @@ export class DynamoRouteCachingProvider extends IRouteCachingProvider {
const individualCachedRoutes = new CachedRoutes({
routes: [route],
chainId: cachedRoutes.chainId,
tokenIn: cachedRoutes.tokenIn,
tokenOut: cachedRoutes.tokenOut,
currencyIn: cachedRoutes.currencyIn,
currencyOut: cachedRoutes.currencyOut,
protocolsCovered: cachedRoutes.protocolsCovered,
blockNumber: cachedRoutes.blockNumber,
tradeType: cachedRoutes.tradeType,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ChainId, TradeType } from '@uniswap/sdk-core'
import { CachedRoutes } from '@uniswap/smart-order-router'
import { CachedRoutes, getAddress } from '@uniswap/smart-order-router'

interface PairTradeTypeChainIdArgs {
tokenIn: string
tokenOut: string
currencyIn: string
currencyOut: string
tradeType: TradeType
chainId: ChainId
}
Expand All @@ -12,26 +12,26 @@ interface PairTradeTypeChainIdArgs {
* Class used to model the partition key of the CachedRoutes cache database and configuration.
*/
export class PairTradeTypeChainId {
public readonly tokenIn: string
public readonly tokenOut: string
public readonly currencyIn: string
public readonly currencyOut: string
public readonly tradeType: TradeType
public readonly chainId: ChainId

constructor({ tokenIn, tokenOut, tradeType, chainId }: PairTradeTypeChainIdArgs) {
this.tokenIn = tokenIn.toLowerCase() // All token addresses should be lower case for normalization.
this.tokenOut = tokenOut.toLowerCase() // All token addresses should be lower case for normalization.
constructor({ currencyIn, currencyOut, tradeType, chainId }: PairTradeTypeChainIdArgs) {
this.currencyIn = currencyIn.toLowerCase() // All currency addresses should be lower case for normalization.
this.currencyOut = currencyOut.toLowerCase() // All currency addresses should be lower case for normalization.
this.tradeType = tradeType
this.chainId = chainId
}

public toString(): string {
return `${this.tokenIn}/${this.tokenOut}/${this.tradeType}/${this.chainId}`
return `${this.currencyIn}/${this.currencyOut}/${this.tradeType}/${this.chainId}`
}

public static fromCachedRoutes(cachedRoutes: CachedRoutes): PairTradeTypeChainId {
return new PairTradeTypeChainId({
tokenIn: cachedRoutes.tokenIn.address,
tokenOut: cachedRoutes.tokenOut.address,
currencyIn: getAddress(cachedRoutes.currencyIn),
currencyOut: getAddress(cachedRoutes.currencyOut),
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.52.0",
"@uniswap/smart-order-router": "3.53.0",
"@uniswap/token-lists": "^1.0.0-beta.33",
"@uniswap/universal-router-sdk": "^3.0.2",
"@uniswap/v2-sdk": "^4.3.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ describe('PairTradeTypeChainId', () => {
describe('toString', () => {
it('returns a stringified version of the object', () => {
const pairTradeTypeChainId = new PairTradeTypeChainId({
tokenIn: WETH,
tokenOut: USDC,
currencyIn: WETH,
currencyOut: USDC,
tradeType: TradeType.EXACT_INPUT,
chainId: ChainId.MAINNET,
})
Expand All @@ -22,8 +22,8 @@ describe('PairTradeTypeChainId', () => {

it('token addresses are converted to lowercase', () => {
const pairTradeTypeChainId = new PairTradeTypeChainId({
tokenIn: WETH.toUpperCase(),
tokenOut: USDC.toUpperCase(),
currencyIn: WETH.toUpperCase(),
currencyOut: USDC.toUpperCase(),
tradeType: TradeType.EXACT_INPUT,
chainId: ChainId.MAINNET,
})
Expand All @@ -35,8 +35,8 @@ describe('PairTradeTypeChainId', () => {

it('works with ExactOutput too', () => {
const pairTradeTypeChainId = new PairTradeTypeChainId({
tokenIn: WETH.toUpperCase(),
tokenOut: USDC.toUpperCase(),
currencyIn: WETH.toUpperCase(),
currencyOut: USDC.toUpperCase(),
tradeType: TradeType.EXACT_OUTPUT,
chainId: ChainId.MAINNET,
})
Expand All @@ -48,8 +48,8 @@ describe('PairTradeTypeChainId', () => {

it('works with other chains', () => {
const pairTradeTypeChainId = new PairTradeTypeChainId({
tokenIn: WETH.toUpperCase(),
tokenOut: USDC.toUpperCase(),
currencyIn: WETH.toUpperCase(),
currencyOut: USDC.toUpperCase(),
tradeType: TradeType.EXACT_OUTPUT,
chainId: ChainId.ARBITRUM_ONE,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ const TEST_CACHED_ROUTE = new CachedRoute({ route: TEST_WETH_USDC_V3_ROUTE, perc
const TEST_CACHED_ROUTES = new CachedRoutes({
routes: [TEST_CACHED_ROUTE],
chainId: TEST_CACHED_ROUTE.route.chainId,
tokenIn: WETH,
tokenOut: USDC_MAINNET,
currencyIn: WETH,
currencyOut: USDC_MAINNET,
protocolsCovered: [TEST_CACHED_ROUTE.protocol],
blockNumber: 0,
tradeType: TradeType.EXACT_INPUT,
Expand All @@ -109,8 +109,8 @@ const TEST_UNCACHED_ROUTE = new CachedRoute({ route: TEST_UNI_USDC_ROUTE, percen
const TEST_UNCACHED_ROUTES = new CachedRoutes({
routes: [TEST_UNCACHED_ROUTE],
chainId: TEST_UNCACHED_ROUTE.route.chainId,
tokenIn: UNI_MAINNET,
tokenOut: USDC_MAINNET,
currencyIn: UNI_MAINNET,
currencyOut: USDC_MAINNET,
protocolsCovered: [TEST_UNCACHED_ROUTE.protocol],
blockNumber: 0,
tradeType: TradeType.EXACT_INPUT,
Expand Down
Loading