Skip to content

Commit

Permalink
Namadillo: fixing some errors on re-delegate flow (#955)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pedrorezende authored Jul 31, 2024
1 parent 1f83246 commit 1647f21
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
20 changes: 16 additions & 4 deletions apps/namadillo/src/App/Staking/ReDelegate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -101,7 +102,7 @@ export const ReDelegate = (): JSX.Element => {
errorMessage={
redelegateTxError instanceof Error ?
redelegateTxError.message
: undefined
: undefined
}
/>
),
Expand All @@ -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");
Expand All @@ -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)
Expand Down
21 changes: 13 additions & 8 deletions apps/namadillo/src/lib/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> = {
Expand Down Expand Up @@ -60,6 +60,16 @@ const getTxProps = (
};
};

const isPublicKeyRevealed = async (address: Address): Promise<boolean> => {
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`.
Expand All @@ -78,12 +88,8 @@ export const buildBatchTx = async <T>(
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);
}
Expand All @@ -96,7 +102,6 @@ export const buildBatchTx = async <T>(

const initialTx = encodedTxs[0].tx;
const wrapperTxMsg = initialTx.wrapper_tx_msg();

const batchTx = tx.buildBatch(txs, wrapperTxMsg);

return {
Expand Down
6 changes: 4 additions & 2 deletions apps/namadillo/src/lib/staking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ export const buildRedelegateChange = (
): RedelegateChange => ({ sourceValidator, destinationValidator, amount });

export const getAmountDistribution = (
reducedAmounts: Record<string, BigNumber>,
increasedAmounts: Record<string, BigNumber>
_reducedAmounts: Record<string, BigNumber>,
_increasedAmounts: Record<string, BigNumber>
): 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) {
Expand Down

0 comments on commit 1647f21

Please sign in to comment.