From c36c663604eb8fab010ae45aacd5e36630a0748f Mon Sep 17 00:00:00 2001 From: N V Rakesh Reddy Date: Tue, 4 Nov 2025 11:19:04 +0530 Subject: [PATCH] chore(sdk-coin-polyx): use token instead of coin in tx decoding TICKET: WIN-7579 --- modules/sdk-coin-polyx/src/lib/transaction.ts | 35 ++++++++++++++++--- .../preApproveAssetBuilder.ts | 2 +- .../tokenTransferBuilder.ts | 2 +- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/modules/sdk-coin-polyx/src/lib/transaction.ts b/modules/sdk-coin-polyx/src/lib/transaction.ts index 563d94dc3c..434d0bb49d 100644 --- a/modules/sdk-coin-polyx/src/lib/transaction.ts +++ b/modules/sdk-coin-polyx/src/lib/transaction.ts @@ -2,6 +2,7 @@ import { Transaction as SubstrateTransaction, utils, KeyPair } from '@bitgo/abst import { InvalidTransactionError, TransactionType } from '@bitgo/sdk-core'; import { construct, decode } from '@substrate/txwrapper-polkadot'; import { decodeAddress } from '@polkadot/keyring'; +import { coins, PolyxCoin } from '@bitgo/statics'; import { DecodedTx, RegisterDidWithCDDArgs, @@ -24,6 +25,28 @@ export class Transaction extends SubstrateTransaction { return polyxUtils.getAddressFormat(this._coinConfig.name); } + /** + * Helper method to find a token name by assetId + * @param assetId The assetId to search for + * @returns The token name if found + * @throws {InvalidTransactionError} If no matching token is found + */ + private getTokenNameByAssetId(assetId: string): string { + // Search through all coins to find the one with matching assetId + let foundToken: string | undefined; + coins.forEach((coin) => { + if (coin instanceof PolyxCoin && coin.assetId === assetId) { + foundToken = coin.name; + } + }); + + if (!foundToken) { + throw new InvalidTransactionError(`No token found for assetId: ${assetId}`); + } + + return foundToken; + } + /** @inheritdoc */ toJson(): TxData { if (!this._substrateTransaction) { @@ -128,19 +151,21 @@ export class Transaction extends SubstrateTransaction { } private decodeInputsAndOutputsForPreApproveAsset(decodedTx: DecodedTx) { + const txMethod = decodedTx.method.args as PreApproveAssetArgs; const sender = decodedTx.address; const value = '0'; // Pre-approval does not transfer any value + const tokenName = this.getTokenNameByAssetId(txMethod.assetId); this._inputs.push({ address: sender, value, - coin: this._coinConfig.name, + coin: tokenName, }); this._outputs.push({ address: sender, // In pre-approval, the output is the same as the input value, - coin: this._coinConfig.name, + coin: tokenName, }); } @@ -149,17 +174,19 @@ export class Transaction extends SubstrateTransaction { const fromDID = txMethod.legs[0].fungible.sender.did; const toDID = txMethod.legs[0].fungible.receiver.did; const amount = txMethod.legs[0].fungible.amount.toString(); + const assetId = txMethod.legs[0].fungible.assetId; + const tokenName = this.getTokenNameByAssetId(assetId); this._inputs.push({ address: fromDID, value: amount, - coin: this._coinConfig.name, + coin: tokenName, }); this._outputs.push({ address: toDID, value: amount, - coin: this._coinConfig.name, + coin: tokenName, }); } diff --git a/modules/sdk-coin-polyx/test/unit/transactionBuilder/preApproveAssetBuilder.ts b/modules/sdk-coin-polyx/test/unit/transactionBuilder/preApproveAssetBuilder.ts index 39d5f03a8a..504438df4c 100644 --- a/modules/sdk-coin-polyx/test/unit/transactionBuilder/preApproveAssetBuilder.ts +++ b/modules/sdk-coin-polyx/test/unit/transactionBuilder/preApproveAssetBuilder.ts @@ -10,7 +10,7 @@ describe('Polyx Pre Approve Asset Builder - Testnet', () => { let builder: PreApproveAssetBuilder; const sender = accounts.rbitgoTokenOwner; - const assetId = '0x2ffe769d862a89948e1ccf1423bfc7f8'; + const assetId = '0x780602887b358cf48989d0d9aa6c8d28'; beforeEach(() => { const config = buildTestConfig(); diff --git a/modules/sdk-coin-polyx/test/unit/transactionBuilder/tokenTransferBuilder.ts b/modules/sdk-coin-polyx/test/unit/transactionBuilder/tokenTransferBuilder.ts index cee39dbf94..fd1ccc5b9c 100644 --- a/modules/sdk-coin-polyx/test/unit/transactionBuilder/tokenTransferBuilder.ts +++ b/modules/sdk-coin-polyx/test/unit/transactionBuilder/tokenTransferBuilder.ts @@ -12,7 +12,7 @@ describe('Polyx token transfer Builder - Testnet', () => { const sender = accounts.rbitgoTokenOwner; const senderDID = '0x28e8649fec23dd688090b9b5bb950fd34bf20a014cf05542e3ad0264915ee775'; const receiverDID = '0x9202856204a721d2f5e8b85408067d54f1ca84390bf4f558b5615a5a6d3bddb8'; - const assetId = '0x2ffe769d862a89948e1ccf1423bfc7f8'; + const assetId = '0x780602887b358cf48989d0d9aa6c8d28'; beforeEach(() => { const config = buildTestConfig();