Skip to content
Merged
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
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": "1.0.0-beta.31",
"version": "1.0.0-beta.32",
"description": "🛠 An SDK for building applications on top of Clober V2",
"files": [
"dist"
Expand Down
28 changes: 22 additions & 6 deletions src/entities/pool/apis/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type PoolPeriodDataDto = {
export const fetchPoolSnapshotFromSubgraph = async (
chainId: CHAIN_IDS,
poolKey: `0x${string}`,
prices: Record<`0x${string}`, number>,
): Promise<PoolSnapshot | null> => {
const {
data: { pool, poolDayDatas, poolHourDatas },
Expand All @@ -88,12 +89,16 @@ export const fetchPoolSnapshotFromSubgraph = async (
symbol: pool.tokenA.symbol,
decimals: Number(pool.tokenA.decimals),
}
const currencyAPrice =
prices?.[pool.tokenA.id.toLowerCase() as `0x${string}`] ?? 0
const currencyB: Currency = {
address: getAddress(pool.tokenB.id),
name: pool.tokenB.name,
symbol: pool.tokenB.symbol,
decimals: Number(pool.tokenB.decimals),
}
const currencyBPrice =
prices?.[pool.tokenB.id.toLowerCase() as `0x${string}`] ?? 0
const lpCurrency = {
id: pool.id as `0x${string}`,
address: getContractAddresses({ chainId }).Rebalancer,
Expand All @@ -111,13 +116,19 @@ export const fetchPoolSnapshotFromSubgraph = async (
)
const initialTotalSupply = formatUnits(BigInt(pool.initialTotalSupply), 18)
const performanceHistories = poolDayDatas
.map((poolDayData) => {
.map((poolDayData, index) => {
const priceAUSD =
pool.tokenA.tokenDayData.find(({ date }) => date === poolDayData.date)
?.priceUSD ?? '0'
index === poolDayDatas.length - 1 && currencyAPrice
? currencyAPrice.toString()
: pool.tokenA.tokenDayData.find(
({ date }) => date === poolDayData.date,
)?.priceUSD ?? '0'
const priceBUSD =
pool.tokenB.tokenDayData.find(({ date }) => date === poolDayData.date)
?.priceUSD ?? '0'
index === poolDayDatas.length - 1 && currencyBPrice
? currencyBPrice.toString()
: pool.tokenB.tokenDayData.find(
({ date }) => date === poolDayData.date,
)?.priceUSD ?? '0'

const onHoldUSDValuePerLp = new BigNumber(initialTokenAAmount)
.multipliedBy(priceAUSD)
Expand Down Expand Up @@ -198,6 +209,7 @@ export const fetchPoolSnapshotFromSubgraph = async (

export const fetchPoolSnapshotsFromSubgraph = async (
chainId: CHAIN_IDS,
prices: Record<`0x${string}`, number>,
): Promise<PoolSnapshot[]> => {
const {
data: { pools },
Expand All @@ -210,7 +222,11 @@ export const fetchPoolSnapshotsFromSubgraph = async (
}>(chainId, 'getPoolKeys', 'query getPoolKeys { pools { id } }', {})
return Promise.all(
pools.map(async (pool) => {
return fetchPoolSnapshotFromSubgraph(chainId, pool.id as `0x${string}`)
return fetchPoolSnapshotFromSubgraph(
chainId,
pool.id as `0x${string}`,
prices,
)
}),
) as Promise<PoolSnapshot[]>
}
Expand Down
12 changes: 10 additions & 2 deletions src/views/pool/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ import {
export const getPoolSnapshot = async ({
chainId,
poolKey,
prices = {},
}: {
chainId: CHAIN_IDS
poolKey: `0x${string}`
prices: Record<`0x${string}`, number>
}): Promise<PoolSnapshot> => {
const poolSnapshot = await fetchPoolSnapshotFromSubgraph(chainId, poolKey)
const poolSnapshot = await fetchPoolSnapshotFromSubgraph(
chainId,
poolKey,
prices,
)
if (!poolSnapshot) {
throw new Error('Pool is not existed')
}
Expand All @@ -21,8 +27,10 @@ export const getPoolSnapshot = async ({

export const getPoolSnapshots = async ({
chainId,
prices = {},
}: {
chainId: CHAIN_IDS
prices: Record<`0x${string}`, number>
}): Promise<PoolSnapshot[]> => {
return fetchPoolSnapshotsFromSubgraph(chainId)
return fetchPoolSnapshotsFromSubgraph(chainId, prices)
}
Loading