Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit c677c58

Browse files
authored
Add JS test for creating accounts and swapping in one transaction (#568)
* Add test for doing all swap in one transaction * Fix instruction to minimize signatures required * Run prettier
1 parent d62ddd2 commit c677c58

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

token-swap/js/cli/main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import {
88
loadPrograms,
9+
createAccountAndSwapAtomic,
910
createTokenSwap,
1011
swap,
1112
deposit,
@@ -24,6 +25,8 @@ async function main() {
2425
await withdraw();
2526
console.log('Run test: swap');
2627
await swap();
28+
console.log('Run test: create account, approve, swap all at once');
29+
await createAccountAndSwapAtomic();
2730
console.log('Success\n');
2831
}
2932

token-swap/js/cli/token-swap-test.js

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ import {
66
Connection,
77
BpfLoader,
88
PublicKey,
9+
SystemProgram,
10+
Transaction,
911
BPF_LOADER_PROGRAM_ID,
1012
} from '@solana/web3.js';
1113

12-
import {Token} from '../../../token/js/client/token';
14+
import {AccountLayout, Token} from '../../../token/js/client/token';
1315
import {TokenSwap, CurveType} from '../client/token-swap';
16+
import {sendAndConfirmTransaction} from '../client/util/send-and-confirm-transaction';
1417
import {Store} from '../client/util/store';
1518
import {newAccountWithLamports} from '../client/util/new-account-with-lamports';
1619
import {url} from '../url';
@@ -393,6 +396,75 @@ export async function withdraw(): Promise<void> {
393396
currentFeeAmount = feeAmount;
394397
}
395398

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+
396468
export async function swap(): Promise<void> {
397469
console.log('Creating swap token a account');
398470
let userAccountA = await mintA.createAccount(owner.publicKey);

0 commit comments

Comments
 (0)