Skip to content

Commit

Permalink
Feat/improve send token (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
rustin01 authored Oct 20, 2024
1 parent d1ea9ae commit e0beb04
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 27 deletions.
2 changes: 1 addition & 1 deletion apps/wallet/src/components/TokenList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const TokenListItem: FC<{ token: RootAssetInfo }> = ({ token }) => {
>
<TokenIcon token={token} />
<div className="flex-1 flex flex-col items-end">
{balanceQuery.isFetching ? (
{balanceQuery.isLoading ? (
<span className="loading loading-spinner loading-sm"></span>
) : (
<span className="text-sm">{balanceQuery.data?.toString() ?? '0'}</span>
Expand Down
24 changes: 4 additions & 20 deletions apps/wallet/src/pages/send-token/confirm-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import BigNumber from "bignumber.js";
import toaster from "../../components/Toaster";
import { useMutation, useQuery } from "@tanstack/react-query";
import CopyButton from "../../components/CopyButton";
import { sendTokenStore } from "./store";
import { sendTokenStore, useFeeQuery } from "./store";
import { formatNumber } from "../../utils/formatter";
import { ChainAssetType } from "../../utils/basicTypes";
import { getChainTxLink } from "../../utils/link";
Expand All @@ -33,20 +33,7 @@ const SendTokenConfirmPage: FC = observer(() => {
enabled: !!tokenListQuery.data
})
const nativeBalanceQuery = useTokenBalanceQuery(nativeTokenQuery.data || undefined)
const feeQuery = useQuery({
queryKey: ['estimatedFee', state],
queryFn: async () => {
if (!hibitIdSession.walletPool || !state.token) {
return null
}
return await hibitIdSession.walletPool.getEstimatedFee(
state.toAddress,
new BigNumber(state.amount),
state.token,
)
},
refetchInterval: 5000,
})
const feeQuery = useFeeQuery(state.toAddress, state.amount, state.token)

const minNativeBalance = useMemo(() => {
if (!feeQuery.data || !state.token) {
Expand All @@ -55,7 +42,7 @@ const SendTokenConfirmPage: FC = observer(() => {
if (state.token.chainAssetType.equals(ChainAssetType.Native)) {
return new BigNumber(state.amount).plus(feeQuery.data)
} else {
return feeQuery.data
return hibitIdSession.chainInfo.feeTokenType === 'native' ? feeQuery.data : new BigNumber(0)
}
}, [state, feeQuery.data])

Expand Down Expand Up @@ -180,12 +167,9 @@ const SendTokenConfirmPage: FC = observer(() => {
</div>
<div className="flex items-center justify-between font-bold">
<span className="text-primary text-sm">
{!feeQuery.isPending ? (
{!feeQuery.isLoading ? (
<span className="flex items-center gap-2">
<span>~{formatNumber(feeQuery.data)}</span>
{feeQuery.isFetching && (
<span className="loading loading-spinner size-4" />
)}
</span>
) : (
<span className="loading loading-spinner size-4" />
Expand Down
10 changes: 7 additions & 3 deletions apps/wallet/src/pages/send-token/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { walletAddressValidate } from "../../utils/validator";
import { yupResolver } from "@hookform/resolvers/yup";
import { Controller, useForm } from "react-hook-form";
import { SYSTEM_MAX_DECIMALS } from "../../utils/formatter/numberFormatter";
import { sendTokenStore } from "./store";
import { sendTokenStore, useFeeQuery } from "./store";
import PageHeader from "../../components/PageHeader";
import { useTranslation } from "react-i18next";

Expand Down Expand Up @@ -60,6 +60,7 @@ const SendTokenPage: FC = observer(() => {
control,
handleSubmit,
formState: { errors },
watch,
} = useForm({
defaultValues: {
toAddress: state.toAddress || '',
Expand All @@ -68,6 +69,9 @@ const SendTokenPage: FC = observer(() => {
resolver: yupResolver(formSchema),
mode: 'onChange'
})
const values = watch()

useFeeQuery(values.toAddress, values.amount, token)

useEffect(() => {
if (state.token) {
Expand All @@ -84,7 +88,7 @@ const SendTokenPage: FC = observer(() => {
setState({
toAddress,
token,
amount
amount,
})
navigate('/send/confirm')
})
Expand Down Expand Up @@ -127,7 +131,7 @@ const SendTokenPage: FC = observer(() => {
}}
>
{t('common_max')}:
{balanceQuery.isFetching ? (
{balanceQuery.isLoading ? (
<span className="loading loading-spinner loading-xs"></span>
) : (
<span className="mx-1">{formatNumber(balanceQuery.data || 0)}</span>
Expand Down
21 changes: 21 additions & 0 deletions apps/wallet/src/pages/send-token/store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import BigNumber from "bignumber.js";
import { RootAssetInfo } from "../../apis/models";
import { makeAutoObservable } from "mobx";
import { useQuery } from "@tanstack/react-query";
import hibitIdSession from "../../stores/session";

export interface SendTokenState {
token: RootAssetInfo | null
Expand Down Expand Up @@ -32,3 +35,21 @@ export class SendTokenStore {
}

export const sendTokenStore = new SendTokenStore()

export const useFeeQuery = (toAddress: string, amount: string, token: RootAssetInfo | null) => {
return useQuery({
queryKey: ['estimatedFee', toAddress, amount, token],
queryFn: async () => {
if (!hibitIdSession.walletPool || !token) {
return null
}
return await hibitIdSession.walletPool.getEstimatedFee(
toAddress,
new BigNumber(amount),
token,
)
},
staleTime: 10000,
refetchInterval: 5000,
})
}
2 changes: 1 addition & 1 deletion apps/wallet/src/pages/token-detail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const TokenDetailPage: FC = observer(() => {
</div>
<div className="flex flex-col items-center">
<span className="text-2xl">
{balanceQuery.isFetching ? (
{balanceQuery.isLoading ? (
<span className="loading loading-spinner loading-sm"></span>
) : (
<span>{`${formatNumber(balanceQuery.data)} ${token.assetSymbol}`}</span>
Expand Down
15 changes: 13 additions & 2 deletions apps/wallet/src/utils/chain/chain-wallets/dfinity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class DfinityChainWallet extends BaseChainWallet {
private identity: Secp256k1KeyIdentity | null = null
private agent: HttpAgent | null = null
private readyPromise: Promise<void>
private feeCache: Record<string | 'native', BigNumber> = {}

constructor(chainInfo: ChainInfo, phrase: string) {
if (!chainInfo.chainId.type.equals(Chain.Dfinity)) {
Expand Down Expand Up @@ -122,17 +123,27 @@ export class DfinityChainWallet extends BaseChainWallet {

// native
if (assetInfo.chainAssetType.equals(ChainAssetType.Native)) {
if (this.feeCache['native']) {
return this.feeCache['native']
}
const ledger = this.getIcpLedger()
const fee = await ledger.transactionFee()
return new BigNumber(String(fee)).shiftedBy(-assetInfo.decimalPlaces.value)
const result = new BigNumber(String(fee)).shiftedBy(-assetInfo.decimalPlaces.value)
this.feeCache['native'] = result
return result
}

// ICRC
if (assetInfo.chainAssetType.equals(ChainAssetType.ICRC1)) {
if (this.feeCache[assetInfo.contractAddress]) {
return this.feeCache[assetInfo.contractAddress]
}
const ledger = this.getIcrcLedger(assetInfo.contractAddress)
const fee = await ledger.transactionFee({})
const decimals = assetInfo.decimalPlaces?.value ?? (await this.getIcrcDecimals(ledger))
return new BigNumber(String(fee)).shiftedBy(-decimals)
const result = new BigNumber(String(fee)).shiftedBy(-decimals)
this.feeCache[assetInfo.contractAddress] = result
return result
}

throw new Error(`Dfinity: unsupported chain asset type ${assetInfo.chainAssetType.toString()}`);
Expand Down

0 comments on commit e0beb04

Please sign in to comment.