From 2d0918ff51fdd3f4fe43b6b702cf4504072e1058 Mon Sep 17 00:00:00 2001 From: sonlha Date: Wed, 20 Nov 2024 18:11:54 +0700 Subject: [PATCH] optimize decode --- .../sign/components/render-cosmos-args.tsx | 109 ++++++++++-------- 1 file changed, 61 insertions(+), 48 deletions(-) diff --git a/apps/extension/src/pages/sign/components/render-cosmos-args.tsx b/apps/extension/src/pages/sign/components/render-cosmos-args.tsx index 218bcb373..a1fcf317a 100644 --- a/apps/extension/src/pages/sign/components/render-cosmos-args.tsx +++ b/apps/extension/src/pages/sign/components/render-cosmos-args.tsx @@ -211,73 +211,86 @@ export const CosmosRenderArgs: FunctionComponent<{ console.log('txInfo', txInfo); - const extraInfo = decodeMsg?.send?.msg ? atob(decodeMsg.send.msg) : null; - txInfo.extraInfo = extraInfo ? JSON.parse(extraInfo) : null; + const parseJSON = data => { + try { + return JSON.parse(data); + } catch { + return null; + } + }; - const decodeData = txInfo.extraInfo ?? txInfo.decode; - let fromToken; - let toToken; - let toAddress; + const extractLastOperation = operations => operations?.[operations.length - 1]; - if (decodeData.swap_and_action?.post_swap_action?.transfer?.to_address) { - toAddress = decodeData.swap_and_action.post_swap_action.transfer.to_address; - } + const getSwapTokens = swapData => { + const firstOperation = swapData?.operations?.[0]; + const lastOperation = extractLastOperation(swapData?.operations); - if (decodeData.swap_and_action?.user_swap?.swap_exact_asset_in?.operations?.[0]) { - fromToken = getIconWithChainRegistry( - decodeData.swap_and_action.user_swap?.swap_exact_asset_in?.operations?.[0]?.denom_in - ); - } + return { + fromToken: firstOperation ? getIconWithChainRegistry(firstOperation.denom_in) : null, + toToken: lastOperation ? getIconWithChainRegistry(lastOperation.denom_out) : null + }; + }; - const lastItem = decodeData.swap_and_action?.user_swap?.swap_exact_asset_in?.operations?.pop(); + const getTokenFromCW20 = (chainStore, chainId, address) => { + const chainInfo = chainStore.chainInfos.find(c => c.chainId === chainId); + return chainInfo?.currencies.find(cur => cur.contractAddress === address) ?? null; + }; - if (lastItem) { - toToken = getIconWithChainRegistry(lastItem.denom_out); - } + // Decode extra information + const extraInfo = decodeMsg?.send?.msg ? atob(decodeMsg.send.msg) : null; + txInfo.extraInfo = extraInfo ? parseJSON(extraInfo) : null; - if (decodeData.swap_and_action?.post_swap_action?.ibc_transfer?.ibc_info) { - toAddress = decodeData.swap_and_action.post_swap_action.ibc_transfer.ibc_info.receiver; - if (decodeData.swap_and_action.post_swap_action.ibc_transfer.ibc_info.memo !== '') { - const info = JSON.parse(decodeData.swap_and_action.post_swap_action.ibc_transfer.ibc_info.memo); - const next = JSON.parse(info.forward?.next); + const decodeData = txInfo.extraInfo ?? txInfo.decode; - if (next) { - const lastDes = next.wasm?.msg?.swap_and_action?.user_swap?.swap_exact_asset_in?.operations?.pop(); + let fromToken = null; + let toToken = null; + let toAddress = null; + // Handle swap and transfer data + if (decodeData.swap_and_action) { + const swapAction = decodeData.swap_and_action; + + // Extract toAddress from post-swap actions + toAddress = + swapAction.post_swap_action?.transfer?.to_address || + swapAction.post_swap_action?.ibc_transfer?.ibc_info?.receiver; + + // Extract tokens from swap operations + if (swapAction.user_swap?.swap_exact_asset_in) { + const swapTokens = getSwapTokens(swapAction.user_swap.swap_exact_asset_in); + fromToken = swapTokens.fromToken; + toToken = swapTokens.toToken; + } + + // Handle IBC transfer memo + const ibcMemo = swapAction.post_swap_action?.ibc_transfer?.ibc_info?.memo; + if (ibcMemo) { + const info = parseJSON(ibcMemo); + const next = parseJSON(info?.forward?.next); + if (next) { + const lastDes = extractLastOperation( + next.wasm?.msg?.swap_and_action?.user_swap?.swap_exact_asset_in?.operations + ); if (lastDes) { toToken = getIconWithChainRegistry(lastDes.denom_out); } } } - } - if (decodeData.swap_and_action) { - if (decodeData.swap_and_action.min_asset) { - if (decodeData.swap_and_action.min_asset.cw20) { - chainStore.chainInfos.forEach(c => { - if (c.chainId === chain?.chainId) { - toToken = c.currencies.find( - //@ts-ignore - cur => cur.contractAddress === decodeData.swap_and_action.min_asset.cw20.address - ); - } - }); - } + // Extract token from CW20 asset + const minAsset = swapAction.min_asset?.cw20; + if (minAsset) { + toToken = getTokenFromCW20(chainStore, chain?.chainId, minAsset.address); } } + // Handle memo data if (decodeData.memo && !decodeData.memo.startsWith('orai')) { - const info = JSON.parse(decodeData.memo); - + const info = parseJSON(decodeData.memo); if (info) { - const firstDes = info.wasm?.msg?.swap_and_action?.user_swap?.swap_exact_asset_in?.operations?.[0]; - const lastDes = info.wasm?.msg?.swap_and_action?.user_swap?.swap_exact_asset_in?.operations?.pop(); - if (firstDes) { - fromToken = getIconWithChainRegistry(firstDes.denom_in); - } - if (lastDes) { - toToken = getIconWithChainRegistry(lastDes.denom_out); - } + const swapTokens = getSwapTokens(info.wasm?.msg?.swap_and_action?.user_swap?.swap_exact_asset_in); + fromToken = swapTokens.fromToken || fromToken; + toToken = swapTokens.toToken || toToken; } }