Updated index.ts and tests (#3094)

This commit is contained in:
Anurag Gupta 2022-04-21 04:30:43 +05:30 committed by GitHub
parent 4825298bc9
commit fe20132b07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 45 deletions

View File

@ -2,17 +2,21 @@ import assert from 'assert';
import BN from 'bn.js';
import {Buffer} from 'buffer';
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 {
Account,
PublicKey,
SystemProgram,
Transaction,
TransactionInstruction,
sendAndConfirmTransaction,
} from '@solana/web3.js';
import * as Layout from './layout';
import {sendAndConfirmTransaction} from './util/send-and-confirm-transaction';
import {loadAccount} from './util/account';
export const TOKEN_SWAP_PROGRAM_ID: PublicKey = new PublicKey(
@ -365,6 +369,7 @@ export class TokenSwap {
hostFeeDenominator: number,
curveType: number,
curveParameters?: Numberu64,
confirmOptions?: ConfirmOptions,
): Promise<TokenSwap> {
let transaction;
const tokenSwap = new TokenSwap(
@ -430,11 +435,10 @@ export class TokenSwap {
transaction.add(instruction);
await sendAndConfirmTransaction(
'createAccount and InitializeSwap',
connection,
transaction,
payer,
tokenSwapAccount,
[payer, tokenSwapAccount],
confirmOptions,
);
return tokenSwap;
@ -461,9 +465,9 @@ export class TokenSwap {
userTransferAuthority: Account,
amountIn: number | Numberu64,
minimumAmountOut: number | Numberu64,
confirmOptions?: ConfirmOptions,
): Promise<TransactionSignature> {
return await sendAndConfirmTransaction(
'swap',
this.connection,
new Transaction().add(
TokenSwap.swapInstruction(
@ -483,8 +487,8 @@ export class TokenSwap {
minimumAmountOut,
),
),
this.payer,
userTransferAuthority,
[this.payer, userTransferAuthority],
confirmOptions,
);
}
@ -560,9 +564,9 @@ export class TokenSwap {
poolTokenAmount: number | Numberu64,
maximumTokenA: number | Numberu64,
maximumTokenB: number | Numberu64,
confirmOptions?: ConfirmOptions,
): Promise<TransactionSignature> {
return await sendAndConfirmTransaction(
'depositAllTokenTypes',
this.connection,
new Transaction().add(
TokenSwap.depositAllTokenTypesInstruction(
@ -582,8 +586,8 @@ export class TokenSwap {
maximumTokenB,
),
),
this.payer,
userTransferAuthority,
[this.payer, userTransferAuthority],
confirmOptions,
);
}
@ -659,9 +663,9 @@ export class TokenSwap {
poolTokenAmount: number | Numberu64,
minimumTokenA: number | Numberu64,
minimumTokenB: number | Numberu64,
confirmOptions?: ConfirmOptions,
): Promise<TransactionSignature> {
return await sendAndConfirmTransaction(
'withdraw',
this.connection,
new Transaction().add(
TokenSwap.withdrawAllTokenTypesInstruction(
@ -682,8 +686,8 @@ export class TokenSwap {
minimumTokenB,
),
),
this.payer,
userTransferAuthority,
[this.payer, userTransferAuthority],
confirmOptions,
);
}
@ -756,9 +760,9 @@ export class TokenSwap {
userTransferAuthority: Account,
sourceTokenAmount: number | Numberu64,
minimumPoolTokenAmount: number | Numberu64,
confirmOptions?: ConfirmOptions,
): Promise<TransactionSignature> {
return await sendAndConfirmTransaction(
'depositSingleTokenTypeExactAmountIn',
this.connection,
new Transaction().add(
TokenSwap.depositSingleTokenTypeExactAmountInInstruction(
@ -776,8 +780,8 @@ export class TokenSwap {
minimumPoolTokenAmount,
),
),
this.payer,
userTransferAuthority,
[this.payer, userTransferAuthority],
confirmOptions,
);
}
@ -846,9 +850,9 @@ export class TokenSwap {
userTransferAuthority: Account,
destinationTokenAmount: number | Numberu64,
maximumPoolTokenAmount: number | Numberu64,
confirmOptions?: ConfirmOptions,
): Promise<TransactionSignature> {
return await sendAndConfirmTransaction(
'withdrawSingleTokenTypeExactAmountOut',
this.connection,
new Transaction().add(
TokenSwap.withdrawSingleTokenTypeExactAmountOutInstruction(
@ -867,8 +871,8 @@ export class TokenSwap {
maximumPoolTokenAmount,
),
),
this.payer,
userTransferAuthority,
[this.payer, userTransferAuthority],
confirmOptions,
);
}

View File

@ -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',
});
}

View File

@ -4,11 +4,11 @@ import {
PublicKey,
SystemProgram,
Transaction,
sendAndConfirmTransaction
} from '@solana/web3.js';
import {AccountLayout, Token, TOKEN_PROGRAM_ID} from '@solana/spl-token';
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 {sleep} from '../src/util/sleep';
import {Numberu64} from '../src';
@ -253,6 +253,10 @@ export async function depositAllTokenTypes(): Promise<void> {
console.log('Creating depositor pool token account');
const newAccountPool = await tokenPool.createAccount(owner.publicKey);
const confirmOptions = {
skipPreflight: true
}
console.log('Depositing into swap');
await tokenSwap.depositAllTokenTypes(
userAccountA,
@ -262,6 +266,7 @@ export async function depositAllTokenTypes(): Promise<void> {
POOL_TOKEN_AMOUNT,
tokenA,
tokenB,
confirmOptions
);
let info;
@ -314,6 +319,10 @@ export async function withdrawAllTokenTypes(): Promise<void> {
POOL_TOKEN_AMOUNT,
);
const confirmOptions = {
skipPreflight: true
}
console.log('Withdrawing pool tokens for A and B tokens');
await tokenSwap.withdrawAllTokenTypes(
userAccountA,
@ -323,6 +332,7 @@ export async function withdrawAllTokenTypes(): Promise<void> {
POOL_TOKEN_AMOUNT,
tokenA,
tokenB,
confirmOptions
);
//const poolMintInfo = await tokenPool.getMintInfo();
@ -407,15 +417,17 @@ export async function createAccountAndSwapAtomic(): Promise<void> {
),
);
const confirmOptions = {
skipPreflight: true
}
// Send the instructions
console.log('sending big instruction');
await sendAndConfirmTransaction(
'create account, approve transfer, swap',
connection,
transaction,
owner,
newAccount,
userTransferAuthority,
[owner, newAccount, userTransferAuthority],
confirmOptions
);
let info;
@ -443,6 +455,10 @@ export async function swap(): Promise<void> {
? await tokenPool.createAccount(owner.publicKey)
: null;
const confirmOptions = {
skipPreflight: true
}
console.log('Swapping');
await tokenSwap.swap(
userAccountA,
@ -453,6 +469,7 @@ export async function swap(): Promise<void> {
userTransferAuthority,
SWAP_AMOUNT_IN,
SWAP_AMOUNT_OUT,
confirmOptions
);
await sleep(500);
@ -541,6 +558,10 @@ export async function depositSingleTokenTypeExactAmountIn(): Promise<void> {
console.log('Creating depositor pool token account');
const newAccountPool = await tokenPool.createAccount(owner.publicKey);
const confirmOptions = {
skipPreflight: true
}
console.log('Depositing token A into swap');
await tokenSwap.depositSingleTokenTypeExactAmountIn(
userAccountA,
@ -548,6 +569,7 @@ export async function depositSingleTokenTypeExactAmountIn(): Promise<void> {
userTransferAuthority,
depositAmount,
poolTokenA,
confirmOptions
);
let info;
@ -564,6 +586,7 @@ export async function depositSingleTokenTypeExactAmountIn(): Promise<void> {
userTransferAuthority,
depositAmount,
poolTokenB,
confirmOptions
);
info = await mintB.getAccountInfo(userAccountB);
@ -625,6 +648,10 @@ export async function withdrawSingleTokenTypeExactAmountOut(): Promise<void> {
adjustedPoolTokenA + adjustedPoolTokenB,
);
const confirmOptions = {
skipPreflight: true
}
console.log('Withdrawing token A only');
await tokenSwap.withdrawSingleTokenTypeExactAmountOut(
userAccountA,
@ -632,6 +659,7 @@ export async function withdrawSingleTokenTypeExactAmountOut(): Promise<void> {
userTransferAuthority,
withdrawAmount,
adjustedPoolTokenA,
confirmOptions
);
let info;
@ -650,6 +678,7 @@ export async function withdrawSingleTokenTypeExactAmountOut(): Promise<void> {
userTransferAuthority,
withdrawAmount,
adjustedPoolTokenB,
confirmOptions
);
info = await mintB.getAccountInfo(userAccountB);