vega-frontend-monorepo/libs/web3/src/lib/use-get-withdraw-threshold.tsx
Art 87e1f9998e
feat: transaction store and toasts (#2382)
* feat: add eth and vega transaction stores

feat: replace useStoredEthereumTransaction with useEthTransactionManager

feat: add event bus subsciption to vega transaction store

feat: handle order cancellation

feat: rename Deposit, Order and Withdraw status field to be unique

Revert "feat: rename Deposit, Order and Withdraw status field to be unique"

This reverts commit f0b314d53fb3ada6fbebaba4fd1e5af6f38beaed.

feat: split transaction update subscription

feat: handle order and deposit transaction

feat: handle withdrawal creation through transaction store

feat: handle withdraw approval

feat: handle panding withdrawls, add createdAt

feat: handle transaction toast/dialog dismissal

feat: add use vega transaction store tests

feat: add use vega transaction store tests

feat: add use vega transaction menager tests

feat: add use vega transaction menager tests

feat: add use vega transaction updater tests

feat: improve use vega transaction updater tests

feat: add use eth transaction store

feat: add use eth withdraw approvals store

feat: add use eth transaction updater tests

fixed tests

* feat: toasts

feat: toasts

feat: toasts

* feat: add use eth withdraw approval manager tests

* feat: add use eth transaction manager tests

* feat: add use eth transaction manager tests

* feat: add useEthWithdrawApprovalsManager tests

* feat: remove Web3Container react container from CreateWithdrawalDialog

* feat: remove Web3Container react container around TransactionsHandler

* feat: remove unnecessary async from PendingWithdrawalsTable

* feat: remove comments from WithdrawalFeedback

* fixed z-index issue

* cypress

Co-authored-by: Bartłomiej Głownia <bglownia@gmail.com>
2022-12-21 10:29:32 +01:00

37 lines
1.3 KiB
TypeScript

import { useCallback } from 'react';
import { useBridgeContract } from './use-bridge-contract';
import BigNumber from 'bignumber.js';
import { addDecimal } from '@vegaprotocol/react-helpers';
import type { WithdrawalBusEventFieldsFragment } from '@vegaprotocol/wallet';
/**
* Returns a function to get the threshold amount for a withdrawal. If a withdrawal amount
* is greater than this value it will incur a delay before being able to be completed. The delay is set
* on the smart contract and can be retrieved using contract.default_withdraw_delay
*/
export const useGetWithdrawThreshold = () => {
const contract = useBridgeContract();
const getThreshold = useCallback(
async (
asset:
| Pick<WithdrawalBusEventFieldsFragment['asset'], 'source' | 'decimals'>
| undefined
) => {
if (!contract || asset?.source.__typename !== 'ERC20') {
return new BigNumber(Infinity);
}
const res = await contract.get_withdraw_threshold(
asset.source.contractAddress
);
const value = new BigNumber(addDecimal(res.toString(), asset.decimals));
const threshold = value.isEqualTo(0)
? new BigNumber(Infinity)
: value.minus(new BigNumber(addDecimal('1', asset.decimals)));
return threshold;
},
[contract]
);
return getThreshold;
};