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,27 +10,20 @@ import {
10
10
createMintToInstruction ,
11
11
getAssociatedTokenAddressSync ,
12
12
getMinimumBalanceForRentExemptMint ,
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" ;
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' ;
23
17
24
- import { confirmTransaction , makeKeypairs } from " @solana-developers/helpers" ;
18
+ import { confirmTransaction , makeKeypairs } from ' @solana-developers/helpers' ;
25
19
26
- const TOKEN_PROGRAM : typeof TOKEN_2022_PROGRAM_ID | typeof TOKEN_PROGRAM_ID =
27
- TOKEN_2022_PROGRAM_ID ;
20
+ const TOKEN_PROGRAM : typeof TOKEN_2022_PROGRAM_ID | typeof TOKEN_PROGRAM_ID = TOKEN_2022_PROGRAM_ID ;
28
21
29
22
const getRandomBigNumber = ( size = 8 ) => {
30
23
return new BN ( randomBytes ( size ) ) ;
31
24
} ;
32
25
33
- describe ( " escrow" , async ( ) => {
26
+ describe ( ' escrow' , async ( ) => {
34
27
anchor . setProvider ( anchor . AnchorProvider . env ( ) ) ;
35
28
36
29
const provider = anchor . getProvider ( ) ;
@@ -46,112 +39,66 @@ describe("escrow", async () => {
46
39
47
40
const [ alice , bob , tokenMintA , tokenMintB ] = makeKeypairs ( 4 ) ;
48
41
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
- ) ;
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
+ } ) ;
155
102
156
103
const tokenAOfferedAmount = new BN ( 1_000_000 ) ;
157
104
const tokenBWantedAmount = new BN ( 1_000_000 ) ;
@@ -163,20 +110,11 @@ describe("escrow", async () => {
163
110
164
111
// Then determine the account addresses we'll use for the offer and the vault
165
112
const offer = PublicKey . findProgramAddressSync (
166
- [
167
- Buffer . from ( "offer" ) ,
168
- accounts . maker . toBuffer ( ) ,
169
- offerId . toArrayLike ( Buffer , "le" , 8 ) ,
170
- ] ,
171
- program . programId
113
+ [ Buffer . from ( 'offer' ) , accounts . maker . toBuffer ( ) , offerId . toArrayLike ( Buffer , 'le' , 8 ) ] ,
114
+ program . programId ,
172
115
) [ 0 ] ;
173
116
174
- const vault = getAssociatedTokenAddressSync (
175
- accounts . tokenMintA ,
176
- offer ,
177
- true ,
178
- TOKEN_PROGRAM
179
- ) ;
117
+ const vault = getAssociatedTokenAddressSync ( accounts . tokenMintA , offer , true , TOKEN_PROGRAM ) ;
180
118
181
119
accounts . offer = offer ;
182
120
accounts . vault = vault ;
@@ -215,24 +153,18 @@ describe("escrow", async () => {
215
153
216
154
// Check the offered tokens are now in Bob's account
217
155
// (note: there is no before balance as Bob didn't have any offered tokens before the transaction)
218
- const bobTokenAccountBalanceAfterResponse =
219
- await connection . getTokenAccountBalance ( accounts . takerTokenAccountA ) ;
220
- const bobTokenAccountBalanceAfter = new BN (
221
- bobTokenAccountBalanceAfterResponse . value . amount
222
- ) ;
156
+ const bobTokenAccountBalanceAfterResponse = await connection . getTokenAccountBalance ( accounts . takerTokenAccountA ) ;
157
+ const bobTokenAccountBalanceAfter = new BN ( bobTokenAccountBalanceAfterResponse . value . amount ) ;
223
158
assert ( bobTokenAccountBalanceAfter . eq ( tokenAOfferedAmount ) ) ;
224
159
225
160
// Check the wanted tokens are now in Alice's account
226
161
// (note: there is no before balance as Alice didn't have any wanted tokens before the transaction)
227
- const aliceTokenAccountBalanceAfterResponse =
228
- await connection . getTokenAccountBalance ( accounts . makerTokenAccountB ) ;
229
- const aliceTokenAccountBalanceAfter = new BN (
230
- aliceTokenAccountBalanceAfterResponse . value . amount
231
- ) ;
162
+ const aliceTokenAccountBalanceAfterResponse = await connection . getTokenAccountBalance ( accounts . makerTokenAccountB ) ;
163
+ const aliceTokenAccountBalanceAfter = new BN ( aliceTokenAccountBalanceAfterResponse . value . amount ) ;
232
164
assert ( aliceTokenAccountBalanceAfter . eq ( tokenBWantedAmount ) ) ;
233
165
} ;
234
166
235
- it ( " Puts the tokens Alice offers into the vault when Alice makes an offer" , async ( ) => {
167
+ it ( ' Puts the tokens Alice offers into the vault when Alice makes an offer' , async ( ) => {
236
168
await make ( ) ;
237
169
} ) ;
238
170
0 commit comments