Skip to content

Commit

Permalink
Add JS test for creating accounts and swapping in one transaction (so…
Browse files Browse the repository at this point in the history
…lana-labs#568)

* Add test for doing all swap in one transaction

* Fix instruction to minimize signatures required

* Run prettier
  • Loading branch information
joncinque authored Nov 17, 2020
1 parent d62ddd2 commit c677c58
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
3 changes: 3 additions & 0 deletions token-swap/js/cli/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import {
loadPrograms,
createAccountAndSwapAtomic,
createTokenSwap,
swap,
deposit,
Expand All @@ -24,6 +25,8 @@ async function main() {
await withdraw();
console.log('Run test: swap');
await swap();
console.log('Run test: create account, approve, swap all at once');
await createAccountAndSwapAtomic();
console.log('Success\n');
}

Expand Down
74 changes: 73 additions & 1 deletion token-swap/js/cli/token-swap-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import {
Connection,
BpfLoader,
PublicKey,
SystemProgram,
Transaction,
BPF_LOADER_PROGRAM_ID,
} from '@solana/web3.js';

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

export async function createAccountAndSwapAtomic(): Promise<void> {
console.log('Creating swap token a account');
let userAccountA = await mintA.createAccount(owner.publicKey);
await mintA.mintTo(userAccountA, owner, [], SWAP_AMOUNT_IN);

const balanceNeeded = await Token.getMinBalanceRentForExemptAccount(
connection,
);
const newAccount = new Account();
const transaction = new Transaction();
transaction.add(
SystemProgram.createAccount({
fromPubkey: owner.publicKey,
newAccountPubkey: newAccount.publicKey,
lamports: balanceNeeded,
space: AccountLayout.span,
programId: mintB.programId,
}),
);

transaction.add(
Token.createInitAccountInstruction(
mintB.programId,
mintB.publicKey,
newAccount.publicKey,
owner.publicKey,
),
);

transaction.add(
Token.createApproveInstruction(
mintA.programId,
userAccountA,
authority,
owner.publicKey,
[owner],
SWAP_AMOUNT_IN,
),
);

transaction.add(
TokenSwap.swapInstruction(
tokenSwap.tokenSwap,
tokenSwap.authority,
userAccountA,
tokenSwap.tokenAccountA,
tokenSwap.tokenAccountB,
newAccount.publicKey,
tokenSwap.poolToken,
tokenSwap.feeAccount,
null,
tokenSwap.swapProgramId,
tokenSwap.tokenProgramId,
SWAP_AMOUNT_IN,
0,
),
);

// Send the instructions
console.log('sending big instruction');
await sendAndConfirmTransaction(
'create account, approve transfer, swap',
connection,
transaction,
owner,
newAccount,
);
}

export async function swap(): Promise<void> {
console.log('Creating swap token a account');
let userAccountA = await mintA.createAccount(owner.publicKey);
Expand Down

0 comments on commit c677c58

Please sign in to comment.