diff --git a/package-lock.json b/package-lock.json index 306f687..113557a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@clober/v2-sdk", - "version": "0.0.70", + "version": "0.0.71", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@clober/v2-sdk", - "version": "0.0.70", + "version": "0.0.71", "license": "MIT", "dependencies": { "axios": "^1.7.7", diff --git a/package.json b/package.json index f8fa24a..2bb1ba2 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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" diff --git a/src/model/open-order.ts b/src/model/open-order.ts index 275546d..d005d1a 100644 --- a/src/model/open-order.ts +++ b/src/model/open-order.ts @@ -1,3 +1,5 @@ +import { CurrencyAmount } from '../type' + import type { Currency } from './currency' export type OpenOrderDto = { @@ -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 } diff --git a/src/model/pool.ts b/src/model/pool.ts index 10cfbb8..e59ee1a 100644 --- a/src/model/pool.ts +++ b/src/model/pool.ts @@ -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 @@ -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 @@ -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 @@ -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()), + } + } } diff --git a/src/type.ts b/src/type.ts index f277dc8..74139f9 100644 --- a/src/type.ts +++ b/src/type.ts @@ -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 = { @@ -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 } diff --git a/src/view.ts b/src/view.ts index e845946..903cc7a 100644 --- a/src/view.ts +++ b/src/view.ts @@ -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 ({ diff --git a/test/adjust-order-price.test.ts b/test/adjust-order-price.test.ts index 3612191..e04e1b8 100644 --- a/test/adjust-order-price.test.ts +++ b/test/adjust-order-price.test.ts @@ -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 () => { diff --git a/test/refill-order.test.ts b/test/refill-order.test.ts index ea703a2..dcc2434 100644 --- a/test/refill-order.test.ts +++ b/test/refill-order.test.ts @@ -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') })