Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt committed May 10, 2024
1 parent bf361a5 commit f59f66c
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 99 deletions.
13 changes: 10 additions & 3 deletions packages/sushi/src/router/data-fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { http, PublicClient, createPublicClient } from 'viem'
import {
http,
MulticallParameters,
PublicClient,
createPublicClient,
} from 'viem'
import { ChainId, TestnetChainId } from '../chain/index.js'
import { publicClientConfig } from '../config/index.js'
import { Type } from '../currency/index.js'
Expand Down Expand Up @@ -60,8 +65,10 @@ export type DataFetcherOptions = {
* price, etc of a certain block
*/
blockNumber?: bigint
/** Determines if memoizer should be used or not */
memoize?: boolean
/** The multicall memoizer function, must be initiated from a viem client multicall method */
multicallMemoizer?: (
args: MulticallParameters<readonly unknown[], boolean>,
) => Promise<Promise<unknown[]>>
/** Determines a timeout (in ms) for fetching pools for a token pair */
fetchPoolsTimeout?: number
}
Expand Down
1 change: 1 addition & 0 deletions packages/sushi/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from './tines-to-route-processor-2.js'
export * from './tines-to-route-processor-4.js'
export * from './PoolBinarySerialization.js'
export * from './Sankey.AnyChart.js'
export * from './memoizer.js'
18 changes: 6 additions & 12 deletions packages/sushi/src/router/liquidity-providers/CurveProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { Native, Token, Type } from '../../currency/index.js'
import { RToken, createCurvePoolsForMultipool } from '../../tines/index.js'
import { DataFetcherOptions } from '../data-fetcher.js'
import { getCurrencyCombinations } from '../get-currency-combinations.js'
import { memoizer } from '../memoizer.js'
import { CurvePoolCode } from '../pool-codes/CurvePool.js'
import { PoolCode } from '../pool-codes/PoolCode.js'
import { LiquidityProvider, LiquidityProviders } from './LiquidityProvider.js'
Expand Down Expand Up @@ -386,8 +385,6 @@ export class CurveProvider extends LiquidityProvider {
const pools: Map<Address, [CurvePoolType, Type[]]> = new Map()
let currencyCombinations = getCurrencyCombinations(this.chainId, t0, t1)

const multicallMemoize = await memoizer.fn(this.client.multicall)

for (let i = 0; currencyCombinations.length > 0; ++i) {
const calls = (CURVE_FACTORY_ADDRESSES[this.chainId] ?? []).flatMap(
(factory) =>
Expand Down Expand Up @@ -421,8 +418,8 @@ export class CurveProvider extends LiquidityProvider {
result?: undefined
status: 'failure'
}
)[] = options?.memoize
? ((await multicallMemoize(newfoundPoolsData)) as any)
)[] = options?.multicallMemoizer
? ((await options.multicallMemoizer(newfoundPoolsData)) as any)
: await this.client.multicall(newfoundPoolsData)

newFoundPools.forEach((pool, i) => {
Expand Down Expand Up @@ -454,8 +451,6 @@ export class CurveProvider extends LiquidityProvider {
options?: DataFetcherOptions,
): Promise<(number[] | undefined)[]> {
if (this.chainId === ChainId.ETHEREUM) {
const multicallMemoize = await memoizer.fn(this.client.multicall)

const ratiosData = {
multicallAddress: this.client.chain?.contracts?.multicall3
?.address as '0x${string}',
Expand Down Expand Up @@ -494,8 +489,8 @@ export class CurveProvider extends LiquidityProvider {
},
],
} as any
const ratios = options?.memoize
? ((await multicallMemoize(ratiosData)) as any)
const ratios = options?.multicallMemoizer
? ((await options.multicallMemoizer(ratiosData)) as any)
: await this.client.multicall(ratiosData)

return pools.map(([poolAddress]) => {
Expand Down Expand Up @@ -543,7 +538,6 @@ export class CurveProvider extends LiquidityProvider {
T
>['args'],
) => {
const multicallMemoize = await memoizer.fn(this.client.multicall)
const data = {
multicallAddress: this.client.chain?.contracts?.multicall3
?.address as '0x${string}',
Expand All @@ -557,8 +551,8 @@ export class CurveProvider extends LiquidityProvider {
args,
})) as any,
} as any
return options?.memoize
? (multicallMemoize(data) as any)
return options?.multicallMemoizer
? (options.multicallMemoizer(data) as any)
: this.client.multicall(data)
}
// const poolContract = getContract({
Expand Down
87 changes: 44 additions & 43 deletions packages/sushi/src/router/liquidity-providers/Trident.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
filterTopPools,
mapToken,
} from '../lib/api.js'
import { memoizer } from '../memoizer.js'
import {
BentoBridgePoolCode,
BentoPoolCode,
Expand Down Expand Up @@ -727,8 +726,6 @@ export class TridentProvider extends LiquidityProvider {
}
})

const multicallMemoize = await memoizer.fn(this.client.multicall)

const classicReservesPromiseData = {
multicallAddress: this.client.chain?.contracts?.multicall3
?.address as Address,
Expand All @@ -744,17 +741,17 @@ export class TridentProvider extends LiquidityProvider {
}) as const,
),
}
const classicReservePromise = options?.memoize
? (multicallMemoize(classicReservesPromiseData) as Promise<any>).catch(
(e) => {
console.warn(
`${this.getLogPrefix()} - UPDATE: multicall failed, message: ${
e.message
}`,
)
return undefined
},
)
const classicReservePromise = options?.multicallMemoizer
? (
options.multicallMemoizer(classicReservesPromiseData) as Promise<any>
).catch((e) => {
console.warn(
`${this.getLogPrefix()} - UPDATE: multicall failed, message: ${
e.message
}`,
)
return undefined
})
: this.client.multicall(classicReservesPromiseData).catch((e) => {
console.warn(
`${this.getLogPrefix()} - UPDATE: multicall failed, message: ${
Expand All @@ -779,17 +776,17 @@ export class TridentProvider extends LiquidityProvider {
}) as const,
),
}
const stableReservePromise = options?.memoize
? (multicallMemoize(stableReservePromiseData) as Promise<any>).catch(
(e) => {
console.warn(
`${this.getLogPrefix()} - UPDATE: multicall failed, message: ${
e.message
}`,
)
return undefined
},
)
const stableReservePromise = options?.multicallMemoizer
? (
options.multicallMemoizer(stableReservePromiseData) as Promise<any>
).catch((e) => {
console.warn(
`${this.getLogPrefix()} - UPDATE: multicall failed, message: ${
e.message
}`,
)
return undefined
})
: this.client.multicall(stableReservePromiseData).catch((e) => {
console.warn(
`${this.getLogPrefix()} - UPDATE: multicall failed, message: ${
Expand All @@ -815,15 +812,17 @@ export class TridentProvider extends LiquidityProvider {
}) as const,
),
}
const totalsPromise = options?.memoize
? (multicallMemoize(totalsPromiseData) as Promise<any>).catch((e) => {
console.warn(
`${this.getLogPrefix()} - UPDATE: multicall failed, message: ${
e.message
}`,
)
return undefined
})
const totalsPromise = options?.multicallMemoizer
? (options.multicallMemoizer(totalsPromiseData) as Promise<any>).catch(
(e) => {
console.warn(
`${this.getLogPrefix()} - UPDATE: multicall failed, message: ${
e.message
}`,
)
return undefined
},
)
: this.client.multicall(totalsPromiseData).catch((e) => {
console.warn(
`${this.getLogPrefix()} - UPDATE: multicall failed, message: ${
Expand All @@ -849,15 +848,17 @@ export class TridentProvider extends LiquidityProvider {
}) as const,
),
}
const balancesPromise = options?.memoize
? (multicallMemoize(balancesPromiseData) as Promise<any>).catch((e) => {
console.warn(
`${this.getLogPrefix()} - UPDATE: multicall failed, message: ${
e.message
}`,
)
return undefined
})
const balancesPromise = options?.multicallMemoizer
? (options.multicallMemoizer(balancesPromiseData) as Promise<any>).catch(
(e) => {
console.warn(
`${this.getLogPrefix()} - UPDATE: multicall failed, message: ${
e.message
}`,
)
return undefined
},
)
: this.client.multicall(balancesPromiseData).catch((e) => {
console.warn(
`${this.getLogPrefix()} - UPDATE: multicall failed, message: ${
Expand Down
23 changes: 11 additions & 12 deletions packages/sushi/src/router/liquidity-providers/UniswapV2Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
filterTopPools,
mapToken,
} from '../lib/api.js'
import { memoizer } from '../memoizer.js'
import { ConstantProductPoolCode, type PoolCode } from '../pool-codes/index.js'
import { LiquidityProvider } from './LiquidityProvider.js'

Expand Down Expand Up @@ -223,8 +222,6 @@ export abstract class UniswapV2BaseProvider extends LiquidityProvider {
}
})

const multicallMemoize = await memoizer.fn(this.client.multicall)

const multicallData = {
multicallAddress: this.client.chain?.contracts?.multicall3
?.address as Address,
Expand All @@ -240,15 +237,17 @@ export abstract class UniswapV2BaseProvider extends LiquidityProvider {
}) as const,
),
}
const reserves = options?.memoize
? await (multicallMemoize(multicallData) as Promise<any>).catch((e) => {
console.warn(
`${this.getLogPrefix()} - UPDATE: on-demand pools multicall failed, message: ${
e.message
}`,
)
return undefined
})
const reserves = options?.multicallMemoizer
? await (options.multicallMemoizer(multicallData) as Promise<any>).catch(
(e) => {
console.warn(
`${this.getLogPrefix()} - UPDATE: on-demand pools multicall failed, message: ${
e.message
}`,
)
return undefined
},
)
: await this.client.multicall(multicallData).catch((e) => {
console.warn(
`${this.getLogPrefix()} - UPDATE: on-demand pools multicall failed, message: ${
Expand Down
39 changes: 19 additions & 20 deletions packages/sushi/src/router/liquidity-providers/UniswapV3Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { computeSushiSwapV3PoolAddress } from '../../pool/index.js'
import { RToken, UniV3Pool } from '../../tines/index.js'
import { DataFetcherOptions } from '../data-fetcher.js'
import { getCurrencyCombinations } from '../get-currency-combinations.js'
import { memoizer } from '../memoizer.js'
import { type PoolCode, UniV3PoolCode } from '../pool-codes/index.js'
import { LiquidityProvider } from './LiquidityProvider.js'

Expand Down Expand Up @@ -86,8 +85,6 @@ export abstract class UniswapV3BaseProvider extends LiquidityProvider {
if (excludePools)
staticPools = staticPools.filter((p) => !excludePools.has(p.address))

const multicallMemoize = await memoizer.fn(this.client.multicall)

const slot0Data = {
multicallAddress: this.client.chain?.contracts?.multicall3
?.address as Address,
Expand Down Expand Up @@ -139,15 +136,17 @@ export abstract class UniswapV3BaseProvider extends LiquidityProvider {
}) as const,
),
}
const slot0 = options?.memoize
? await (multicallMemoize(slot0Data) as Promise<any>).catch((e) => {
console.warn(
`${this.getLogPrefix()} - INIT: multicall failed, message: ${
e.message
}`,
)
return undefined
})
const slot0 = options?.multicallMemoizer
? await (options.multicallMemoizer(slot0Data) as Promise<any>).catch(
(e) => {
console.warn(
`${this.getLogPrefix()} - INIT: multicall failed, message: ${
e.message
}`,
)
return undefined
},
)
: await this.client.multicall(slot0Data).catch((e) => {
console.warn(
`${this.getLogPrefix()} - INIT: multicall failed, message: ${
Expand Down Expand Up @@ -214,8 +213,8 @@ export abstract class UniswapV3BaseProvider extends LiquidityProvider {
status: 'failure'
}
)[]
> = options?.memoize
? (multicallMemoize(liquidityContractsData) as Promise<any>)
> = options?.multicallMemoizer
? (options.multicallMemoizer(liquidityContractsData) as Promise<any>)
: this.client.multicall(liquidityContractsData)

const token0ContractsData = {
Expand Down Expand Up @@ -247,8 +246,8 @@ export abstract class UniswapV3BaseProvider extends LiquidityProvider {
status: 'success'
}
)[]
> = options?.memoize
? (multicallMemoize(token0ContractsData) as Promise<any>)
> = options?.multicallMemoizer
? (options.multicallMemoizer(token0ContractsData) as Promise<any>)
: this.client.multicall(token0ContractsData)

const token1ContractsData = {
Expand Down Expand Up @@ -280,8 +279,8 @@ export abstract class UniswapV3BaseProvider extends LiquidityProvider {
status: 'failure'
}
)[]
> = options?.memoize
? (multicallMemoize(token1ContractsData) as Promise<any>)
> = options?.multicallMemoizer
? (options.multicallMemoizer(token1ContractsData) as Promise<any>)
: this.client.multicall(token1ContractsData)

const minIndexes = existingPools.map((pool) =>
Expand Down Expand Up @@ -341,8 +340,8 @@ export abstract class UniswapV3BaseProvider extends LiquidityProvider {
status: 'failure'
}
)[]
> = options?.memoize
? (multicallMemoize(ticksContractsData) as Promise<any>)
> = options?.multicallMemoizer
? (options.multicallMemoizer(ticksContractsData) as Promise<any>)
: this.client.multicall(ticksContractsData)

const [liquidityResults, token0Balances, token1Balances, tickResults] =
Expand Down
Loading

0 comments on commit f59f66c

Please sign in to comment.