diff --git a/.gitignore b/.gitignore index 90bc25d3..81dda24d 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ hfuzz_workspace **/.DS_Store test-ledger docker-target +.idea diff --git a/token-swap/js/cli/main.ts b/token-swap/js/cli/main.ts index 3bc3dc61..54786f69 100644 --- a/token-swap/js/cli/main.ts +++ b/token-swap/js/cli/main.ts @@ -7,11 +7,16 @@ import { depositSingleTokenTypeExactAmountIn, withdrawSingleTokenTypeExactAmountOut, } from './token-swap-test'; +import {CurveType, Numberu64} from '../dist'; async function main() { // These test cases are designed to run sequentially and in the following order - console.log('Run test: createTokenSwap'); - await createTokenSwap(); + console.log('Run test: createTokenSwap (constant price)'); + await createTokenSwap(CurveType.ConstantPrice, new Numberu64(1)); + console.log( + 'Run test: createTokenSwap (constant product, used further in tests)', + ); + await createTokenSwap(CurveType.ConstantProduct); console.log('Run test: deposit all token types'); await depositAllTokenTypes(); console.log('Run test: withdraw all token types'); diff --git a/token-swap/js/cli/token-swap-test.ts b/token-swap/js/cli/token-swap-test.ts index 3aaf22e0..e9fb0440 100644 --- a/token-swap/js/cli/token-swap-test.ts +++ b/token-swap/js/cli/token-swap-test.ts @@ -12,6 +12,7 @@ import {sendAndConfirmTransaction} from '../src/util/send-and-confirm-transactio import {newAccountWithLamports} from '../src/util/new-account-with-lamports'; import {url} from '../src/util/url'; import {sleep} from '../src/util/sleep'; +import {Numberu64} from '../dist'; // The following globals are created by `createTokenSwap` and used by subsequent tests // Token swap @@ -46,9 +47,6 @@ const OWNER_WITHDRAW_FEE_DENOMINATOR = SWAP_PROGRAM_OWNER_FEE_ADDRESS ? 0 : 6; const HOST_FEE_NUMERATOR = 20; const HOST_FEE_DENOMINATOR = 100; -// curve type used to calculate swaps and deposits -const CURVE_TYPE = CurveType.ConstantProduct; - // Initial amount in each swap token let currentSwapTokenA = 1000000; let currentSwapTokenB = 1000000; @@ -88,7 +86,10 @@ async function getConnection(): Promise { return connection; } -export async function createTokenSwap(): Promise { +export async function createTokenSwap( + curveType: number, + curveParameters?: Numberu64, +): Promise { const connection = await getConnection(); const payer = await newAccountWithLamports(connection, 1000000000); owner = await newAccountWithLamports(connection, 1000000000); @@ -169,7 +170,8 @@ export async function createTokenSwap(): Promise { OWNER_WITHDRAW_FEE_DENOMINATOR, HOST_FEE_NUMERATOR, HOST_FEE_DENOMINATOR, - CURVE_TYPE, + curveType, + curveParameters, ); console.log('loading token swap'); @@ -213,7 +215,7 @@ export async function createTokenSwap(): Promise { assert( HOST_FEE_DENOMINATOR == fetchedTokenSwap.hostFeeDenominator.toNumber(), ); - assert(CURVE_TYPE == fetchedTokenSwap.curveType); + assert(curveType == fetchedTokenSwap.curveType); } export async function depositAllTokenTypes(): Promise { diff --git a/token-swap/js/src/index.ts b/token-swap/js/src/index.ts index 6fba6f63..899c36a0 100644 --- a/token-swap/js/src/index.ts +++ b/token-swap/js/src/index.ts @@ -190,6 +190,7 @@ export class TokenSwap { hostFeeNumerator: number, hostFeeDenominator: number, curveType: number, + curveParameters: Numberu64 = new Numberu64(0), ): TransactionInstruction { const keys = [ {pubkey: tokenSwapAccount.publicKey, isSigner: false, isWritable: true}, @@ -216,6 +217,13 @@ export class TokenSwap { BufferLayout.blob(32, 'curveParameters'), ]); let data = Buffer.alloc(1024); + + // package curve parameters + // NOTE: currently assume all curves take a single parameter, u64 int + // the remaining 24 of the 32 bytes available are filled with 0s + let curveParamsBuffer = Buffer.alloc(32); + curveParameters.toBuffer().copy(curveParamsBuffer); + { const encodeLength = commandDataLayout.encode( { @@ -230,6 +238,7 @@ export class TokenSwap { hostFeeNumerator, hostFeeDenominator, curveType, + curveParameters: curveParamsBuffer, }, data, ); @@ -360,6 +369,7 @@ export class TokenSwap { hostFeeNumerator: number, hostFeeDenominator: number, curveType: number, + curveParameters?: Numberu64, ): Promise { let transaction; const tokenSwap = new TokenSwap( @@ -421,6 +431,7 @@ export class TokenSwap { hostFeeNumerator, hostFeeDenominator, curveType, + curveParameters, ); transaction.add(instruction);