Skip to content

Commit

Permalink
Merge pull request #114 from clober-dex/refactor/pool-dto
Browse files Browse the repository at this point in the history
Refactor/pool dto
  • Loading branch information
graykode authored Sep 30, 2024
2 parents 1e26f51 + 1f58e26 commit a8b5200
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 70 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.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@clober/v2-sdk",
"version": "0.0.70",
"version": "0.0.71",
"description": "🛠 An SDK for building applications on top of Clober V2",
"files": [
"dist"
Expand All @@ -18,7 +18,7 @@
"docs": "typedoc --out docs src/index.ts --sort kind",
"test": "vitest --root test --config vitest.config.ts --max-concurrency=0",
"clean": "rm -rf dist",
"build": "npm run clean && npm install && npm run build:cjs && npm run build:esm && npm run build:types",
"build": "npm run clean && npm install . && npm run build:cjs && npm run build:esm && npm run build:types",
"build:cjs": "tsc --project tsconfig.build.json --module commonjs --outDir ./dist/cjs --removeComments && printf '{\"type\":\"commonjs\"}' > ./dist/cjs/package.json",
"build:esm": "tsc --project tsconfig.build.json --module es2015 --outDir ./dist/esm && printf '{\"type\": \"module\",\"sideEffects\":false}' > ./dist/esm/package.json",
"build:types": "tsc --project tsconfig.build.json --module esnext --outDir ./dist/types --declarationDir ./dist/types --emitDeclarationOnly --declaration --declarationMap"
Expand Down
12 changes: 7 additions & 5 deletions src/model/open-order.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { CurrencyAmount } from '../type'

import type { Currency } from './currency'

export type OpenOrderDto = {
Expand Down Expand Up @@ -38,14 +40,14 @@ export type OnChainOpenOrder = {
price: string
tick: number
orderIndex: string
claimable: { currency: Currency; value: string }
cancelable: { currency: Currency; value: string }
claimable: CurrencyAmount
cancelable: CurrencyAmount
}

export type OpenOrder = OnChainOpenOrder & {
txHash: `0x${string}`
createdAt: number
amount: { currency: Currency; value: string }
filled: { currency: Currency; value: string }
claimed: { currency: Currency; value: string }
amount: CurrencyAmount
filled: CurrencyAmount
claimed: CurrencyAmount
}
66 changes: 61 additions & 5 deletions src/model/pool.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { formatUnits } from 'viem'

import { CHAIN_IDS, Currency, Market } from '../type'
import { Pool as PoolType, CHAIN_IDS, Currency, Market } from '../type'
import { CONTRACT_ADDRESSES } from '../constants/addresses'
import { toPoolKey } from '../utils/pool-key'

import { Currency6909 } from './currency'

export class Pool {
chainId: CHAIN_IDS
key: `0x${string}`
market: Market
isOpened: boolean
Expand All @@ -17,8 +18,8 @@ export class Pool {

totalSupply: bigint
decimals: number
reserveA: string
reserveB: string
reserveA: bigint
reserveB: bigint
liquidityA: bigint
liquidityB: bigint
cancelableA: bigint
Expand Down Expand Up @@ -69,6 +70,7 @@ export class Pool {
orderListA: bigint[]
orderListB: bigint[]
}) {
this.chainId = chainId
this.key = toPoolKey(bookIdA, bookIdB, salt)
this.market = market
this.isOpened = isOpened
Expand Down Expand Up @@ -107,9 +109,63 @@ export class Pool {
this.cancelableB = cancelableB
this.claimableA = claimableA
this.claimableB = claimableB
this.reserveA = formatUnits(reserveA, this.currencyA.decimals)
this.reserveB = formatUnits(reserveB, this.currencyB.decimals)
this.reserveA = reserveA
this.reserveB = reserveB
this.orderListA = orderListA
this.orderListB = orderListB
}

toJson = (): PoolType => {
return {
chainId: this.chainId,
key: this.key,
market: this.market,
isOpened: this.isOpened,
strategy: this.strategy,
currencyA: this.currencyA,
currencyB: this.currencyB,
reserveA: {
total: {
currency: this.currencyA,
value: formatUnits(this.reserveA, this.currencyA.decimals),
},
liquidity: {
currency: this.currencyA,
value: formatUnits(this.liquidityA, this.currencyA.decimals),
},
cancelable: {
currency: this.currencyA,
value: formatUnits(this.cancelableA, this.currencyA.decimals),
},
claimable: {
currency: this.currencyA,
value: formatUnits(this.claimableA, this.currencyA.decimals),
},
},
reserveB: {
total: {
currency: this.currencyB,
value: formatUnits(this.reserveB, this.currencyB.decimals),
},
liquidity: {
currency: this.currencyB,
value: formatUnits(this.liquidityB, this.currencyB.decimals),
},
cancelable: {
currency: this.currencyB,
value: formatUnits(this.cancelableB, this.currencyB.decimals),
},
claimable: {
currency: this.currencyB,
value: formatUnits(this.claimableB, this.currencyB.decimals),
},
},
totalSupply: {
currency: this.currencyLp,
value: formatUnits(this.totalSupply, this.decimals),
},
orderListA: this.orderListA.map((order) => order.toString()),
orderListB: this.orderListB.map((order) => order.toString()),
}
}
}
29 changes: 18 additions & 11 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,21 @@ export type Pool = {
strategy: `0x${string}`
currencyA: Currency
currencyB: Currency
reserveA: string
reserveB: string
totalSupply: bigint
liquidityA: bigint
liquidityB: bigint
cancelableA: bigint
cancelableB: bigint
claimableA: bigint
claimableB: bigint
orderListA: bigint[]
orderListB: bigint[]
reserveA: {
total: CurrencyAmount
liquidity: CurrencyAmount
cancelable: CurrencyAmount
claimable: CurrencyAmount
}
reserveB: {
total: CurrencyAmount
liquidity: CurrencyAmount
cancelable: CurrencyAmount
claimable: CurrencyAmount
}
totalSupply: Currency6909Amount
orderListA: string[]
orderListB: string[]
}

export type Transaction = {
Expand Down Expand Up @@ -121,3 +125,6 @@ export enum CHART_LOG_INTERVALS {
oneDay = '1d',
oneWeek = '1w',
}

export type CurrencyAmount = { currency: Currency; value: string }
export type Currency6909Amount = { currency: Currency6909; value: string }
21 changes: 1 addition & 20 deletions src/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,26 +183,7 @@ export const getPool = async ({
!!(options && options.useSubgraph),
options?.market,
)
return {
chainId,
key: pool.key,
market: pool.market,
isOpened: pool.isOpened,
strategy: pool.strategy,
currencyA: pool.currencyA,
currencyB: pool.currencyB,
reserveA: pool.reserveA,
reserveB: pool.reserveB,
totalSupply: pool.totalSupply,
liquidityA: pool.liquidityA,
liquidityB: pool.liquidityB,
cancelableA: pool.cancelableA,
cancelableB: pool.cancelableB,
claimableA: pool.claimableA,
claimableB: pool.claimableB,
orderListA: pool.orderListA,
orderListB: pool.orderListB,
}
return pool.toJson()
}

export const getStrategyPrice = async ({
Expand Down
34 changes: 17 additions & 17 deletions test/adjust-order-price.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,26 +138,26 @@ test('Adjust order price', async () => {
},
})

expect(BigNumber(poolStep1.reserveA).plus('2000').toString()).toBe(
poolStep2.reserveA,
expect(
BigNumber(poolStep1.reserveA.total.value).plus('2000').toString(),
).toBe(poolStep2.reserveA.total.value)
expect(BigNumber(poolStep1.reserveB.total.value).plus('1').toString()).toBe(
poolStep2.reserveB.total.value,
)
expect(BigNumber(poolStep1.reserveB).plus('1').toString()).toBe(
poolStep2.reserveB,
)
expect(poolStep1.cancelableA).toBe(0n)
expect(poolStep1.cancelableB).toBe(0n)
expect(poolStep1.claimableA).toBe(0n)
expect(poolStep1.claimableB).toBe(0n)
expect(poolStep1.reserveA.cancelable.value).toBe('0')
expect(poolStep1.reserveB.cancelable.value).toBe('0')
expect(poolStep1.reserveA.claimable.value).toBe('0')
expect(poolStep1.reserveB.claimable.value).toBe('0')

expect(poolStep2.cancelableA).toBe(0n)
expect(poolStep2.cancelableB).toBe(0n)
expect(poolStep2.claimableA).toBe(0n)
expect(poolStep2.claimableB).toBe(0n)
expect(poolStep2.reserveA.cancelable.value).toBe('0')
expect(poolStep2.reserveB.cancelable.value).toBe('0')
expect(poolStep2.reserveA.claimable.value).toBe('0')
expect(poolStep2.reserveB.claimable.value).toBe('0')

expect(poolStep3.cancelableA).toBe(99999999n)
expect(poolStep3.cancelableB).toBe(38167546300000000n)
expect(poolStep3.claimableA).toBe(0n)
expect(poolStep3.claimableB).toBe(0n)
expect(poolStep3.reserveA.cancelable.value).toBe('99.999999')
expect(poolStep3.reserveB.cancelable.value).toBe('0.0381675463')
expect(poolStep3.reserveA.claimable.value).toBe('0')
expect(poolStep3.reserveB.claimable.value).toBe('0')
})

test('Adjust order price with invalid alpha', async () => {
Expand Down
16 changes: 8 additions & 8 deletions test/refill-order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,13 @@ test('Refill order', async () => {
},
})

expect(poolStep4.cancelableA).toBe(0n)
expect(poolStep4.cancelableB).toBe(155953200000000n)
expect(poolStep4.claimableA).toBe(99997469n)
expect(poolStep4.claimableB).toBe(38329361063758775n)
expect(poolStep4.reserveA.cancelable.value).toBe('0')
expect(poolStep4.reserveB.cancelable.value).toBe('0.0001559532')
expect(poolStep4.reserveA.claimable.value).toBe('99.997469')
expect(poolStep4.reserveB.claimable.value).toBe('0.038329361063758775')

expect(poolStep5.cancelableA).toBe(99999872n)
expect(poolStep5.cancelableB).toBe(38167546300000000n)
expect(poolStep5.claimableA).toBe(0n)
expect(poolStep5.claimableB).toBe(0n)
expect(poolStep5.reserveA.cancelable.value).toBe('99.999872')
expect(poolStep5.reserveB.cancelable.value).toBe('0.0381675463')
expect(poolStep5.reserveA.claimable.value).toBe('0')
expect(poolStep5.reserveB.claimable.value).toBe('0')
})

0 comments on commit a8b5200

Please sign in to comment.