Updated index.ts and tests (#3094)
This commit is contained in:
parent
4825298bc9
commit
fe20132b07
|
@ -2,17 +2,21 @@ import assert from 'assert';
|
||||||
import BN from 'bn.js';
|
import BN from 'bn.js';
|
||||||
import {Buffer} from 'buffer';
|
import {Buffer} from 'buffer';
|
||||||
import * as BufferLayout from '@solana/buffer-layout';
|
import * as BufferLayout from '@solana/buffer-layout';
|
||||||
import type {Connection, TransactionSignature} from '@solana/web3.js';
|
import type {
|
||||||
|
ConfirmOptions,
|
||||||
|
Connection,
|
||||||
|
TransactionSignature,
|
||||||
|
} from '@solana/web3.js';
|
||||||
import {
|
import {
|
||||||
Account,
|
Account,
|
||||||
PublicKey,
|
PublicKey,
|
||||||
SystemProgram,
|
SystemProgram,
|
||||||
Transaction,
|
Transaction,
|
||||||
TransactionInstruction,
|
TransactionInstruction,
|
||||||
|
sendAndConfirmTransaction,
|
||||||
} from '@solana/web3.js';
|
} from '@solana/web3.js';
|
||||||
|
|
||||||
import * as Layout from './layout';
|
import * as Layout from './layout';
|
||||||
import {sendAndConfirmTransaction} from './util/send-and-confirm-transaction';
|
|
||||||
import {loadAccount} from './util/account';
|
import {loadAccount} from './util/account';
|
||||||
|
|
||||||
export const TOKEN_SWAP_PROGRAM_ID: PublicKey = new PublicKey(
|
export const TOKEN_SWAP_PROGRAM_ID: PublicKey = new PublicKey(
|
||||||
|
@ -365,6 +369,7 @@ export class TokenSwap {
|
||||||
hostFeeDenominator: number,
|
hostFeeDenominator: number,
|
||||||
curveType: number,
|
curveType: number,
|
||||||
curveParameters?: Numberu64,
|
curveParameters?: Numberu64,
|
||||||
|
confirmOptions?: ConfirmOptions,
|
||||||
): Promise<TokenSwap> {
|
): Promise<TokenSwap> {
|
||||||
let transaction;
|
let transaction;
|
||||||
const tokenSwap = new TokenSwap(
|
const tokenSwap = new TokenSwap(
|
||||||
|
@ -430,11 +435,10 @@ export class TokenSwap {
|
||||||
|
|
||||||
transaction.add(instruction);
|
transaction.add(instruction);
|
||||||
await sendAndConfirmTransaction(
|
await sendAndConfirmTransaction(
|
||||||
'createAccount and InitializeSwap',
|
|
||||||
connection,
|
connection,
|
||||||
transaction,
|
transaction,
|
||||||
payer,
|
[payer, tokenSwapAccount],
|
||||||
tokenSwapAccount,
|
confirmOptions,
|
||||||
);
|
);
|
||||||
|
|
||||||
return tokenSwap;
|
return tokenSwap;
|
||||||
|
@ -461,9 +465,9 @@ export class TokenSwap {
|
||||||
userTransferAuthority: Account,
|
userTransferAuthority: Account,
|
||||||
amountIn: number | Numberu64,
|
amountIn: number | Numberu64,
|
||||||
minimumAmountOut: number | Numberu64,
|
minimumAmountOut: number | Numberu64,
|
||||||
|
confirmOptions?: ConfirmOptions,
|
||||||
): Promise<TransactionSignature> {
|
): Promise<TransactionSignature> {
|
||||||
return await sendAndConfirmTransaction(
|
return await sendAndConfirmTransaction(
|
||||||
'swap',
|
|
||||||
this.connection,
|
this.connection,
|
||||||
new Transaction().add(
|
new Transaction().add(
|
||||||
TokenSwap.swapInstruction(
|
TokenSwap.swapInstruction(
|
||||||
|
@ -483,8 +487,8 @@ export class TokenSwap {
|
||||||
minimumAmountOut,
|
minimumAmountOut,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
this.payer,
|
[this.payer, userTransferAuthority],
|
||||||
userTransferAuthority,
|
confirmOptions,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -560,9 +564,9 @@ export class TokenSwap {
|
||||||
poolTokenAmount: number | Numberu64,
|
poolTokenAmount: number | Numberu64,
|
||||||
maximumTokenA: number | Numberu64,
|
maximumTokenA: number | Numberu64,
|
||||||
maximumTokenB: number | Numberu64,
|
maximumTokenB: number | Numberu64,
|
||||||
|
confirmOptions?: ConfirmOptions,
|
||||||
): Promise<TransactionSignature> {
|
): Promise<TransactionSignature> {
|
||||||
return await sendAndConfirmTransaction(
|
return await sendAndConfirmTransaction(
|
||||||
'depositAllTokenTypes',
|
|
||||||
this.connection,
|
this.connection,
|
||||||
new Transaction().add(
|
new Transaction().add(
|
||||||
TokenSwap.depositAllTokenTypesInstruction(
|
TokenSwap.depositAllTokenTypesInstruction(
|
||||||
|
@ -582,8 +586,8 @@ export class TokenSwap {
|
||||||
maximumTokenB,
|
maximumTokenB,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
this.payer,
|
[this.payer, userTransferAuthority],
|
||||||
userTransferAuthority,
|
confirmOptions,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -659,9 +663,9 @@ export class TokenSwap {
|
||||||
poolTokenAmount: number | Numberu64,
|
poolTokenAmount: number | Numberu64,
|
||||||
minimumTokenA: number | Numberu64,
|
minimumTokenA: number | Numberu64,
|
||||||
minimumTokenB: number | Numberu64,
|
minimumTokenB: number | Numberu64,
|
||||||
|
confirmOptions?: ConfirmOptions,
|
||||||
): Promise<TransactionSignature> {
|
): Promise<TransactionSignature> {
|
||||||
return await sendAndConfirmTransaction(
|
return await sendAndConfirmTransaction(
|
||||||
'withdraw',
|
|
||||||
this.connection,
|
this.connection,
|
||||||
new Transaction().add(
|
new Transaction().add(
|
||||||
TokenSwap.withdrawAllTokenTypesInstruction(
|
TokenSwap.withdrawAllTokenTypesInstruction(
|
||||||
|
@ -682,8 +686,8 @@ export class TokenSwap {
|
||||||
minimumTokenB,
|
minimumTokenB,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
this.payer,
|
[this.payer, userTransferAuthority],
|
||||||
userTransferAuthority,
|
confirmOptions,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -756,9 +760,9 @@ export class TokenSwap {
|
||||||
userTransferAuthority: Account,
|
userTransferAuthority: Account,
|
||||||
sourceTokenAmount: number | Numberu64,
|
sourceTokenAmount: number | Numberu64,
|
||||||
minimumPoolTokenAmount: number | Numberu64,
|
minimumPoolTokenAmount: number | Numberu64,
|
||||||
|
confirmOptions?: ConfirmOptions,
|
||||||
): Promise<TransactionSignature> {
|
): Promise<TransactionSignature> {
|
||||||
return await sendAndConfirmTransaction(
|
return await sendAndConfirmTransaction(
|
||||||
'depositSingleTokenTypeExactAmountIn',
|
|
||||||
this.connection,
|
this.connection,
|
||||||
new Transaction().add(
|
new Transaction().add(
|
||||||
TokenSwap.depositSingleTokenTypeExactAmountInInstruction(
|
TokenSwap.depositSingleTokenTypeExactAmountInInstruction(
|
||||||
|
@ -776,8 +780,8 @@ export class TokenSwap {
|
||||||
minimumPoolTokenAmount,
|
minimumPoolTokenAmount,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
this.payer,
|
[this.payer, userTransferAuthority],
|
||||||
userTransferAuthority,
|
confirmOptions,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -846,9 +850,9 @@ export class TokenSwap {
|
||||||
userTransferAuthority: Account,
|
userTransferAuthority: Account,
|
||||||
destinationTokenAmount: number | Numberu64,
|
destinationTokenAmount: number | Numberu64,
|
||||||
maximumPoolTokenAmount: number | Numberu64,
|
maximumPoolTokenAmount: number | Numberu64,
|
||||||
|
confirmOptions?: ConfirmOptions,
|
||||||
): Promise<TransactionSignature> {
|
): Promise<TransactionSignature> {
|
||||||
return await sendAndConfirmTransaction(
|
return await sendAndConfirmTransaction(
|
||||||
'withdrawSingleTokenTypeExactAmountOut',
|
|
||||||
this.connection,
|
this.connection,
|
||||||
new Transaction().add(
|
new Transaction().add(
|
||||||
TokenSwap.withdrawSingleTokenTypeExactAmountOutInstruction(
|
TokenSwap.withdrawSingleTokenTypeExactAmountOutInstruction(
|
||||||
|
@ -867,8 +871,8 @@ export class TokenSwap {
|
||||||
maximumPoolTokenAmount,
|
maximumPoolTokenAmount,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
this.payer,
|
[this.payer, userTransferAuthority],
|
||||||
userTransferAuthority,
|
confirmOptions,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,20 +0,0 @@
|
||||||
import {sendAndConfirmTransaction as realSendAndConfirmTransaction} from '@solana/web3.js';
|
|
||||||
import type {
|
|
||||||
Account,
|
|
||||||
Connection,
|
|
||||||
Transaction,
|
|
||||||
TransactionSignature,
|
|
||||||
} from '@solana/web3.js';
|
|
||||||
|
|
||||||
export function sendAndConfirmTransaction(
|
|
||||||
title: string,
|
|
||||||
connection: Connection,
|
|
||||||
transaction: Transaction,
|
|
||||||
...signers: Array<Account>
|
|
||||||
): Promise<TransactionSignature> {
|
|
||||||
return realSendAndConfirmTransaction(connection, transaction, signers, {
|
|
||||||
skipPreflight: false,
|
|
||||||
commitment: 'recent',
|
|
||||||
preflightCommitment: 'recent',
|
|
||||||
});
|
|
||||||
}
|
|
|
@ -4,11 +4,11 @@ import {
|
||||||
PublicKey,
|
PublicKey,
|
||||||
SystemProgram,
|
SystemProgram,
|
||||||
Transaction,
|
Transaction,
|
||||||
|
sendAndConfirmTransaction
|
||||||
} from '@solana/web3.js';
|
} from '@solana/web3.js';
|
||||||
import {AccountLayout, Token, TOKEN_PROGRAM_ID} from '@solana/spl-token';
|
import {AccountLayout, Token, TOKEN_PROGRAM_ID} from '@solana/spl-token';
|
||||||
|
|
||||||
import {TokenSwap, CurveType, TOKEN_SWAP_PROGRAM_ID} from '../src';
|
import {TokenSwap, CurveType, TOKEN_SWAP_PROGRAM_ID} from '../src';
|
||||||
import {sendAndConfirmTransaction} from '../src/util/send-and-confirm-transaction';
|
|
||||||
import {newAccountWithLamports} from '../src/util/new-account-with-lamports';
|
import {newAccountWithLamports} from '../src/util/new-account-with-lamports';
|
||||||
import {sleep} from '../src/util/sleep';
|
import {sleep} from '../src/util/sleep';
|
||||||
import {Numberu64} from '../src';
|
import {Numberu64} from '../src';
|
||||||
|
@ -253,6 +253,10 @@ export async function depositAllTokenTypes(): Promise<void> {
|
||||||
console.log('Creating depositor pool token account');
|
console.log('Creating depositor pool token account');
|
||||||
const newAccountPool = await tokenPool.createAccount(owner.publicKey);
|
const newAccountPool = await tokenPool.createAccount(owner.publicKey);
|
||||||
|
|
||||||
|
const confirmOptions = {
|
||||||
|
skipPreflight: true
|
||||||
|
}
|
||||||
|
|
||||||
console.log('Depositing into swap');
|
console.log('Depositing into swap');
|
||||||
await tokenSwap.depositAllTokenTypes(
|
await tokenSwap.depositAllTokenTypes(
|
||||||
userAccountA,
|
userAccountA,
|
||||||
|
@ -262,6 +266,7 @@ export async function depositAllTokenTypes(): Promise<void> {
|
||||||
POOL_TOKEN_AMOUNT,
|
POOL_TOKEN_AMOUNT,
|
||||||
tokenA,
|
tokenA,
|
||||||
tokenB,
|
tokenB,
|
||||||
|
confirmOptions
|
||||||
);
|
);
|
||||||
|
|
||||||
let info;
|
let info;
|
||||||
|
@ -314,6 +319,10 @@ export async function withdrawAllTokenTypes(): Promise<void> {
|
||||||
POOL_TOKEN_AMOUNT,
|
POOL_TOKEN_AMOUNT,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const confirmOptions = {
|
||||||
|
skipPreflight: true
|
||||||
|
}
|
||||||
|
|
||||||
console.log('Withdrawing pool tokens for A and B tokens');
|
console.log('Withdrawing pool tokens for A and B tokens');
|
||||||
await tokenSwap.withdrawAllTokenTypes(
|
await tokenSwap.withdrawAllTokenTypes(
|
||||||
userAccountA,
|
userAccountA,
|
||||||
|
@ -323,6 +332,7 @@ export async function withdrawAllTokenTypes(): Promise<void> {
|
||||||
POOL_TOKEN_AMOUNT,
|
POOL_TOKEN_AMOUNT,
|
||||||
tokenA,
|
tokenA,
|
||||||
tokenB,
|
tokenB,
|
||||||
|
confirmOptions
|
||||||
);
|
);
|
||||||
|
|
||||||
//const poolMintInfo = await tokenPool.getMintInfo();
|
//const poolMintInfo = await tokenPool.getMintInfo();
|
||||||
|
@ -407,15 +417,17 @@ export async function createAccountAndSwapAtomic(): Promise<void> {
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const confirmOptions = {
|
||||||
|
skipPreflight: true
|
||||||
|
}
|
||||||
|
|
||||||
// Send the instructions
|
// Send the instructions
|
||||||
console.log('sending big instruction');
|
console.log('sending big instruction');
|
||||||
await sendAndConfirmTransaction(
|
await sendAndConfirmTransaction(
|
||||||
'create account, approve transfer, swap',
|
|
||||||
connection,
|
connection,
|
||||||
transaction,
|
transaction,
|
||||||
owner,
|
[owner, newAccount, userTransferAuthority],
|
||||||
newAccount,
|
confirmOptions
|
||||||
userTransferAuthority,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
let info;
|
let info;
|
||||||
|
@ -443,6 +455,10 @@ export async function swap(): Promise<void> {
|
||||||
? await tokenPool.createAccount(owner.publicKey)
|
? await tokenPool.createAccount(owner.publicKey)
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
|
const confirmOptions = {
|
||||||
|
skipPreflight: true
|
||||||
|
}
|
||||||
|
|
||||||
console.log('Swapping');
|
console.log('Swapping');
|
||||||
await tokenSwap.swap(
|
await tokenSwap.swap(
|
||||||
userAccountA,
|
userAccountA,
|
||||||
|
@ -453,6 +469,7 @@ export async function swap(): Promise<void> {
|
||||||
userTransferAuthority,
|
userTransferAuthority,
|
||||||
SWAP_AMOUNT_IN,
|
SWAP_AMOUNT_IN,
|
||||||
SWAP_AMOUNT_OUT,
|
SWAP_AMOUNT_OUT,
|
||||||
|
confirmOptions
|
||||||
);
|
);
|
||||||
|
|
||||||
await sleep(500);
|
await sleep(500);
|
||||||
|
@ -541,6 +558,10 @@ export async function depositSingleTokenTypeExactAmountIn(): Promise<void> {
|
||||||
console.log('Creating depositor pool token account');
|
console.log('Creating depositor pool token account');
|
||||||
const newAccountPool = await tokenPool.createAccount(owner.publicKey);
|
const newAccountPool = await tokenPool.createAccount(owner.publicKey);
|
||||||
|
|
||||||
|
const confirmOptions = {
|
||||||
|
skipPreflight: true
|
||||||
|
}
|
||||||
|
|
||||||
console.log('Depositing token A into swap');
|
console.log('Depositing token A into swap');
|
||||||
await tokenSwap.depositSingleTokenTypeExactAmountIn(
|
await tokenSwap.depositSingleTokenTypeExactAmountIn(
|
||||||
userAccountA,
|
userAccountA,
|
||||||
|
@ -548,6 +569,7 @@ export async function depositSingleTokenTypeExactAmountIn(): Promise<void> {
|
||||||
userTransferAuthority,
|
userTransferAuthority,
|
||||||
depositAmount,
|
depositAmount,
|
||||||
poolTokenA,
|
poolTokenA,
|
||||||
|
confirmOptions
|
||||||
);
|
);
|
||||||
|
|
||||||
let info;
|
let info;
|
||||||
|
@ -564,6 +586,7 @@ export async function depositSingleTokenTypeExactAmountIn(): Promise<void> {
|
||||||
userTransferAuthority,
|
userTransferAuthority,
|
||||||
depositAmount,
|
depositAmount,
|
||||||
poolTokenB,
|
poolTokenB,
|
||||||
|
confirmOptions
|
||||||
);
|
);
|
||||||
|
|
||||||
info = await mintB.getAccountInfo(userAccountB);
|
info = await mintB.getAccountInfo(userAccountB);
|
||||||
|
@ -625,6 +648,10 @@ export async function withdrawSingleTokenTypeExactAmountOut(): Promise<void> {
|
||||||
adjustedPoolTokenA + adjustedPoolTokenB,
|
adjustedPoolTokenA + adjustedPoolTokenB,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const confirmOptions = {
|
||||||
|
skipPreflight: true
|
||||||
|
}
|
||||||
|
|
||||||
console.log('Withdrawing token A only');
|
console.log('Withdrawing token A only');
|
||||||
await tokenSwap.withdrawSingleTokenTypeExactAmountOut(
|
await tokenSwap.withdrawSingleTokenTypeExactAmountOut(
|
||||||
userAccountA,
|
userAccountA,
|
||||||
|
@ -632,6 +659,7 @@ export async function withdrawSingleTokenTypeExactAmountOut(): Promise<void> {
|
||||||
userTransferAuthority,
|
userTransferAuthority,
|
||||||
withdrawAmount,
|
withdrawAmount,
|
||||||
adjustedPoolTokenA,
|
adjustedPoolTokenA,
|
||||||
|
confirmOptions
|
||||||
);
|
);
|
||||||
|
|
||||||
let info;
|
let info;
|
||||||
|
@ -650,6 +678,7 @@ export async function withdrawSingleTokenTypeExactAmountOut(): Promise<void> {
|
||||||
userTransferAuthority,
|
userTransferAuthority,
|
||||||
withdrawAmount,
|
withdrawAmount,
|
||||||
adjustedPoolTokenB,
|
adjustedPoolTokenB,
|
||||||
|
confirmOptions
|
||||||
);
|
);
|
||||||
|
|
||||||
info = await mintB.getAccountInfo(userAccountB);
|
info = await mintB.getAccountInfo(userAccountB);
|
||||||
|
|
Loading…
Reference in New Issue