Skip to content

Commit

Permalink
optimize decode
Browse files Browse the repository at this point in the history
  • Loading branch information
marxeille committed Nov 20, 2024
1 parent 118b43d commit 2d0918f
Showing 1 changed file with 61 additions and 48 deletions.
109 changes: 61 additions & 48 deletions apps/extension/src/pages/sign/components/render-cosmos-args.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down

0 comments on commit 2d0918f

Please sign in to comment.