Skip to content

Commit

Permalink
Peter/fix staking limit bug (#1515)
Browse files Browse the repository at this point in the history
* chore: update monetary to latest 0.7.3

* fix: use maximum stakable amount fetched from rpc

* delete stray comment

---------

Co-authored-by: Thomas Jeatt <[email protected]>
  • Loading branch information
peterslany and tomjeatt authored Aug 17, 2023
1 parent 67ba4b3 commit a896259
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
38 changes: 38 additions & 0 deletions src/hooks/api/escrow/use-get-account-stakable-limit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { CurrencyExt, newMonetaryAmount } from '@interlay/interbtc-api';
import { MonetaryAmount } from '@interlay/monetary-js';
import { AccountId } from '@polkadot/types/interfaces';
import { useErrorHandler } from 'react-error-boundary';
import { useQuery } from 'react-query';

import { GOVERNANCE_TOKEN } from '@/config/relay-chains';
import useAccountId from '@/hooks/use-account-id';
import { BLOCKTIME_REFETCH_INTERVAL } from '@/utils/constants/api';

interface GetAccountStakableLimitResult {
data: MonetaryAmount<CurrencyExt> | undefined;
refetch: () => void;
}

const getAccountStakableLimit = async (account: AccountId) => {
const { amount } = await window.bridge.api.rpc.escrow.totalSupply(account);
return newMonetaryAmount(amount, GOVERNANCE_TOKEN);
};

const useGetAccountStakableLimit = (): GetAccountStakableLimitResult => {
const account = useAccountId();

const queryKey = ['staking', account];

const { data, error, refetch } = useQuery({
queryKey,
queryFn: () => account && getAccountStakableLimit(account),
refetchInterval: BLOCKTIME_REFETCH_INTERVAL,
enabled: !!account
});

useErrorHandler(error);

return { data, refetch };
};

export { useGetAccountStakableLimit };
17 changes: 6 additions & 11 deletions src/pages/Staking/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
VOTE_GOVERNANCE_TOKEN_SYMBOL,
VoteGovernanceTokenMonetaryAmount
} from '@/config/relay-chains';
import { useGetBalances } from '@/hooks/api/tokens/use-get-balances';
import { useGetAccountStakableLimit } from '@/hooks/api/escrow/use-get-account-stakable-limit';
import { useGetPrices } from '@/hooks/api/use-get-prices';
import { Transaction, useTransaction } from '@/hooks/transaction';
import { useSignMessage } from '@/hooks/use-sign-message';
Expand Down Expand Up @@ -109,8 +109,7 @@ const Staking = (): JSX.Element => {

const { bridgeLoaded } = useSelector((state: StoreType) => state.general);

const { data: balances, isLoading: isBalancesLoading } = useGetBalances();
const governanceTokenBalance = balances?.[GOVERNANCE_TOKEN.ticker];
const { data: maxStakableAmount } = useGetAccountStakableLimit();

const queryClient = useQueryClient();

Expand Down Expand Up @@ -319,7 +318,7 @@ const Staking = (): JSX.Element => {

const availableBalance = React.useMemo(() => {
if (
isBalancesLoading ||
maxStakableAmount === undefined ||
stakedAmountAndEndBlockIdle ||
stakedAmountAndEndBlockLoading ||
transactionFeeReserveIdle ||
Expand All @@ -332,21 +331,17 @@ const Staking = (): JSX.Element => {
if (transactionFeeReserve === undefined) {
throw new Error('Transaction fee reserve value returned undefined!');
}
if (governanceTokenBalance === undefined) {
throw new Error('Governance token balance value returned undefined!');
}

const calculatedBalance = governanceTokenBalance.free.sub(stakedAmount).sub(transactionFeeReserve);
const calculatedBalance = maxStakableAmount.sub(transactionFeeReserve);

return calculatedBalance.toBig().gte(0) ? calculatedBalance : newMonetaryAmount(0, GOVERNANCE_TOKEN);
}, [
isBalancesLoading,
governanceTokenBalance,
maxStakableAmount,
stakedAmountAndEndBlockIdle,
stakedAmountAndEndBlockLoading,
stakedAmount,
transactionFeeReserveIdle,
transactionFeeReserveLoading,
stakedAmount,
transactionFeeReserve
]);

Expand Down

2 comments on commit a896259

@vercel
Copy link

@vercel vercel bot commented on a896259 Aug 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on a896259 Aug 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.