2022-09-06 01:30:13 +00:00
|
|
|
import { useCallback } from 'react';
|
2022-12-21 09:29:32 +00:00
|
|
|
import { useBridgeContract } from './use-bridge-contract';
|
2022-09-06 01:30:13 +00:00
|
|
|
import BigNumber from 'bignumber.js';
|
|
|
|
import { addDecimal } from '@vegaprotocol/react-helpers';
|
2022-12-21 09:29:32 +00:00
|
|
|
import type { WithdrawalBusEventFieldsFragment } from '@vegaprotocol/wallet';
|
2022-09-06 01:30:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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(
|
2022-11-10 14:13:58 +00:00
|
|
|
async (
|
|
|
|
asset:
|
2022-12-21 09:29:32 +00:00
|
|
|
| Pick<WithdrawalBusEventFieldsFragment['asset'], 'source' | 'decimals'>
|
2022-11-10 14:13:58 +00:00
|
|
|
| undefined
|
|
|
|
) => {
|
2022-09-06 01:30:13 +00:00
|
|
|
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;
|
|
|
|
};
|