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

Commit fe20132

Browse files
authored
Updated index.ts and tests (#3094)
1 parent 4825298 commit fe20132

File tree

3 files changed

+58
-45
lines changed

3 files changed

+58
-45
lines changed

token-swap/js/src/index.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@ import assert from 'assert';
22
import BN from 'bn.js';
33
import {Buffer} from 'buffer';
44
import * as BufferLayout from '@solana/buffer-layout';
5-
import type {Connection, TransactionSignature} from '@solana/web3.js';
5+
import type {
6+
ConfirmOptions,
7+
Connection,
8+
TransactionSignature,
9+
} from '@solana/web3.js';
610
import {
711
Account,
812
PublicKey,
913
SystemProgram,
1014
Transaction,
1115
TransactionInstruction,
16+
sendAndConfirmTransaction,
1217
} from '@solana/web3.js';
1318

1419
import * as Layout from './layout';
15-
import {sendAndConfirmTransaction} from './util/send-and-confirm-transaction';
1620
import {loadAccount} from './util/account';
1721

1822
export const TOKEN_SWAP_PROGRAM_ID: PublicKey = new PublicKey(
@@ -365,6 +369,7 @@ export class TokenSwap {
365369
hostFeeDenominator: number,
366370
curveType: number,
367371
curveParameters?: Numberu64,
372+
confirmOptions?: ConfirmOptions,
368373
): Promise<TokenSwap> {
369374
let transaction;
370375
const tokenSwap = new TokenSwap(
@@ -430,11 +435,10 @@ export class TokenSwap {
430435

431436
transaction.add(instruction);
432437
await sendAndConfirmTransaction(
433-
'createAccount and InitializeSwap',
434438
connection,
435439
transaction,
436-
payer,
437-
tokenSwapAccount,
440+
[payer, tokenSwapAccount],
441+
confirmOptions,
438442
);
439443

440444
return tokenSwap;
@@ -461,9 +465,9 @@ export class TokenSwap {
461465
userTransferAuthority: Account,
462466
amountIn: number | Numberu64,
463467
minimumAmountOut: number | Numberu64,
468+
confirmOptions?: ConfirmOptions,
464469
): Promise<TransactionSignature> {
465470
return await sendAndConfirmTransaction(
466-
'swap',
467471
this.connection,
468472
new Transaction().add(
469473
TokenSwap.swapInstruction(
@@ -483,8 +487,8 @@ export class TokenSwap {
483487
minimumAmountOut,
484488
),
485489
),
486-
this.payer,
487-
userTransferAuthority,
490+
[this.payer, userTransferAuthority],
491+
confirmOptions,
488492
);
489493
}
490494

@@ -560,9 +564,9 @@ export class TokenSwap {
560564
poolTokenAmount: number | Numberu64,
561565
maximumTokenA: number | Numberu64,
562566
maximumTokenB: number | Numberu64,
567+
confirmOptions?: ConfirmOptions,
563568
): Promise<TransactionSignature> {
564569
return await sendAndConfirmTransaction(
565-
'depositAllTokenTypes',
566570
this.connection,
567571
new Transaction().add(
568572
TokenSwap.depositAllTokenTypesInstruction(
@@ -582,8 +586,8 @@ export class TokenSwap {
582586
maximumTokenB,
583587
),
584588
),
585-
this.payer,
586-
userTransferAuthority,
589+
[this.payer, userTransferAuthority],
590+
confirmOptions,
587591
);
588592
}
589593

@@ -659,9 +663,9 @@ export class TokenSwap {
659663
poolTokenAmount: number | Numberu64,
660664
minimumTokenA: number | Numberu64,
661665
minimumTokenB: number | Numberu64,
666+
confirmOptions?: ConfirmOptions,
662667
): Promise<TransactionSignature> {
663668
return await sendAndConfirmTransaction(
664-
'withdraw',
665669
this.connection,
666670
new Transaction().add(
667671
TokenSwap.withdrawAllTokenTypesInstruction(
@@ -682,8 +686,8 @@ export class TokenSwap {
682686
minimumTokenB,
683687
),
684688
),
685-
this.payer,
686-
userTransferAuthority,
689+
[this.payer, userTransferAuthority],
690+
confirmOptions,
687691
);
688692
}
689693

@@ -756,9 +760,9 @@ export class TokenSwap {
756760
userTransferAuthority: Account,
757761
sourceTokenAmount: number | Numberu64,
758762
minimumPoolTokenAmount: number | Numberu64,
763+
confirmOptions?: ConfirmOptions,
759764
): Promise<TransactionSignature> {
760765
return await sendAndConfirmTransaction(
761-
'depositSingleTokenTypeExactAmountIn',
762766
this.connection,
763767
new Transaction().add(
764768
TokenSwap.depositSingleTokenTypeExactAmountInInstruction(
@@ -776,8 +780,8 @@ export class TokenSwap {
776780
minimumPoolTokenAmount,
777781
),
778782
),
779-
this.payer,
780-
userTransferAuthority,
783+
[this.payer, userTransferAuthority],
784+
confirmOptions,
781785
);
782786
}
783787

@@ -846,9 +850,9 @@ export class TokenSwap {
846850
userTransferAuthority: Account,
847851
destinationTokenAmount: number | Numberu64,
848852
maximumPoolTokenAmount: number | Numberu64,
853+
confirmOptions?: ConfirmOptions,
849854
): Promise<TransactionSignature> {
850855
return await sendAndConfirmTransaction(
851-
'withdrawSingleTokenTypeExactAmountOut',
852856
this.connection,
853857
new Transaction().add(
854858
TokenSwap.withdrawSingleTokenTypeExactAmountOutInstruction(
@@ -867,8 +871,8 @@ export class TokenSwap {
867871
maximumPoolTokenAmount,
868872
),
869873
),
870-
this.payer,
871-
userTransferAuthority,
874+
[this.payer, userTransferAuthority],
875+
confirmOptions,
872876
);
873877
}
874878

token-swap/js/src/util/send-and-confirm-transaction.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

token-swap/js/test/token-swap-test.ts

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import {
44
PublicKey,
55
SystemProgram,
66
Transaction,
7+
sendAndConfirmTransaction
78
} from '@solana/web3.js';
89
import {AccountLayout, Token, TOKEN_PROGRAM_ID} from '@solana/spl-token';
910

1011
import {TokenSwap, CurveType, TOKEN_SWAP_PROGRAM_ID} from '../src';
11-
import {sendAndConfirmTransaction} from '../src/util/send-and-confirm-transaction';
1212
import {newAccountWithLamports} from '../src/util/new-account-with-lamports';
1313
import {sleep} from '../src/util/sleep';
1414
import {Numberu64} from '../src';
@@ -253,6 +253,10 @@ export async function depositAllTokenTypes(): Promise<void> {
253253
console.log('Creating depositor pool token account');
254254
const newAccountPool = await tokenPool.createAccount(owner.publicKey);
255255

256+
const confirmOptions = {
257+
skipPreflight: true
258+
}
259+
256260
console.log('Depositing into swap');
257261
await tokenSwap.depositAllTokenTypes(
258262
userAccountA,
@@ -262,6 +266,7 @@ export async function depositAllTokenTypes(): Promise<void> {
262266
POOL_TOKEN_AMOUNT,
263267
tokenA,
264268
tokenB,
269+
confirmOptions
265270
);
266271

267272
let info;
@@ -314,6 +319,10 @@ export async function withdrawAllTokenTypes(): Promise<void> {
314319
POOL_TOKEN_AMOUNT,
315320
);
316321

322+
const confirmOptions = {
323+
skipPreflight: true
324+
}
325+
317326
console.log('Withdrawing pool tokens for A and B tokens');
318327
await tokenSwap.withdrawAllTokenTypes(
319328
userAccountA,
@@ -323,6 +332,7 @@ export async function withdrawAllTokenTypes(): Promise<void> {
323332
POOL_TOKEN_AMOUNT,
324333
tokenA,
325334
tokenB,
335+
confirmOptions
326336
);
327337

328338
//const poolMintInfo = await tokenPool.getMintInfo();
@@ -407,15 +417,17 @@ export async function createAccountAndSwapAtomic(): Promise<void> {
407417
),
408418
);
409419

420+
const confirmOptions = {
421+
skipPreflight: true
422+
}
423+
410424
// Send the instructions
411425
console.log('sending big instruction');
412426
await sendAndConfirmTransaction(
413-
'create account, approve transfer, swap',
414427
connection,
415428
transaction,
416-
owner,
417-
newAccount,
418-
userTransferAuthority,
429+
[owner, newAccount, userTransferAuthority],
430+
confirmOptions
419431
);
420432

421433
let info;
@@ -443,6 +455,10 @@ export async function swap(): Promise<void> {
443455
? await tokenPool.createAccount(owner.publicKey)
444456
: null;
445457

458+
const confirmOptions = {
459+
skipPreflight: true
460+
}
461+
446462
console.log('Swapping');
447463
await tokenSwap.swap(
448464
userAccountA,
@@ -453,6 +469,7 @@ export async function swap(): Promise<void> {
453469
userTransferAuthority,
454470
SWAP_AMOUNT_IN,
455471
SWAP_AMOUNT_OUT,
472+
confirmOptions
456473
);
457474

458475
await sleep(500);
@@ -541,13 +558,18 @@ export async function depositSingleTokenTypeExactAmountIn(): Promise<void> {
541558
console.log('Creating depositor pool token account');
542559
const newAccountPool = await tokenPool.createAccount(owner.publicKey);
543560

561+
const confirmOptions = {
562+
skipPreflight: true
563+
}
564+
544565
console.log('Depositing token A into swap');
545566
await tokenSwap.depositSingleTokenTypeExactAmountIn(
546567
userAccountA,
547568
newAccountPool,
548569
userTransferAuthority,
549570
depositAmount,
550571
poolTokenA,
572+
confirmOptions
551573
);
552574

553575
let info;
@@ -564,6 +586,7 @@ export async function depositSingleTokenTypeExactAmountIn(): Promise<void> {
564586
userTransferAuthority,
565587
depositAmount,
566588
poolTokenB,
589+
confirmOptions
567590
);
568591

569592
info = await mintB.getAccountInfo(userAccountB);
@@ -625,13 +648,18 @@ export async function withdrawSingleTokenTypeExactAmountOut(): Promise<void> {
625648
adjustedPoolTokenA + adjustedPoolTokenB,
626649
);
627650

651+
const confirmOptions = {
652+
skipPreflight: true
653+
}
654+
628655
console.log('Withdrawing token A only');
629656
await tokenSwap.withdrawSingleTokenTypeExactAmountOut(
630657
userAccountA,
631658
tokenAccountPool,
632659
userTransferAuthority,
633660
withdrawAmount,
634661
adjustedPoolTokenA,
662+
confirmOptions
635663
);
636664

637665
let info;
@@ -650,6 +678,7 @@ export async function withdrawSingleTokenTypeExactAmountOut(): Promise<void> {
650678
userTransferAuthority,
651679
withdrawAmount,
652680
adjustedPoolTokenB,
681+
confirmOptions
653682
);
654683

655684
info = await mintB.getAccountInfo(userAccountB);

0 commit comments

Comments
 (0)