-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #648 from curvefi/feat/table-headers
feat: separate graph columns
- Loading branch information
Showing
13 changed files
with
147 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
apps/main/src/loan/components/PageLlamaMarkets/cells/RateCell.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { LlamaMarket } from '@/loan/entities/llama-markets' | ||
import { useSnapshots } from '../hooks/useSnapshots' | ||
import { GraphType } from '@/loan/components/PageLlamaMarkets/hooks/useSnapshots' | ||
|
||
export const RateCell = ({ market, type }: { market: LlamaMarket; type: GraphType }) => { | ||
const { rate } = useSnapshots(market, type) | ||
return rate == null ? '-' : `${rate.toPrecision(4)}%` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './CompactUsdCell' | ||
export * from './PoolTitleCell' | ||
export * from './LineGraphCell' | ||
export * from './RateCell' | ||
export * from './UtilizationCell' |
46 changes: 46 additions & 0 deletions
46
apps/main/src/loan/components/PageLlamaMarkets/hooks/useSnapshots.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { LlamaMarket, LlamaMarketType } from '@/loan/entities/llama-markets' | ||
import { LendingSnapshot, useLendingSnapshots } from '@/loan/entities/lending-snapshots' | ||
import { CrvUsdSnapshot, useCrvUsdSnapshots } from '@/loan/entities/crvusd' | ||
import { useMemo } from 'react' | ||
import { meanBy } from 'lodash' | ||
|
||
export type GraphType = 'borrow' | 'lend' | ||
|
||
type UseSnapshotsResult<T> = { | ||
snapshots: T[] | null | ||
isLoading: boolean | ||
snapshotKey: keyof T | ||
rate: number | null | ||
} | ||
|
||
export function useSnapshots<T = CrvUsdSnapshot | LendingSnapshot>( | ||
{ address, blockchainId, controllerAddress, type: marketType, rates }: LlamaMarket, | ||
type: GraphType, | ||
): UseSnapshotsResult<T> { | ||
const isPool = marketType == LlamaMarketType.Pool | ||
const showMintGraph = !isPool && type === 'borrow' | ||
const contractAddress = isPool ? controllerAddress : address | ||
const params = { blockchainId: blockchainId, contractAddress } | ||
const { data: poolSnapshots, isLoading: poolIsLoading } = useLendingSnapshots(params, isPool) | ||
const { data: mintSnapshots, isLoading: mintIsLoading } = useCrvUsdSnapshots(params, showMintGraph) | ||
|
||
const currentValue = rates[type] | ||
const { snapshots, isLoading, snapshotKey } = isPool | ||
? { | ||
snapshots: poolSnapshots ?? null, | ||
isLoading: poolIsLoading, | ||
snapshotKey: `${type}_apy` as const, | ||
} | ||
: { | ||
snapshots: (showMintGraph && mintSnapshots) || null, | ||
isLoading: mintIsLoading, | ||
snapshotKey: 'rate' as const, | ||
} | ||
|
||
const rate = useMemo( | ||
() => (snapshots?.length ? meanBy(snapshots as T[], (row) => row[snapshotKey as keyof T]) : (currentValue ?? null)), | ||
[snapshots, currentValue, snapshotKey], | ||
) | ||
|
||
return { snapshots, isLoading, snapshotKey, rate } as UseSnapshotsResult<T> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,20 @@ | ||
import { ContractParams, PoolParams, PoolQuery, queryFactory, rootKeys } from '@ui-kit/lib/model/query' | ||
import { ContractParams, ContractQuery, queryFactory, rootKeys } from '@ui-kit/lib/model/query' | ||
import { contractValidationSuite } from '@ui-kit/lib/model/query/contract-validation' | ||
import { getSnapshots } from '@curvefi/prices-api/crvusd' | ||
import { Chain } from '@curvefi/prices-api' | ||
import type { Snapshot } from '@curvefi/prices-api/crvusd/models' | ||
|
||
type CrvUsdSnapshotFromApi = { | ||
rate: number | ||
borrow_apy: number | ||
lend_apy: number | ||
liquidation_discount: number | ||
loan_discount: number | ||
n_loans: number | ||
price_oracle: number | ||
amm_price: number | ||
base_price: number | ||
total_debt: number | ||
total_assets: number | ||
total_debt_usd: number | ||
total_assets_usd: number | ||
minted: number | ||
redeemed: number | ||
minted_usd: number | ||
redeemed_usd: number | ||
min_band: number | ||
max_band: number | ||
collateral_balance: number | ||
borrowed_balance: number | ||
collateral_balance_usd: number | ||
borrowed_balance_usd: number | ||
sum_debt_squared: number | ||
timestamp: string | ||
} | ||
|
||
type CrvUsdSnapshotsFromApi = { | ||
chain: string | ||
market_id: number | ||
data: CrvUsdSnapshotFromApi[] | ||
} | ||
|
||
export const _getCrvUsdSnapshots = async ({ | ||
blockchainId, | ||
contractAddress, | ||
}: ContractParams): Promise<CrvUsdSnapshotFromApi[]> => { | ||
const url = `https://prices.curve.fi/v1/crvusd/markets/${blockchainId}/${contractAddress}/snapshots` | ||
const response = await fetch(url) | ||
const { data } = (await response.json()) as CrvUsdSnapshotsFromApi | ||
if (!data) { | ||
throw new Error('Failed to fetch crvUSD snapshots') | ||
} | ||
return data | ||
} | ||
export type CrvUsdSnapshot = Snapshot | ||
|
||
export const { useQuery: useCrvUsdSnapshots } = queryFactory({ | ||
queryKey: (params: ContractParams) => [...rootKeys.contract(params), 'crvUsd', 'snapshots'] as const, | ||
queryFn: _getCrvUsdSnapshots, | ||
staleTime: '1d', | ||
queryFn: async ({ blockchainId, contractAddress }: ContractQuery): Promise<CrvUsdSnapshot[]> => { | ||
const snapshots = await getSnapshots(blockchainId as Chain, contractAddress) | ||
return snapshots.map((snapshot) => ({ | ||
...snapshot, | ||
rate: snapshot.rate * 100, // Convert to percentage for consistency with lending snapshots | ||
})) | ||
}, | ||
staleTime: '10m', | ||
validationSuite: contractValidationSuite, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.