Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dynamic fee #151

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/market-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const main = async () => {

const {
transaction,
result: { spend, take },
result: { spent, taken },
} = await marketOrder({
chainId: arbitrumSepolia.id,
userAddress: walletClient.account.address,
Expand All @@ -30,8 +30,8 @@ const main = async () => {
gasPrice: parseUnits('1', 9),
})
console.log(`market order hash: ${hash}`)
console.log(`spend: `, spend)
console.log(`take: `, take)
console.log(`spend: `, spent)
console.log(`taken: `, taken)

const market = await getMarket({
chainId: arbitrumSepolia.id,
Expand Down
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.95",
"version": "0.0.96",
"description": "🛠 An SDK for building applications on top of Clober V2",
"files": [
"dist"
Expand Down
36 changes: 33 additions & 3 deletions src/apis/market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { BOOK_VIEWER_ABI } from '../abis/core/book-viewer-abi'
import { fetchIsMarketOpened } from '../utils/open'
import { fetchCurrency } from '../utils/currency'
import { Subgraph } from '../constants/subgraph'
import { FeePolicy } from '../model/fee-policy'

const fetchBookFromSubgraph = async (chainId: CHAIN_IDS, bookId: string) => {
return Subgraph.get<{
Expand Down Expand Up @@ -39,14 +40,17 @@ const getBook = async (
chainId: CHAIN_IDS,
quoteCurrency: Currency,
baseCurrency: Currency,
makerFeePolicy: FeePolicy,
takerFeePolicy: FeePolicy,
useSubgraph: boolean,
n: number,
): Promise<Book> => {
const unitSize = await calculateUnitSize(publicClient, chainId, quoteCurrency)
const bookId = toBookId(
chainId,
quoteCurrency.address,
baseCurrency.address,
makerFeePolicy,
takerFeePolicy,
unitSize,
)
if (useSubgraph) {
Expand All @@ -68,6 +72,8 @@ const getBook = async (
)
: [],
isOpened: book !== null,
makerFeePolicy,
takerFeePolicy,
})
}

Expand All @@ -92,13 +98,19 @@ const getBook = async (
unitAmount: depth,
})),
isOpened,
makerFeePolicy,
takerFeePolicy,
})
}

export async function fetchMarket(
publicClient: PublicClient,
chainId: CHAIN_IDS,
tokenAddresses: `0x${string}`[],
bidBookMakerFeePolicy: FeePolicy,
bidBookTakerFeePolicy: FeePolicy,
askBookMakerFeePolicy: FeePolicy,
askBookTakerFeePolicy: FeePolicy,
useSubgraph: boolean,
n = 100,
): Promise<Market> {
Expand All @@ -116,8 +128,26 @@ export async function fetchMarket(
fetchCurrency(publicClient, chainId, baseTokenAddress),
])
const [bidBook, askBook] = await Promise.all([
getBook(publicClient, chainId, quoteCurrency, baseCurrency, useSubgraph, n),
getBook(publicClient, chainId, baseCurrency, quoteCurrency, useSubgraph, n),
getBook(
publicClient,
chainId,
quoteCurrency,
baseCurrency,
bidBookMakerFeePolicy,
bidBookTakerFeePolicy,
useSubgraph,
n,
),
getBook(
publicClient,
chainId,
baseCurrency,
quoteCurrency,
askBookMakerFeePolicy,
askBookTakerFeePolicy,
useSubgraph,
n,
),
])

return new Market({
Expand Down
16 changes: 10 additions & 6 deletions src/apis/open-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { invertTick, toPrice } from '../utils/tick'
import type { OpenOrder, OpenOrderDto } from '../model/open-order'
import { fetchCurrency } from '../utils/currency'
import { applyPercent } from '../utils/bigint'
import { MAKER_DEFAULT_POLICY } from '../constants/fee'
import { Subgraph } from '../constants/subgraph'
import { FeePolicy } from '../model/fee-policy'

const getOpenOrderFromSubgraph = async (
chainId: CHAIN_IDS,
Expand Down Expand Up @@ -70,6 +70,7 @@ export async function fetchOpenOrdersByUserAddress(
publicClient: PublicClient,
chainId: CHAIN_IDS,
userAddress: `0x${string}`,
makerFeePolicy: FeePolicy,
): Promise<OpenOrder[]> {
const {
data: { openOrders },
Expand All @@ -88,14 +89,15 @@ export async function fetchOpenOrdersByUserAddress(
.map((address) => fetchCurrency(publicClient, chainId, address)),
)
return openOrders.map((openOrder) =>
toOpenOrder(chainId, currencies, openOrder),
toOpenOrder(chainId, currencies, openOrder, makerFeePolicy),
)
}

export async function fetchOpenOrder(
publicClient: PublicClient,
chainId: CHAIN_IDS,
id: string,
makerFeePolicy: FeePolicy,
): Promise<OpenOrder> {
const {
data: { openOrder },
Expand All @@ -107,13 +109,14 @@ export async function fetchOpenOrder(
fetchCurrency(publicClient, chainId, getAddress(openOrder.book.base.id)),
fetchCurrency(publicClient, chainId, getAddress(openOrder.book.quote.id)),
])
return toOpenOrder(chainId, currencies, openOrder)
return toOpenOrder(chainId, currencies, openOrder, makerFeePolicy)
}

export async function fetchOpenOrders(
publicClient: PublicClient,
chainId: CHAIN_IDS,
ids: string[],
makerFeePolicy: FeePolicy,
): Promise<OpenOrder[]> {
const {
data: { openOrders },
Expand All @@ -132,14 +135,15 @@ export async function fetchOpenOrders(
.map((address) => fetchCurrency(publicClient, chainId, address)),
)
return openOrders.map((openOrder) =>
toOpenOrder(chainId, currencies, openOrder),
toOpenOrder(chainId, currencies, openOrder, makerFeePolicy),
)
}

const toOpenOrder = (
chainId: CHAIN_IDS,
currencies: Currency[],
openOrder: OpenOrderDto,
makerFeePolicy: FeePolicy,
): OpenOrder => {
const inputCurrency = currencies.find((c: Currency) =>
isAddressEqual(c.address, getAddress(openOrder.book.quote.id)),
Expand Down Expand Up @@ -210,8 +214,8 @@ const toOpenOrder = (
applyPercent(
cancelable,
100 +
(Number(MAKER_DEFAULT_POLICY[chainId].rate) * 100) /
Number(MAKER_DEFAULT_POLICY[chainId].RATE_PRECISION),
(Number(makerFeePolicy.rate) * 100) /
Number(makerFeePolicy.RATE_PRECISION),
6,
),
inputCurrency.decimals,
Expand Down
12 changes: 11 additions & 1 deletion src/apis/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { REBALANCER_ABI } from '../abis/rebalancer/rebalancer-abi'
import { CHART_LOG_INTERVALS, Market } from '../type'
import { Subgraph } from '../constants/subgraph'
import { STRATEGY_ABI } from '../abis/rebalancer/strategy-abi'
import { MAKER_DEFAULT_POLICY, TAKER_DEFAULT_POLICY } from '../constants/fee'

import { fetchMarket } from './market'

Expand Down Expand Up @@ -68,7 +69,16 @@ export async function fetchPool(
}
if (!market) {
market = (
await fetchMarket(publicClient, chainId, tokenAddresses, useSubgraph)
await fetchMarket(
publicClient,
chainId,
tokenAddresses,
MAKER_DEFAULT_POLICY[chainId],
TAKER_DEFAULT_POLICY[chainId],
MAKER_DEFAULT_POLICY[chainId],
TAKER_DEFAULT_POLICY[chainId],
useSubgraph,
)
).toJson()
}
const poolKey = toPoolKey(
Expand Down
Loading
Loading