@@ -2,6 +2,7 @@ import { Transaction as SubstrateTransaction, utils, KeyPair } from '@bitgo/abst
22import { InvalidTransactionError , TransactionType } from '@bitgo/sdk-core' ;
33import { construct , decode } from '@substrate/txwrapper-polkadot' ;
44import { decodeAddress } from '@polkadot/keyring' ;
5+ import { coins , PolyxCoin } from '@bitgo/statics' ;
56import {
67 DecodedTx ,
78 RegisterDidWithCDDArgs ,
@@ -24,6 +25,28 @@ export class Transaction extends SubstrateTransaction {
2425 return polyxUtils . getAddressFormat ( this . _coinConfig . name ) ;
2526 }
2627
28+ /**
29+ * Helper method to find a token name by assetId
30+ * @param assetId The assetId to search for
31+ * @returns The token name if found
32+ * @throws {InvalidTransactionError } If no matching token is found
33+ */
34+ private getTokenNameByAssetId ( assetId : string ) : string {
35+ // Search through all coins to find the one with matching assetId
36+ let foundToken : string | undefined ;
37+ coins . forEach ( ( coin ) => {
38+ if ( coin instanceof PolyxCoin && coin . assetId === assetId ) {
39+ foundToken = coin . name ;
40+ }
41+ } ) ;
42+
43+ if ( ! foundToken ) {
44+ throw new InvalidTransactionError ( `No token found for assetId: ${ assetId } ` ) ;
45+ }
46+
47+ return foundToken ;
48+ }
49+
2750 /** @inheritdoc */
2851 toJson ( ) : TxData {
2952 if ( ! this . _substrateTransaction ) {
@@ -128,19 +151,21 @@ export class Transaction extends SubstrateTransaction {
128151 }
129152
130153 private decodeInputsAndOutputsForPreApproveAsset ( decodedTx : DecodedTx ) {
154+ const txMethod = decodedTx . method . args as PreApproveAssetArgs ;
131155 const sender = decodedTx . address ;
132156 const value = '0' ; // Pre-approval does not transfer any value
157+ const tokenName = this . getTokenNameByAssetId ( txMethod . assetId ) ;
133158
134159 this . _inputs . push ( {
135160 address : sender ,
136161 value,
137- coin : this . _coinConfig . name ,
162+ coin : tokenName ,
138163 } ) ;
139164
140165 this . _outputs . push ( {
141166 address : sender , // In pre-approval, the output is the same as the input
142167 value,
143- coin : this . _coinConfig . name ,
168+ coin : tokenName ,
144169 } ) ;
145170 }
146171
@@ -149,17 +174,19 @@ export class Transaction extends SubstrateTransaction {
149174 const fromDID = txMethod . legs [ 0 ] . fungible . sender . did ;
150175 const toDID = txMethod . legs [ 0 ] . fungible . receiver . did ;
151176 const amount = txMethod . legs [ 0 ] . fungible . amount . toString ( ) ;
177+ const assetId = txMethod . legs [ 0 ] . fungible . assetId ;
178+ const tokenName = this . getTokenNameByAssetId ( assetId ) ;
152179
153180 this . _inputs . push ( {
154181 address : fromDID ,
155182 value : amount ,
156- coin : this . _coinConfig . name ,
183+ coin : tokenName ,
157184 } ) ;
158185
159186 this . _outputs . push ( {
160187 address : toDID ,
161188 value : amount ,
162- coin : this . _coinConfig . name ,
189+ coin : tokenName ,
163190 } ) ;
164191 }
165192
0 commit comments