-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
try-catch check token
- Loading branch information
Showing
6 changed files
with
144 additions
and
96 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
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,7 +1,7 @@ | ||
export function formatNumber(num: number | undefined, digits = 2) { | ||
if (!num) return num | ||
return num.toLocaleString('en-US', { | ||
minimumFractionDigits: 2, | ||
minimumFractionDigits: Math.min(2, digits), | ||
maximumFractionDigits: digits, | ||
}) | ||
} |
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,121 @@ | ||
import { useToast } from '@chakra-ui/react' | ||
import { t } from '@lingui/macro' | ||
import { useMutation } from '@tanstack/react-query' | ||
import { noop } from 'lodash-es' | ||
import { useState } from 'react' | ||
import { TransactionExecutionError, UserRejectedRequestError } from 'viem' | ||
import { useChainId, useConfig, useSwitchChain, useWriteContract } from 'wagmi' | ||
import { waitForTransactionReceipt } from 'wagmi/actions' | ||
|
||
import { StakeManagerABI } from '@/abis/stakeManager' | ||
import { TxToastDescription } from '@/components/TxToastDescription' | ||
import { queryClient } from '@/configs/queryClient' | ||
import { formatNumber } from '@/helpers/formatNumber' | ||
import { resolveTxLink } from '@/helpers/resolveTxLink' | ||
import { useCheckStats } from '@/hooks/useCheckStats' | ||
import { useHandleError } from '@/hooks/useHandleError' | ||
import { resultModal } from '@/modals/ResultModal' | ||
import { usePoolStore } from '@/store/poolStore' | ||
import { sleep } from '@/utils/sleep' | ||
|
||
interface Params { | ||
amount: bigint | ||
rawAmount: string | ||
} | ||
|
||
export function useStake() { | ||
const { chainId, maskTokenAddress, stakeManagerAddress } = usePoolStore() | ||
const config = useConfig() | ||
const currentChainId = useChainId() | ||
const { switchChainAsync, isPending: isSwitchingChain } = useSwitchChain() | ||
const { writeContractAsync, isPending: isStaking } = useWriteContract() | ||
|
||
const [waiting, setWaiting] = useState(false) | ||
const [updating, setUpdating] = useState(false) | ||
|
||
const toast = useToast({ title: t`Stake` }) | ||
const handleError = useHandleError() | ||
const checkStats = useCheckStats() | ||
|
||
const mutation = useMutation({ | ||
mutationFn: async ({ amount, rawAmount }: Params) => { | ||
if (!maskTokenAddress || !stakeManagerAddress) { | ||
toast({ | ||
status: 'error', | ||
title: t`Can not get determination MASK token`, | ||
}) | ||
return | ||
} | ||
try { | ||
if (currentChainId !== chainId) { | ||
await switchChainAsync({ chainId }) | ||
} | ||
toast({ | ||
status: 'loading', | ||
description: t`Confirm this transaction in your wallet.`, | ||
}) | ||
const hash = await writeContractAsync({ | ||
abi: StakeManagerABI, | ||
address: stakeManagerAddress, | ||
functionName: 'depositAndLock', | ||
args: [amount], | ||
}) | ||
const txLink = resolveTxLink(chainId, hash) | ||
|
||
toast({ | ||
status: 'loading', | ||
description: <TxToastDescription link={txLink} text={t`Transaction submitted!`} color="primary.4" />, | ||
}) | ||
setWaiting(true) | ||
const receipt = await waitForTransactionReceipt(config, { | ||
hash, | ||
confirmations: 1, | ||
}) | ||
setWaiting(false) | ||
if (receipt.status === 'reverted') { | ||
toast({ | ||
status: 'error', | ||
description: <TxToastDescription link={txLink} text={t`Transaction failed.`} />, | ||
}) | ||
throw new Error('The transaction gets reverted!') | ||
} else { | ||
toast({ | ||
status: 'success', | ||
description: <TxToastDescription link={txLink} text={t`Successfully staked MASK Tokens.`} />, | ||
}) | ||
setUpdating(true) | ||
await Promise.allSettled([ | ||
checkStats() | ||
.then(async (res) => { | ||
if (res && BigInt(res.height) < receipt.blockNumber) await sleep(6000) | ||
}) | ||
.catch(noop) | ||
.finally(() => setUpdating(false)), | ||
resultModal.show({ | ||
title: t`Stake`, | ||
message: t`Stake Successfully`, | ||
description: t`You have successfully staked ${formatNumber(+rawAmount, 4)} MASK.`, | ||
}), | ||
]) | ||
queryClient.refetchQueries({ queryKey: ['pool-info'] }) | ||
queryClient.refetchQueries({ queryKey: ['user-info'] }) | ||
} | ||
} catch (err) { | ||
const cause = err instanceof TransactionExecutionError ? err.cause : err | ||
if (cause instanceof UserRejectedRequestError) { | ||
toast({ | ||
status: 'error', | ||
description: t`Your wallet cancelled the transaction.`, | ||
}) | ||
return | ||
} else if (handleError(err)) return | ||
throw err | ||
} finally { | ||
setWaiting(false) | ||
setUpdating(false) | ||
} | ||
}, | ||
}) | ||
|
||
return [{ updating, waiting, isSwitchingChain, isStaking }, mutation] as const | ||
} |
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