Skip to content

Commit

Permalink
fix display
Browse files Browse the repository at this point in the history
  • Loading branch information
hazae41 committed Nov 2, 2024
1 parent abbca05 commit 98ae602
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 56 deletions.
18 changes: 16 additions & 2 deletions src/mods/background/service_worker/entities/tokens/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,22 @@ export namespace BgToken {
}

export function schema(account: ZeroHexString, block: string, context: BgEthereumContext, storage: IDBQueryStorage) {
const fetcher = async (request: RpcRequestPreinit<unknown>, more: FetcherMore) =>
await BgEthereumContext.fetchOrFail<ZeroHexString>(context, request, more).then(f => f.mapSync(x => new ZeroHexFixedInit(x, context.chain.token.decimals)))
const fetcher = async (request: RpcRequestPreinit<unknown>, more: FetcherMore) => {
try {
const fetched = await BgEthereumContext.fetchOrFail<ZeroHexString>(context, request, more)

if (fetched.isErr())
return fetched
if (!ZeroHexString.Unknown.is(fetched.get()))
throw new Error("Invalid response")

const fixed = new ZeroHexFixedInit(fetched.get(), context.chain.token.decimals)

return new Data(fixed)
} catch (e: unknown) {
return new Fail(Catched.wrap(e))
}
}

const indexer = async (states: States<Data, Fail>) => {
if (block !== "pending")
Expand Down
18 changes: 16 additions & 2 deletions src/mods/foreground/entities/tokens/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,22 @@ export namespace FgToken {
if (block == null)
return

const fetcher = async (request: RpcRequestPreinit<unknown>, more: FetcherMore = {}) =>
await fetchOrFail<ZeroHexString>(request, context).then(f => f.mapSync(x => new ZeroHexFixedInit(x, context.chain.token.decimals)))
const fetcher = async (request: RpcRequestPreinit<unknown>, more: FetcherMore = {}) => {
try {
const fetched = await fetchOrFail<ZeroHexString>(request, context)

if (fetched.isErr())
return fetched
if (!ZeroHexString.Unknown.is(fetched.get()))
throw new Error("Invalid response")

const fixed = new ZeroHexFixedInit(fetched.get(), context.chain.token.decimals)

return new Data(fixed)
} catch (e: unknown) {
return new Fail(Catched.wrap(e))
}
}

const indexer = async (states: States<Data, Fail>) => {
if (block !== "pending")
Expand Down
4 changes: 2 additions & 2 deletions src/mods/foreground/entities/wallets/card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { useEnsReverseNoFetch } from "../names/data"
import { useTotalWalletPricedBalance } from "../unknown/data"
import { useWalletDataContext } from "./context"
import { useEthereumContext } from "./data"
import { useCompactDisplayUsdOrZeroOrError } from "./page"
import { useCompactDisplayUsd } from "./page"

export function RawWalletDataCard(props: { index?: number } & { href?: string } & { privateKey?: string } & { flip?: boolean } & { unflip?: () => void }) {
const wallet = useWalletDataContext().getOrThrow()
Expand Down Expand Up @@ -59,7 +59,7 @@ export function RawWalletCard(props: { type?: WalletData["type"] } & { uuid: str
const onClickCopyEthereumAddress = useMouseCancel(copyEthereumAddress.run)

const totalBalanceQuery = useTotalWalletPricedBalance(finalAddress, "usd")
const totalBalanceDisplay = useCompactDisplayUsdOrZeroOrError(totalBalanceQuery.current)
const totalBalanceDisplay = useCompactDisplayUsd(totalBalanceQuery.current)

const [preflip = false, setPreflip] = useState(flip)
const [postflip, setPostflip] = useState(false)
Expand Down
83 changes: 35 additions & 48 deletions src/mods/foreground/entities/wallets/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,63 +49,50 @@ export function WalletPage(props: UUIDProps) {
</WalletDataProvider>
}

export function useDisplay(result: Nullable<Result<Fixed.From, Error>>) {
export function useDisplayRaw(result: Nullable<Result<Fixed.From, Error>>) {
return useMemo(() => {
if (result == null)
return "0.00"
if (result.isErr())
return "0.00"
const number = Number(Fixed.from(result.getOrThrow()).move(5).toString())
return "???"
return result.andThenSync(fixed => Result.runAndDoubleWrapSync(() => {
const fixed5 = Fixed.from(fixed).move(5)
const float = Number(fixed5.toString())

return number.toLocaleString(undefined)
return float.toLocaleString(undefined)
})).getOr("Error")
}, [result])
}

export function useDisplayUsdOrNull(result: Nullable<Result<Fixed.From, Error>>) {
export function useDisplayUsd(result: Nullable<Result<Fixed.From, Error>>) {
return useMemo(() => {
if (result == null)
return
if (result.isErr())
return
const number = Number(Fixed.from(result.getOrThrow()).move(2).toString())

return number.toLocaleString(undefined, {
style: "currency",
currency: "USD",
notation: "standard"
})
}, [result])
}
return "???"
return result.andThenSync(fixed => Result.runAndDoubleWrapSync(() => {
const fixed2 = Fixed.from(fixed).move(2)
const float = Number(fixed2.toString())

export function useDisplayUsdOrZeroOrError(result: Nullable<Result<Fixed.From, Error>>) {
return useMemo(() => {
if (result == null)
return "0.00"
if (result.isErr())
return "Error"
const number = Number(Fixed.from(result.getOrThrow()).move(2).toString())

return number.toLocaleString(undefined, {
style: "currency",
currency: "USD",
notation: "standard"
})
const style = "currency"
const currency = "USD"
const notation = "standard"

return float.toLocaleString(undefined, { style, currency, notation })
})).getOr("Error")
}, [result])
}

export function useCompactDisplayUsdOrZeroOrError(result: Nullable<Result<Fixed.From, Error>>) {
export function useCompactDisplayUsd(result: Nullable<Result<Fixed.From, Error>>) {
return useMemo(() => {
if (result == null)
return "0.00"
if (result.isErr())
return "Error"
const number = Number(Fixed.from(result.getOrThrow()).move(2).toString())

return number.toLocaleString(undefined, {
style: "currency",
currency: "USD",
notation: "compact"
})
return "???"
return result.andThenSync(fixed => Result.runAndDoubleWrapSync(() => {
const fixed2 = Fixed.from(fixed).move(2)
const float = Number(fixed2.toString())

const style = "currency"
const currency = "USD"
const notation = "compact"

return float.toLocaleString(undefined, { style, currency, notation })
})).getOr("Error")
}, [result])
}

Expand Down Expand Up @@ -596,10 +583,10 @@ function NativeTokenRow(props: { token: NativeTokenData } & { chain: ChainData }
const [prices, setPrices] = useState(new Array<Nullable<Fixed.From>>(token.pairs?.length ?? 0))

const balanceQuery = useNativeBalance(wallet.address, "pending", context, prices)
const balanceDisplay = useDisplay(balanceQuery.current)
const balanceDisplay = useDisplayRaw(balanceQuery.current)

const balanceUsdFixed = useNativePricedBalance(wallet.address, "usd", context)
const balanceUsdDisplay = useDisplayUsdOrNull(balanceUsdFixed.current)
const balanceUsdDisplay = useDisplayUsd(balanceUsdFixed.current)

const onPrice = useCallback(([index, data]: [number, Nullable<Fixed.From>]) => {
setPrices(prices => {
Expand Down Expand Up @@ -696,10 +683,10 @@ function ContractTokenRow(props: { token: ContractTokenData } & { chain: ChainDa
const [prices, setPrices] = useState(new Array<Nullable<Fixed.From>>(token.pairs?.length ?? 0))

const balanceQuery = useContractBalance(wallet.address, token, "pending", context, prices)
const balanceDisplay = useDisplay(balanceQuery.current)
const balanceDisplay = useDisplayRaw(balanceQuery.current)

const balanceUsdFixed = useContractPricedBalance(wallet.address, token, "usd", context)
const balanceUsdDisplay = useDisplayUsdOrNull(balanceUsdFixed.current)
const balanceUsdDisplay = useDisplayUsd(balanceUsdFixed.current)

const onPrice = useCallback(([index, data]: [number, Nullable<Fixed.From>]) => {
setPrices(prices => {
Expand Down Expand Up @@ -749,7 +736,7 @@ export function PriceResolver(props: { index: number } & { address: string } & O
return null
}

function ClickableTokenRow(props: { token: TokenData } & { chain: ChainData } & { balanceDisplay: string } & { balanceUsdDisplay?: string } & AnchorProps) {
function ClickableTokenRow(props: { token: TokenData } & { chain: ChainData } & { balanceDisplay: string } & { balanceUsdDisplay: string } & AnchorProps) {
const { token, chain, balanceDisplay, balanceUsdDisplay, ...others } = props

const tokenId = token.type === "native"
Expand Down
4 changes: 2 additions & 2 deletions src/mods/foreground/home/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { useBackgroundContext } from "@/mods/foreground/background/context"
import { useCallback, useEffect, useState } from "react"
import { useTotalPricedBalance } from "../entities/unknown/data"
import { useUserContext } from "../entities/users/context"
import { useDisplayUsdOrZeroOrError } from "../entities/wallets/page"
import { useDisplayUsd } from "../entities/wallets/page"

export function HomePage() {
const userData = useUserContext().getOrThrow()
const background = useBackgroundContext().getOrThrow()

const totalPricedBalanceQuery = useTotalPricedBalance("usd")
const totalPricedBalanceDisplay = useDisplayUsdOrZeroOrError(totalPricedBalanceQuery.current)
const totalPricedBalanceDisplay = useDisplayUsd(totalPricedBalanceQuery.current)

useEffect(() => {
background.requestOrThrow({
Expand Down

0 comments on commit 98ae602

Please sign in to comment.