From 41bfce3ba4248887c8050f3cceeb92fd3bc27a27 Mon Sep 17 00:00:00 2001 From: Kurdistan Tech Ministry Date: Sat, 1 Nov 2025 15:27:57 +0300 Subject: [PATCH] fix: Correct token symbol parsing in swap history MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Fixed path array parsing for SwapExecuted events - Handle different Polkadot.js enum formats (nativeOrAsset vs NativeOrAsset) - Added toJSON() conversion for proper array parsing - Now correctly shows HEZ↔PEZ swaps instead of HEZ→HEZ - Applied fix to both initial history load and post-swap refresh 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- src/components/TokenSwap.tsx | 52 ++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/src/components/TokenSwap.tsx b/src/components/TokenSwap.tsx index 37c12129..8cf0c523 100644 --- a/src/components/TokenSwap.tsx +++ b/src/components/TokenSwap.tsx @@ -370,9 +370,32 @@ const TokenSwap = () => { if (api.events.assetConversion?.SwapExecuted?.is(event)) { const [who, path, amountIn, amountOut] = event.data; - // Parse path to get token symbols - const fromAssetId = path[0]?.nativeOrAsset?.asset?.toNumber() || 0; - const toAssetId = path[1]?.nativeOrAsset?.asset?.toNumber() || 0; + // Parse path to get token symbols - path is Vec + let fromAssetId = 0; + let toAssetId = 0; + + try { + // Try different path formats + const pathArray = path.toJSON ? path.toJSON() : path; + + if (Array.isArray(pathArray) && pathArray.length >= 2) { + // Extract asset IDs from path + const asset0 = pathArray[0]; + const asset1 = pathArray[1]; + + // Handle different enum formats + if (typeof asset0 === 'object') { + fromAssetId = asset0.nativeOrAsset?.asset || asset0.NativeOrAsset?.Asset || 0; + } + if (typeof asset1 === 'object') { + toAssetId = asset1.nativeOrAsset?.asset || asset1.NativeOrAsset?.Asset || 0; + } + } + + console.log('🔍 Parsed swap path:', { pathArray, fromAssetId, toAssetId }); + } catch (err) { + console.warn('Failed to parse path:', err); + } const fromTokenSymbol = fromAssetId === 0 ? 'wHEZ' : fromAssetId === 1 ? 'PEZ' : `Asset${fromAssetId}`; const toTokenSymbol = toAssetId === 0 ? 'wHEZ' : toAssetId === 1 ? 'PEZ' : `Asset${toAssetId}`; @@ -591,10 +614,29 @@ const TokenSwap = () => { const { event } = record; if (api.events.assetConversion?.SwapExecuted?.is(event)) { const [who, path, amountIn, amountOut] = event.data; - const fromAssetId = path[0]?.nativeOrAsset?.asset?.toNumber() || 0; - const toAssetId = path[1]?.nativeOrAsset?.asset?.toNumber() || 0; + + // Parse path (same logic as main history fetch) + let fromAssetId = 0; + let toAssetId = 0; + try { + const pathArray = path.toJSON ? path.toJSON() : path; + if (Array.isArray(pathArray) && pathArray.length >= 2) { + const asset0 = pathArray[0]; + const asset1 = pathArray[1]; + if (typeof asset0 === 'object') { + fromAssetId = asset0.nativeOrAsset?.asset || asset0.NativeOrAsset?.Asset || 0; + } + if (typeof asset1 === 'object') { + toAssetId = asset1.nativeOrAsset?.asset || asset1.NativeOrAsset?.Asset || 0; + } + } + } catch (err) { + console.warn('Failed to parse path in refresh:', err); + } + const fromTokenSymbol = fromAssetId === 0 ? 'wHEZ' : fromAssetId === 1 ? 'PEZ' : `Asset${fromAssetId}`; const toTokenSymbol = toAssetId === 0 ? 'wHEZ' : toAssetId === 1 ? 'PEZ' : `Asset${toAssetId}`; + if (who.toString() === selectedAccount.address) { transactions.push({ blockNumber: blockNum,