@@ -53,21 +53,22 @@ export function instructionParamsFactory(
5353 type : TransactionType ,
5454 instructions : TransactionInstruction [ ] ,
5555 coinName ?: string ,
56- instructionMetadata ?: InstructionParams [ ]
56+ instructionMetadata ?: InstructionParams [ ] ,
57+ _useTokenAddressTokenName ?: boolean
5758) : InstructionParams [ ] {
5859 switch ( type ) {
5960 case TransactionType . WalletInitialization :
6061 return parseWalletInitInstructions ( instructions ) ;
6162 case TransactionType . Send :
62- return parseSendInstructions ( instructions , instructionMetadata ) ;
63+ return parseSendInstructions ( instructions , instructionMetadata , _useTokenAddressTokenName ) ;
6364 case TransactionType . StakingActivate :
6465 return parseStakingActivateInstructions ( instructions ) ;
6566 case TransactionType . StakingDeactivate :
6667 return parseStakingDeactivateInstructions ( instructions , coinName ) ;
6768 case TransactionType . StakingWithdraw :
6869 return parseStakingWithdrawInstructions ( instructions ) ;
6970 case TransactionType . AssociatedTokenAccountInitialization :
70- return parseAtaInitInstructions ( instructions , instructionMetadata ) ;
71+ return parseAtaInitInstructions ( instructions , instructionMetadata , _useTokenAddressTokenName ) ;
7172 case TransactionType . CloseAssociatedTokenAccount :
7273 return parseAtaCloseInstructions ( instructions ) ;
7374 case TransactionType . StakingAuthorize :
@@ -122,7 +123,8 @@ function parseWalletInitInstructions(instructions: TransactionInstruction[]): Ar
122123 */
123124function parseSendInstructions (
124125 instructions : TransactionInstruction [ ] ,
125- instructionMetadata ?: InstructionParams [ ]
126+ instructionMetadata ?: InstructionParams [ ] ,
127+ _useTokenAddressTokenName ?: boolean
126128) : Array < Nonce | Memo | Transfer | TokenTransfer | AtaInit | AtaClose | SetPriorityFee > {
127129 const instructionData : Array < Nonce | Memo | Transfer | TokenTransfer | AtaInit | AtaClose | SetPriorityFee > = [ ] ;
128130 for ( const instruction of instructions ) {
@@ -162,7 +164,12 @@ function parseSendInstructions(
162164 } else {
163165 tokenTransferInstruction = decodeTransferCheckedInstruction ( instruction , TOKEN_2022_PROGRAM_ID ) ;
164166 }
165- const tokenName = findTokenName ( tokenTransferInstruction . keys . mint . pubkey . toString ( ) , instructionMetadata ) ;
167+ const tokenAddress = tokenTransferInstruction . keys . mint . pubkey . toString ( ) ;
168+ const tokenName = findTokenName ( tokenAddress , instructionMetadata , _useTokenAddressTokenName ) ;
169+ let programIDForTokenTransfer : string | undefined ;
170+ if ( instruction . programId ) {
171+ programIDForTokenTransfer = instruction . programId . toString ( ) ;
172+ }
166173 const tokenTransfer : TokenTransfer = {
167174 type : InstructionBuilderTypes . TokenTransfer ,
168175 params : {
@@ -171,13 +178,20 @@ function parseSendInstructions(
171178 amount : tokenTransferInstruction . data . amount . toString ( ) ,
172179 tokenName,
173180 sourceAddress : tokenTransferInstruction . keys . source . pubkey . toString ( ) ,
181+ tokenAddress : tokenAddress ,
182+ programId : programIDForTokenTransfer ,
183+ decimalPlaces : tokenTransferInstruction . data . decimals ,
174184 } ,
175185 } ;
176186 instructionData . push ( tokenTransfer ) ;
177187 break ;
178188 case ValidInstructionTypesEnum . InitializeAssociatedTokenAccount :
179189 const mintAddress = instruction . keys [ ataInitInstructionKeysIndexes . MintAddress ] . pubkey . toString ( ) ;
180- const mintTokenName = findTokenName ( mintAddress , instructionMetadata ) ;
190+ const mintTokenName = findTokenName ( mintAddress , instructionMetadata , _useTokenAddressTokenName ) ;
191+ let programID : string | undefined ;
192+ if ( instruction . programId ) {
193+ programID = instruction . programId . toString ( ) ;
194+ }
181195
182196 const ataInit : AtaInit = {
183197 type : InstructionBuilderTypes . CreateAssociatedTokenAccount ,
@@ -187,6 +201,7 @@ function parseSendInstructions(
187201 ownerAddress : instruction . keys [ ataInitInstructionKeysIndexes . OwnerAddress ] . pubkey . toString ( ) ,
188202 payerAddress : instruction . keys [ ataInitInstructionKeysIndexes . PayerAddress ] . pubkey . toString ( ) ,
189203 tokenName : mintTokenName ,
204+ programId : programID ,
190205 } ,
191206 } ;
192207 instructionData . push ( ataInit ) ;
@@ -656,7 +671,8 @@ const closeAtaInstructionKeysIndexes = {
656671 */
657672function parseAtaInitInstructions (
658673 instructions : TransactionInstruction [ ] ,
659- instructionMetadata ?: InstructionParams [ ]
674+ instructionMetadata ?: InstructionParams [ ] ,
675+ _useTokenAddressTokenName ?: boolean
660676) : Array < AtaInit | Memo | Nonce > {
661677 const instructionData : Array < AtaInit | Memo | Nonce > = [ ] ;
662678 let memo : Memo | undefined ;
@@ -680,8 +696,11 @@ function parseAtaInitInstructions(
680696 break ;
681697 case ValidInstructionTypesEnum . InitializeAssociatedTokenAccount :
682698 const mintAddress = instruction . keys [ ataInitInstructionKeysIndexes . MintAddress ] . pubkey . toString ( ) ;
683- const tokenName = findTokenName ( mintAddress , instructionMetadata ) ;
684-
699+ const tokenName = findTokenName ( mintAddress , instructionMetadata , _useTokenAddressTokenName ) ;
700+ let programID : string | undefined ;
701+ if ( instruction . programId ) {
702+ programID = instruction . programId . toString ( ) ;
703+ }
685704 const ataInit : AtaInit = {
686705 type : InstructionBuilderTypes . CreateAssociatedTokenAccount ,
687706 params : {
@@ -690,6 +709,7 @@ function parseAtaInitInstructions(
690709 ownerAddress : instruction . keys [ ataInitInstructionKeysIndexes . OwnerAddress ] . pubkey . toString ( ) ,
691710 payerAddress : instruction . keys [ ataInitInstructionKeysIndexes . PayerAddress ] . pubkey . toString ( ) ,
692711 tokenName,
712+ programId : programID ,
693713 } ,
694714 } ;
695715 instructionData . push ( ataInit ) ;
@@ -836,7 +856,11 @@ function parseStakingAuthorizeRawInstructions(instructions: TransactionInstructi
836856 return instructionData ;
837857}
838858
839- function findTokenName ( mintAddress : string , instructionMetadata ?: InstructionParams [ ] ) : string {
859+ function findTokenName (
860+ mintAddress : string ,
861+ instructionMetadata ?: InstructionParams [ ] ,
862+ _useTokenAddressTokenName ?: boolean
863+ ) : string {
840864 let token : string | undefined ;
841865
842866 coins . forEach ( ( value , key ) => {
@@ -860,6 +884,11 @@ function findTokenName(mintAddress: string, instructionMetadata?: InstructionPar
860884 }
861885 } ) ;
862886 }
887+
888+ if ( ! token && _useTokenAddressTokenName ) {
889+ token = mintAddress ;
890+ }
891+
863892 assert ( token ) ;
864893
865894 return token ;
0 commit comments