Skip to content

Commit 99abf3a

Browse files
fix: wrong ibc channels for withdraw (#2340)
Co-authored-by: neocybereth <[email protected]>
1 parent b1e4304 commit 99abf3a

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

apps/namadillo/src/App/Ibc/IbcWithdraw.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { IbcTransferProps } from "@namada/sdk-multicore";
22
import { AccountType } from "@namada/types";
33
import { mapUndefined } from "@namada/utils";
44
import { routes } from "App/routes";
5-
import { isShieldedAddress } from "App/Transfer/common";
5+
import { isIbcAddress, isShieldedAddress } from "App/Transfer/common";
66
import { TransferModule } from "App/Transfer/TransferModule";
77
import { OnSubmitTransferParams } from "App/Transfer/types";
88
import {
@@ -28,6 +28,7 @@ import { useTransaction } from "hooks/useTransaction";
2828
import { useTransactionActions } from "hooks/useTransactionActions";
2929
import { useWalletManager } from "hooks/useWalletManager";
3030
import { KeplrWalletManager } from "integrations/Keplr";
31+
import { getChainFromAddress } from "integrations/utils";
3132
import invariant from "invariant";
3233
import { useAtom, useAtomValue } from "jotai";
3334
import { TransactionPair } from "lib/query";
@@ -84,6 +85,7 @@ export const IbcWithdraw = ({
8485
walletAddress: keplrAddress,
8586
chainId,
8687
registry,
88+
connectToChainId,
8789
} = useWalletManager(keplrWalletManager);
8890
const transparentAccount = useAtomValue(defaultAccountAtom);
8991
const namadaChain = useAtomValue(chainAtom);
@@ -142,6 +144,22 @@ export const IbcWithdraw = ({
142144
}
143145
};
144146

147+
// Connect to IBC chain if destination address is an IBC address
148+
useEffect(() => {
149+
const connectIfIbc = async (address: string): Promise<void> => {
150+
const chain = getChainFromAddress(address);
151+
if (chain?.chain_id) {
152+
try {
153+
await connectToChainId(chain.chain_id);
154+
} catch (error) {
155+
console.error("Failed to connect to IBC chain:", error);
156+
}
157+
}
158+
};
159+
160+
if (isIbcAddress(destinationAddress)) connectIfIbc(destinationAddress);
161+
}, [destinationAddress]);
162+
145163
const {
146164
data: ibcChannels,
147165
isError: unknownIbcChannels,

apps/namadillo/src/App/Transfer/SelectToken.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
connectedWalletsAtom,
88
getChainRegistryByChainName,
99
namadaRegistryChainAssetsMapAtom,
10+
SUPPORTED_ASSETS_MAP,
1011
} from "atoms/integrations";
1112
import { tokenPricesFamily } from "atoms/prices/atoms";
1213
import clsx from "clsx";
@@ -125,6 +126,7 @@ export const SelectToken = ({
125126
async (token: AssetWithAmountAndChain): Promise<void> => {
126127
// Check if current address is Keplr and if we need to connect to specific chain for this token
127128
const isIbcOrKeplrToken = !isNamadaAddress(sourceAddress);
129+
const destinationIsIbcOrKeplrToken = !isNamadaAddress(destinationAddress);
128130
// only used for IBC tokens
129131
let newSourceAddress: string | undefined;
130132
try {
@@ -163,6 +165,19 @@ export const SelectToken = ({
163165
} finally {
164166
setIsConnectingKeplr(false);
165167
}
168+
} else if (destinationIsIbcOrKeplrToken) {
169+
// Because IbcWithdraw uses registry from KeplrWalletManager, we need to connect to
170+
// the source chain of the selected asset. Otherwise channels may not be correct as
171+
// ibcChannelsFamily relies on connected registry.
172+
const chainName = [...SUPPORTED_ASSETS_MAP.entries()].find(
173+
([_, assetSymbols]) => {
174+
return assetSymbols.includes(token.asset.symbol);
175+
}
176+
)?.[0];
177+
invariant(chainName, "Chain name not found for selected asset");
178+
const registry = getChainRegistryByChainName(chainName);
179+
invariant(registry, "Chain registry not found for counterparty");
180+
await connectToChainId(registry.chain.chain_id);
166181
}
167182

168183
onSelect?.(token, newSourceAddress);

apps/namadillo/src/App/Transfer/TransferLayout.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { determineTransferType } from "./utils";
2020

2121
export const TransferLayout: React.FC = () => {
2222
const keplrWalletManager = new KeplrWalletManager();
23+
2324
const userHasAccount = useUserHasAccount();
2425
const [sourceAddressUrl, setSourceAddressUrl] = useUrlState("source");
2526
const [destinationAddressUrl, setDestinationAddressUrl] =
@@ -59,12 +60,11 @@ export const TransferLayout: React.FC = () => {
5960

6061
// Refetch shielded balance for MASP operations
6162
useEffect(() => {
62-
if (transferType === "shield" || transferType === "unshield") {
63-
refetchShieldedBalance();
64-
}
63+
if (["shield", "unshield"].includes(transferType)) refetchShieldedBalance();
6564
}, [transferType, refetchShieldedBalance]);
6665

6766
// Validate source address - check if it's from keyring or Keplr
67+
// If not it means the address is invalid at best, poisoned at worst.
6868
useEffect(() => {
6969
const validateSourceAddress = async (): Promise<void> => {
7070
if (!sourceAddressUrl || !userHasAccount || !accounts) return;

0 commit comments

Comments
 (0)