added support for curve params (#2271)

* added support for curve params

* Run pretty:fix

Co-authored-by: Jon Cinque <jon.cinque@gmail.com>
This commit is contained in:
ilja 2021-08-13 07:30:30 +03:00 committed by GitHub
parent 1acbb4776c
commit 672d28fe2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 8 deletions

1
.gitignore vendored
View File

@ -11,3 +11,4 @@ hfuzz_workspace
**/.DS_Store **/.DS_Store
test-ledger test-ledger
docker-target docker-target
.idea

View File

@ -7,11 +7,16 @@ import {
depositSingleTokenTypeExactAmountIn, depositSingleTokenTypeExactAmountIn,
withdrawSingleTokenTypeExactAmountOut, withdrawSingleTokenTypeExactAmountOut,
} from './token-swap-test'; } from './token-swap-test';
import {CurveType, Numberu64} from '../dist';
async function main() { async function main() {
// These test cases are designed to run sequentially and in the following order // These test cases are designed to run sequentially and in the following order
console.log('Run test: createTokenSwap'); console.log('Run test: createTokenSwap (constant price)');
await createTokenSwap(); 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'); console.log('Run test: deposit all token types');
await depositAllTokenTypes(); await depositAllTokenTypes();
console.log('Run test: withdraw all token types'); console.log('Run test: withdraw all token types');

View File

@ -12,6 +12,7 @@ import {sendAndConfirmTransaction} from '../src/util/send-and-confirm-transactio
import {newAccountWithLamports} from '../src/util/new-account-with-lamports'; import {newAccountWithLamports} from '../src/util/new-account-with-lamports';
import {url} from '../src/util/url'; import {url} from '../src/util/url';
import {sleep} from '../src/util/sleep'; import {sleep} from '../src/util/sleep';
import {Numberu64} from '../dist';
// The following globals are created by `createTokenSwap` and used by subsequent tests // The following globals are created by `createTokenSwap` and used by subsequent tests
// Token swap // 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_NUMERATOR = 20;
const HOST_FEE_DENOMINATOR = 100; const HOST_FEE_DENOMINATOR = 100;
// curve type used to calculate swaps and deposits
const CURVE_TYPE = CurveType.ConstantProduct;
// Initial amount in each swap token // Initial amount in each swap token
let currentSwapTokenA = 1000000; let currentSwapTokenA = 1000000;
let currentSwapTokenB = 1000000; let currentSwapTokenB = 1000000;
@ -88,7 +86,10 @@ async function getConnection(): Promise<Connection> {
return connection; return connection;
} }
export async function createTokenSwap(): Promise<void> { export async function createTokenSwap(
curveType: number,
curveParameters?: Numberu64,
): Promise<void> {
const connection = await getConnection(); const connection = await getConnection();
const payer = await newAccountWithLamports(connection, 1000000000); const payer = await newAccountWithLamports(connection, 1000000000);
owner = await newAccountWithLamports(connection, 1000000000); owner = await newAccountWithLamports(connection, 1000000000);
@ -169,7 +170,8 @@ export async function createTokenSwap(): Promise<void> {
OWNER_WITHDRAW_FEE_DENOMINATOR, OWNER_WITHDRAW_FEE_DENOMINATOR,
HOST_FEE_NUMERATOR, HOST_FEE_NUMERATOR,
HOST_FEE_DENOMINATOR, HOST_FEE_DENOMINATOR,
CURVE_TYPE, curveType,
curveParameters,
); );
console.log('loading token swap'); console.log('loading token swap');
@ -213,7 +215,7 @@ export async function createTokenSwap(): Promise<void> {
assert( assert(
HOST_FEE_DENOMINATOR == fetchedTokenSwap.hostFeeDenominator.toNumber(), HOST_FEE_DENOMINATOR == fetchedTokenSwap.hostFeeDenominator.toNumber(),
); );
assert(CURVE_TYPE == fetchedTokenSwap.curveType); assert(curveType == fetchedTokenSwap.curveType);
} }
export async function depositAllTokenTypes(): Promise<void> { export async function depositAllTokenTypes(): Promise<void> {

View File

@ -190,6 +190,7 @@ export class TokenSwap {
hostFeeNumerator: number, hostFeeNumerator: number,
hostFeeDenominator: number, hostFeeDenominator: number,
curveType: number, curveType: number,
curveParameters: Numberu64 = new Numberu64(0),
): TransactionInstruction { ): TransactionInstruction {
const keys = [ const keys = [
{pubkey: tokenSwapAccount.publicKey, isSigner: false, isWritable: true}, {pubkey: tokenSwapAccount.publicKey, isSigner: false, isWritable: true},
@ -216,6 +217,13 @@ export class TokenSwap {
BufferLayout.blob(32, 'curveParameters'), BufferLayout.blob(32, 'curveParameters'),
]); ]);
let data = Buffer.alloc(1024); 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( const encodeLength = commandDataLayout.encode(
{ {
@ -230,6 +238,7 @@ export class TokenSwap {
hostFeeNumerator, hostFeeNumerator,
hostFeeDenominator, hostFeeDenominator,
curveType, curveType,
curveParameters: curveParamsBuffer,
}, },
data, data,
); );
@ -360,6 +369,7 @@ export class TokenSwap {
hostFeeNumerator: number, hostFeeNumerator: number,
hostFeeDenominator: number, hostFeeDenominator: number,
curveType: number, curveType: number,
curveParameters?: Numberu64,
): Promise<TokenSwap> { ): Promise<TokenSwap> {
let transaction; let transaction;
const tokenSwap = new TokenSwap( const tokenSwap = new TokenSwap(
@ -421,6 +431,7 @@ export class TokenSwap {
hostFeeNumerator, hostFeeNumerator,
hostFeeDenominator, hostFeeDenominator,
curveType, curveType,
curveParameters,
); );
transaction.add(instruction); transaction.add(instruction);