Skip to content

Commit 98208aa

Browse files
committed
fix: ibc updates
1 parent 16b53c0 commit 98208aa

File tree

2 files changed

+79
-74
lines changed

2 files changed

+79
-74
lines changed

apps/namada-interface/src/App/Token/IBCTransfer/IBCTransfer.tsx

Lines changed: 58 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
Extensions,
1313
TokenType,
1414
Tokens,
15+
ChainKey,
1516
} from "@namada/types";
1617
import {
1718
Alert,
@@ -42,6 +43,7 @@ import {
4243
} from "./IBCTransfer.components";
4344

4445
export const submitIbcTransfer = async (
46+
chainKey: ChainKey,
4547
ibcArgs: TxIbcTransferArgs
4648
): Promise<void> => {
4749
const {
@@ -52,7 +54,7 @@ export const submitIbcTransfer = async (
5254
portId,
5355
channelId,
5456
} = ibcArgs;
55-
const integration = getIntegration(chainId);
57+
const integration = getIntegration(chainKey);
5658

5759
return await integration.submitBridgeTransfer(
5860
{
@@ -69,7 +71,7 @@ export const submitIbcTransfer = async (
6971
feeAmount: new BigNumber(0),
7072
gasLimit: new BigNumber(20_000),
7173
publicKey,
72-
chainId: chainId || defaultChainId,
74+
chainId,
7375
},
7476
},
7577
type
@@ -81,7 +83,6 @@ const IBCTransfer = (): JSX.Element => {
8183
const { derived } = useAppSelector<AccountsState>((state) => state.accounts);
8284
const [error, setError] = useState<string>();
8385
const [currentBalance, setCurrentBalance] = useState(new BigNumber(0));
84-
const [chainId, setChainId] = useState(defaultChainId)
8586
const { channelsByChain = {} } = useAppSelector<ChannelsState>(
8687
(state) => state.channels
8788
);
@@ -94,25 +95,28 @@ const IBCTransfer = (): JSX.Element => {
9495
});
9596
const [isFormValid, setIsFormValid] = useState(false);
9697

97-
const bridgedChains = Object.values(chains).filter(
98-
(chain: Chain) => chain.chainId !== chainId
98+
const [sourceChainId, setSourceChainId] = useState(chains.namada.chainId)
99+
const sourceChain: Chain = Object.values(chains)
100+
.find((chain: Chain) => chain.chainId === sourceChainId) || chains.namada;
101+
102+
const ibcChains = Object.values(chains).filter(
103+
(chain: Chain) => chain.chainId !== sourceChainId && chain.bridgeType.includes(BridgeType.IBC)
99104
);
100105

101-
const sourceChain = chains.namada || null;
102-
const [selectedChainId, setSelectedChainId] = useState(defaultChainId);
103-
const destinationChain = bridgedChains[0];
106+
const [destinationChainId, setDestinationChainId] = useState(ibcChains[0].chainId);
107+
const destinationChain = ibcChains[0];
104108

105-
const selectDestinationChainData = bridgedChains.map((chain) => ({
109+
const selectDestinationChainData = ibcChains.map((chain) => ({
106110
value: chain.chainId,
107111
label: chain.alias,
108112
}));
109113

110-
const [destinationIntegration, _isDestinationConnectingToExtension, withDestinationConnection] =
111-
useIntegrationConnection(destinationChain.id);
112-
113114
const [sourceIntegration, _isSourceConnectingToExtension, withSourceConnection] =
114115
useIntegrationConnection(sourceChain.id);
115116

117+
const [destinationIntegration, _isDestinationConnectingToExtension, withDestinationConnection] =
118+
useIntegrationConnection(destinationChain.id);
119+
116120
const [amount, setAmount] = useState<BigNumber>(new BigNumber(0));
117121
const [selectedChannelId, setSelectedChannelId] = useState("");
118122
const [showAddChannelForm, setShowAddChannelForm] = useState(false);
@@ -126,25 +130,21 @@ const IBCTransfer = (): JSX.Element => {
126130
const [sourceAccount, setSourceAccount] = useState<Account>();
127131
const [token, setToken] = useState<TokenType>(chains.namada.currency.symbol as TokenType);
128132

129-
const chain: Chain = Object.values(chains).find((chain: Chain) => chain.chainId === chainId) || chains.namada;
130-
const sourceExtensionAlias = Extensions[sourceChain.extension.id].alias;
131-
const destinationExtensionAlias = Extensions[destinationChain.extension.id].alias;
132-
133-
const extensionAttachStatus = useUntilIntegrationAttached(chain);
133+
const extensionAttachStatus = useUntilIntegrationAttached(sourceChain);
134134
const currentExtensionAttachStatus =
135-
extensionAttachStatus[chain.extension.id];
135+
extensionAttachStatus[sourceChain.extension.id];
136136

137137
const channels =
138-
channelsByChain[chainId] && channelsByChain[chainId][selectedChainId]
139-
? [...channelsByChain[chainId][selectedChainId]].reverse()
138+
channelsByChain[sourceChainId] && channelsByChain[sourceChainId][destinationChainId]
139+
? [...channelsByChain[sourceChainId][destinationChainId]].reverse()
140140
: [];
141141

142142
const selectChannelsData = channels.map((channel: string) => ({
143143
value: channel,
144144
label: channel,
145145
}));
146146

147-
const accounts = Object.values(derived[chain.id]);
147+
const accounts = Object.values(derived[sourceChain.id]);
148148
const sourceAccounts = accounts.filter(({ details }) => !details.isShielded);
149149

150150
const tokenData: Option<string>[] = sourceAccounts.flatMap(
@@ -176,7 +176,7 @@ const IBCTransfer = (): JSX.Element => {
176176
}, [sourceAccount, token]);
177177

178178
useEffect(() => {
179-
const chain = Object.values(chains).find((chain) => chain.chainId === selectedChainId);
179+
const chain = Object.values(chains).find((chain) => chain.chainId === destinationChainId);
180180
if (!chain) return;
181181

182182
const destinationAccounts = Object.values(derived[chain.id]).filter(
@@ -190,58 +190,59 @@ const IBCTransfer = (): JSX.Element => {
190190
})
191191
);
192192
setDestinationAccountData(destinationAccountsData);
193-
}, [derived, selectedChainId]);
193+
}, [derived, destinationChainId]);
194194

195195
useEffect(() => {
196196
// Set recipient to first destination account
197197
if (destinationAccounts?.length > 0) {
198198
setRecipient(destinationAccounts[0].details.address);
199199
}
200-
}, [selectedChainId, destinationAccounts]);
200+
}, [destinationChainId, destinationAccounts]);
201201

202202
useEffect(() => {
203-
if (bridgedChains.length > 0) {
204-
const selectedChain = bridgedChains[0].chainId;
205-
setSelectedChainId(selectedChain);
203+
if (ibcChains.length > 0) {
204+
setDestinationChainId(ibcChains[0].chainId);
206205
setSourceAccount(sourceAccounts[0]);
207206
}
208-
}, [chainId]);
207+
}, [sourceChainId]);
209208

210209
useEffect(() => {
211210
// Set a default selectedChannelId if none are selected, but channels are available
212-
if (selectedChainId && !selectedChannelId) {
213-
const chains = channelsByChain[chainId] || {};
214-
const channels = chains[selectedChainId] || [];
211+
if (destinationChainId && !selectedChannelId) {
212+
const chains = channelsByChain[sourceChainId] || {};
213+
const channels = chains[destinationChainId] || [];
215214
if (channels && channels.length > 0) {
216215
setSelectedChannelId(channels[channels.length - 1]);
217216
}
218217
}
219-
}, [selectedChainId, channelsByChain]);
218+
}, [destinationChainId, channelsByChain]);
220219

221220
const handleFocus = (e: React.ChangeEvent<HTMLInputElement>): void =>
222221
e.target.select();
223222

224223
// transform for select component
225-
const networks = Object.values(chains).map(({ chainId, alias }: Chain) => ({
226-
label: alias,
227-
value: chainId,
228-
}));
224+
const networks = Object.values(chains)
225+
.filter((chain) => chain.bridgeType.includes(BridgeType.IBC))
226+
.map(({ chainId, alias }: Chain) => ({
227+
label: alias,
228+
value: chainId,
229+
}));
229230

230231
const handleNetworkSelect = (
231232
event: React.ChangeEvent<HTMLSelectElement>
232233
): void => {
233234
const { value: chainId } = event.target;
234-
setChainId(chainId)
235-
setToken(chain.currency.symbol as TokenType)
235+
setSourceChainId(chainId)
236+
setToken(sourceChain.currency.symbol as TokenType)
236237
};
237238

238239
const { portId = "transfer" } = sourceChain.ibc || {};
239240
const handleAddChannel = (): void => {
240241
if (channelId) {
241242
dispatch(
242243
addChannel({
243-
chainId,
244-
destinationChainId: selectedChainId,
244+
chainId: sourceChainId,
245+
destinationChainId: destinationChainId,
245246
channelId,
246247
})
247248
);
@@ -267,11 +268,11 @@ const IBCTransfer = (): JSX.Element => {
267268
setError(undefined)
268269
const tokens = sourceChain.chainId === defaultChainId ? Tokens : CosmosTokens;
269270
if (sourceAccount && token) {
270-
submitIbcTransfer({
271+
submitIbcTransfer(sourceChain.id, {
271272
account: sourceAccount.details,
272273
token: tokens[token as TokenType & CosmosTokenType],
273274
amount,
274-
chainId,
275+
chainId: sourceChainId,
275276
target: recipient,
276277
channelId: selectedChannelId,
277278
portId,
@@ -287,45 +288,45 @@ const IBCTransfer = (): JSX.Element => {
287288
const accounts = await sourceIntegration?.accounts();
288289
if (accounts) {
289290
dispatch(addAccounts(accounts as AccountType[]));
290-
dispatch(setIsConnected(chain.chainId));
291+
dispatch(setIsConnected(sourceChain.chainId));
291292
}
292293

293294
setIsExtensionConnected({
294295
...isExtensionConnected,
295-
[chain.extension.id]: true,
296+
[sourceChain.extension.id]: true,
296297
});
297298
},
298299
async () => {
299300
setIsExtensionConnected({
300301
...isExtensionConnected,
301-
[chain.extension.id]: false,
302+
[sourceChain.extension.id]: false,
302303
});
303304
}
304305
);
305-
}, [chain]);
306+
}, [sourceChain]);
306307

307308
const handleConnectDestinationExtension = useCallback(async (): Promise<void> => {
308309
withDestinationConnection(
309310
async () => {
310311
const accounts = await destinationIntegration?.accounts();
311312
if (accounts) {
312313
dispatch(addAccounts(accounts as AccountType[]));
313-
dispatch(setIsConnected(chainId));
314+
dispatch(setIsConnected(sourceChainId));
314315
}
315316

316317
setIsExtensionConnected({
317318
...isExtensionConnected,
318-
[chain.extension.id]: true,
319+
[sourceChain.extension.id]: true,
319320
});
320321
},
321322
async () => {
322323
setIsExtensionConnected({
323324
...isExtensionConnected,
324-
[chain.extension.id]: false,
325+
[sourceChain.extension.id]: false,
325326
});
326327
}
327328
);
328-
}, [chain]);
329+
}, [sourceChain]);
329330

330331
const handleDownloadExtension = (url: string): void => {
331332
window.open(url, "_blank", "noopener,noreferrer");
@@ -364,7 +365,7 @@ const IBCTransfer = (): JSX.Element => {
364365
currentBalance,
365366
destinationChain,
366367
recipient,
367-
selectedChainId,
368+
destinationChainId,
368369
selectedChannelId,
369370
sourceAccount,
370371
]);
@@ -376,7 +377,7 @@ const IBCTransfer = (): JSX.Element => {
376377
{error && <Alert type="error">{error}</Alert>}
377378
<InputContainer>
378379
<Select
379-
value={chainId}
380+
value={sourceChainId}
380381
data={networks}
381382
label="Source chain"
382383
onChange={handleNetworkSelect}
@@ -401,7 +402,7 @@ const IBCTransfer = (): JSX.Element => {
401402
>
402403
{currentExtensionAttachStatus === "attached" ||
403404
currentExtensionAttachStatus === "pending"
404-
? `Load accounts from ${sourceExtensionAlias} Extension`
405+
? `Load accounts from ${Extensions[sourceChain.extension.id].alias} Extension`
405406
: "Click to download the extension"}
406407
</ActionButton>
407408
)}
@@ -423,11 +424,11 @@ const IBCTransfer = (): JSX.Element => {
423424
<InputContainer>
424425
<Select<string>
425426
data={selectDestinationChainData}
426-
value={selectedChainId}
427+
value={destinationChainId}
427428
label="Destination Chain"
428429
onChange={(e) => {
429430
setRecipient("");
430-
setSelectedChainId(e.target.value);
431+
setDestinationChainId(e.target.value);
431432
}}
432433
/>
433434
</InputContainer>
@@ -483,7 +484,7 @@ const IBCTransfer = (): JSX.Element => {
483484
</ActionButton>
484485
</InputContainer>
485486
)}
486-
{!isExtensionConnected[chain.extension.id] &&
487+
{!isExtensionConnected[sourceChain.extension.id] &&
487488
destinationAccounts.length === 0 && (
488489
<ActionButton
489490
onClick={
@@ -502,7 +503,7 @@ const IBCTransfer = (): JSX.Element => {
502503
>
503504
{currentExtensionAttachStatus === "attached" ||
504505
currentExtensionAttachStatus === "pending"
505-
? `Load accounts from ${destinationExtensionAlias} Extension`
506+
? `Load accounts from ${Extensions[destinationChain.extension.id].alias} Extension`
506507
: "Click to download the extension"}
507508
</ActionButton>
508509
)}

0 commit comments

Comments
 (0)