@@ -39,6 +39,10 @@ let tokenAccountB: PublicKey;
39
39
const BASE_AMOUNT = 1000 ;
40
40
// Amount passed to instructions
41
41
const USER_AMOUNT = 100 ;
42
+ // Pool token amount minted on init
43
+ const DEFAULT_POOL_TOKEN_AMOUNT = 1000000000 ;
44
+ // Pool token amount to withdraw / deposit
45
+ const POOL_TOKEN_AMOUNT = 1000000 ;
42
46
43
47
function assert ( condition , message ) {
44
48
if ( ! condition ) {
@@ -219,16 +223,23 @@ export async function createTokenSwap(): Promise<void> {
219
223
}
220
224
221
225
export async function deposit ( ) : Promise < void > {
226
+ const poolMintInfo = await tokenPool . getMintInfo ( ) ;
227
+ const supply = poolMintInfo . supply . toNumber ( ) ;
228
+ const swapTokenA = await mintA . getAccountInfo ( tokenAccountA ) ;
229
+ const tokenA = ( swapTokenA . amount . toNumber ( ) * POOL_TOKEN_AMOUNT ) / supply ;
230
+ const swapTokenB = await mintB . getAccountInfo ( tokenAccountB ) ;
231
+ const tokenB = ( swapTokenB . amount . toNumber ( ) * POOL_TOKEN_AMOUNT ) / supply ;
232
+
222
233
console . log ( 'Creating depositor token a account' ) ;
223
- let userAccountA = await mintA . createAccount ( owner . publicKey ) ;
224
- await mintA . mintTo ( userAccountA , owner , [ ] , USER_AMOUNT ) ;
225
- await mintA . approve ( userAccountA , authority , owner , [ ] , USER_AMOUNT ) ;
234
+ const userAccountA = await mintA . createAccount ( owner . publicKey ) ;
235
+ await mintA . mintTo ( userAccountA , owner , [ ] , tokenA ) ;
236
+ await mintA . approve ( userAccountA , authority , owner , [ ] , tokenA ) ;
226
237
console . log ( 'Creating depositor token b account' ) ;
227
- let userAccountB = await mintB . createAccount ( owner . publicKey ) ;
228
- await mintB . mintTo ( userAccountB , owner , [ ] , USER_AMOUNT ) ;
229
- await mintB . approve ( userAccountB , authority , owner , [ ] , USER_AMOUNT ) ;
238
+ const userAccountB = await mintB . createAccount ( owner . publicKey ) ;
239
+ await mintB . mintTo ( userAccountB , owner , [ ] , tokenB ) ;
240
+ await mintB . approve ( userAccountB , authority , owner , [ ] , tokenB ) ;
230
241
console . log ( 'Creating depositor pool token account' ) ;
231
- let newAccountPool = await tokenPool . createAccount ( owner . publicKey ) ;
242
+ const newAccountPool = await tokenPool . createAccount ( owner . publicKey ) ;
232
243
const [ tokenProgramId ] = await GetPrograms ( connection ) ;
233
244
234
245
console . log ( 'Depositing into swap' ) ;
@@ -241,7 +252,7 @@ export async function deposit(): Promise<void> {
241
252
tokenPool . publicKey ,
242
253
newAccountPool ,
243
254
tokenProgramId ,
244
- USER_AMOUNT ,
255
+ POOL_TOKEN_AMOUNT ,
245
256
) ;
246
257
247
258
let info ;
@@ -250,21 +261,34 @@ export async function deposit(): Promise<void> {
250
261
info = await mintB . getAccountInfo ( userAccountB ) ;
251
262
assert ( info . amount . toNumber ( ) == 0 ) ;
252
263
info = await mintA . getAccountInfo ( tokenAccountA ) ;
253
- assert ( info . amount . toNumber ( ) == BASE_AMOUNT + USER_AMOUNT ) ;
264
+ assert ( info . amount . toNumber ( ) == BASE_AMOUNT + tokenA ) ;
254
265
info = await mintB . getAccountInfo ( tokenAccountB ) ;
255
- assert ( info . amount . toNumber ( ) == BASE_AMOUNT + USER_AMOUNT ) ;
266
+ assert ( info . amount . toNumber ( ) == BASE_AMOUNT + tokenB ) ;
256
267
info = await tokenPool . getAccountInfo ( newAccountPool ) ;
257
- assert ( info . amount . toNumber ( ) == USER_AMOUNT ) ;
268
+ assert ( info . amount . toNumber ( ) == POOL_TOKEN_AMOUNT ) ;
258
269
}
259
270
260
271
export async function withdraw ( ) : Promise < void > {
272
+ const poolMintInfo = await tokenPool . getMintInfo ( ) ;
273
+ const supply = poolMintInfo . supply . toNumber ( ) ;
274
+ let swapTokenA = await mintA . getAccountInfo ( tokenAccountA ) ;
275
+ let swapTokenB = await mintB . getAccountInfo ( tokenAccountB ) ;
276
+ const tokenA = ( swapTokenA . amount . toNumber ( ) * POOL_TOKEN_AMOUNT ) / supply ;
277
+ const tokenB = ( swapTokenB . amount . toNumber ( ) * POOL_TOKEN_AMOUNT ) / supply ;
278
+
261
279
console . log ( 'Creating withdraw token A account' ) ;
262
280
let userAccountA = await mintA . createAccount ( owner . publicKey ) ;
263
281
console . log ( 'Creating withdraw token B account' ) ;
264
282
let userAccountB = await mintB . createAccount ( owner . publicKey ) ;
265
283
266
284
console . log ( 'Approving withdrawal from pool account' ) ;
267
- await tokenPool . approve ( tokenAccountPool , authority , owner , [ ] , USER_AMOUNT ) ;
285
+ await tokenPool . approve (
286
+ tokenAccountPool ,
287
+ authority ,
288
+ owner ,
289
+ [ ] ,
290
+ POOL_TOKEN_AMOUNT ,
291
+ ) ;
268
292
const [ tokenProgramId ] = await GetPrograms ( connection ) ;
269
293
270
294
console . log ( 'Withdrawing pool tokens for A and B tokens' ) ;
@@ -277,19 +301,23 @@ export async function withdraw(): Promise<void> {
277
301
userAccountA ,
278
302
userAccountB ,
279
303
tokenProgramId ,
280
- USER_AMOUNT ,
304
+ POOL_TOKEN_AMOUNT ,
281
305
) ;
282
306
307
+ //const poolMintInfo = await tokenPool.getMintInfo();
308
+ swapTokenA = await mintA . getAccountInfo ( tokenAccountA ) ;
309
+ swapTokenB = await mintB . getAccountInfo ( tokenAccountB ) ;
310
+
283
311
let info = await tokenPool . getAccountInfo ( tokenAccountPool ) ;
284
- assert ( info . amount . toNumber ( ) == BASE_AMOUNT - USER_AMOUNT ) ;
285
- info = await mintA . getAccountInfo ( tokenAccountA ) ;
286
- assert ( info . amount . toNumber ( ) == BASE_AMOUNT ) ;
287
- info = await mintB . getAccountInfo ( tokenAccountB ) ;
288
- assert ( info . amount . toNumber ( ) == BASE_AMOUNT ) ;
312
+ assert (
313
+ info . amount . toNumber ( ) == DEFAULT_POOL_TOKEN_AMOUNT - POOL_TOKEN_AMOUNT ,
314
+ ) ;
315
+ assert ( swapTokenA . amount . toNumber ( ) == BASE_AMOUNT ) ;
316
+ assert ( swapTokenB . amount . toNumber ( ) == BASE_AMOUNT ) ;
289
317
info = await mintA . getAccountInfo ( userAccountA ) ;
290
- assert ( info . amount . toNumber ( ) == USER_AMOUNT ) ;
318
+ assert ( info . amount . toNumber ( ) == tokenA ) ;
291
319
info = await mintB . getAccountInfo ( userAccountB ) ;
292
- assert ( info . amount . toNumber ( ) == USER_AMOUNT ) ;
320
+ assert ( info . amount . toNumber ( ) == tokenB ) ;
293
321
}
294
322
295
323
export async function swap ( ) : Promise < void > {
@@ -322,5 +350,7 @@ export async function swap(): Promise<void> {
322
350
info = await mintB . getAccountInfo ( userAccountB ) ;
323
351
assert ( info . amount . toNumber ( ) == 69 ) ;
324
352
info = await tokenPool . getAccountInfo ( tokenAccountPool ) ;
325
- assert ( info . amount . toNumber ( ) == BASE_AMOUNT - USER_AMOUNT ) ;
353
+ assert (
354
+ info . amount . toNumber ( ) == DEFAULT_POOL_TOKEN_AMOUNT - POOL_TOKEN_AMOUNT ,
355
+ ) ;
326
356
}
0 commit comments