Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improvements on IBC withdrawal #1574

Merged
merged 5 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/namadillo/src/App/Governance/SubmitVote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ import clsx from "clsx";
import { useProposalIdParam } from "hooks";
import invariant from "invariant";
import { useAtomValue, useSetAtom } from "jotai";
import { TransactionPair, broadcastTx } from "lib/query";
import { TransactionPair, broadcastTxWithEvents } from "lib/query";
import { useEffect, useState } from "react";
import { IoClose } from "react-icons/io5";
import { useNavigate } from "react-router-dom";

const dispatchVoteTx = (tx: TransactionPair<VoteProposalProps>): void => {
broadcastTx(
broadcastTxWithEvents(
tx.encodedTxData,
tx.signedTxs,
tx.encodedTxData.meta?.props,
Expand Down
103 changes: 58 additions & 45 deletions apps/namadillo/src/App/Ibc/IbcWithdraw.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
TransferStep,
TransferTransactionData,
} from "types";
import { toBaseAmount } from "utils";
import { toBaseAmount, toDisplayAmount } from "utils";
import { IbcTopHeader } from "./IbcTopHeader";

const defaultChainId = "cosmoshub-4";
Expand All @@ -61,6 +61,11 @@ export const IbcWithdraw: React.FC = () => {
selectedAssetAddress
);

const selectedAsset = mapUndefined(
(address) => availableAssets?.[address],
selectedAssetAddress
);

const { data: gasConfig } = useAtomValue(
defaultGasConfigFamily(["IbcTransfer"])
);
Expand Down Expand Up @@ -90,10 +95,15 @@ export const IbcWithdraw: React.FC = () => {
setSourceChannel(ibcChannels?.namadaChannel || "");
}, [ibcChannels]);

const { execute: performWithdraw, isPending } = useTransaction({
const {
execute: performWithdraw,
isPending,
isSuccess,
} = useTransaction({
eventType: "IbcTransfer",
createTxAtom: createIbcTxAtom,
params: [],
gasConfig,
parsePendingTxNotification: () => ({
title: "IBC withdrawal transaction in progress",
description: "Your IBC transaction is being processed",
Expand All @@ -102,6 +112,29 @@ export const IbcWithdraw: React.FC = () => {
title: "IBC withdrawal failed",
description: "",
}),
onSuccess: (tx) => {
const props = tx.encodedTxData.meta?.props[0];
invariant(props, "EncodedTxData not provided");
invariant(selectedAsset, "Selected asset is not defined");
invariant(chainId, "Chain ID is not provided");

const displayAmount = toDisplayAmount(
selectedAsset.asset,
props.amountInBaseDenom
);

const transferTransaction = storeTransferTransaction(
tx,
displayAmount,
chainId,
selectedAsset.asset
);

redirectToTimeline(transferTransaction);
},
onError: (err) => {
setGeneralErrorMessage(String(err));
},
});

const storeTransferTransaction = (
Expand Down Expand Up @@ -147,49 +180,26 @@ export const IbcWithdraw: React.FC = () => {
destinationAddress,
memo,
}: OnSubmitTransferParams): Promise<void> => {
try {
const selectedAsset = mapUndefined(
(address) => availableAssets?.[address],
selectedAssetAddress
);

invariant(selectedAsset, "No asset is selected");
invariant(sourceChannel, "No channel ID is set");
invariant(chainId, "No chain is selected");
invariant(gasConfig, "No gas config");
invariant(keplrAddress, "No address is selected");
invariant(selectedAsset, "No asset is selected");
invariant(sourceChannel, "No channel ID is set");
invariant(chainId, "No chain is selected");
invariant(gasConfig, "No gas config");
invariant(keplrAddress, "No address is selected");

const amountInBaseDenom = toBaseAmount(
selectedAsset.asset,
displayAmount
);

const tx = await performWithdraw({
params: [
{
amountInBaseDenom,
channelId: sourceChannel.trim(),
portId: "transfer",
token: selectedAsset.originalAddress,
source: keplrAddress,
receiver: destinationAddress,
memo,
},
],
});

if (tx) {
const transferTransaction = storeTransferTransaction(
tx,
displayAmount,
chainId,
selectedAsset.asset
);
redirectToTimeline(transferTransaction);
}
} catch (err) {
setGeneralErrorMessage(String(err));
}
const amountInBaseDenom = toBaseAmount(selectedAsset.asset, displayAmount);
await performWithdraw({
params: [
{
amountInBaseDenom,
channelId: sourceChannel.trim(),
portId: "transfer",
token: selectedAsset.originalAddress,
source: keplrAddress,
receiver: destinationAddress,
memo,
},
],
});
};

const requiresIbcChannels = !isLoadingIbcChannels && unknownIbcChannels;
Expand Down Expand Up @@ -234,7 +244,10 @@ export const IbcWithdraw: React.FC = () => {
onChangeChain,
isShielded: false,
}}
isSubmitting={isPending}
isSubmitting={
isPending ||
isSuccess /* should redirect to timeline to wait for confirmation */
}
isIbcTransfer={true}
requiresIbcChannels={requiresIbcChannels}
ibcOptions={{
Expand Down
2 changes: 1 addition & 1 deletion apps/namadillo/src/App/Staking/IncrementBonding.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const IncrementBonding = (): JSX.Element => {
title: "Staking transaction failed",
description: "",
}),
onSuccess: () => {
onBroadcasted: () => {
onCloseModal();
},
});
Expand Down
2 changes: 1 addition & 1 deletion apps/namadillo/src/App/Staking/ReDelegate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const ReDelegate = (): JSX.Element => {
title: "Staking redelegation failed",
description: "",
}),
onSuccess: () => {
onBroadcasted: () => {
onCloseModal();
},
});
Expand Down
4 changes: 2 additions & 2 deletions apps/namadillo/src/App/Staking/StakingRewards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const StakingRewards = (): JSX.Element => {
title: "Claim rewards transaction is in progress",
description: <>Your rewards claim is being processed</>,
}),
onSuccess: () => {
onBroadcasted: () => {
onCloseModal();
},
});
Expand All @@ -76,7 +76,7 @@ export const StakingRewards = (): JSX.Element => {
</>
),
}),
onSuccess: () => {
onBroadcasted: () => {
onCloseModal();
},
});
Expand Down
2 changes: 1 addition & 1 deletion apps/namadillo/src/App/Staking/Unstake.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const Unstake = (): JSX.Element => {
title: "Unstake transaction failed",
description: "",
}),
onSuccess: () => {
onBroadcasted: () => {
onCloseModal();
},
});
Expand Down
8 changes: 4 additions & 4 deletions apps/namadillo/src/atoms/notifications/functions.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { TxProps } from "@namada/types";

