1
- import * as anchor from " @coral-xyz/anchor" ;
2
- import type { Program } from " @coral-xyz/anchor" ;
1
+ import * as anchor from ' @coral-xyz/anchor' ;
2
+ import type { Program } from ' @coral-xyz/anchor' ;
3
3
import {
4
4
ASSOCIATED_TOKEN_PROGRAM_ID ,
5
5
ExtensionType ,
@@ -11,24 +11,17 @@ import {
11
11
createTransferCheckedWithTransferHookInstruction ,
12
12
getAssociatedTokenAddressSync ,
13
13
getMintLen ,
14
- } from "@solana/spl-token" ;
15
- import {
16
- Keypair ,
17
- PublicKey ,
18
- SendTransactionError ,
19
- SystemProgram ,
20
- Transaction ,
21
- sendAndConfirmTransaction ,
22
- } from "@solana/web3.js" ;
23
- import type { TransferHook } from "../target/types/transfer_hook" ;
24
- import { BN } from "bn.js" ;
25
- import { expect } from "chai" ;
26
- import chai from "chai" ;
27
- import chaiAsPromised from "chai-as-promised" ;
14
+ } from '@solana/spl-token' ;
15
+ import { Keypair , PublicKey , SendTransactionError , SystemProgram , Transaction , sendAndConfirmTransaction } from '@solana/web3.js' ;
16
+ import { BN } from 'bn.js' ;
17
+ import { expect } from 'chai' ;
18
+ import chai from 'chai' ;
19
+ import chaiAsPromised from 'chai-as-promised' ;
20
+ import type { TransferHook } from '../target/types/transfer_hook' ;
28
21
29
22
chai . use ( chaiAsPromised ) ;
30
23
31
- describe ( " transfer-hook" , ( ) => {
24
+ describe ( ' transfer-hook' , ( ) => {
32
25
// Configure the client to use the local cluster.
33
26
const provider = anchor . AnchorProvider . env ( ) ;
34
27
anchor . setProvider ( provider ) ;
@@ -47,7 +40,7 @@ describe("transfer-hook", () => {
47
40
wallet . publicKey ,
48
41
false ,
49
42
TOKEN_2022_PROGRAM_ID ,
50
- ASSOCIATED_TOKEN_PROGRAM_ID
43
+ ASSOCIATED_TOKEN_PROGRAM_ID ,
51
44
) ;
52
45
53
46
// Recipient token account address
@@ -57,26 +50,22 @@ describe("transfer-hook", () => {
57
50
recipient . publicKey ,
58
51
false ,
59
52
TOKEN_2022_PROGRAM_ID ,
60
- ASSOCIATED_TOKEN_PROGRAM_ID
53
+ ASSOCIATED_TOKEN_PROGRAM_ID ,
61
54
) ;
62
55
63
56
// ExtraAccountMetaList address
64
57
// Store extra accounts required by the custom transfer hook instruction
65
58
const [ extraAccountMetaListPDA ] = PublicKey . findProgramAddressSync (
66
- [ Buffer . from ( " extra-account-metas" ) , mint . publicKey . toBuffer ( ) ] ,
67
- program . programId
59
+ [ Buffer . from ( ' extra-account-metas' ) , mint . publicKey . toBuffer ( ) ] ,
60
+ program . programId ,
68
61
) ;
69
62
70
- const [ counterPDA ] = PublicKey . findProgramAddressSync (
71
- [ Buffer . from ( "counter" ) ] ,
72
- program . programId
73
- ) ;
63
+ const [ counterPDA ] = PublicKey . findProgramAddressSync ( [ Buffer . from ( 'counter' ) ] , program . programId ) ;
74
64
75
- it ( " Create Mint Account with Transfer Hook Extension" , async ( ) => {
65
+ it ( ' Create Mint Account with Transfer Hook Extension' , async ( ) => {
76
66
const extensions = [ ExtensionType . TransferHook ] ;
77
67
const mintLen = getMintLen ( extensions ) ;
78
- const lamports =
79
- await provider . connection . getMinimumBalanceForRentExemption ( mintLen ) ;
68
+ const lamports = await provider . connection . getMinimumBalanceForRentExemption ( mintLen ) ;
80
69
81
70
const transaction = new Transaction ( ) . add (
82
71
SystemProgram . createAccount ( {
@@ -90,30 +79,19 @@ describe("transfer-hook", () => {
90
79
mint . publicKey ,
91
80
wallet . publicKey ,
92
81
program . programId , // Transfer Hook Program ID
93
- TOKEN_2022_PROGRAM_ID
82
+ TOKEN_2022_PROGRAM_ID ,
94
83
) ,
95
- createInitializeMintInstruction (
96
- mint . publicKey ,
97
- decimals ,
98
- wallet . publicKey ,
99
- null ,
100
- TOKEN_2022_PROGRAM_ID
101
- )
84
+ createInitializeMintInstruction ( mint . publicKey , decimals , wallet . publicKey , null , TOKEN_2022_PROGRAM_ID ) ,
102
85
) ;
103
86
104
- const txSig = await sendAndConfirmTransaction (
105
- provider . connection ,
106
- transaction ,
107
- [ wallet . payer , mint ] ,
108
- {
109
- skipPreflight : true ,
110
- commitment : "finalized" ,
111
- }
112
- ) ;
87
+ const txSig = await sendAndConfirmTransaction ( provider . connection , transaction , [ wallet . payer , mint ] , {
88
+ skipPreflight : true ,
89
+ commitment : 'finalized' ,
90
+ } ) ;
113
91
114
92
const txDetails = await program . provider . connection . getTransaction ( txSig , {
115
93
maxSupportedTransactionVersion : 0 ,
116
- commitment : " confirmed" ,
94
+ commitment : ' confirmed' ,
117
95
} ) ;
118
96
console . log ( txDetails . meta . logMessages ) ;
119
97
@@ -122,7 +100,7 @@ describe("transfer-hook", () => {
122
100
123
101
// Create the two token accounts for the transfer-hook enabled mint
124
102
// Fund the sender token account with 100 tokens
125
- it ( " Create Token Accounts and Mint Tokens" , async ( ) => {
103
+ it ( ' Create Token Accounts and Mint Tokens' , async ( ) => {
126
104
// 100 tokens
127
105
const amount = 100 * 10 ** decimals ;
128
106
@@ -133,95 +111,68 @@ describe("transfer-hook", () => {
133
111
wallet . publicKey ,
134
112
mint . publicKey ,
135
113
TOKEN_2022_PROGRAM_ID ,
136
- ASSOCIATED_TOKEN_PROGRAM_ID
114
+ ASSOCIATED_TOKEN_PROGRAM_ID ,
137
115
) ,
138
116
createAssociatedTokenAccountInstruction (
139
117
wallet . publicKey ,
140
118
destinationTokenAccount ,
141
119
recipient . publicKey ,
142
120
mint . publicKey ,
143
121
TOKEN_2022_PROGRAM_ID ,
144
- ASSOCIATED_TOKEN_PROGRAM_ID
122
+ ASSOCIATED_TOKEN_PROGRAM_ID ,
145
123
) ,
146
- createMintToInstruction (
147
- mint . publicKey ,
148
- sourceTokenAccount ,
149
- wallet . publicKey ,
150
- amount ,
151
- [ ] ,
152
- TOKEN_2022_PROGRAM_ID
153
- )
124
+ createMintToInstruction ( mint . publicKey , sourceTokenAccount , wallet . publicKey , amount , [ ] , TOKEN_2022_PROGRAM_ID ) ,
154
125
) ;
155
126
156
- const txSig = await sendAndConfirmTransaction (
157
- connection ,
158
- transaction ,
159
- [ wallet . payer ] ,
160
- { skipPreflight : true }
161
- ) ;
127
+ const txSig = await sendAndConfirmTransaction ( connection , transaction , [ wallet . payer ] , { skipPreflight : true } ) ;
162
128
163
129
console . log ( `Transaction Signature: ${ txSig } ` ) ;
164
130
} ) ;
165
131
166
132
// Account to store extra accounts required by the transfer hook instruction
167
- it ( " Create ExtraAccountMetaList Account" , async ( ) => {
133
+ it ( ' Create ExtraAccountMetaList Account' , async ( ) => {
168
134
const initializeExtraAccountMetaListInstruction = await program . methods
169
135
. initializeExtraAccountMetaList ( )
170
136
. accounts ( {
171
137
mint : mint . publicKey ,
172
138
} )
173
139
. instruction ( ) ;
174
140
175
- const transaction = new Transaction ( ) . add (
176
- initializeExtraAccountMetaListInstruction
177
- ) ;
141
+ const transaction = new Transaction ( ) . add ( initializeExtraAccountMetaListInstruction ) ;
178
142
179
- const txSig = await sendAndConfirmTransaction (
180
- provider . connection ,
181
- transaction ,
182
- [ wallet . payer ] ,
183
- { skipPreflight : true , commitment : "confirmed" }
184
- ) ;
185
- console . log ( "Transaction Signature:" , txSig ) ;
143
+ const txSig = await sendAndConfirmTransaction ( provider . connection , transaction , [ wallet . payer ] , { skipPreflight : true , commitment : 'confirmed' } ) ;
144
+ console . log ( 'Transaction Signature:' , txSig ) ;
186
145
} ) ;
187
146
188
- it ( " Transfer Hook with Extra Account Meta" , async ( ) => {
147
+ it ( ' Transfer Hook with Extra Account Meta' , async ( ) => {
189
148
// 1 tokens
190
149
const amount = 1 * 10 ** decimals ;
191
150
const amountBigInt = BigInt ( amount ) ;
192
151
193
- const transferInstructionWithHelper =
194
- await createTransferCheckedWithTransferHookInstruction (
195
- connection ,
196
- sourceTokenAccount ,
197
- mint . publicKey ,
198
- destinationTokenAccount ,
199
- wallet . publicKey ,
200
- amountBigInt ,
201
- decimals ,
202
- [ ] ,
203
- "confirmed" ,
204
- TOKEN_2022_PROGRAM_ID
205
- ) ;
152
+ const transferInstructionWithHelper = await createTransferCheckedWithTransferHookInstruction (
153
+ connection ,
154
+ sourceTokenAccount ,
155
+ mint . publicKey ,
156
+ destinationTokenAccount ,
157
+ wallet . publicKey ,
158
+ amountBigInt ,
159
+ decimals ,
160
+ [ ] ,
161
+ 'confirmed' ,
162
+ TOKEN_2022_PROGRAM_ID ,
163
+ ) ;
206
164
207
165
console . log ( `Extra accounts meta: ${ extraAccountMetaListPDA } ` ) ;
208
166
console . log ( `Counter PDA: ${ counterPDA } ` ) ;
209
- console . log (
210
- `Transfer Instruction: ${ JSON . stringify ( transferInstructionWithHelper ) } `
211
- ) ;
167
+ console . log ( `Transfer Instruction: ${ JSON . stringify ( transferInstructionWithHelper ) } ` ) ;
212
168
213
169
const transaction = new Transaction ( ) . add ( transferInstructionWithHelper ) ;
214
170
215
- const txSig = await sendAndConfirmTransaction (
216
- connection ,
217
- transaction ,
218
- [ wallet . payer ] ,
219
- { skipPreflight : true }
220
- ) ;
221
- console . log ( "Transfer Signature:" , txSig ) ;
171
+ const txSig = await sendAndConfirmTransaction ( connection , transaction , [ wallet . payer ] , { skipPreflight : true } ) ;
172
+ console . log ( 'Transfer Signature:' , txSig ) ;
222
173
} ) ;
223
174
224
- it ( " Try call transfer hook without transfer" , async ( ) => {
175
+ it ( ' Try call transfer hook without transfer' , async ( ) => {
225
176
const transferHookIx = await program . methods
226
177
. transferHook ( new BN ( 1 ) )
227
178
. accounts ( {
@@ -234,16 +185,8 @@ describe("transfer-hook", () => {
234
185
235
186
const transaction = new Transaction ( ) . add ( transferHookIx ) ;
236
187
237
- const sendPromise = sendAndConfirmTransaction (
238
- connection ,
239
- transaction ,
240
- [ wallet . payer ] ,
241
- { skipPreflight : false }
242
- ) ;
188
+ const sendPromise = sendAndConfirmTransaction ( connection , transaction , [ wallet . payer ] , { skipPreflight : false } ) ;
243
189
244
- await expect ( sendPromise ) . to . eventually . be . rejectedWith (
245
- SendTransactionError ,
246
- program . idl . errors [ 1 ] . msg
247
- ) ;
190
+ await expect ( sendPromise ) . to . eventually . be . rejectedWith ( SendTransactionError , program . idl . errors [ 1 ] . msg ) ;
248
191
} ) ;
249
192
} ) ;
0 commit comments