1
- import {
2
- createAssociatedTokenAccountInstruction ,
3
- getAssociatedTokenAddress ,
4
- } from "@solana/spl-token" ;
1
+ import { createAssociatedTokenAccountInstruction , getAssociatedTokenAddress } from "@solana/spl-token" ;
5
2
import { SignerWalletAdapter } from "@solana/wallet-adapter-base" ;
6
3
import {
7
4
BlockheightBasedTransactionConfirmationStrategy ,
@@ -48,9 +45,7 @@ export async function getProgramAccounts(
48
45
* @param {Keypair | SignerWalletAdapter } walletOrKeypair - Wallet or Keypair in question
49
46
* @return {boolean } - Returns true if parameter is a Wallet.
50
47
*/
51
- export function isSignerWallet (
52
- walletOrKeypair : Keypair | SignerWalletAdapter
53
- ) : walletOrKeypair is SignerWalletAdapter {
48
+ export function isSignerWallet ( walletOrKeypair : Keypair | SignerWalletAdapter ) : walletOrKeypair is SignerWalletAdapter {
54
49
return ( < SignerWalletAdapter > walletOrKeypair ) . signTransaction !== undefined ;
55
50
}
56
51
@@ -59,20 +54,15 @@ export function isSignerWallet(
59
54
* @param walletOrKeypair {Keypair | SignerWalletAdapter} walletOrKeypair - Wallet or Keypair in question
60
55
* @returns {boolean } - Returns true if parameter is a Keypair.
61
56
*/
62
- export function isSignerKeypair (
63
- walletOrKeypair : Keypair | SignerWalletAdapter
64
- ) : walletOrKeypair is Keypair {
57
+ export function isSignerKeypair ( walletOrKeypair : Keypair | SignerWalletAdapter ) : walletOrKeypair is Keypair {
65
58
return (
66
59
walletOrKeypair instanceof Keypair ||
67
60
walletOrKeypair . constructor === Keypair ||
68
61
walletOrKeypair . constructor . name === Keypair . prototype . constructor . name
69
62
) ;
70
63
}
71
64
72
- export async function signTransaction (
73
- invoker : Keypair | SignerWalletAdapter ,
74
- tx : Transaction
75
- ) : Promise < Transaction > {
65
+ export async function signTransaction ( invoker : Keypair | SignerWalletAdapter , tx : Transaction ) : Promise < Transaction > {
76
66
let signedTx : Transaction ;
77
67
if ( isSignerWallet ( invoker ) ) {
78
68
signedTx = await invoker . signTransaction ( tx ) ;
@@ -103,17 +93,12 @@ export async function signAndExecuteTransaction(
103
93
if ( ! hash . lastValidBlockHeight || ! signedTx . signature || ! hash . blockhash )
104
94
throw Error ( "Error with transaction parameters." ) ;
105
95
106
- const confirmationStrategy : BlockheightBasedTransactionConfirmationStrategy =
107
- {
108
- lastValidBlockHeight : hash . lastValidBlockHeight ,
109
- signature : bs58 . encode ( signedTx . signature ) ,
110
- blockhash : hash . blockhash ,
111
- } ;
112
- const signature = await sendAndConfirmRawTransaction (
113
- connection ,
114
- rawTx ,
115
- confirmationStrategy
116
- ) ;
96
+ const confirmationStrategy : BlockheightBasedTransactionConfirmationStrategy = {
97
+ lastValidBlockHeight : hash . lastValidBlockHeight ,
98
+ signature : bs58 . encode ( signedTx . signature ) ,
99
+ blockhash : hash . blockhash ,
100
+ } ;
101
+ const signature = await sendAndConfirmRawTransaction ( connection , rawTx , confirmationStrategy ) ;
117
102
return signature ;
118
103
}
119
104
@@ -133,10 +118,7 @@ export function ata(mint: PublicKey, owner: PublicKey): Promise<PublicKey> {
133
118
* @param paramsBatch - Array of Params for an each ATA account: {mint, owner}
134
119
* @returns Array of boolean where each members corresponds to owners member
135
120
*/
136
- export async function ataBatchExist (
137
- connection : Connection ,
138
- paramsBatch : AtaParams [ ]
139
- ) : Promise < boolean [ ] > {
121
+ export async function ataBatchExist ( connection : Connection , paramsBatch : AtaParams [ ] ) : Promise < boolean [ ] > {
140
122
const tokenAccounts = await Promise . all (
141
123
paramsBatch . map ( async ( { mint, owner } ) => {
142
124
const pubkey = await ata ( mint , owner ) ;
@@ -164,12 +146,7 @@ export async function generateCreateAtaBatchTx(
164
146
} > {
165
147
const ixs : TransactionInstruction [ ] = await Promise . all (
166
148
paramsBatch . map ( async ( { mint, owner } ) => {
167
- return createAssociatedTokenAccountInstruction (
168
- payer ,
169
- await ata ( mint , owner ) ,
170
- owner ,
171
- mint
172
- ) ;
149
+ return createAssociatedTokenAccountInstruction ( payer , await ata ( mint , owner ) , owner , mint ) ;
173
150
} )
174
151
) ;
175
152
const hash = await connection . getLatestBlockhash ( ) ;
@@ -193,17 +170,8 @@ export async function createAtaBatch(
193
170
invoker : Keypair | SignerWalletAdapter ,
194
171
paramsBatch : AtaParams [ ]
195
172
) : Promise < string > {
196
- const { tx, hash } = await generateCreateAtaBatchTx (
197
- connection ,
198
- invoker . publicKey ! ,
199
- paramsBatch
200
- ) ;
201
- const signature = await signAndExecuteTransaction (
202
- connection ,
203
- invoker ,
204
- tx ,
205
- hash
206
- ) ;
173
+ const { tx, hash } = await generateCreateAtaBatchTx ( connection , invoker . publicKey ! , paramsBatch ) ;
174
+ const signature = await signAndExecuteTransaction ( connection , invoker , tx , hash ) ;
207
175
return signature ;
208
176
}
209
177
@@ -230,14 +198,7 @@ export async function checkOrCreateAtaBatch(
230
198
const response = await connection . getMultipleAccountsInfo ( atas ) ;
231
199
for ( let i = 0 ; i < response . length ; i ++ ) {
232
200
if ( ! response [ i ] ) {
233
- ixs . push (
234
- createAssociatedTokenAccountInstruction (
235
- invoker . publicKey ! ,
236
- atas [ i ] ,
237
- owners [ i ] ,
238
- mint
239
- )
240
- ) ;
201
+ ixs . push ( createAssociatedTokenAccountInstruction ( invoker . publicKey ! , atas [ i ] , owners [ i ] , mint ) ) ;
241
202
}
242
203
}
243
204
return ixs ;
0 commit comments