From 1647f21e8616c3aba03b8d040e473ea2f1ca5d5b Mon Sep 17 00:00:00 2001 From: Pedro Rezende Date: Wed, 31 Jul 2024 13:00:48 -0300 Subject: [PATCH] Namadillo: fixing some errors on re-delegate flow (#955) * feat(namadillo): fixing public key reveal error * fix(namadillo): change view after submitting step 1 of re-delegate * fix(namadillo): fixing property being deleted from original reference --- apps/namadillo/src/App/Staking/ReDelegate.tsx | 20 ++++++++++++++---- apps/namadillo/src/lib/query.ts | 21 ++++++++++++------- apps/namadillo/src/lib/staking.ts | 6 ++++-- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/apps/namadillo/src/App/Staking/ReDelegate.tsx b/apps/namadillo/src/App/Staking/ReDelegate.tsx index 2af6c953f..c38e760e2 100644 --- a/apps/namadillo/src/App/Staking/ReDelegate.tsx +++ b/apps/namadillo/src/App/Staking/ReDelegate.tsx @@ -37,10 +37,11 @@ export const ReDelegate = (): JSX.Element => { const dispatchNotification = useSetAtom(dispatchToastNotificationAtom); const { data: account } = useAtomValue(defaultAccountAtom); const validators = useAtomValue(allValidatorsAtom); + const { totalStakedAmount, - totalUpdatedAmount: totalToRedelegate, stakedAmountByAddress, + totalUpdatedAmount: totalToRedelegate, updatedAmountByAddress: amountsRemovedByAddress, onChangeValidatorAmount, myValidators, @@ -101,7 +102,7 @@ export const ReDelegate = (): JSX.Element => { errorMessage={ redelegateTxError instanceof Error ? redelegateTxError.message - : undefined + : undefined } /> ), @@ -121,8 +122,7 @@ export const ReDelegate = (): JSX.Element => { ); }; - const onSubmit = (e: React.FormEvent): void => { - e.preventDefault(); + const performRedelegate = (): void => { invariant(account, `Extension is connected but you don't have an account`); invariant(gasPrice, "Gas price loading is still pending"); invariant(gasLimits.isSuccess, "Gas limit loading is still pending"); @@ -141,6 +141,18 @@ export const ReDelegate = (): JSX.Element => { }); }; + const onSubmit = (e: React.FormEvent): void => { + e.preventDefault(); + // Go to next page or do nothing + if (step === "remove" && totalToRedelegate) { + if (totalToRedelegate.gt(0)) { + setStep("assign"); + } + return; + } + performRedelegate(); + }; + const totalAssignedAmounts = BigNumber.sum( new BigNumber(0), ...Object.values(amountsToAssignByAddress) diff --git a/apps/namadillo/src/lib/query.ts b/apps/namadillo/src/lib/query.ts index 2cf84b73d..61179d618 100644 --- a/apps/namadillo/src/lib/query.ts +++ b/apps/namadillo/src/lib/query.ts @@ -11,7 +11,7 @@ import { chainParametersAtom } from "atoms/chain"; import { getSdkInstance } from "hooks"; import invariant from "invariant"; import { getDefaultStore } from "jotai"; -import { ChainSettings, GasConfig } from "types"; +import { Address, ChainSettings, GasConfig } from "types"; import { TransactionEventsClasses } from "types/events"; export type TransactionPair = { @@ -60,6 +60,16 @@ const getTxProps = ( }; }; +const isPublicKeyRevealed = async (address: Address): Promise => { + const api = getIndexerApi(); + let publicKey: string | undefined; + try { + publicKey = (await api.apiV1RevealedPublicKeyAddressGet(address)).data + ?.publicKey; + } catch (err) {} + return Boolean(publicKey); +}; + /** * Builds an batch transactions based on the provided query properties. * Each transaction is built through the provided transaction function `txFn`. @@ -78,12 +88,8 @@ export const buildBatchTx = async ( const txs: BuiltTx[] = []; // Determine if RevealPK is needed: - const api = getIndexerApi(); - const { publicKey } = ( - await api.apiV1RevealedPublicKeyAddressGet(account.address) - ).data; - - if (!publicKey) { + const publicKeyRevealed = await isPublicKeyRevealed(account.address); + if (!publicKeyRevealed) { const revealPkTx = await tx.buildRevealPk(wrapperTxProps); txs.push(revealPkTx.tx); } @@ -96,7 +102,6 @@ export const buildBatchTx = async ( const initialTx = encodedTxs[0].tx; const wrapperTxMsg = initialTx.wrapper_tx_msg(); - const batchTx = tx.buildBatch(txs, wrapperTxMsg); return { diff --git a/apps/namadillo/src/lib/staking.ts b/apps/namadillo/src/lib/staking.ts index d458dddb5..b891fc035 100644 --- a/apps/namadillo/src/lib/staking.ts +++ b/apps/namadillo/src/lib/staking.ts @@ -64,10 +64,12 @@ export const buildRedelegateChange = ( ): RedelegateChange => ({ sourceValidator, destinationValidator, amount }); export const getAmountDistribution = ( - reducedAmounts: Record, - increasedAmounts: Record + _reducedAmounts: Record, + _increasedAmounts: Record ): RedelegateChange[] => { const redelegateChanges: RedelegateChange[] = []; + const reducedAmounts = { ..._reducedAmounts }; + const increasedAmounts = { ..._increasedAmounts }; // If two addresses are present in both objects, fix the assigned amounts for (const address in reducedAmounts) {