Skip to content

Commit

Permalink
refactor: use prices api for llamma events
Browse files Browse the repository at this point in the history
  • Loading branch information
Alunara committed Jan 25, 2025
1 parent e492fa5 commit 881edea
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 83 deletions.
24 changes: 10 additions & 14 deletions apps/main/src/lend/components/ChartOhlcWrapper/LiquidityData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@ const LiquidityData: React.FC<LiquidityDataProps> = ({ lendControllerData, chain
<>
{coins &&
lendControllerData.map((transaction, index) => (
<TransactionRow key={`${transaction.transaction_hash}-lp-${index}`}>
<LiquidityEvent
href={networks[chainId].scanTxPath(transaction.transaction_hash)}
rel="noopener"
target="_blank"
>
{transaction.deposit !== null && (
<TransactionRow key={`${transaction.txHash}-lp-${index}`}>
<LiquidityEvent href={networks[chainId].scanTxPath(transaction.txHash)} rel="noopener" target="_blank">
{!!transaction.deposit && (
<>
<Box flex flexColumn>
<LiquidityEventTitle>{t`Deposit`}</LiquidityEventTitle>
Expand All @@ -49,15 +45,15 @@ const LiquidityData: React.FC<LiquidityDataProps> = ({ lendControllerData, chain
</LiquidityEventRow>
</>
)}
{transaction.withdrawal !== null && (
{!!transaction.withdrawal && (
<>
<LiquidityEventTitle className="remove">{t`Withdrawal`}</LiquidityEventTitle>
<Box flex flexColumn margin="0 0 0 auto">
{+transaction.withdrawal.amount_collateral !== 0 && (
{+transaction.withdrawal.amountCollateral !== 0 && (
<LiquidityEventRow>
<Chip isBold isNumber>
{formatNumber(transaction.withdrawal.amount_collateral, {
...getFractionDigitsOptions(transaction.withdrawal.amount_collateral, 2),
{formatNumber(transaction.withdrawal.amountCollateral, {
...getFractionDigitsOptions(transaction.withdrawal.amountCollateral, 2),
})}
</Chip>
<LiquiditySymbol>{coins.collateralToken.symbol}</LiquiditySymbol>
Expand All @@ -69,11 +65,11 @@ const LiquidityData: React.FC<LiquidityDataProps> = ({ lendControllerData, chain
/>
</LiquidityEventRow>
)}
{+transaction.withdrawal.amount_borrowed !== 0 && (
{+transaction.withdrawal.amountBorrowed !== 0 && (
<LiquidityEventRow>
<Chip isBold isNumber>
{formatNumber(transaction.withdrawal.amount_borrowed, {
...getFractionDigitsOptions(transaction.withdrawal.amount_borrowed, 2),
{formatNumber(transaction.withdrawal.amountBorrowed, {
...getFractionDigitsOptions(transaction.withdrawal.amountBorrowed, 2),
})}
</Chip>
<LiquiditySymbol>{coins.borrowedToken.symbol}</LiquiditySymbol>
Expand Down
5 changes: 2 additions & 3 deletions apps/main/src/lend/components/ChartOhlcWrapper/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { LlammaControllerEvent } from '@ui/Chart/types'
import { ChainId } from '@lend/types/lend.types'
import type { LlammaTrade } from '@curvefi/prices-api/llamma'
import type { LlammaTrade, LlammaEvent } from '@curvefi/prices-api/llamma'

export type LendingMarketTokens = {
borrowedToken: {
Expand All @@ -20,7 +19,7 @@ export interface PoolActivityProps {
}

export interface LiquidityDataProps {
lendControllerData: LlammaControllerEvent[]
lendControllerData: LlammaEvent[]
chainId: ChainId
coins: LendingMarketTokens
}
Expand Down
29 changes: 15 additions & 14 deletions apps/main/src/lend/store/createOhlcChartSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ import type {
VolumeData,
LlamaBaselinePriceData,
OraclePriceData,
LlammaControllerApiResponse,
LlammaControllerEvent,
} from 'ui/src/Chart/types'
import type { UTCTimestamp } from 'lightweight-charts'
import type { Address, Chain } from '@curvefi/prices-api'
import { getOHLC, getTrades } from '@curvefi/prices-api/llamma'
import { getOHLC, getTrades, getEvents } from '@curvefi/prices-api/llamma'

import produce from 'immer'

Expand Down Expand Up @@ -51,7 +49,7 @@ type SliceState = {
dataDisabled: boolean
}
lendTradesData: Awaited<ReturnType<typeof getTrades>>['trades']
lendControllerData: LlammaControllerEvent[]
lendControllerData: Awaited<ReturnType<typeof getEvents>>['events']
activityFetchStatus: FetchingStatus
timeOption: TimeOptions
activityHidden: boolean
Expand Down Expand Up @@ -681,31 +679,34 @@ const createOhlcChart = (set: SetState<State>, get: GetState<State>) => ({
)
}

const controllerEventsRes = await fetch(
`https://prices.curve.fi/v1/lending/llamma_events/${network}/${poolAddress}?page=1&per_page=100`,
)
const controllerEventsData: LlammaControllerApiResponse = await controllerEventsRes.json()
const { events } = await getEvents({
endpoint: 'lending',
chain: network as Chain,
llamma: poolAddress as Address,
page: 1,
perPage: 100,
})

const formattedLiquidityEventsData = controllerEventsData.data.map((data) => ({
const formattedLiquidityEventsData = events.map((data) => ({
...data,
deposit:
data.deposit === null
data.deposit === null || data.deposit === undefined
? null
: {
...data.deposit,
amount: data.deposit.amount,
},
withdrawal:
data.withdrawal === null
data.withdrawal === null || data.withdrawal === undefined
? null
: {
...data.withdrawal,
amount_borrowed: data.withdrawal.amount_borrowed,
amount_collateral: data.withdrawal.amount_collateral,
amount_borrowed: data.withdrawal.amountBorrowed,
amount_collateral: data.withdrawal.amountCollateral,
},
}))

if (controllerEventsData) {
if (events) {
set(
produce((state: State) => {
state[sliceKey].lendControllerData = formattedLiquidityEventsData
Expand Down
24 changes: 10 additions & 14 deletions apps/main/src/loan/components/ChartOhlcWrapper/LiquidityData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@ const LiquidityData: React.FC<LiqudityDataProps> = ({ llammaControllerData, chai
<>
{coins &&
llammaControllerData.map((transaction, index) => (
<TransactionRow key={`${transaction.transaction_hash}-lp-${index}`}>
<LiquidityEvent
href={networks[chainId].scanTxPath(transaction.transaction_hash)}
rel="noopener"
target="_blank"
>
{transaction.deposit !== null && (
<TransactionRow key={`${transaction.txHash}-lp-${index}`}>
<LiquidityEvent href={networks[chainId].scanTxPath(transaction.txHash)} rel="noopener" target="_blank">
{!!transaction.deposit && (
<>
<Box flex flexColumn>
<LiquidityEventTitle>{t`Deposit`}</LiquidityEventTitle>
Expand All @@ -47,15 +43,15 @@ const LiquidityData: React.FC<LiqudityDataProps> = ({ llammaControllerData, chai
</LiquidityEventRow>
</>
)}
{transaction.withdrawal !== null && (
{!!transaction.withdrawal && (
<>
<LiquidityEventTitle className="remove">{t`Withdrawal`}</LiquidityEventTitle>
<Box flex flexColumn margin="0 0 0 auto">
{+transaction.withdrawal.amount_collateral !== 0 && (
{+transaction.withdrawal.amountCollateral !== 0 && (
<LiquidityEventRow>
<Chip isBold isNumber>
{formatNumber(transaction.withdrawal.amount_collateral, {
...getFractionDigitsOptions(transaction.withdrawal.amount_collateral, 2),
{formatNumber(transaction.withdrawal.amountCollateral, {
...getFractionDigitsOptions(transaction.withdrawal.amountCollateral, 2),
})}
</Chip>
<LiquiditySymbol>{coins.collateral.symbol}</LiquiditySymbol>
Expand All @@ -67,11 +63,11 @@ const LiquidityData: React.FC<LiqudityDataProps> = ({ llammaControllerData, chai
/>
</LiquidityEventRow>
)}
{+transaction.withdrawal.amount_borrowed !== 0 && (
{+transaction.withdrawal.amountBorrowed !== 0 && (
<LiquidityEventRow>
<Chip isBold isNumber>
{formatNumber(transaction.withdrawal.amount_borrowed, {
...getFractionDigitsOptions(transaction.withdrawal.amount_borrowed, 2),
{formatNumber(transaction.withdrawal.amountBorrowed, {
...getFractionDigitsOptions(transaction.withdrawal.amountBorrowed, 2),
})}
</Chip>
<LiquiditySymbol>{coins.crvusd.symbol}</LiquiditySymbol>
Expand Down
5 changes: 2 additions & 3 deletions apps/main/src/loan/components/ChartOhlcWrapper/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { LlammaControllerEvent } from '@ui/Chart/types'
import { ChainId, Llamma } from '@loan/types/loan.types'
import { LlammaTrade } from '@curvefi/prices-api/llamma'
import type { LlammaTrade, LlammaEvent } from '@curvefi/prices-api/llamma'

export type LlammaLiquidityCoins = {
crvusd: {
Expand All @@ -20,7 +19,7 @@ export interface ChartOhlcWrapperProps {
}

export interface LiqudityDataProps {
llammaControllerData: LlammaControllerEvent[]
llammaControllerData: LlammaEvent[]
chainId: ChainId
coins: LlammaLiquidityCoins
}
Expand Down
29 changes: 15 additions & 14 deletions apps/main/src/loan/store/createOhlcChartSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import type {
VolumeData,
LlamaBaselinePriceData,
OraclePriceData,
LlammaControllerApiResponse,
LlammaControllerEvent,
} from 'ui/src/Chart/types'
import type { UTCTimestamp } from 'lightweight-charts'
import type { Address, Chain } from '@curvefi/prices-api'
import { getOHLC, getTrades } from '@curvefi/prices-api/llamma'
import { getOHLC, getTrades, getEvents } from '@curvefi/prices-api/llamma'

import produce from 'immer'

Expand All @@ -25,7 +23,7 @@ type SliceState = {
oraclePriceData: OraclePriceData[]
baselinePriceData: LlamaBaselinePriceData[]
llammaTradesData: Awaited<ReturnType<typeof getTrades>>['trades']
llammaControllerData: LlammaControllerEvent[]
llammaControllerData: Awaited<ReturnType<typeof getEvents>>['events']
chartFetchStatus: FetchingStatus
activityFetchStatus: FetchingStatus
timeOption: TimeOptions
Expand Down Expand Up @@ -308,31 +306,34 @@ const createOhlcChart = (set: SetState<State>, get: GetState<State>) => ({
)
}

const controllerEventsRes = await fetch(
`https://prices.curve.fi/v1/crvusd/llamma_events/${network}/${poolAddress}?page=1&per_page=100`,
)
const controllerEventsData: LlammaControllerApiResponse = await controllerEventsRes.json()
const { events } = await getEvents({
endpoint: 'crvusd',
chain: network as Chain,
llamma: poolAddress as Address,
page: 1,
perPage: 100,
})

const formattedLiquidityEventsData = controllerEventsData.data.map((data) => ({
const formattedLiquidityEventsData = events.map((data) => ({
...data,
deposit:
data.deposit === null
data.deposit === null || data.deposit === undefined
? null
: {
...data.deposit,
amount: data.deposit.amount,
},
withdrawal:
data.withdrawal === null
data.withdrawal === null || data.withdrawal === undefined
? null
: {
...data.withdrawal,
amount_borrowed: data.withdrawal.amount_borrowed,
amount_collateral: data.withdrawal.amount_collateral,
amount_borrowed: data.withdrawal.amountBorrowed,
amount_collateral: data.withdrawal.amountCollateral,
},
}))

if (controllerEventsData) {
if (events) {
set(
produce((state: State) => {
state[sliceKey].llammaControllerData = formattedLiquidityEventsData
Expand Down
20 changes: 18 additions & 2 deletions packages/prices-api/src/llamma/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,26 @@ import * as Parsers from './parsers'

export type Endpoint = 'crvusd' | 'lending'

export async function getEvents(endpoint: Endpoint, chain: Chain, llamma: string, page: number, options?: Options) {
type GetEventsParams = {
endpoint: Endpoint
chain: Chain
llamma: Address
page?: number
perPage?: number
}

export async function getEvents(
{ endpoint, chain, llamma, page = 1, perPage = 10 }: GetEventsParams,
options?: Options,
) {
const host = await getHost(options)
const params = new URLSearchParams({
page: page.toString(),
per_page: perPage.toString(),
})

const resp = await fetch<Responses.GetLlammaEventsResponse>(
`${host}/v1/${endpoint}/llamma_events/${chain}/${llamma}?page=${page}&per_page=10`,
`${host}/v1/${endpoint}/llamma_events/${chain}/${llamma}??${params.toString()}`,
)

return {
Expand Down
19 changes: 0 additions & 19 deletions packages/ui/src/Chart/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,25 +135,6 @@ export interface LpTradeToken {
event_index: number
}

export interface LlammaControllerEvent {
provider: string
deposit: {
amount: string
n1: number
n2: number
} | null
withdrawal: { amount_borrowed: string; amount_collateral: string } | null
block_number: number
timestamp: number
transaction_hash: string
}

export interface LlammaControllerApiResponse {
chain: string
address: string
data: LlammaControllerEvent[]
}

export type LiquidationRange = { value: number; time: UTCTimestamp }

export type LlammaLiquididationRange = {
Expand Down

0 comments on commit 881edea

Please sign in to comment.