@@ -52,21 +52,23 @@ import { getInstructionType } from './utils';
52
52
export function instructionParamsFactory (
53
53
type : TransactionType ,
54
54
instructions : TransactionInstruction [ ] ,
55
- coinName ?: string
55
+ coinName ?: string ,
56
+ instructionMetadata ?: InstructionParams [ ] ,
57
+ _useTokenAddressTokenName ?: boolean
56
58
) : InstructionParams [ ] {
57
59
switch ( type ) {
58
60
case TransactionType . WalletInitialization :
59
61
return parseWalletInitInstructions ( instructions ) ;
60
62
case TransactionType . Send :
61
- return parseSendInstructions ( instructions ) ;
63
+ return parseSendInstructions ( instructions , instructionMetadata , _useTokenAddressTokenName ) ;
62
64
case TransactionType . StakingActivate :
63
65
return parseStakingActivateInstructions ( instructions ) ;
64
66
case TransactionType . StakingDeactivate :
65
67
return parseStakingDeactivateInstructions ( instructions , coinName ) ;
66
68
case TransactionType . StakingWithdraw :
67
69
return parseStakingWithdrawInstructions ( instructions ) ;
68
70
case TransactionType . AssociatedTokenAccountInitialization :
69
- return parseAtaInitInstructions ( instructions ) ;
71
+ return parseAtaInitInstructions ( instructions , instructionMetadata , _useTokenAddressTokenName ) ;
70
72
case TransactionType . CloseAssociatedTokenAccount :
71
73
return parseAtaCloseInstructions ( instructions ) ;
72
74
case TransactionType . StakingAuthorize :
@@ -120,7 +122,9 @@ function parseWalletInitInstructions(instructions: TransactionInstruction[]): Ar
120
122
* @returns {InstructionParams[] } An array containing instruction params for Send tx
121
123
*/
122
124
function parseSendInstructions (
123
- instructions : TransactionInstruction [ ]
125
+ instructions : TransactionInstruction [ ] ,
126
+ instructionMetadata ?: InstructionParams [ ] ,
127
+ _useTokenAddressTokenName ?: boolean
124
128
) : Array < Nonce | Memo | Transfer | TokenTransfer | AtaInit | AtaClose | SetPriorityFee > {
125
129
const instructionData : Array < Nonce | Memo | Transfer | TokenTransfer | AtaInit | AtaClose | SetPriorityFee > = [ ] ;
126
130
for ( const instruction of instructions ) {
@@ -160,7 +164,12 @@ function parseSendInstructions(
160
164
} else {
161
165
tokenTransferInstruction = decodeTransferCheckedInstruction ( instruction , TOKEN_2022_PROGRAM_ID ) ;
162
166
}
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
+ }
164
173
const tokenTransfer : TokenTransfer = {
165
174
type : InstructionBuilderTypes . TokenTransfer ,
166
175
params : {
@@ -169,13 +178,20 @@ function parseSendInstructions(
169
178
amount : tokenTransferInstruction . data . amount . toString ( ) ,
170
179
tokenName,
171
180
sourceAddress : tokenTransferInstruction . keys . source . pubkey . toString ( ) ,
181
+ tokenAddress : tokenAddress ,
182
+ programId : programIDForTokenTransfer ,
183
+ decimalPlaces : tokenTransferInstruction . data . decimals ,
172
184
} ,
173
185
} ;
174
186
instructionData . push ( tokenTransfer ) ;
175
187
break ;
176
188
case ValidInstructionTypesEnum . InitializeAssociatedTokenAccount :
177
189
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
+ }
179
195
180
196
const ataInit : AtaInit = {
181
197
type : InstructionBuilderTypes . CreateAssociatedTokenAccount ,
@@ -185,6 +201,7 @@ function parseSendInstructions(
185
201
ownerAddress : instruction . keys [ ataInitInstructionKeysIndexes . OwnerAddress ] . pubkey . toString ( ) ,
186
202
payerAddress : instruction . keys [ ataInitInstructionKeysIndexes . PayerAddress ] . pubkey . toString ( ) ,
187
203
tokenName : mintTokenName ,
204
+ programId : programID ,
188
205
} ,
189
206
} ;
190
207
instructionData . push ( ataInit ) ;
@@ -652,7 +669,11 @@ const closeAtaInstructionKeysIndexes = {
652
669
* @param {TransactionInstruction[] } instructions - an array of supported Solana instructions
653
670
* @returns {InstructionParams[] } An array containing instruction params for Send tx
654
671
*/
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 > {
656
677
const instructionData : Array < AtaInit | Memo | Nonce > = [ ] ;
657
678
let memo : Memo | undefined ;
658
679
@@ -675,8 +696,11 @@ function parseAtaInitInstructions(instructions: TransactionInstruction[]): Array
675
696
break ;
676
697
case ValidInstructionTypesEnum . InitializeAssociatedTokenAccount :
677
698
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
+ }
680
704
const ataInit : AtaInit = {
681
705
type : InstructionBuilderTypes . CreateAssociatedTokenAccount ,
682
706
params : {
@@ -685,6 +709,7 @@ function parseAtaInitInstructions(instructions: TransactionInstruction[]): Array
685
709
ownerAddress : instruction . keys [ ataInitInstructionKeysIndexes . OwnerAddress ] . pubkey . toString ( ) ,
686
710
payerAddress : instruction . keys [ ataInitInstructionKeysIndexes . PayerAddress ] . pubkey . toString ( ) ,
687
711
tokenName,
712
+ programId : programID ,
688
713
} ,
689
714
} ;
690
715
instructionData . push ( ataInit ) ;
@@ -831,7 +856,11 @@ function parseStakingAuthorizeRawInstructions(instructions: TransactionInstructi
831
856
return instructionData ;
832
857
}
833
858
834
- function findTokenName ( mintAddress : string ) : string {
859
+ function findTokenName (
860
+ mintAddress : string ,
861
+ instructionMetadata ?: InstructionParams [ ] ,
862
+ _useTokenAddressTokenName ?: boolean
863
+ ) : string {
835
864
let token : string | undefined ;
836
865
837
866
coins . forEach ( ( value , key ) => {
@@ -840,6 +869,26 @@ function findTokenName(mintAddress: string): string {
840
869
}
841
870
} ) ;
842
871
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
+
843
892
assert ( token ) ;
844
893
845
894
return token ;
0 commit comments