1
- import { randomBytes } from ' node:crypto' ;
2
- import * as anchor from ' @coral-xyz/anchor' ;
3
- import { BN , type Program } from ' @coral-xyz/anchor' ;
1
+ import { randomBytes } from " node:crypto" ;
2
+ import * as anchor from " @coral-xyz/anchor" ;
3
+ import { BN , type Program } from " @coral-xyz/anchor" ;
4
4
import {
5
5
MINT_SIZE ,
6
6
TOKEN_2022_PROGRAM_ID ,
@@ -10,20 +10,27 @@ import {
10
10
createMintToInstruction ,
11
11
getAssociatedTokenAddressSync ,
12
12
getMinimumBalanceForRentExemptMint ,
13
- } from '@solana/spl-token' ;
14
- import { LAMPORTS_PER_SOL , PublicKey , SystemProgram , Transaction , type TransactionInstruction } from '@solana/web3.js' ;
15
- import { assert } from 'chai' ;
16
- import type { Escrow } from '../target/types/escrow' ;
13
+ } from "@solana/spl-token" ;
14
+ import {
15
+ LAMPORTS_PER_SOL ,
16
+ PublicKey ,
17
+ SystemProgram ,
18
+ Transaction ,
19
+ type TransactionInstruction ,
20
+ } from "@solana/web3.js" ;
21
+ import { assert } from "chai" ;
22
+ import type { Escrow } from "../target/types/escrow" ;
17
23
18
- import { confirmTransaction , makeKeypairs } from ' @solana-developers/helpers' ;
24
+ import { confirmTransaction , makeKeypairs } from " @solana-developers/helpers" ;
19
25
20
- const TOKEN_PROGRAM : typeof TOKEN_2022_PROGRAM_ID | typeof TOKEN_PROGRAM_ID = TOKEN_2022_PROGRAM_ID ;
26
+ const TOKEN_PROGRAM : typeof TOKEN_2022_PROGRAM_ID | typeof TOKEN_PROGRAM_ID =
27
+ TOKEN_2022_PROGRAM_ID ;
21
28
22
29
const getRandomBigNumber = ( size = 8 ) => {
23
30
return new BN ( randomBytes ( size ) ) ;
24
31
} ;
25
32
26
- describe ( ' escrow' , async ( ) => {
33
+ describe ( " escrow" , async ( ) => {
27
34
anchor . setProvider ( anchor . AnchorProvider . env ( ) ) ;
28
35
29
36
const provider = anchor . getProvider ( ) ;
@@ -39,66 +46,112 @@ describe('escrow', async () => {
39
46
40
47
const [ alice , bob , tokenMintA , tokenMintB ] = makeKeypairs ( 4 ) ;
41
48
42
- before ( 'Creates Alice and Bob accounts, 2 token mints, and associated token accounts for both tokens for both users' , async ( ) => {
43
- const [ aliceTokenAccountA , aliceTokenAccountB , bobTokenAccountA , bobTokenAccountB ] = [ alice , bob ] . flatMap ( ( keypair ) =>
44
- [ tokenMintA , tokenMintB ] . map ( ( mint ) => getAssociatedTokenAddressSync ( mint . publicKey , keypair . publicKey , false , TOKEN_PROGRAM ) ) ,
45
- ) ;
46
-
47
- // Airdrops to users, and creates two tokens mints 'A' and 'B'"
48
- const minimumLamports = await getMinimumBalanceForRentExemptMint ( connection ) ;
49
-
50
- const sendSolInstructions : Array < TransactionInstruction > = [ alice , bob ] . map ( ( account ) =>
51
- SystemProgram . transfer ( {
52
- fromPubkey : provider . publicKey ,
53
- toPubkey : account . publicKey ,
54
- lamports : 10 * LAMPORTS_PER_SOL ,
55
- } ) ,
56
- ) ;
57
-
58
- const createMintInstructions : Array < TransactionInstruction > = [ tokenMintA , tokenMintB ] . map ( ( mint ) =>
59
- SystemProgram . createAccount ( {
60
- fromPubkey : provider . publicKey ,
61
- newAccountPubkey : mint . publicKey ,
62
- lamports : minimumLamports ,
63
- space : MINT_SIZE ,
64
- programId : TOKEN_PROGRAM ,
65
- } ) ,
66
- ) ;
67
-
68
- // Make tokenA and tokenB mints, mint tokens and create ATAs
69
- const mintTokensInstructions : Array < TransactionInstruction > = [
70
- {
71
- mint : tokenMintA . publicKey ,
72
- authority : alice . publicKey ,
73
- ata : aliceTokenAccountA ,
74
- } ,
75
- {
76
- mint : tokenMintB . publicKey ,
77
- authority : bob . publicKey ,
78
- ata : bobTokenAccountB ,
79
- } ,
80
- ] . flatMap ( ( mintDetails ) => [
81
- createInitializeMint2Instruction ( mintDetails . mint , 6 , mintDetails . authority , null , TOKEN_PROGRAM ) ,
82
- createAssociatedTokenAccountIdempotentInstruction ( provider . publicKey , mintDetails . ata , mintDetails . authority , mintDetails . mint , TOKEN_PROGRAM ) ,
83
- createMintToInstruction ( mintDetails . mint , mintDetails . ata , mintDetails . authority , 1_000_000_000 , [ ] , TOKEN_PROGRAM ) ,
84
- ] ) ;
85
-
86
- // Add all these instructions to our transaction
87
- const tx = new Transaction ( ) ;
88
- tx . instructions = [ ...sendSolInstructions , ...createMintInstructions , ...mintTokensInstructions ] ;
89
-
90
- await provider . sendAndConfirm ( tx , [ tokenMintA , tokenMintB , alice , bob ] ) ;
91
-
92
- // Save the accounts for later use
93
- accounts . maker = alice . publicKey ;
94
- accounts . taker = bob . publicKey ;
95
- accounts . tokenMintA = tokenMintA . publicKey ;
96
- accounts . makerTokenAccountA = aliceTokenAccountA ;
97
- accounts . takerTokenAccountA = bobTokenAccountA ;
98
- accounts . tokenMintB = tokenMintB . publicKey ;
99
- accounts . makerTokenAccountB = aliceTokenAccountB ;
100
- accounts . takerTokenAccountB = bobTokenAccountB ;
101
- } ) ;
49
+ before (
50
+ "Creates Alice and Bob accounts, 2 token mints, and associated token accounts for both tokens for both users" ,
51
+ async ( ) => {
52
+ const [
53
+ aliceTokenAccountA ,
54
+ aliceTokenAccountB ,
55
+ bobTokenAccountA ,
56
+ bobTokenAccountB ,
57
+ ] = [ alice , bob ] . flatMap ( ( keypair ) =>
58
+ [ tokenMintA , tokenMintB ] . map ( ( mint ) =>
59
+ getAssociatedTokenAddressSync (
60
+ mint . publicKey ,
61
+ keypair . publicKey ,
62
+ false ,
63
+ TOKEN_PROGRAM
64
+ )
65
+ )
66
+ ) ;
67
+
68
+ // Airdrops to users, and creates two tokens mints 'A' and 'B'"
69
+ const minimumLamports = await getMinimumBalanceForRentExemptMint (
70
+ connection
71
+ ) ;
72
+
73
+ const sendSolInstructions : Array < TransactionInstruction > = [
74
+ alice ,
75
+ bob ,
76
+ ] . map ( ( account ) =>
77
+ SystemProgram . transfer ( {
78
+ fromPubkey : provider . publicKey ,
79
+ toPubkey : account . publicKey ,
80
+ lamports : 10 * LAMPORTS_PER_SOL ,
81
+ } )
82
+ ) ;
83
+
84
+ const createMintInstructions : Array < TransactionInstruction > = [
85
+ tokenMintA ,
86
+ tokenMintB ,
87
+ ] . map ( ( mint ) =>
88
+ SystemProgram . createAccount ( {
89
+ fromPubkey : provider . publicKey ,
90
+ newAccountPubkey : mint . publicKey ,
91
+ lamports : minimumLamports ,
92
+ space : MINT_SIZE ,
93
+ programId : TOKEN_PROGRAM ,
94
+ } )
95
+ ) ;
96
+
97
+ // Make tokenA and tokenB mints, mint tokens and create ATAs
98
+ const mintTokensInstructions : Array < TransactionInstruction > = [
99
+ {
100
+ mint : tokenMintA . publicKey ,
101
+ authority : alice . publicKey ,
102
+ ata : aliceTokenAccountA ,
103
+ } ,
104
+ {
105
+ mint : tokenMintB . publicKey ,
106
+ authority : bob . publicKey ,
107
+ ata : bobTokenAccountB ,
108
+ } ,
109
+ ] . flatMap ( ( mintDetails ) => [
110
+ createInitializeMint2Instruction (
111
+ mintDetails . mint ,
112
+ 6 ,
113
+ mintDetails . authority ,
114
+ null ,
115
+ TOKEN_PROGRAM
116
+ ) ,
117
+ createAssociatedTokenAccountIdempotentInstruction (
118
+ provider . publicKey ,
119
+ mintDetails . ata ,
120
+ mintDetails . authority ,
121
+ mintDetails . mint ,
122
+ TOKEN_PROGRAM
123
+ ) ,
124
+ createMintToInstruction (
125
+ mintDetails . mint ,
126
+ mintDetails . ata ,
127
+ mintDetails . authority ,
128
+ 1_000_000_000 ,
129
+ [ ] ,
130
+ TOKEN_PROGRAM
131
+ ) ,
132
+ ] ) ;
133
+
134
+ // Add all these instructions to our transaction
135
+ const tx = new Transaction ( ) ;
136
+ tx . instructions = [
137
+ ...sendSolInstructions ,
138
+ ...createMintInstructions ,
139
+ ...mintTokensInstructions ,
140
+ ] ;
141
+
142
+ await provider . sendAndConfirm ( tx , [ tokenMintA , tokenMintB , alice , bob ] ) ;
143
+
144
+ // Save the accounts for later use
145
+ accounts . maker = alice . publicKey ;
146
+ accounts . taker = bob . publicKey ;
147
+ accounts . tokenMintA = tokenMintA . publicKey ;
148
+ accounts . makerTokenAccountA = aliceTokenAccountA ;
149
+ accounts . takerTokenAccountA = bobTokenAccountA ;
150
+ accounts . tokenMintB = tokenMintB . publicKey ;
151
+ accounts . makerTokenAccountB = aliceTokenAccountB ;
152
+ accounts . takerTokenAccountB = bobTokenAccountB ;
153
+ }
154
+ ) ;
102
155
103
156
const tokenAOfferedAmount = new BN ( 1_000_000 ) ;
104
157
const tokenBWantedAmount = new BN ( 1_000_000 ) ;
@@ -110,11 +163,20 @@ describe('escrow', async () => {
110
163
111
164
// Then determine the account addresses we'll use for the offer and the vault
112
165
const offer = PublicKey . findProgramAddressSync (
113
- [ Buffer . from ( 'offer' ) , accounts . maker . toBuffer ( ) , offerId . toArrayLike ( Buffer , 'le' , 8 ) ] ,
114
- program . programId ,
166
+ [
167
+ Buffer . from ( "offer" ) ,
168
+ accounts . maker . toBuffer ( ) ,
169
+ offerId . toArrayLike ( Buffer , "le" , 8 ) ,
170
+ ] ,
171
+ program . programId
115
172
) [ 0 ] ;
116
173
117
- const vault = getAssociatedTokenAddressSync ( accounts . tokenMintA , offer , true , TOKEN_PROGRAM ) ;
174
+ const vault = getAssociatedTokenAddressSync (
175
+ accounts . tokenMintA ,
176
+ offer ,
177
+ true ,
178
+ TOKEN_PROGRAM
179
+ ) ;
118
180
119
181
accounts . offer = offer ;
120
182
accounts . vault = vault ;
@@ -153,18 +215,24 @@ describe('escrow', async () => {
153
215
154
216
// Check the offered tokens are now in Bob's account
155
217
// (note: there is no before balance as Bob didn't have any offered tokens before the transaction)
156
- const bobTokenAccountBalanceAfterResponse = await connection . getTokenAccountBalance ( accounts . takerTokenAccountA ) ;
157
- const bobTokenAccountBalanceAfter = new BN ( bobTokenAccountBalanceAfterResponse . value . amount ) ;
218
+ const bobTokenAccountBalanceAfterResponse =
219
+ await connection . getTokenAccountBalance ( accounts . takerTokenAccountA ) ;
220
+ const bobTokenAccountBalanceAfter = new BN (
221
+ bobTokenAccountBalanceAfterResponse . value . amount
222
+ ) ;
158
223
assert ( bobTokenAccountBalanceAfter . eq ( tokenAOfferedAmount ) ) ;
159
224
160
225
// Check the wanted tokens are now in Alice's account
161
226
// (note: there is no before balance as Alice didn't have any wanted tokens before the transaction)
162
- const aliceTokenAccountBalanceAfterResponse = await connection . getTokenAccountBalance ( accounts . makerTokenAccountB ) ;
163
- const aliceTokenAccountBalanceAfter = new BN ( aliceTokenAccountBalanceAfterResponse . value . amount ) ;
227
+ const aliceTokenAccountBalanceAfterResponse =
228
+ await connection . getTokenAccountBalance ( accounts . makerTokenAccountB ) ;
229
+ const aliceTokenAccountBalanceAfter = new BN (
230
+ aliceTokenAccountBalanceAfterResponse . value . amount
231
+ ) ;
164
232
assert ( aliceTokenAccountBalanceAfter . eq ( tokenBWantedAmount ) ) ;
165
233
} ;
166
234
167
- it ( ' Puts the tokens Alice offers into the vault when Alice makes an offer' , async ( ) => {
235
+ it ( " Puts the tokens Alice offers into the vault when Alice makes an offer" , async ( ) => {
168
236
await make ( ) ;
169
237
} ) ;
170
238
0 commit comments