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

Commit 672d28f

Browse files
ilmoijoncinque
andauthored
added support for curve params (#2271)
* added support for curve params * Run pretty:fix Co-authored-by: Jon Cinque <[email protected]>
1 parent 1acbb47 commit 672d28f

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ hfuzz_workspace
1111
**/.DS_Store
1212
test-ledger
1313
docker-target
14+
.idea

token-swap/js/cli/main.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ import {
77
depositSingleTokenTypeExactAmountIn,
88
withdrawSingleTokenTypeExactAmountOut,
99
} from './token-swap-test';
10+
import {CurveType, Numberu64} from '../dist';
1011

1112
async function main() {
1213
// These test cases are designed to run sequentially and in the following order
13-
console.log('Run test: createTokenSwap');
14-
await createTokenSwap();
14+
console.log('Run test: createTokenSwap (constant price)');
15+
await createTokenSwap(CurveType.ConstantPrice, new Numberu64(1));
16+
console.log(
17+
'Run test: createTokenSwap (constant product, used further in tests)',
18+
);
19+
await createTokenSwap(CurveType.ConstantProduct);
1520
console.log('Run test: deposit all token types');
1621
await depositAllTokenTypes();
1722
console.log('Run test: withdraw all token types');

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {sendAndConfirmTransaction} from '../src/util/send-and-confirm-transactio
1212
import {newAccountWithLamports} from '../src/util/new-account-with-lamports';
1313
import {url} from '../src/util/url';
1414
import {sleep} from '../src/util/sleep';
15+
import {Numberu64} from '../dist';
1516

1617
// The following globals are created by `createTokenSwap` and used by subsequent tests
1718
// Token swap
@@ -46,9 +47,6 @@ const OWNER_WITHDRAW_FEE_DENOMINATOR = SWAP_PROGRAM_OWNER_FEE_ADDRESS ? 0 : 6;
4647
const HOST_FEE_NUMERATOR = 20;
4748
const HOST_FEE_DENOMINATOR = 100;
4849

49-
// curve type used to calculate swaps and deposits
50-
const CURVE_TYPE = CurveType.ConstantProduct;
51-
5250
// Initial amount in each swap token
5351
let currentSwapTokenA = 1000000;
5452
let currentSwapTokenB = 1000000;
@@ -88,7 +86,10 @@ async function getConnection(): Promise<Connection> {
8886
return connection;
8987
}
9088

91-
export async function createTokenSwap(): Promise<void> {
89+
export async function createTokenSwap(
90+
curveType: number,
91+
curveParameters?: Numberu64,
92+
): Promise<void> {
9293
const connection = await getConnection();
9394
const payer = await newAccountWithLamports(connection, 1000000000);
9495
owner = await newAccountWithLamports(connection, 1000000000);
@@ -169,7 +170,8 @@ export async function createTokenSwap(): Promise<void> {
169170
OWNER_WITHDRAW_FEE_DENOMINATOR,
170171
HOST_FEE_NUMERATOR,
171172
HOST_FEE_DENOMINATOR,
172-
CURVE_TYPE,
173+
curveType,
174+
curveParameters,
173175
);
174176

175177
console.log('loading token swap');
@@ -213,7 +215,7 @@ export async function createTokenSwap(): Promise<void> {
213215
assert(
214216
HOST_FEE_DENOMINATOR == fetchedTokenSwap.hostFeeDenominator.toNumber(),
215217
);
216-
assert(CURVE_TYPE == fetchedTokenSwap.curveType);
218+
assert(curveType == fetchedTokenSwap.curveType);
217219
}
218220

219221
export async function depositAllTokenTypes(): Promise<void> {

token-swap/js/src/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ export class TokenSwap {
190190
hostFeeNumerator: number,
191191
hostFeeDenominator: number,
192192
curveType: number,
193+
curveParameters: Numberu64 = new Numberu64(0),
193194
): TransactionInstruction {
194195
const keys = [
195196
{pubkey: tokenSwapAccount.publicKey, isSigner: false, isWritable: true},
@@ -216,6 +217,13 @@ export class TokenSwap {
216217
BufferLayout.blob(32, 'curveParameters'),
217218
]);
218219
let data = Buffer.alloc(1024);
220+
221+
// package curve parameters
222+
// NOTE: currently assume all curves take a single parameter, u64 int
223+
// the remaining 24 of the 32 bytes available are filled with 0s
224+
let curveParamsBuffer = Buffer.alloc(32);
225+
curveParameters.toBuffer().copy(curveParamsBuffer);
226+
219227
{
220228
const encodeLength = commandDataLayout.encode(
221229
{
@@ -230,6 +238,7 @@ export class TokenSwap {
230238
hostFeeNumerator,
231239
hostFeeDenominator,
232240
curveType,
241+
curveParameters: curveParamsBuffer,
233242
},
234243
data,
235244
);
@@ -360,6 +369,7 @@ export class TokenSwap {
360369
hostFeeNumerator: number,
361370
hostFeeDenominator: number,
362371
curveType: number,
372+
curveParameters?: Numberu64,
363373
): Promise<TokenSwap> {
364374
let transaction;
365375
const tokenSwap = new TokenSwap(
@@ -421,6 +431,7 @@ export class TokenSwap {
421431
hostFeeNumerator,
422432
hostFeeDenominator,
423433
curveType,
434+
curveParameters,
424435
);
425436

426437
transaction.add(instruction);

0 commit comments

Comments
 (0)