@@ -2,6 +2,7 @@ import BigNumber from 'bignumber.js';
22import crypto from 'crypto' ;
33
44import { BaseUtils , isValidEd25519PublicKey , TransactionType } from '@bitgo/sdk-core' ;
5+ import { coins , CantonToken } from '@bitgo/statics' ;
56
67import { computePreparedTransaction } from '../../resources/hash/hash.js' ;
78import { PreparedTransaction } from '../../resources/proto/preparedTransaction.js' ;
@@ -93,6 +94,9 @@ export class Utils implements BaseUtils {
9394 let receiver = '' ;
9495 let amount = '' ;
9596 let memoId : string | undefined ;
97+ let instrumentId : string | undefined ;
98+ let instrumentAdmin : string | undefined ;
99+ let token : string | undefined ;
96100 let preApprovalNode : RecordField [ ] = [ ] ;
97101 let transferNode : RecordField [ ] = [ ] ;
98102 let transferAcceptRejectNode : RecordField [ ] = [ ] ;
@@ -165,6 +169,21 @@ export class Utils implements BaseUtils {
165169 const amountData = getField ( transferRecord , 'amount' ) ;
166170 if ( amountData ?. oneofKind === 'numeric' ) amount = amountData . numeric ?? '' ;
167171
172+ const instrumentField = getField ( transferRecord , 'instrumentId' ) ;
173+ if ( instrumentField ?. oneofKind === 'record' ) {
174+ const instrumentFields = instrumentField . record ?. fields ?? [ ] ;
175+
176+ const adminData = getField ( instrumentFields , 'admin' ) ;
177+ if ( adminData ?. oneofKind === 'party' ) {
178+ instrumentAdmin = adminData . party ?? '' ;
179+ }
180+
181+ const idData = getField ( instrumentFields , 'id' ) ;
182+ if ( idData ?. oneofKind === 'text' ) {
183+ instrumentId = idData . text ?? '' ;
184+ }
185+ }
186+
168187 const metaField = getField ( transferRecord , 'meta' ) ;
169188 if ( metaField ?. oneofKind === 'record' ) {
170189 const metaFields = metaField . record ?. fields ;
@@ -214,6 +233,10 @@ export class Utils implements BaseUtils {
214233 if ( memoId ) {
215234 parsedData . memoId = memoId ;
216235 }
236+ if ( instrumentId && instrumentAdmin ) {
237+ token = this . findTokenNameByContractAddress ( `${ instrumentAdmin } :${ instrumentId } ` ) ;
238+ parsedData . token = token ;
239+ }
217240 return parsedData ;
218241 }
219242
@@ -371,6 +394,21 @@ export class Utils implements BaseUtils {
371394 private convertAmountToLowestUnit ( value : BigNumber ) : string {
372395 return value . multipliedBy ( new BigNumber ( 10 ) . pow ( 10 ) ) . toFixed ( 0 ) ;
373396 }
397+
398+ /**
399+ * Get the bitgo token name using the on-chain instrument details
400+ * @param contractAddress - the contract address of the form, `instrumentAdmin:instrumentId`
401+ * @returns tokenName if contractAddress matches with any supported canton tokens
402+ */
403+ private findTokenNameByContractAddress ( contractAddress : string ) : string | undefined {
404+ if ( contractAddress . includes ( 'Amulet' ) ) {
405+ return undefined ;
406+ }
407+ const cantonToken = coins
408+ . filter ( ( coin ) => coin instanceof CantonToken && coin . contractAddress === contractAddress )
409+ . map ( ( coin ) => coin as CantonToken ) ;
410+ return cantonToken ? cantonToken [ 0 ] . name : undefined ;
411+ }
374412}
375413
376414const utils = new Utils ( ) ;
0 commit comments