Skip to content

Commit

Permalink
Refactor for showing token balance
Browse files Browse the repository at this point in the history
  • Loading branch information
kkosiorowska committed Dec 4, 2023
1 parent 1da9e86 commit 816f7e4
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 69 deletions.
30 changes: 0 additions & 30 deletions dapp/src/components/AssetBalance/index.tsx

This file was deleted.

33 changes: 16 additions & 17 deletions dapp/src/components/Modals/StakeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,25 @@ import {
} from "@chakra-ui/react"
import { useStakingFlowContext } from "../../hooks"
import BaseModal from "./BaseModal"
import { BITCOIN, USD } from "../../constants"
import { AssetBalance } from "../AssetBalance"
import { TokenBalance } from "../TokenBalance"
import { BITCOIN } from "../../constants"

function StakeDetails({
text,
amount,
usdAmount,
tokenBalance,
usdBalance,
}: {
text: string
amount: string
usdAmount?: string
tokenBalance: string
usdBalance: string
}) {
return (
<Flex justifyContent="space-between">
<Text>{text}</Text>
<AssetBalance
asset={{ amount, currency: BITCOIN }}
currencyAsset={
usdAmount ? { amount: usdAmount, currency: USD } : undefined
}
<TokenBalance
tokenBalance={tokenBalance}
currency={BITCOIN}
usdBalance={usdBalance}
/>
</Flex>
)
Expand Down Expand Up @@ -65,18 +64,18 @@ export default function ActionModal() {
{/* TODO: Use the real data */}
<StakeDetails
text="Amount to be staked"
amount="1.75"
usdAmount="45.725,91"
tokenBalance="179776555"
usdBalance="45.725,91"
/>
<StakeDetails
text="Protocol fee (0.01%)"
amount="0.0020"
usdAmount="0.024,91"
tokenBalance="20"
usdBalance="0.024,91"
/>
<StakeDetails
text="Approximately staked tokens"
amount="1.74"
usdAmount="44.762,21"
tokenBalance="13456775"
usdBalance="44.762,21"
/>
</Flex>
</Flex>
Expand Down
18 changes: 6 additions & 12 deletions dapp/src/components/Navbar/ConnectWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import {
} from "@chakra-ui/react"
import { Account } from "@ledgerhq/wallet-api-client"
import { Bitcoin, Ethereum, Info } from "../../static/icons"
import { BITCOIN } from "../../constants"
import {
useRequestBitcoinAccount,
useRequestEthereumAccount,
useWalletContext,
} from "../../hooks"
import { formatSatoshiAmount, truncateAddress } from "../../utils"
import { AssetBalance } from "../AssetBalance"
import { truncateAddress } from "../../utils"
import { BITCOIN } from "../../constants"
import { TokenBalance } from "../TokenBalance"

export type ConnectButtonsProps = {
leftIcon: typeof Icon
Expand Down Expand Up @@ -63,15 +63,9 @@ export default function ConnectWallet() {
<HStack>
<HStack mr="16px">
<Text>Balance</Text>
<AssetBalance
// TODO: This value should be calculated after the account is connected and stored in the context or redux.
asset={{
amount:
!btcAccount || btcAccount?.balance.isZero()
? "0.00"
: formatSatoshiAmount(btcAccount.balance.toString()),
currency: BITCOIN,
}}
<TokenBalance
tokenBalance={btcAccount?.balance.toString() ?? "0"}
currency={BITCOIN}
/>
</HStack>
<ConnectButton
Expand Down
12 changes: 7 additions & 5 deletions dapp/src/components/Overview/PositionDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import {
useColorModeValue,
Flex,
} from "@chakra-ui/react"
import { BITCOIN, USD } from "../../constants"
import { BITCOIN } from "../../constants"
import { Info } from "../../static/icons"
import { useStakingFlowContext } from "../../hooks"
import Staking from "../Staking"
import { AssetBalance } from "../AssetBalance"
import { TokenBalance } from "../TokenBalance"

export default function PositionDetails() {
const { setModalType } = useStakingFlowContext()
Expand All @@ -29,9 +29,11 @@ export default function PositionDetails() {
</Tooltip>
</Flex>
<Flex direction="column" alignItems="flex-start">
<AssetBalance
asset={{ amount: "34.75", currency: BITCOIN }}
currencyAsset={{ amount: "1.245.148,1", currency: USD }}
{/* TODO: Use the real data */}
<TokenBalance
tokenBalance="132231212"
currency={BITCOIN}
usdBalance="1.245.148,1"
alignItems="start"
/>
</Flex>
Expand Down
38 changes: 38 additions & 0 deletions dapp/src/components/TokenBalance/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from "react"
import { HStack, Text, Flex } from "@chakra-ui/react"
import { Currency } from "../../types"
import { formatTokenAmount } from "../../utils"
import { USD } from "../../constants"

type TokenBalanceProps = {
currency: Currency
tokenBalance: string | number
usdBalance?: string
desiredDecimals?: number
alignItems?: "end" | "start"
}

export function TokenBalance({
currency,
tokenBalance,
usdBalance,
desiredDecimals = 2,
alignItems = "end",
}: TokenBalanceProps) {
return (
<Flex direction="column" alignItems={alignItems}>
<HStack>
<Text>
{formatTokenAmount(tokenBalance, currency.decimals, desiredDecimals)}
</Text>
<Text>{currency.symbol}</Text>
</HStack>
{usdBalance && (
<HStack color="gray.500">
<Text>{usdBalance}</Text>
<Text>{USD.symbol}</Text>
</HStack>
)}
</Flex>
)
}
5 changes: 0 additions & 5 deletions dapp/src/types/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,3 @@ export type Currency = {
symbol: string
decimals: number
}

export type Asset = {
amount: string
currency: Currency
}
4 changes: 4 additions & 0 deletions dapp/src/utils/numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export function bigIntToUserAmount(
fixedPointDecimals: number,
desiredDecimals = 2,
): string {
if (fixedPoint === BigInt(0)) {
return `0.${"0".repeat(desiredDecimals)}`
}

const fixedPointDesiredDecimalsAmount =
fixedPoint /
10n ** BigInt(Math.max(1, fixedPointDecimals - desiredDecimals))
Expand Down

0 comments on commit 816f7e4

Please sign in to comment.