Skip to content

Commit

Permalink
token-swap: Add curve and pool token trait and integrate in processor (
Browse files Browse the repository at this point in the history
…solana-labs#624)

* Add SwapCurve trait and integrate in processor

* Add PoolTokenConverter trait to correspond

* Add curve type parameter to initialization and JS

* Refactor for flat curve test, fmt

* Update token-swap/program/src/curve.rs

Co-authored-by: Tyera Eulberg <[email protected]>

* Refactor swap curve to allow for any implementation

* Rename SwapCurveWrapper -> SwapCurve

* Run cargo fmt

* Update CurveType to enum in JS

Co-authored-by: Tyera Eulberg <[email protected]>
  • Loading branch information
joncinque and CriesofCarrots authored Oct 21, 2020
1 parent ed11438 commit db42f7a
Show file tree
Hide file tree
Showing 8 changed files with 736 additions and 353 deletions.
8 changes: 6 additions & 2 deletions token-swap/js/cli/token-swap-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '@solana/web3.js';

import {Token} from '../../../token/js/client/token';
import {TokenSwap} from '../client/token-swap';
import {TokenSwap, CurveType} from '../client/token-swap';
import {Store} from '../client/util/store';
import {newAccountWithLamports} from '../client/util/new-account-with-lamports';
import {url} from '../url';
Expand All @@ -34,6 +34,8 @@ let mintB: Token;
let tokenAccountA: PublicKey;
let tokenAccountB: PublicKey;

// curve type used to calculate swaps and deposits
const CURVE_TYPE = CurveType.ConstantProduct;
// Initial amount in each swap token
const BASE_AMOUNT = 1000;
// Amount passed to swap instruction
Expand Down Expand Up @@ -194,13 +196,14 @@ export async function createTokenSwap(): Promise<void> {
swapPayer,
tokenSwapAccount,
authority,
nonce,
tokenAccountA,
tokenAccountB,
tokenPool.publicKey,
tokenAccountPool,
tokenSwapProgramId,
tokenProgramId,
nonce,
CURVE_TYPE,
1,
4,
);
Expand All @@ -217,6 +220,7 @@ export async function createTokenSwap(): Promise<void> {
assert(fetchedTokenSwap.tokenAccountA.equals(tokenAccountA));
assert(fetchedTokenSwap.tokenAccountB.equals(tokenAccountB));
assert(fetchedTokenSwap.poolToken.equals(tokenPool.publicKey));
assert(CURVE_TYPE == fetchedTokenSwap.curveType);
assert(1 == fetchedTokenSwap.feeNumerator.toNumber());
assert(4 == fetchedTokenSwap.feeDenominator.toNumber());
}
Expand Down
33 changes: 28 additions & 5 deletions token-swap/js/client/token-swap.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,18 @@ export const TokenSwapLayout: typeof BufferLayout.Structure = BufferLayout.struc
Layout.publicKey('tokenAccountA'),
Layout.publicKey('tokenAccountB'),
Layout.publicKey('tokenPool'),
BufferLayout.u8('curveType'),
Layout.uint64('feeNumerator'),
Layout.uint64('feeDenominator'),
BufferLayout.blob(48, 'padding'),
],
);

export const CurveType = Object.freeze({
ConstantProduct: 0, // Constant product curve, Uniswap-style
Flat: 1, // Flat curve, always 1:1 trades
});

