Skip to content

Commit 3579f4c

Browse files
committed
enh(index.d.ts,index.ts): export getFIATValue & enhance type docs
1 parent d7ad1e9 commit 3579f4c

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

lib/index.d.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,16 @@ export function useMetamask(
3939
): UseMatamaskAPI
4040
/**
4141
* Fetch token price in USD,JPY & EUR from CoinGecko public api
42-
* @param coinGeckoID API ID, like `bitcoin`
42+
* @param coinGeckoID API ID, like "bitcoin"
4343
* @returns [{ usd, jpy, eur }, triggerReFetchFn]
44+
* @example
45+
* const [btcPrice, reFetchFn] = useTokenPrice("bitcoin")
46+
* console.log(btcPrice.usd) // Math.random() * 45_000
4447
*/
4548
export function useTokenPrice(
4649
coinGeckoID: string,
4750
refreshInternvalInSecs?: number
48-
): [FIATCurrencies, /** Triggers a re-fetch to the API */ VoidCallback]
51+
): [FIATCurrencies, VoidCallback]
4952
export function addEtherNetwork(props: AddEtherNetwork): Promise<null>
5053
export function addEtherToken(props: AddEtherToken): Promise<null>
5154
export function getMetamaskProvider(): Metamask | null
@@ -58,9 +61,21 @@ export function switchOrAppendNetwork(props: AddEtherNetwork): Promise<null>
5861
export function sendEther(props: SendMethodProps): Promise<string>
5962
export function connectToMetamask(): Promise<string>
6063
export function formatEther(balance: number): string
61-
export function getFIATBalance(
64+
/**
65+
* Returns a formatted string with a token FIAT value
66+
* @param tokenBalance The amount of tokens to calculate
67+
* @param tokenPrice The current token price per unit
68+
* @param currency The currency token price is matched to
69+
* @example
70+
* const amount = 12
71+
* const priceInUSD = 200
72+
* const formattedValue = getFIATValue(amount, priceInUSD) // "2,400"
73+
*/
74+
export function getFIATValue(
6275
tokenBalance: number | string,
63-
tokenPrice: number
64-
): number
76+
tokenPrice: number | string,
77+
/** Define a currency for `tokenPrice`. Defaults to "USD" */
78+
currency?: "USD" | "JPY" | "EUR"
79+
): string
6580
export const metamaskRequest: OnRequest["request"]
6681
export const parse: Parse

lib/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ export {
1010
sendEther,
1111
parse,
1212
connectToMetamask,
13-
getFIATBalance,
13+
getFIATValue,
1414
formatEther,
1515
} from "./utils"

lib/utils/index.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import internals from "./internals"
66
const { withError, runIfMetamask, switchNetwork } = internals
77

88
export { default as parse } from "./parse"
9-
9+
export { default as getFIATValue } from "./getFIATValue"
1010
export const getMetamaskProvider = internals.getMetamaskProvider
1111

1212
export const addEtherToken = ({
@@ -135,10 +135,12 @@ export const connectToMetamask = () => {
135135
}
136136

137137
export const formatEther = (balance: number) => {
138-
const result = balance < 0 ? balance.toPrecision(2) : balance.toFixed(3)
139-
return `${parseFloat(result)}`
140-
}
141-
142-
export const getFIATBalance = (balance, price) => {
143-
return parseFloat((balance * price).toFixed(2))
138+
const min = 1e-4
139+
if (balance < min && balance > 0) {
140+
return `${min}`
141+
}
142+
if (balance < 1) {
143+
return `${balance}`
144+
}
145+
return `${parseFloat(balance.toFixed(4))}`
144146
}

0 commit comments

Comments
 (0)