Skip to content

Commit

Permalink
Create a separate type for the currency kind
Browse files Browse the repository at this point in the history
  • Loading branch information
kkosiorowska committed Dec 14, 2023
1 parent 4d01503 commit 4cd2ef6
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
3 changes: 1 addition & 2 deletions dapp/src/components/Header/ConnectWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from "react"
import { Button, HStack, Icon } from "@chakra-ui/react"
import { Account } from "@ledgerhq/wallet-api-client"
import { Bitcoin, Ethereum } from "../../static/icons"
import { BITCOIN } from "../../constants"
import {
useRequestBitcoinAccount,
useRequestEthereumAccount,
Expand Down Expand Up @@ -47,7 +46,7 @@ export default function ConnectWallet() {
<HStack display={{ base: "none", md: "flex" }}>
<TextMd color="grey.500">Balance</TextMd>
<CurrencyBalance
currency={BITCOIN}
currencyType="bitcoin"
amount={btcAccount?.balance.toString()}
/>
</HStack>
Expand Down
5 changes: 2 additions & 3 deletions dapp/src/components/Overview/PositionDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
HStack,
CardProps,
} from "@chakra-ui/react"
import { BITCOIN, USD } from "../../constants"
import { Info } from "../../static/icons"
import { TokenBalance } from "../shared/TokenBalance"
import { TextMd } from "../shared/Typography"
Expand All @@ -27,12 +26,12 @@ export default function PositionDetails(props: CardProps) {
</HStack>
<TokenBalance
token={{
currency: BITCOIN,
currencyType: "bitcoin",
amount: "2398567898",
variant: "greater-balance",
}}
fiatCurrency={{
currency: USD,
currencyType: "usd",
amount: 419288.98,
shouldBeFormatted: false,
size: "lg",
Expand Down
9 changes: 6 additions & 3 deletions dapp/src/components/shared/CurrencyBalance/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React, { useMemo } from "react"
import { Box, useMultiStyleConfig } from "@chakra-ui/react"
import { formatTokenAmount, toLocaleString } from "../../../utils"
import { Currency } from "../../../types"
import { CurrencyType } from "../../../types"
import { CURRENCIES_BY_TYPE } from "../../../constants"

export type CurrencyBalanceProps = {
currency: Currency
currencyType: CurrencyType
amount?: string | number
shouldBeFormatted?: boolean
desiredDecimals?: number
Expand All @@ -13,7 +14,7 @@ export type CurrencyBalanceProps = {
}

export function CurrencyBalance({
currency,
currencyType,
amount,
shouldBeFormatted = true,
desiredDecimals = 2,
Expand All @@ -22,6 +23,8 @@ export function CurrencyBalance({
}: CurrencyBalanceProps) {
const styles = useMultiStyleConfig("CurrencyBalance", { size, variant })

const currency = CURRENCIES_BY_TYPE[currencyType]

const balance = useMemo(() => {
if (shouldBeFormatted)
return formatTokenAmount(amount ?? 0, currency.decimals, desiredDecimals)
Expand Down
8 changes: 7 additions & 1 deletion dapp/src/constants/currency.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Currency } from "../types"
import { Currency, CurrencyType } from "../types"

export const BITCOIN: Currency = {
name: "Bitcoin",
Expand All @@ -23,3 +23,9 @@ export const CURRENCY_ID_BITCOIN =

export const CURRENCY_ID_ETHEREUM =
import.meta.env.VITE_USE_TESTNET === "true" ? "ethereum_goerli" : "ethereum"

export const CURRENCIES_BY_TYPE: Record<CurrencyType, Currency> = {
bitcoin: BITCOIN,
ethereum: ETHEREUM,
usd: ETHEREUM,
}
2 changes: 2 additions & 0 deletions dapp/src/types/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export type Currency = {
symbol: string
decimals: number
}

export type CurrencyType = "bitcoin" | "ethereum" | "usd"

0 comments on commit 4cd2ef6

Please sign in to comment.