/**
* A program to exchange tokens against a pool of liquidity
*/
Expand Down Expand Up @@ -123,6 +130,11 @@ export class TokenSwap {
*/
feeDenominator: Numberu64;

/**
* CurveType, current options are:
*/
curveType: number;

/**
* Fee payer
*/
Expand Down Expand Up @@ -150,6 +162,7 @@ export class TokenSwap {
authority: PublicKey,
tokenAccountA: PublicKey,
tokenAccountB: PublicKey,
curveType: number,
feeNumerator: Numberu64,
feeDenominator: Numberu64,
payer: Account,
Expand All @@ -163,6 +176,7 @@ export class TokenSwap {
authority,
tokenAccountA,
tokenAccountB,
curveType,
feeNumerator,
feeDenominator,
payer,
Expand All @@ -185,13 +199,14 @@ export class TokenSwap {
static createInitSwapInstruction(
tokenSwapAccount: Account,
authority: PublicKey,
nonce: number,
tokenAccountA: PublicKey,
tokenAccountB: PublicKey,
tokenPool: PublicKey,
tokenAccountPool: PublicKey,
tokenProgramId: PublicKey,
swapProgramId: PublicKey,
nonce: number,
curveType: number,
feeNumerator: number,
feeDenominator: number,
): TransactionInstruction {
Expand All @@ -206,18 +221,21 @@ export class TokenSwap {
];
const commandDataLayout = BufferLayout.struct([
BufferLayout.u8('instruction'),
BufferLayout.u8('nonce'),
BufferLayout.u8('curveType'),
BufferLayout.nu64('feeNumerator'),
BufferLayout.nu64('feeDenominator'),
BufferLayout.u8('nonce'),
BufferLayout.blob(48, 'padding'),
]);
let data = Buffer.alloc(1024);
{
const encodeLength = commandDataLayout.encode(
{
instruction: 0, // InitializeSwap instruction
nonce,
curveType,
feeNumerator,
feeDenominator,
nonce,
},
data,
);
Expand Down Expand Up @@ -254,6 +272,7 @@ export class TokenSwap {

const feeNumerator = Numberu64.fromBuffer(tokenSwapData.feeNumerator);
const feeDenominator = Numberu64.fromBuffer(tokenSwapData.feeDenominator);
const curveType = tokenSwapData.curveType;

return new TokenSwap(
connection,
Expand All @@ -264,6 +283,7 @@ export class TokenSwap {
authority,
tokenAccountA,
tokenAccountB,
curveType,
feeNumerator,
feeDenominator,
payer,
Expand Down Expand Up @@ -293,13 +313,14 @@ export class TokenSwap {
payer: Account,
tokenSwapAccount: Account,
authority: PublicKey,
nonce: number,
tokenAccountA: PublicKey,
tokenAccountB: PublicKey,
poolToken: PublicKey,
tokenAccountPool: PublicKey,
swapProgramId: PublicKey,
tokenProgramId: PublicKey,
nonce: number,
curveType: number,
feeNumerator: number,
feeDenominator: number,
): Promise<TokenSwap> {
Expand All @@ -313,6 +334,7 @@ export class TokenSwap {
authority,
tokenAccountA,
tokenAccountB,
curveType,
new Numberu64(feeNumerator),
new Numberu64(feeDenominator),
payer,
Expand All @@ -336,13 +358,14 @@ export class TokenSwap {
const instruction = TokenSwap.createInitSwapInstruction(
tokenSwapAccount,
authority,
nonce,
tokenAccountA,
tokenAccountB,
poolToken,
tokenAccountPool,
tokenProgramId,
swapProgramId,
nonce,
curveType,
feeNumerator,
feeDenominator,
);
Expand Down
6 changes: 5 additions & 1 deletion token-swap/js/module.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ declare module '@solana/spl-token-swap' {
}

export const TokenSwapLayout: Layout;
export const CurveType: Object;

export class TokenSwap {
constructor(
Expand All @@ -28,6 +29,7 @@ declare module '@solana/spl-token-swap' {
authority: PublicKey,
tokenAccountA: PublicKey,
tokenAccountB: PublicKey,
curveType: number,
feeNumerator: Numberu64,
feeDenominator: Numberu64,
payer: Account,
Expand All @@ -40,13 +42,14 @@ declare module '@solana/spl-token-swap' {
static createInitSwapInstruction(
tokenSwapAccount: Account,
authority: PublicKey,
nonce: number,
tokenAccountA: PublicKey,
tokenAccountB: PublicKey,
tokenPool: PublicKey,
tokenAccountPool: PublicKey,
tokenProgramId: PublicKey,
swapProgramId: PublicKey,
nonce: number,
curveType: number,
feeNumerator: number,
feeDenominator: number,
): TransactionInstruction;
Expand All @@ -69,6 +72,7 @@ declare module '@solana/spl-token-swap' {
tokenAccountPool: PublicKey,
tokenProgramId: PublicKey,
nonce: number,
curveType: number,
feeNumerator: number,
feeDenominator: number,
swapProgramId: PublicKey,
Expand Down
7 changes: 6 additions & 1 deletion token-swap/js/module.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ declare module '@solana/spl-token-swap' {

declare export var TokenSwapLayout: Layout;

declare export var CurveType: Object;

declare export class TokenSwap {
constructor(
connection: Connection,
Expand All @@ -24,6 +26,7 @@ declare module '@solana/spl-token-swap' {
authority: PublicKey,
tokenAccountA: PublicKey,
tokenAccountB: PublicKey,
curveType: number,
feeNumerator: Numberu64,
feeDenominator: Numberu64,
payer: Account,
Expand All @@ -37,12 +40,13 @@ declare module '@solana/spl-token-swap' {
programId: PublicKey,
tokenSwapAccount: Account,
authority: PublicKey,
nonce: number,
tokenAccountA: PublicKey,
tokenAccountB: PublicKey,
tokenPool: PublicKey,
tokenAccountPool: PublicKey,
tokenProgramId: PublicKey,
nonce: number,
curveType: number,
feeNumerator: number,
feeDenominator: number,
): TransactionInstruction;
Expand All @@ -65,6 +69,7 @@ declare module '@solana/spl-token-swap' {
tokenAccountPool: PublicKey,
tokenProgramId: PublicKey,
nonce: number,
curveType: number,
feeNumerator: number,
feeDenominator: number,
programId: PublicKey,
Expand Down
Loading

0 comments on commit db42f7a

Please sign in to comment.