@@ -6,11 +6,14 @@ import {
6
6
Connection ,
7
7
BpfLoader ,
8
8
PublicKey ,
9
+ SystemProgram ,
10
+ Transaction ,
9
11
BPF_LOADER_PROGRAM_ID ,
10
12
} from '@solana/web3.js' ;
11
13
12
- import { Token } from '../../../token/js/client/token' ;
14
+ import { AccountLayout , Token } from '../../../token/js/client/token' ;
13
15
import { TokenSwap , CurveType } from '../client/token-swap' ;
16
+ import { sendAndConfirmTransaction } from '../client/util/send-and-confirm-transaction' ;
14
17
import { Store } from '../client/util/store' ;
15
18
import { newAccountWithLamports } from '../client/util/new-account-with-lamports' ;
16
19
import { url } from '../url' ;
@@ -393,6 +396,75 @@ export async function withdraw(): Promise<void> {
393
396
currentFeeAmount = feeAmount ;
394
397
}
395
398
399
+ export async function createAccountAndSwapAtomic ( ) : Promise < void > {
400
+ console . log ( 'Creating swap token a account' ) ;
401
+ let userAccountA = await mintA . createAccount ( owner . publicKey ) ;
402
+ await mintA . mintTo ( userAccountA , owner , [ ] , SWAP_AMOUNT_IN ) ;
403
+
404
+ const balanceNeeded = await Token . getMinBalanceRentForExemptAccount (
405
+ connection ,
406
+ ) ;
407
+ const newAccount = new Account ( ) ;
408
+ const transaction = new Transaction ( ) ;
409
+ transaction . add (
410
+ SystemProgram . createAccount ( {
411
+ fromPubkey : owner . publicKey ,
412
+ newAccountPubkey : newAccount . publicKey ,
413
+ lamports : balanceNeeded ,
414
+ space : AccountLayout . span ,
415
+ programId : mintB . programId ,
416
+ } ) ,
417
+ ) ;
418
+
419
+ transaction . add (
420
+ Token . createInitAccountInstruction (
421
+ mintB . programId ,
422
+ mintB . publicKey ,
423
+ newAccount . publicKey ,
424
+ owner . publicKey ,
425
+ ) ,
426
+ ) ;
427
+
428
+ transaction . add (
429
+ Token . createApproveInstruction (
430
+ mintA . programId ,
431
+ userAccountA ,
432
+ authority ,
433
+ owner . publicKey ,
434
+ [ owner ] ,
435
+ SWAP_AMOUNT_IN ,
436
+ ) ,
437
+ ) ;
438
+
439
+ transaction . add (
440
+ TokenSwap . swapInstruction (
441
+ tokenSwap . tokenSwap ,
442
+ tokenSwap . authority ,
443
+ userAccountA ,
444
+ tokenSwap . tokenAccountA ,
445
+ tokenSwap . tokenAccountB ,
446
+ newAccount . publicKey ,
447
+ tokenSwap . poolToken ,
448
+ tokenSwap . feeAccount ,
449
+ null ,
450
+ tokenSwap . swapProgramId ,
451
+ tokenSwap . tokenProgramId ,
452
+ SWAP_AMOUNT_IN ,
453
+ 0 ,
454
+ ) ,
455
+ ) ;
456
+
457
+ // Send the instructions
458
+ console . log ( 'sending big instruction' ) ;
459
+ await sendAndConfirmTransaction (
460
+ 'create account, approve transfer, swap' ,
461
+ connection ,
462
+ transaction ,
463
+ owner ,
464
+ newAccount ,
465
+ ) ;
466
+ }
467
+
396
468
export async function swap ( ) : Promise < void > {
397
469
console . log ( 'Creating swap token a account' ) ;
398
470
let userAccountA = await mintA . createAccount ( owner . publicKey ) ;
0 commit comments