@@ -52,21 +52,23 @@ import { getInstructionType } from './utils';
5252export function instructionParamsFactory (
5353 type : TransactionType ,
5454 instructions : TransactionInstruction [ ] ,
55- coinName ?: string
55+ coinName ?: string ,
56+ instructionMetadata ?: InstructionParams [ ] ,
57+ _useTokenAddressTokenName ?: boolean
5658) : InstructionParams [ ] {
5759 switch ( type ) {
5860 case TransactionType . WalletInitialization :
5961 return parseWalletInitInstructions ( instructions ) ;
6062 case TransactionType . Send :
61- return parseSendInstructions ( instructions ) ;
63+ return parseSendInstructions ( instructions , instructionMetadata , _useTokenAddressTokenName ) ;
6264 case TransactionType . StakingActivate :
6365 return parseStakingActivateInstructions ( instructions ) ;
6466 case TransactionType . StakingDeactivate :
6567 return parseStakingDeactivateInstructions ( instructions , coinName ) ;
6668 case TransactionType . StakingWithdraw :
6769 return parseStakingWithdrawInstructions ( instructions ) ;
6870 case TransactionType . AssociatedTokenAccountInitialization :
69- return parseAtaInitInstructions ( instructions ) ;
71+ return parseAtaInitInstructions ( instructions , instructionMetadata , _useTokenAddressTokenName ) ;
7072 case TransactionType . CloseAssociatedTokenAccount :
7173 return parseAtaCloseInstructions ( instructions ) ;
7274 case TransactionType . StakingAuthorize :
@@ -120,7 +122,9 @@ function parseWalletInitInstructions(instructions: TransactionInstruction[]): Ar
120122 * @returns {InstructionParams[] } An array containing instruction params for Send tx
121123 */
122124function parseSendInstructions (
123- instructions : TransactionInstruction [ ]
125+ instructions : TransactionInstruction [ ] ,
126+ instructionMetadata ?: InstructionParams [ ] ,
127+ _useTokenAddressTokenName ?: boolean
124128) : Array < Nonce | Memo | Transfer | TokenTransfer | AtaInit | AtaClose | SetPriorityFee > {
125129 const instructionData : Array < Nonce | Memo | Transfer | TokenTransfer | AtaInit | AtaClose | SetPriorityFee > = [ ] ;
126130 for ( const instruction of instructions ) {
@@ -160,7 +164,12 @@ function parseSendInstructions(
160164 } else {
161165 tokenTransferInstruction = decodeTransferCheckedInstruction ( instruction , TOKEN_2022_PROGRAM_ID ) ;
162166 }
163- const tokenName = findTokenName ( tokenTransferInstruction . keys . mint . pubkey . toString ( ) ) ;
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+ }
164173 const tokenTransfer : TokenTransfer = {
165174 type : InstructionBuilderTypes . TokenTransfer ,
166175 params : {
@@ -169,13 +178,20 @@ function parseSendInstructions(
169178 amount : tokenTransferInstruction . data . amount . toString ( ) ,
170179 tokenName,
171180 sourceAddress : tokenTransferInstruction . keys . source . pubkey . toString ( ) ,
181+ tokenAddress : tokenAddress ,
182+ programId : programIDForTokenTransfer ,
183+ decimalPlaces : tokenTransferInstruction . data . decimals ,
172184 } ,
173185 } ;
174186 instructionData . push ( tokenTransfer ) ;
175187 break ;
176188 case ValidInstructionTypesEnum . InitializeAssociatedTokenAccount :
177189 const mintAddress = instruction . keys [ ataInitInstructionKeysIndexes . MintAddress ] . pubkey . toString ( ) ;
178- const mintTokenName = findTokenName ( mintAddress ) ;
190+ const mintTokenName = findTokenName ( mintAddress , instructionMetadata , _useTokenAddressTokenName ) ;
191+ let programID : string | undefined ;
192+ if ( instruction . programId ) {
193+ programID = instruction . programId . toString ( ) ;
194+ }
179195
180196 const ataInit : AtaInit = {
181197 type : InstructionBuilderTypes . CreateAssociatedTokenAccount ,
@@ -185,6 +201,7 @@ function parseSendInstructions(
185201 ownerAddress : instruction . keys [ ataInitInstructionKeysIndexes . OwnerAddress ] . pubkey . toString ( ) ,
186202 payerAddress : instruction . keys [ ataInitInstructionKeysIndexes . PayerAddress ] . pubkey . toString ( ) ,
187203 tokenName : mintTokenName ,
204+ programId : programID ,
188205 } ,
189206 } ;
190207 instructionData . push ( ataInit ) ;
@@ -652,7 +669,11 @@ const closeAtaInstructionKeysIndexes = {
652669 * @param {TransactionInstruction[] } instructions - an array of supported Solana instructions
653670 * @returns {InstructionParams[] } An array containing instruction params for Send tx
654671 */
655- function parseAtaInitInstructions ( instructions : TransactionInstruction [ ] ) : Array < AtaInit | Memo | Nonce > {
672+ function parseAtaInitInstructions (
673+ instructions : TransactionInstruction [ ] ,
674+ instructionMetadata ?: InstructionParams [ ] ,
675+ _useTokenAddressTokenName ?: boolean
676+ ) : Array < AtaInit | Memo | Nonce > {
656677 const instructionData : Array < AtaInit | Memo | Nonce > = [ ] ;
657678 let memo : Memo | undefined ;
658679
@@ -675,8 +696,11 @@ function parseAtaInitInstructions(instructions: TransactionInstruction[]): Array
675696 break ;
676697 case ValidInstructionTypesEnum . InitializeAssociatedTokenAccount :
677698 const mintAddress = instruction . keys [ ataInitInstructionKeysIndexes . MintAddress ] . pubkey . toString ( ) ;
678- const tokenName = findTokenName ( mintAddress ) ;
679-
699+ const tokenName = findTokenName ( mintAddress , instructionMetadata , _useTokenAddressTokenName ) ;
700+ let programID : string | undefined ;
701+ if ( instruction . programId ) {
702+ programID = instruction . programId . toString ( ) ;
703+ }
680704 const ataInit : AtaInit = {
681705 type : InstructionBuilderTypes . CreateAssociatedTokenAccount ,
682706 params : {
@@ -685,6 +709,7 @@ function parseAtaInitInstructions(instructions: TransactionInstruction[]): Array
685709 ownerAddress : instruction . keys [ ataInitInstructionKeysIndexes . OwnerAddress ] . pubkey . toString ( ) ,
686710 payerAddress : instruction . keys [ ataInitInstructionKeysIndexes . PayerAddress ] . pubkey . toString ( ) ,
687711 tokenName,
712+ programId : programID ,
688713 } ,
689714 } ;
690715 instructionData . push ( ataInit ) ;
@@ -831,7 +856,11 @@ function parseStakingAuthorizeRawInstructions(instructions: TransactionInstructi
831856 return instructionData ;
832857}
833858
834- function findTokenName ( mintAddress : string ) : string {
859+ function findTokenName (
860+ mintAddress : string ,
861+ instructionMetadata ?: InstructionParams [ ] ,
862+ _useTokenAddressTokenName ?: boolean
863+ ) : string {
835864 let token : string | undefined ;
836865
837866 coins . forEach ( ( value , key ) => {
@@ -840,6 +869,26 @@ function findTokenName(mintAddress: string): string {
840869 }
841870 } ) ;
842871
872+ if ( ! token && instructionMetadata ) {
873+ instructionMetadata . forEach ( ( instruction ) => {
874+ if (
875+ instruction . type === InstructionBuilderTypes . CreateAssociatedTokenAccount &&
876+ instruction . params . mintAddress === mintAddress
877+ ) {
878+ token = instruction . params . tokenName ;
879+ } else if (
880+ instruction . type === InstructionBuilderTypes . TokenTransfer &&
881+ instruction . params . tokenAddress === mintAddress
882+ ) {
883+ token = instruction . params . tokenName ;
884+ }
885+ } ) ;
886+ }
887+
888+ if ( ! token && _useTokenAddressTokenName ) {
889+ token = mintAddress ;
890+ }
891+
843892 assert ( token ) ;
844893
845894 return token ;
0 commit comments