Skip to content

Commit

Permalink
Merge pull request #940 from hemilabs/add-prices-your-stake
Browse files Browse the repository at this point in the history
Add usd value on 'Your Stake' card
  • Loading branch information
gndelia authored Mar 3, 2025
2 parents 21ed27e + 51092a2 commit a01d298
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import Big from 'big.js'
import { Card } from 'components/card'
import { useTokenPrices } from 'hooks/useTokenPrices'
import Image, { StaticImageData } from 'next/image'
import { useTranslations } from 'next-intl'
import { ReactNode } from 'react'
import { formatFiatNumber } from 'utils/format'
import { getTokenPrice } from 'utils/token'
import { formatUnits } from 'viem'

import { useStakePositions } from '../../../_hooks/useStakedBalance'

import communityIcon from './icons/community.svg'
import stakeIcon from './icons/stake.svg'
Expand Down Expand Up @@ -86,14 +93,34 @@ export const TotalStaked = function () {
}

export const YourStake = function () {
const { data: prices, isPending: loadingPrices } = useTokenPrices()
const { loading: loadingPosition, tokensWithPosition } = useStakePositions()
const t = useTranslations('stake-page.dashboard')
// TODO TBD how to load this data https://github.com/hemilabs/ui-monorepo/issues/750
const points = '$ 962.163'

const getPosition = function () {
if (prices === undefined) {
// if Prices API is missing, return "-"
return '-'
}
const userPosition = tokensWithPosition.reduce(
(acc, staked) =>
acc.plus(
Big(formatUnits(staked.balance, staked.decimals)).times(
getTokenPrice(staked, prices),
),
),
Big(0),
)
return formatFiatNumber(userPosition.toFixed())
}

return (
<Container>
<div className="flex flex-shrink-0 flex-col gap-y-3 p-6">
<Heading heading={t('your-stake')} />
<Points points={points} />
<Points
points={loadingPosition || loadingPrices ? '...' : getPosition()}
/>
</div>
<Image
alt="Stake icon"
Expand Down
7 changes: 2 additions & 5 deletions webapp/components/fiatBalance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Skeleton from 'react-loading-skeleton'
import { type BtcToken, type EvmToken, type Token } from 'types/token'
import { formatFiatNumber } from 'utils/format'
import { isNativeToken } from 'utils/nativeToken'
import { isEvmToken } from 'utils/token'
import { getTokenPrice, isEvmToken } from 'utils/token'
import { formatUnits } from 'viem'

import { ErrorBoundary } from './errorBoundary'
Expand Down Expand Up @@ -37,10 +37,7 @@ const RenderFiatBalanceUnsafe = function ({

const stringBalance = formatUnits(balance, token.decimals)

const priceSymbol = (
token.extensions?.priceSymbol ?? token.symbol
).toUpperCase()
const price = data?.[priceSymbol] ?? '0'
const price = getTokenPrice(token, data)

const mergedFetchStatuses = function () {
const fetchStatuses = [fetchStatus, tokenPricesFetchStatus]
Expand Down
15 changes: 15 additions & 0 deletions webapp/test/utils/token.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
getTokenPrice,
isStakeToken,
isTunnelToken,
isEvmToken,
Expand All @@ -7,6 +8,20 @@ import {
import { describe, expect, it } from 'vitest'

describe('utils/token', function () {
describe('getTokenPrice', function () {
it('should return the price based in the token symbol', function () {
const token = { symbol: 'usdt' }
const prices = { USDT: '0.99' }
expect(getTokenPrice(token, prices)).toBe('0.99')
})

it('should return the price based in the token priceSymbol if defined', function () {
const token = { extensions: { priceSymbol: 'usdt' }, symbol: 'usdt.e' }
const prices = { USDT: '0.99' }
expect(getTokenPrice(token, prices)).toBe('0.99')
})
})

describe('isEvmToken', function () {
it('should return true if token is an EVM token', function () {
const token = {
Expand Down
11 changes: 11 additions & 0 deletions webapp/utils/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,14 @@ export const isTunnelToken = (token: Token) => token.extensions?.tunnel === true

export const tunnelsThroughPartner = (token: Token) =>
token.extensions?.tunnelPartner !== undefined

export const getTokenPrice = function (
token: Token,
prices: Record<string, string>,
) {
const priceSymbol = (
token.extensions?.priceSymbol ?? token.symbol
).toUpperCase()
const price = prices?.[priceSymbol] ?? '0'
return price
}

0 comments on commit a01d298

Please sign in to comment.