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

refactor: prices api minor feedback #669

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
10 changes: 4 additions & 6 deletions apps/main/src/dao/components/PageAnalytics/CrvStats/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ const CrvStats: React.FC = () => {
}
}, [veCrvData.totalCrv, veCrvData.fetchStatus, getVeCrvData, provider])

const veCrvApr = aprLoading
? 0
: calculateApr(veCrvFees.fees[1].feesUsd, Number(veCrvData.totalVeCrv) / 10 ** 18, crv)
const veCrvApr = aprLoading ? 0 : calculateApr(veCrvFees.fees[1].feesUsd, veCrvData.totalVeCrv.fromWei(), crv)
0xAlunara marked this conversation as resolved.
Show resolved Hide resolved

const loading = Boolean(provider && veCrvLoading)
return (
Expand All @@ -40,7 +38,7 @@ const CrvStats: React.FC = () => {
title={t`Total CRV`}
data={
<MetricsColumnData>
{noProvider ? '-' : formatNumber(Number(veCrvData.totalCrv) / 10 ** 18, { notation: 'compact' })}
{noProvider ? '-' : formatNumber(veCrvData.totalCrv.fromWei(), { notation: 'compact' })}
</MetricsColumnData>
}
/>
Expand All @@ -49,7 +47,7 @@ const CrvStats: React.FC = () => {
title={t`Locked CRV`}
data={
<MetricsColumnData>
{noProvider ? '-' : formatNumber(Number(veCrvData.totalLockedCrv) / 10 ** 18, { notation: 'compact' })}
{noProvider ? '-' : formatNumber(veCrvData.totalLockedCrv.fromWei(), { notation: 'compact' })}
</MetricsColumnData>
}
/>
Expand All @@ -58,7 +56,7 @@ const CrvStats: React.FC = () => {
title={t`veCRV`}
data={
<MetricsColumnData>
{noProvider ? '-' : formatNumber(Number(veCrvData.totalVeCrv) / 10 ** 18, { notation: 'compact' })}
{noProvider ? '-' : formatNumber(veCrvData.totalVeCrv.fromWei(), { notation: 'compact' })}
</MetricsColumnData>
}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const PositiveAndNegativeBarChart: React.FC<PositiveAndNegativeBarChartProps> =
<BarChart
width={500}
height={300}
data={data.map((x) => ({ ...x, amount: Number(x.amount) / 10 ** 18 }))}
data={data.map((x) => ({ ...x, amount: x.amount.fromWei() }))}
margin={{
top: 16,
right: 20,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ const TopHoldersTable: React.FC = () => {
</span>
</StyledTableDataLink>
<TableData className={allHoldersSortBy.key === 'weight' ? 'sortby-active right-padding' : 'right-padding'}>
{formatNumber(Number(holder.weight) / 10 ** 18, { notation: 'compact' })}
{formatNumber(holder.weight.fromWei(), { notation: 'compact' })}
</TableData>
<TableData className={allHoldersSortBy.key === 'locked' ? 'sortby-active right-padding' : 'right-padding'}>
{formatNumber(Number(holder.locked) / 10 ** 18, { notation: 'compact' })}
{formatNumber(holder.locked.fromWei(), { notation: 'compact' })}
</TableData>
<TableData
className={allHoldersSortBy.key === 'weightRatio' ? 'sortby-active right-padding' : 'right-padding'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ const TopHoldersBarChart: React.FC<TopHoldersBarChartProps> = ({ data, filter })

const dataFormatted = data.map((x) => ({
...x,
weight: Number(x.weight) / 10 ** 18,
locked: Number(x.locked) / 10 ** 18,
weight: x.weight.fromWei(),
locked: x.locked.fromWei(),
}))

return (
Expand Down
4 changes: 2 additions & 2 deletions apps/main/src/dao/components/PageUser/UserStats/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const UserStats = ({ veCrvHolder, holdersLoading }: UserStatsProps) => (
title={t`Total veCRV`}
data={
<MetricsColumnData>
{formatNumber(Number(veCrvHolder.weight) / 10 ** 18, { showDecimalIfSmallNumberOnly: true })}
{formatNumber(veCrvHolder.weight.fromWei(), { showDecimalIfSmallNumberOnly: true })}
</MetricsColumnData>
}
/>
Expand All @@ -29,7 +29,7 @@ const UserStats = ({ veCrvHolder, holdersLoading }: UserStatsProps) => (
title={t`Locked CRV`}
data={
<MetricsColumnData>
{formatNumber(Number(veCrvHolder.locked) / 10 ** 18, { showDecimalIfSmallNumberOnly: true })}
{formatNumber(veCrvHolder.locked.fromWei(), { showDecimalIfSmallNumberOnly: true })}
</MetricsColumnData>
}
/>
Expand Down
13 changes: 13 additions & 0 deletions apps/main/src/global-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,24 @@ import type { UTCTimestamp } from 'lightweight-charts'
declare global {
interface Date {
getUTCTimestamp(): UTCTimestamp
getLocalTimestamp(): UTCTimestamp
0xAlunara marked this conversation as resolved.
Show resolved Hide resolved
}

interface BigInt {
fromWei(): number
}
}

Date.prototype.getUTCTimestamp = function () {
return (this.getTime() / 1000) as UTCTimestamp
}

Date.prototype.getLocalTimestamp = function () {
return ((this.getTime() - this.getTimezoneOffset() * 60000) / 1000) as UTCTimestamp
}

BigInt.prototype.fromWei = function () {
return Number(this) / 10 ** 18
}

export {}
36 changes: 22 additions & 14 deletions apps/main/src/lend/store/createOhlcChartSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,24 +233,26 @@ const createOhlcChart = (set: SetState<State>, get: GetState<State>) => ({
let ohlcDataArray: LpPriceOhlcDataFormatted[] = []

for (const item of ohlc) {
const time = item.time.getLocalTimestamp()

volumeArray.push({
time: item.time.getUTCTimestamp(),
time,
value: item.volume,
color: item.open < item.close ? '#26a69982' : '#ef53507e',
})

baselinePriceArray.push({
time: item.time.getUTCTimestamp(),
time,
base_price: item.basePrice,
})

oraclePriceArray.push({
time: item.time.getUTCTimestamp(),
time,
value: item.oraclePrice,
})

ohlcDataArray.push({
time: item.time.getUTCTimestamp(),
time,
open: item.open,
close: item.close,
high: item.high,
Expand Down Expand Up @@ -322,24 +324,26 @@ const createOhlcChart = (set: SetState<State>, get: GetState<State>) => ({
let ohlcDataArray: LpPriceOhlcDataFormatted[] = []

for (const item of ohlc) {
const time = item.time.getLocalTimestamp()

volumeArray.push({
time: item.time.getUTCTimestamp(),
time,
value: item.volume,
color: item.open < item.close ? '#26a69982' : '#ef53507e',
})

baselinePriceArray.push({
time: item.time.getUTCTimestamp(),
time,
base_price: item.basePrice,
})

oraclePriceArray.push({
time: item.time.getUTCTimestamp(),
time,
value: item.oraclePrice,
})

ohlcDataArray.push({
time: item.time.getUTCTimestamp(),
time,
open: item.open,
close: item.close,
high: item.high,
Expand Down Expand Up @@ -418,22 +422,24 @@ const createOhlcChart = (set: SetState<State>, get: GetState<State>) => ({
let ohlcDataArray: LpPriceOhlcDataFormatted[] = []

for (const item of ohlc) {
const time = item.time.getLocalTimestamp()

if (item.basePrice) {
baselinePriceArray.push({
time: item.time.getUTCTimestamp(),
time,
base_price: item.basePrice,
})
}

if (item.oraclePrice) {
oraclePriceArray.push({
time: item.time.getUTCTimestamp(),
time,
value: item.oraclePrice,
})
}

ohlcDataArray.push({
time: item.time.getUTCTimestamp(),
time,
open: item.open,
close: item.close,
high: item.high,
Expand Down Expand Up @@ -504,22 +510,24 @@ const createOhlcChart = (set: SetState<State>, get: GetState<State>) => ({
let ohlcDataArray: LpPriceOhlcDataFormatted[] = []

for (const item of ohlc) {
const time = item.time.getLocalTimestamp()

if (item.basePrice) {
baselinePriceArray.push({
time: item.time.getUTCTimestamp(),
time,
base_price: item.basePrice,
})
}

if (item.oraclePrice) {
oraclePriceArray.push({
time: item.time.getUTCTimestamp(),
time,
value: item.oraclePrice,
})
}

ohlcDataArray.push({
time: item.time.getUTCTimestamp(),
time,
open: item.open,
close: item.close,
high: item.high,
Expand Down
20 changes: 12 additions & 8 deletions apps/main/src/loan/store/createOhlcChartSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,12 @@ const createOhlcChart = (set: SetState<State>, get: GetState<State>) => ({
let ohlcDataArray: LpPriceOhlcDataFormatted[] = []

for (const item of ohlc) {
const time = item.time.getLocalTimestamp()

volumeArray = [
...volumeArray,
{
time: item.time.getUTCTimestamp(),
time,
value: item.volume,
color: item.open < item.close ? '#26a69982' : '#ef53507e',
},
Expand All @@ -144,23 +146,23 @@ const createOhlcChart = (set: SetState<State>, get: GetState<State>) => ({
baselinePriceArray = [
...baselinePriceArray,
{
time: item.time.getUTCTimestamp(),
time,
base_price: item.basePrice,
},
]

oraclePriceArray = [
...oraclePriceArray,
{
time: item.time.getUTCTimestamp(),
time,
value: item.oraclePrice,
},
]

ohlcDataArray = [
...ohlcDataArray,
{
time: item.time.getUTCTimestamp(),
time,
open: item.open,
close: item.close,
high: item.high,
Expand Down Expand Up @@ -223,10 +225,12 @@ const createOhlcChart = (set: SetState<State>, get: GetState<State>) => ({
let ohlcDataArray: LpPriceOhlcDataFormatted[] = []

for (const item of ohlc) {
const time = item.time.getLocalTimestamp()

volumeArray = [
...volumeArray,
{
time: item.time.getUTCTimestamp(),
time,
value: item.volume,
color: item.open < item.close ? '#26a69982' : '#ef53507e',
},
Expand All @@ -235,23 +239,23 @@ const createOhlcChart = (set: SetState<State>, get: GetState<State>) => ({
baselinePriceArray = [
...baselinePriceArray,
{
time: item.time.getUTCTimestamp(),
time,
base_price: item.basePrice,
},
]

oraclePriceArray = [
...oraclePriceArray,
{
time: item.time.getUTCTimestamp(),
time,
value: item.oraclePrice,
},
]

ohlcDataArray = [
...ohlcDataArray,
{
time: item.time.getUTCTimestamp(),
time,
open: item.open,
close: item.close,
high: item.high,
Expand Down
6 changes: 3 additions & 3 deletions packages/prices-api/src/chains/parsers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { chains, type Chain } from '..'
import { toUTC } from '../timestamp'
import { toDate } from '../timestamp'
import type * as Responses from './responses'
import type * as Models from './models'

Expand All @@ -21,7 +21,7 @@ export const parseTxs = (x: Responses.GetTransactionsResponse): Models.Transacti
x.data.flatMap((data) =>
data.transactions.map((tx) => ({
chain: data.chain,
timestamp: toUTC(tx.timestamp),
timestamp: toDate(tx.timestamp),
type: tx.type,
transactions: tx.transactions,
})),
Expand All @@ -31,7 +31,7 @@ export const parseUsers = (x: Responses.GetUsersResponse): Models.Users[] =>
x.data.flatMap((data) =>
data.users.map((tx) => ({
chain: data.chain,
timestamp: toUTC(tx.timestamp),
timestamp: toDate(tx.timestamp),
type: tx.type,
users: tx.users,
})),
Expand Down
14 changes: 7 additions & 7 deletions packages/prices-api/src/crvusd/parsers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { toUTC } from '../timestamp'
import { toDate } from '../timestamp'
import type * as Responses from './responses'
import type * as Models from './models'

Expand Down Expand Up @@ -29,7 +29,7 @@ export const parseMarket = (x: Responses.GetMarketsResponse['data'][number]): Mo
})

export const parseSnapshot = (x: Responses.GetSnapshotsResponse['data'][number]): Models.Snapshot => ({
timestamp: toUTC(x.dt),
timestamp: toDate(x.dt),
rate: x.rate,
nLoans: x.n_loans,
minted: x.minted,
Expand Down Expand Up @@ -59,7 +59,7 @@ export const parseKeeper = (x: Responses.GetKeepersResponse['keepers'][number]):
})

export const parseSupply = (x: Responses.GetSupplyResponse['data'][number]): Models.CrvUsdSupply => ({
timestamp: toUTC(x.timestamp),
timestamp: toDate(x.timestamp),
market: x.market,
supply: x.supply,
borrowable: x.borrowable,
Expand All @@ -69,8 +69,8 @@ export const parseUserMarkets = (x: Responses.GetUserMarketsResponse): Models.Us
x.markets.map((market) => ({
collateral: market.collateral,
controller: market.controller,
snapshotFirst: toUTC(market.first_snapshot),
snapshotLast: toUTC(market.last_snapshot),
snapshotFirst: toDate(market.first_snapshot),
snapshotLast: toDate(market.last_snapshot),
}))

export const parseUserMarketStats = (x: Responses.GetUserMarketStatsResponse) => ({
Expand All @@ -89,7 +89,7 @@ export const parseUserMarketStats = (x: Responses.GetUserMarketStatsResponse) =>
lossPct: x.loss_pct,
oraclePrice: x.oracle_price,
blockNumber: x.block_number,
timestamp: toUTC(x.timestamp),
timestamp: toDate(x.timestamp),
})

export const parseUserMarketSnapshots = (x: Responses.GetUserMarketSnapshotsResponse): Models.UserMarketSnapshots =>
Expand All @@ -106,7 +106,7 @@ export const parseUserCollateralEvents = (
totalBorrowed: x.total_borrowed,
totalBorrowedPrecise: x.total_borrowed_precise,
events: x.data.map((y) => ({
timestamp: toUTC(y.dt),
timestamp: toDate(y.dt),
txHash: y.transaction_hash,
type: y.type,
user: y.user,
Expand Down
Loading