Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt committed Jan 10, 2025
1 parent 99a5600 commit 9e24f39
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 93 deletions.
64 changes: 3 additions & 61 deletions packages/sushi/src/router/liquidity-providers/AlgebraV1Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,73 +116,15 @@ export abstract class AlgebraV1BaseProvider extends UniswapV3BaseProvider {
return undefined
})

let poolsTickSpacing:
| (
| number
| {
error?: undefined
result: number
status: 'success'
}
| {
error: Error
result?: undefined
status: 'failure'
}
)[]
| undefined

try {
const tickSpacingsData = {
multicallAddress: this.client.chain?.contracts?.multicall3?.address!,
allowFailure: true,
blockNumber: options?.blockNumber,
contracts: staticPools.map(
(pool) =>
({
address: pool.address,
chainId: this.chainId,
abi: [
{
inputs: [],
name: 'tickSpacing',
outputs: [{ internalType: 'int24', name: '', type: 'int24' }],
stateMutability: 'view',
type: 'function',
},
] as const,
functionName: 'tickSpacing',
}) as const,
),
}
poolsTickSpacing = options?.memoize
? await (multicallMemoize(tickSpacingsData) as Promise<any>).catch(
(e) => {
console.warn(
`${this.getLogPrefix()} - INIT: multicall failed, message: ${
e.message
}`,
)
return undefined
},
)
: await this.client.multicall(tickSpacingsData).catch((e) => {
console.warn(
`${this.getLogPrefix()} - INIT: multicall failed, message: ${
e.message
}`,
)
return undefined
})
} catch (_error) {}
const tickSpacings = await this.getTickSpacing(staticPools, options)

const existingPools: V3Pool[] = []

staticPools.forEach((pool, i) => {
if (globalState === undefined || !globalState[i]) return
let tickSpacing = this.DEFAULT_TICK_SPACING
if (poolsTickSpacing?.[i] !== undefined) {
const ts = poolsTickSpacing[i]
if (tickSpacings?.[i] !== undefined) {
const ts = tickSpacings[i]
if (typeof ts === 'number') {
tickSpacing = ts
} else {
Expand Down
101 changes: 69 additions & 32 deletions packages/sushi/src/router/liquidity-providers/UniswapV3Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,38 +181,7 @@ export abstract class UniswapV3BaseProvider extends LiquidityProvider {
return undefined
})

const tickSpacingData = {
multicallAddress: this.client.chain?.contracts?.multicall3
?.address as Address,
allowFailure: true,
blockNumber: options?.blockNumber,
contracts: staticPools.map(
(pool) =>
({
address: pool.address as Address,
chainId: this.chainId,
abi: tickSpacingAbi,
functionName: 'tickSpacing',
}) as const,
),
}
const tickSpacings = options?.memoize
? await (multicallMemoize(tickSpacingData) as Promise<any>).catch((e) => {
console.warn(
`${this.getLogPrefix()} - INIT: multicall failed, message: ${
e.message
}`,
)
return undefined
})
: await this.client.multicall(tickSpacingData).catch((e) => {
console.warn(
`${this.getLogPrefix()} - INIT: multicall failed, message: ${
e.message
}`,
)
return undefined
})
const tickSpacings = await this.getTickSpacing(staticPools, options)

const existingPools: V3Pool[] = []

Expand Down Expand Up @@ -264,6 +233,9 @@ export abstract class UniswapV3BaseProvider extends LiquidityProvider {
}

handleTickBoundries(tick: number, pool: V3Pool): CLTick[] {
// calculate ticks min/max range
// reason for this is to be able to calculate the ticks range
// independently instead of passing around the min/max range as args
const currentTickIndex = bitmapIndex(tick, pool.tickSpacing)
if (!pool.ticks.has(currentTickIndex)) return []
let minIndex
Expand Down Expand Up @@ -486,6 +458,15 @@ export abstract class UniswapV3BaseProvider extends LiquidityProvider {
const index = wordList[i]!.index
ticks[index] = (ticks[index] || []).concat(t.result || [])
})
existingPools.forEach((pool, i) => {
pool.ticks.set(
i,
ticks[i]!.map((tick) => ({
index: tick.tick,
DLiquidity: tick.liquidityNet,
})).sort((a, b) => a.index - b.index),
)
})

const transformedV3Pools: PoolCode[] = []
existingPools.forEach((pool, i) => {
Expand Down Expand Up @@ -654,4 +635,60 @@ export abstract class UniswapV3BaseProvider extends LiquidityProvider {
this.TICK_SPACINGS[feeList[i] as SushiSwapV3FeeAmount] === v || v === 0,
)
}

// fetches pool tickSpacing, this will be used
// instead of hardcoded TICK_SPACINGS values
async getTickSpacing(
staticPools: StaticPoolUniV3[],
options?: DataFetcherOptions,
): Promise<
| (
| number
| {
error?: undefined
result: number
status: 'success'
}
| {
error: Error
result?: undefined
status: 'failure'
}
)[]
| undefined
> {
const multicallMemoize = await memoizer.fn(this.client.multicall)
const tickSpacingData = {
multicallAddress: this.client.chain?.contracts?.multicall3
?.address as Address,
allowFailure: true,
blockNumber: options?.blockNumber,
contracts: staticPools.map(
(pool) =>
({
address: pool.address as Address,
chainId: this.chainId,
abi: tickSpacingAbi,
functionName: 'tickSpacing',
}) as const,
),
}
return options?.memoize
? await (multicallMemoize(tickSpacingData) as Promise<any>).catch((e) => {
console.warn(
`${this.getLogPrefix()} - INIT: multicall failed, message: ${
e.message
}`,
)
return undefined
})
: await this.client.multicall(tickSpacingData).catch((e) => {
console.warn(
`${this.getLogPrefix()} - INIT: multicall failed, message: ${
e.message
}`,
)
return undefined
})
}
}

0 comments on commit 9e24f39

Please sign in to comment.