Skip to content

Commit

Permalink
Merge pull request #116 from clober-dex/feat/improve-perf
Browse files Browse the repository at this point in the history
feat: improve perf
  • Loading branch information
Dorvin authored Oct 2, 2024
2 parents acbfc7b + 57ca176 commit a0eb7bc
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 43 deletions.
4 changes: 2 additions & 2 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
@@ -1,6 +1,6 @@
{
"name": "@clober/v2-sdk",
"version": "0.0.72",
"version": "0.0.73",
"description": "🛠 An SDK for building applications on top of Clober V2",
"files": [
"dist"
Expand Down
40 changes: 24 additions & 16 deletions src/apis/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { CHAIN_IDS } from '../constants/chain'
import { CONTRACT_ADDRESSES } from '../constants/addresses'
import { StrategyPrice } from '../model/strategy'
import { STRATEGY_ABI } from '../abis/rebalancer/strategy-abi'
import { formatPrice } from '../utils/prices'
import { Market } from '../type'
import { toPoolKey } from '../utils/pool-key'

import { fetchPool } from './pool'

Expand All @@ -14,27 +15,34 @@ export async function fetchStrategyPrice(
tokenAddresses: `0x${string}`[],
salt: `0x${string}`,
useSubgraph: boolean,
market?: Market,
): Promise<StrategyPrice> {
const pool = await fetchPool(
publicClient,
chainId,
tokenAddresses,
salt,
useSubgraph,
)
let poolKey: `0x${string}` | undefined = undefined
if (market) {
poolKey = toPoolKey(
BigInt(market.bidBook.id),
BigInt(market.askBook.id),
salt,
)
} else {
const pool = await fetchPool(
publicClient,
chainId,
tokenAddresses,
salt,
useSubgraph,
)
poolKey = pool.key
}
const getPriceResult = await publicClient.readContract({
address: CONTRACT_ADDRESSES[chainId]!.Strategy,
abi: STRATEGY_ABI,
functionName: 'getPrice',
args: [pool.key],
args: [poolKey],
})
return {
oraclePrice: formatPrice(
BigInt(getPriceResult.oraclePrice),
pool.currencyA.decimals,
pool.currencyB.decimals,
),
tickA: BigInt(getPriceResult.tickA),
tickB: BigInt(getPriceResult.tickB),
oraclePrice: BigInt(getPriceResult.oraclePrice),
bidTick: BigInt(getPriceResult.tickA),
askTick: BigInt(getPriceResult.tickB),
}
}
53 changes: 33 additions & 20 deletions src/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
CurrencyFlow,
DefaultWriteContractOptions,
ERC20PermitParam,
Pool,
Transaction,
} from './type'
import { calculateUnitSize } from './utils/unit-size'
Expand Down Expand Up @@ -1486,19 +1487,24 @@ export const refillOrder = async ({
salt: `0x${string}`
options?: DefaultWriteContractOptions & {
useSubgraph?: boolean
pool?: Pool
}
}): Promise<Transaction> => {
const publicClient = createPublicClient({
chain: CHAIN_MAP[chainId],
transport: options?.rpcUrl ? http(options.rpcUrl) : http(),
})
const pool = await fetchPool(
publicClient,
chainId,
[token0, token1],
salt,
!!(options && options.useSubgraph),
)
const pool = options?.pool
? options.pool
: (
await fetchPool(
publicClient,
chainId,
[token0, token1],
salt,
!!(options && options.useSubgraph),
)
).toJson()
if (!pool.isOpened) {
throw new Error(`
Open the pool before rebalancing pool.
Expand All @@ -1524,6 +1530,7 @@ export const refillOrder = async ({
args: [pool.key],
},
options?.gasLimit,
options?.gasPriceLimit,
)
}

Expand All @@ -1549,11 +1556,12 @@ export const adjustOrderPrice = async ({
askPrice: string // price with bookA. ask price
alpha: string // alpha value, 0 < alpha <= 1
options?: {
tickA?: bigint
tickB?: bigint
bidTick?: bigint
askTick?: bigint
roundingUpBidPrice?: boolean
roundingUpAskPrice?: boolean
useSubgraph?: boolean
pool?: Pool
} & DefaultWriteContractOptions
}): Promise<Transaction> => {
if (Number(alpha) <= 0 || Number(alpha) > 1) {
Expand All @@ -1569,13 +1577,17 @@ export const adjustOrderPrice = async ({
chain: CHAIN_MAP[chainId],
transport: options?.rpcUrl ? http(options.rpcUrl) : http(),
})
const pool = await fetchPool(
publicClient,
chainId,
[token0, token1],
salt,
!!(options && options.useSubgraph),
)
const pool = options?.pool
? options.pool
: (
await fetchPool(
publicClient,
chainId,
[token0, token1],
salt,
!!(options && options.useSubgraph),
)
).toJson()
if (!pool.isOpened) {
throw new Error(`
Open the pool before updating strategy price.
Expand Down Expand Up @@ -1615,11 +1627,11 @@ export const adjustOrderPrice = async ({
pool.currencyA.decimals,
pool.currencyB.decimals,
)
const tickA = options?.tickA
? Number(options.tickA)
const tickA = options?.bidTick
? Number(options.bidTick)
: Number(roundingUpBidPrice ? roundingUpTickA : roundingDownTickA)
const tickB = options?.tickB
? Number(options.tickB)
const tickB = options?.askTick
? Number(options.askTick)
: Number(
invertTick(roundingUpAskPrice ? roundingUpTickB : roundingDownTickB),
)
Expand All @@ -1637,6 +1649,7 @@ export const adjustOrderPrice = async ({
args: [pool.key, oracleRawPrice, tickA, tickB, alphaRaw],
},
options?.gasLimit,
options?.gasPriceLimit,
)
}

Expand Down
6 changes: 3 additions & 3 deletions src/model/strategy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type StrategyPrice = {
oraclePrice: string
tickA: bigint
tickB: bigint
oraclePrice: bigint
bidTick: bigint
askTick: bigint
}
1 change: 1 addition & 0 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export type DefaultReadContractOptions = DefaultOptions

export type DefaultWriteContractOptions = DefaultOptions & {
gasLimit?: bigint
gasPriceLimit?: bigint
}

export type CurrencyFlow = {
Expand Down
3 changes: 2 additions & 1 deletion src/utils/build-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const buildTransaction = async (
publicClient: PublicClient,
args: WriteContractParameters | SimulateContractParameters,
gasLimit?: bigint,
gasPriceLimit?: bigint,
): Promise<Transaction> => {
const data = encodeFunctionData(args)
const [gas, gasPrice] = await Promise.all([
Expand All @@ -21,7 +22,7 @@ export const buildTransaction = async (
to: args.address,
value: args.value || 0n,
}),
publicClient.getGasPrice(),
gasPriceLimit ?? publicClient.getGasPrice(),
])
return {
gas,
Expand Down
2 changes: 2 additions & 0 deletions src/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ export const getStrategyPrice = async ({
token1: `0x${string}`
salt: `0x${string}`
options?: DefaultReadContractOptions & {
market?: Market
useSubgraph?: boolean
}
}): Promise<StrategyPrice> => {
Expand All @@ -214,6 +215,7 @@ export const getStrategyPrice = async ({
[token0, token1],
salt,
!!(options && options.useSubgraph),
options?.market,
)
}

Expand Down

0 comments on commit a0eb7bc

Please sign in to comment.