fix(withdraws): minimal withdrawal amount validation (#3993)

This commit is contained in:
Art 2023-06-06 23:21:48 +02:00 committed by GitHub
parent 6f9f432c90
commit 878bed9c7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 4 deletions

View File

@ -99,6 +99,8 @@ export const NetworkParams = {
governance_proposal_freeform_minProposerBalance:
'governance_proposal_freeform_minProposerBalance',
validators_delegation_minAmount: 'validators_delegation_minAmount',
spam_protection_minimumWithdrawalQuantumMultiple:
'spam_protection_minimumWithdrawalQuantumMultiple',
spam_protection_voting_min_tokens: 'spam_protection_voting_min_tokens',
spam_protection_proposal_min_tokens: 'spam_protection_proposal_min_tokens',
market_liquidity_stakeToCcyVolume: 'market_liquidity_stakeToCcyVolume',

View File

@ -3,13 +3,14 @@ import { addDecimal } from '@vegaprotocol/utils';
import { localLoggerFactory } from '@vegaprotocol/logger';
import * as Schema from '@vegaprotocol/types';
import BigNumber from 'bignumber.js';
import { useCallback, useEffect } from 'react';
import { useCallback, useEffect, useMemo } from 'react';
import type { AccountFieldsFragment } from '@vegaprotocol/accounts';
import {
useGetWithdrawDelay,
useGetWithdrawThreshold,
} from '@vegaprotocol/web3';
import { useWithdrawStore } from './withdraw-store';
import { useNetworkParam } from '@vegaprotocol/network-parameters';
export const useWithdrawAsset = (
assets: Asset[],
@ -19,6 +20,17 @@ export const useWithdrawAsset = (
const { asset, balance, min, threshold, delay, update } = useWithdrawStore();
const getThreshold = useGetWithdrawThreshold();
const getDelay = useGetWithdrawDelay();
const { param } = useNetworkParam(
'spam_protection_minimumWithdrawalQuantumMultiple'
);
const minimumWithdrawalQuantumMultiple = useMemo(() => {
const factor = new BigNumber(param || '');
if (factor.isNaN()) {
return new BigNumber(1);
}
return factor;
}, [param]);
// Every time an asset is selected we need to find the corresponding
// account, balance, min viable amount and delay threshold
@ -37,7 +49,9 @@ export const useWithdrawAsset = (
const min = asset
? BigNumber.max(
new BigNumber(addDecimal('1', asset.decimals)),
new BigNumber(addDecimal(asset.quantum, asset.decimals))
new BigNumber(addDecimal(asset.quantum, asset.decimals)).times(
minimumWithdrawalQuantumMultiple
)
)
: new BigNumber(0);
// Query collateral bridge for threshold for selected asset
@ -56,7 +70,14 @@ export const useWithdrawAsset = (
update({ asset, balance, min, threshold, delay });
},
[accounts, assets, update, getThreshold, getDelay]
[
assets,
accounts,
minimumWithdrawalQuantumMultiple,
update,
getThreshold,
getDelay,
]
);
useEffect(() => {

View File

@ -15,6 +15,18 @@ jest.mock('@vegaprotocol/data-provider', () => ({
}));
jest.mock('@web3-react/core');
jest.mock('@vegaprotocol/accounts');
jest.mock('@vegaprotocol/network-parameters', () => {
const impl = jest.requireActual('@vegaprotocol/network-parameters');
return {
...impl,
useNetworkParam: jest.fn((param) => {
if (param === 'spam_protection_minimumWithdrawalQuantumMultiple') {
return { param: '10.00', loading: false, error: undefined };
}
return impl.useNetworkParam(param);
}),
};
});
describe('WithdrawFormContainer', () => {
const props = {
@ -99,7 +111,7 @@ describe('WithdrawFormContainer', () => {
accountDecimals: null,
});
});
afterEach(() => {
afterAll(() => {
jest.resetAllMocks();
});
it('should be properly rendered', async () => {