export const createNotificationId = (data?: TxProps | TxProps[]): string => {
export const createNotificationId = (
data?: TxProps | TxProps[] | string
): string => {
if (!data) return Date.now().toString();
if (typeof data === "string") return data;
if (Array.isArray(data)) {
return data.map((tx) => tx.hash).join(";");
}
return data.hash;
};

export const createIbcNotificationId = (hash: string): string => {
return `ibc-transfer-${hash}`;
};
6 changes: 3 additions & 3 deletions apps/namadillo/src/hooks/useIbcTransaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
queryAndStoreRpc,
} from "atoms/integrations";
import {
createIbcNotificationId,
createNotificationId,
dispatchToastNotificationAtom,
} from "atoms/notifications";
import BigNumber from "bignumber.js";
Expand Down Expand Up @@ -101,7 +101,7 @@ export const useIbcTransaction = ({
const dispatchPendingTxNotification = (tx: TransferTransactionData): void => {
invariant(tx.hash, "Error: Transaction hash not provided");
dispatchNotification({
id: createIbcNotificationId(tx.hash),
id: createNotificationId(tx.hash),
title: "IBC transfer transaction in progress",
description: (
<>
Expand All @@ -117,7 +117,7 @@ export const useIbcTransaction = ({
const dispatchErrorTxNotification = (error: unknown): void => {
if (!txHash) return;
dispatchNotification({
id: createIbcNotificationId(txHash),
id: createNotificationId(txHash),
title: "IBC transfer transaction failed",
description: "",
details: error instanceof Error ? error.message : undefined,
Expand Down
Loading
Loading