From bca20e1dfddf38e30f184b0bfedc2851c2b992a1 Mon Sep 17 00:00:00 2001 From: Gala Date: Thu, 1 Dec 2022 12:57:28 +0100 Subject: [PATCH] connecting to the back and making the requests work --- .../Bonding/modals/BondMoreModal.tsx | 27 +++++++++++---- nym-wallet/src/context/bonding.tsx | 33 ++++--------------- nym-wallet/src/context/mocks/bonding.tsx | 2 +- nym-wallet/src/pages/bonding/Bonding.tsx | 11 ++++--- nym-wallet/src/requests/actions.ts | 2 +- nym-wallet/src/requests/simulate.ts | 6 ++++ nym-wallet/src/requests/vesting.ts | 11 +++++++ nym-wallet/src/types/global.ts | 5 +-- 8 files changed, 56 insertions(+), 41 deletions(-) diff --git a/nym-wallet/src/components/Bonding/modals/BondMoreModal.tsx b/nym-wallet/src/components/Bonding/modals/BondMoreModal.tsx index 64f5eb9e65..c0b03df1fb 100644 --- a/nym-wallet/src/components/Bonding/modals/BondMoreModal.tsx +++ b/nym-wallet/src/components/Bonding/modals/BondMoreModal.tsx @@ -8,7 +8,8 @@ import { TokenPoolSelector, TPoolOption } from 'src/components/TokenPoolSelector import { ConfirmTx } from 'src/components/ConfirmTX'; import { useGetFee } from 'src/hooks/useGetFee'; import { validateAmount, validateKey } from 'src/utils'; -import { TBondedMixnode } from 'src/context'; +import { simulateBondMore, simulateVestingBondMore } from 'src/requests'; +import { TBondMoreArgs } from 'src/types'; export const BondMoreModal = ({ currentBond, @@ -16,21 +17,28 @@ export const BondMoreModal = ({ hasVestingTokens, onConfirm, onClose, + onError, }: { currentBond: DecCoin; - mixId: string; userBalance?: string; hasVestingTokens: boolean; - onConfirm: (args: { additionalBond: DecCoin; tokenPool: TPoolOption }) => Promise; + onConfirm: (data: TBondMoreArgs, tokenPool: TPoolOption) => Promise; onClose: () => void; + onError: (e: string) => void; }) => { - const { fee, resetFeeState } = useGetFee(); + const { fee, getFee, resetFeeState, feeError } = useGetFee(); const [additionalBond, setAdditionalBond] = useState({ amount: '0', denom: currentBond.denom }); // const [signature, setSignature] = useState(''); const [tokenPool, setTokenPool] = useState('balance'); const [errorAmount, setErrorAmount] = useState(false); const [errorSignature, setErrorSignature] = useState(false); + useEffect(() => { + if (feeError) { + onError(feeError); + } + }, [feeError]); + const handleOnOk = async () => { const errors = { amount: false, @@ -50,11 +58,18 @@ export const BondMoreModal = ({ } if (!errors.amount) { - onConfirm({ additionalBond, tokenPool }); + const data = { additionalPledge: additionalBond }; + onConfirm(data, tokenPool); } else { setErrorAmount(errors.amount); setErrorSignature(errors.signature); } + + if (tokenPool === 'balance') { + await getFee(simulateBondMore, { additionalPledge: additionalBond }); + } else { + await getFee(simulateVestingBondMore, { additionalPledge: additionalBond }); + } }; useEffect(() => { @@ -67,7 +82,7 @@ export const BondMoreModal = ({ header="Bond more details" open fee={fee} - onConfirm={async () => onConfirm({ additionalBond, tokenPool })} + onConfirm={async () => onConfirm({ additionalPledge: additionalBond }, tokenPool)} onPrev={resetFeeState} > diff --git a/nym-wallet/src/context/bonding.tsx b/nym-wallet/src/context/bonding.tsx index d980e4688e..26337ae939 100644 --- a/nym-wallet/src/context/bonding.tsx +++ b/nym-wallet/src/context/bonding.tsx @@ -8,7 +8,7 @@ import { } from '@nymproject/types'; import React, { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'; import Big from 'big.js'; -import { isGateway, isMixnode, TBondGatewayArgs, TBondMixNodeArgs } from 'src/types'; +import { isGateway, isMixnode, TBondGatewayArgs, TBondMixNodeArgs, TBondMoreArgs } from 'src/types'; import { Console } from 'src/utils/console'; import { bondGateway as bondGatewayRequest, @@ -18,6 +18,8 @@ import { getMixnodeBondDetails, unbondGateway as unbondGatewayRequest, unbondMixNode as unbondMixnodeRequest, + bondMore as bondMoreRequest, + vestingBondMore, vestingBondGateway, vestingBondMixNode, vestingUnbondGateway, @@ -100,7 +102,7 @@ export type TBondingContext = { bondMixnode: (data: TBondMixNodeArgs, tokenPool: TokenPool) => Promise; bondGateway: (data: TBondGatewayArgs, tokenPool: TokenPool) => Promise; unbond: (fee?: FeeDetails) => Promise; - bondMore: (amount: DecCoin, mixId: string) => Promise; + bondMore: (data: TBondMoreArgs, tokenPool: TokenPool) => Promise; redeemRewards: (fee?: FeeDetails) => Promise; updateMixnode: (pm: string, fee?: FeeDetails) => Promise; checkOwnership: () => Promise; @@ -431,40 +433,19 @@ export const BondingContextProvider = ({ children }: { children?: React.ReactNod return tx; }; - const bondGatewayaa = async (data: TBondGatewayArgs, tokenPool: TokenPool) => { + const bondMore = async (data: TBondMoreArgs, tokenPool: TokenPool) => { let tx: TransactionExecuteResult | undefined; setIsLoading(true); try { if (tokenPool === 'balance') { - tx = await bondGatewayRequest(data); + tx = await bondMoreRequest(data); await userBalance.fetchBalance(); } if (tokenPool === 'locked') { - tx = await vestingBondGateway(data); + tx = await vestingBondMore(data); await userBalance.fetchTokenAllocation(); } - return tx; - } catch (e: any) { - Console.warn(e); - setError(`an error occurred: ${e}`); - } finally { - setIsLoading(false); - } - return undefined; - }; - const bondMore = async (amount: DecCoin, mixId: string) => { - let tx: TransactionExecuteResult | undefined; - setIsLoading(true); - try { - if (tokenPool === 'balance') { - tx = await bondGatewayRequest(data); - await userBalance.fetchBalance(); - } - if (tokenPool === 'locked') { - tx = await vestingBondGateway(data); - await userBalance.fetchTokenAllocation(); - } return tx; } catch (e: any) { Console.warn(e); diff --git a/nym-wallet/src/context/mocks/bonding.tsx b/nym-wallet/src/context/mocks/bonding.tsx index 8d97deb090..95e770850b 100644 --- a/nym-wallet/src/context/mocks/bonding.tsx +++ b/nym-wallet/src/context/mocks/bonding.tsx @@ -152,7 +152,7 @@ export const MockBondingContextProvider = ({ return TxResultMock; }; - const bondMore = async (_signature: string, _additionalBond: DecCoin) => { + const bondMore = async (): Promise => { setIsLoading(true); await mockSleep(SLEEP_MS); triggerStateUpdate(); diff --git a/nym-wallet/src/pages/bonding/Bonding.tsx b/nym-wallet/src/pages/bonding/Bonding.tsx index 79d8808c3f..886b39b402 100644 --- a/nym-wallet/src/pages/bonding/Bonding.tsx +++ b/nym-wallet/src/pages/bonding/Bonding.tsx @@ -13,7 +13,7 @@ import { ConfirmationDetailProps, ConfirmationDetailsModal } from 'src/component import { ErrorModal } from 'src/components/Modals/ErrorModal'; import { LoadingModal } from 'src/components/Modals/LoadingModal'; import { AppContext, urls } from 'src/context/main'; -import { isGateway, isMixnode, TBondGatewayArgs, TBondMixNodeArgs } from 'src/types'; +import { isGateway, isMixnode, TBondGatewayArgs, TBondMixNodeArgs, TBondMoreArgs } from 'src/types'; import { BondedGateway } from 'src/components/Bonding/BondedGateway'; import { RedeemRewardsModal } from 'src/components/Bonding/modals/RedeemRewardsModal'; import { BondingContextProvider, useBondingContext } from '../../context'; @@ -25,7 +25,7 @@ const Bonding = () => { const { network, clientDetails, - userBalance: { originalVesting }, + userBalance: { originalVesting, balance }, } = useContext(AppContext); const navigate = useNavigate(); @@ -68,10 +68,9 @@ const Bonding = () => { }); }; - const handleBondMore = async (args: { additionalBond: DecCoin; tokenPool: TPoolOption }) => { - const { additionalBond } = args; + const handleBondMore = async (data: TBondMoreArgs, tokenPool: TPoolOption) => { setShowModal(undefined); - const tx = await bondMore(additionalBond, bondedNode.mixId); + const tx = await bondMore(data, tokenPool); setConfirmationDetails({ status: 'success', title: 'Bond More successful', @@ -152,9 +151,11 @@ const Bonding = () => { {showModal === 'bond-more' && bondedNode && isMixnode(bondedNode) && ( setShowModal(undefined)} + onError={handleError} /> )} diff --git a/nym-wallet/src/requests/actions.ts b/nym-wallet/src/requests/actions.ts index a245b18b20..b4aaa144c0 100644 --- a/nym-wallet/src/requests/actions.ts +++ b/nym-wallet/src/requests/actions.ts @@ -34,4 +34,4 @@ export const unbond = async (type: EnumNodeType) => { }; export const bondMore = async (args: TBondMoreArgs) => - invokeWrapper('bond_gateway', args); + invokeWrapper('pledge_more', args); diff --git a/nym-wallet/src/requests/simulate.ts b/nym-wallet/src/requests/simulate.ts index ddeae6ab2e..4a1b82f0d4 100644 --- a/nym-wallet/src/requests/simulate.ts +++ b/nym-wallet/src/requests/simulate.ts @@ -63,3 +63,9 @@ export const simulateClaimOperatorReward = async () => invokeWrapper export const simulateVestingClaimOperatorReward = async () => invokeWrapper('simulate_vesting_claim_operator_reward'); + +export const simulateBondMore = async (args: any) => + invokeWrapper('simulate_pledge_more', args); + +export const simulateVestingBondMore = async (args: any) => + invokeWrapper('simulate_vesting_pledge_more', args); diff --git a/nym-wallet/src/requests/vesting.ts b/nym-wallet/src/requests/vesting.ts index 907f42c499..bad2f7febf 100644 --- a/nym-wallet/src/requests/vesting.ts +++ b/nym-wallet/src/requests/vesting.ts @@ -105,3 +105,14 @@ export const vestingClaimOperatorReward = async (fee?: Fee) => export const vestingClaimDelegatorRewards = async (mixId: number) => invokeWrapper('vesting_claim_delegator_reward', { mixId }); + +export const vestingBondMore = async ({ + fee, + additionalPledge, +}: { + fee?: Fee; + additionalPledge: DecCoin; +}) => invokeWrapper('vesting_pledge_more', { + fee, + additionalPledge, +}); diff --git a/nym-wallet/src/types/global.ts b/nym-wallet/src/types/global.ts index d0302b9218..2c16901cfe 100644 --- a/nym-wallet/src/types/global.ts +++ b/nym-wallet/src/types/global.ts @@ -50,8 +50,9 @@ export type TBondMixNodeArgs = { }; export type TBondMoreArgs = { - mixId: MixNode; - amount: string; + additionalPledge: DecCoin; + fee?: Fee; + }; export type TNodeDescription = {