-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
021764f
commit f1d5337
Showing
10 changed files
with
147 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import useSWR from "swr"; | ||
|
||
import { COINGECKO_API_ENDPOINT } from "../../constants"; | ||
|
||
const fetcher = (url: string) => | ||
fetch(COINGECKO_API_ENDPOINT + url).then((res) => res.json()); | ||
|
||
const fetcherWithTransform = (transform: Function) => (url: string) => | ||
fetcher(url).then((data) => transform(data)); | ||
|
||
export const useOneNearUsdPrice = () => | ||
useSWR( | ||
"/simple/price?ids=near&vs_currencies=usd", | ||
fetcherWithTransform((response: any) => response.near.usd), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * as coingecko from "./hooks"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import Big from "big.js"; | ||
|
||
import formatWithCommas from "./formatWithCommas"; | ||
import { NEAR_DEFAULT_TOKEN_DECIMALS } from "../constants"; | ||
|
||
export const yoctosToNear = (amountYoctos: string, abbreviate?: boolean) => { | ||
return ( | ||
formatWithCommas(Big(amountYoctos).div(1e24).toFixed(2)) + | ||
(abbreviate ? "N" : " NEAR") | ||
); | ||
}; | ||
|
||
export const bigNumToFloat = (amount: string, decimals: number) => { | ||
const decimalMultiplier = Big(10).pow(decimals); | ||
return parseFloat(Big(amount).div(decimalMultiplier).toFixed(2)); | ||
}; | ||
|
||
export const yoctoNearToFloat = (amountYoctoNear: string) => | ||
bigNumToFloat(amountYoctoNear, NEAR_DEFAULT_TOKEN_DECIMALS); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from "./_address"; | ||
export * from "./yoctosToNear"; | ||
export * from "./converters"; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useMemo } from "react"; | ||
|
||
import { coingecko } from "@/common/api/coingecko"; | ||
import { NEAR_DEFAULT_TOKEN_DECIMALS } from "@/common/constants"; | ||
import { bigNumToFloat } from "@/common/lib"; | ||
import formatWithCommas from "@/common/lib/formatWithCommas"; | ||
|
||
export const useYoctoNearUsdDisplayValue = ( | ||
amountYoctoNear: string, | ||
): string => { | ||
const { data: oneNearUsdPrice } = coingecko.useOneNearUsdPrice(); | ||
|
||
return useMemo( | ||
() => | ||
`~$ ${formatWithCommas( | ||
(oneNearUsdPrice | ||
? bigNumToFloat(amountYoctoNear, NEAR_DEFAULT_TOKEN_DECIMALS) * | ||
oneNearUsdPrice | ||
: 0.0 | ||
).toString(), | ||
)}`, | ||
|
||
[amountYoctoNear, oneNearUsdPrice], | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters