Skip to content

Commit

Permalink
serum: Add limit param to prune ix (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
armaniferrante authored Aug 3, 2021
1 parent 90d7bae commit 5f7ceaf
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 97 deletions.
2 changes: 1 addition & 1 deletion packages/serum/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@project-serum/serum",
"version": "0.13.52",
"version": "0.13.55",
"description": "Library for interacting with the serum dex",
"license": "MIT",
"repository": "project-serum/serum-ts",
Expand Down
183 changes: 94 additions & 89 deletions packages/serum/src/error.ts
Original file line number Diff line number Diff line change
@@ -1,100 +1,103 @@
import { Transaction, SystemProgram } from "@solana/web3.js";
import {PROGRAM_LAYOUT_VERSIONS} from "./tokens_and_markets";
import { Transaction, SystemProgram } from '@solana/web3.js';
import { PROGRAM_LAYOUT_VERSIONS } from './tokens_and_markets';
import { TOKEN_PROGRAM_ID } from './token-instructions';

export enum DexError {
InvalidMarketFlags = 0,
InvalidAskFlags,
InvalidBidFlags,
InvalidQueueLength,
OwnerAccountNotProvided,

ConsumeEventsQueueFailure,
WrongCoinVault,
WrongPcVault,
WrongCoinMint,
WrongPcMint,

CoinVaultProgramId = 10,
PcVaultProgramId,
CoinMintProgramId,
PcMintProgramId,

WrongCoinMintSize,
WrongPcMintSize,
WrongCoinVaultSize,
WrongPcVaultSize,

UninitializedVault,
UninitializedMint,

CoinMintUninitialized = 20,
PcMintUninitialized,
WrongMint,
WrongVaultOwner,
VaultHasDelegate,

AlreadyInitialized,
WrongAccountDataAlignment,
WrongAccountDataPaddingLength,
WrongAccountHeadPadding,
WrongAccountTailPadding,

RequestQueueEmpty = 30,
EventQueueTooSmall,
SlabTooSmall,
BadVaultSignerNonce,
InsufficientFunds,

SplAccountProgramId,
SplAccountLen,
WrongFeeDiscountAccountOwner,
WrongFeeDiscountMint,

CoinPayerProgramId,
PcPayerProgramId = 40,
ClientIdNotFound,
TooManyOpenOrders,

FakeErrorSoWeDontChangeNumbers,
BorrowError,

WrongOrdersAccount,
WrongBidsAccount,
WrongAsksAccount,
WrongRequestQueueAccount,
WrongEventQueueAccount,

RequestQueueFull = 50,
EventQueueFull,
MarketIsDisabled,
WrongSigner,
TransferFailed,
ClientOrderIdIsZero,

WrongRentSysvarAccount,
RentNotProvided,
OrdersNotRentExempt,
OrderNotFound,
OrderNotYours,

WouldSelfTrade,

Unknown = 1000,
InvalidMarketFlags = 0,
InvalidAskFlags,
InvalidBidFlags,
InvalidQueueLength,
OwnerAccountNotProvided,

ConsumeEventsQueueFailure,
WrongCoinVault,
WrongPcVault,
WrongCoinMint,
WrongPcMint,

CoinVaultProgramId = 10,
PcVaultProgramId,
CoinMintProgramId,
PcMintProgramId,

WrongCoinMintSize,
WrongPcMintSize,
WrongCoinVaultSize,
WrongPcVaultSize,

UninitializedVault,
UninitializedMint,

CoinMintUninitialized = 20,
PcMintUninitialized,
WrongMint,
WrongVaultOwner,
VaultHasDelegate,

AlreadyInitialized,
WrongAccountDataAlignment,
WrongAccountDataPaddingLength,
WrongAccountHeadPadding,
WrongAccountTailPadding,

RequestQueueEmpty = 30,
EventQueueTooSmall,
SlabTooSmall,
BadVaultSignerNonce,
InsufficientFunds,

SplAccountProgramId,
SplAccountLen,
WrongFeeDiscountAccountOwner,
WrongFeeDiscountMint,

CoinPayerProgramId,
PcPayerProgramId = 40,
ClientIdNotFound,
TooManyOpenOrders,

FakeErrorSoWeDontChangeNumbers,
BorrowError,

WrongOrdersAccount,
WrongBidsAccount,
WrongAsksAccount,
WrongRequestQueueAccount,
WrongEventQueueAccount,

RequestQueueFull = 50,
EventQueueFull,
MarketIsDisabled,
WrongSigner,
TransferFailed,
ClientOrderIdIsZero,

WrongRentSysvarAccount,
RentNotProvided,
OrdersNotRentExempt,
OrderNotFound,
OrderNotYours,

WouldSelfTrade,

Unknown = 1000,
}

export const KNOWN_PROGRAMS = {
[TOKEN_PROGRAM_ID.toString()]: 'Token program',
[SystemProgram.programId.toString()]: 'System program'
[SystemProgram.programId.toString()]: 'System program',
};

type CustomError = { Custom: number }
type InstructionError = [number, CustomError]
type CustomError = { Custom: number };
type InstructionError = [number, CustomError];

export function parseInstructionErrorResponse(transaction: Transaction, errorResponse: InstructionError): {
failedInstructionIndex: number,
error: string,
failedProgram: string,
export function parseInstructionErrorResponse(
transaction: Transaction,
errorResponse: InstructionError,
): {
failedInstructionIndex: number;
error: string;
failedProgram: string;
} {
const [failedInstructionIndex, customError] = errorResponse;
const failedInstruction = transaction.instructions[failedInstructionIndex];
Expand All @@ -105,11 +108,13 @@ export function parseInstructionErrorResponse(transaction: Transaction, errorRes
const program = KNOWN_PROGRAMS[failedInstruction.programId.toString()];
parsedError = `${program} error ${customError['Custom']}`;
} else {
parsedError = `Unknown program ${failedInstruction.programId.toString()} custom error: ${customError['Custom']}`
parsedError = `Unknown program ${failedInstruction.programId.toString()} custom error: ${
customError['Custom']
}`;
}
return {
failedInstructionIndex,
error: parsedError,
failedProgram: failedInstruction.programId.toString()
failedProgram: failedInstruction.programId.toString(),
};
}
12 changes: 9 additions & 3 deletions packages/serum/src/instructions.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ INSTRUCTION_LAYOUT.inner.addVariant(
);
INSTRUCTION_LAYOUT.inner.addVariant(14, struct([]), 'closeOpenOrders');
INSTRUCTION_LAYOUT.inner.addVariant(15, struct([]), 'initOpenOrders');
INSTRUCTION_LAYOUT.inner.addVariant(16, struct([]), 'prune');
INSTRUCTION_LAYOUT.inner.addVariant(16, struct([u16('limit')]), 'prune');

export function encodeInstruction(instruction) {
const b = Buffer.alloc(100);
Expand Down Expand Up @@ -142,7 +142,12 @@ export class DexInstructions {
{ pubkey: quoteVault, isSigner: false, isWritable: true },
{ pubkey: baseMint, isSigner: false, isWritable: false },
{ pubkey: quoteMint, isSigner: false, isWritable: false },
{ pubkey: quoteMint, isSigner: false, isWritable: false }, // Dummy.
// Use a dummy address if using the new dex upgrade to save tx space.
{
pubkey: authority ? quoteMint : SYSVAR_RENT_PUBKEY,
isSigner: false,
isWritable: false,
},
]
.concat(
authority
Expand Down Expand Up @@ -514,6 +519,7 @@ export class DexInstructions {
openOrders,
openOrdersOwner,
programId,
limit,
}) {
const keys = [
{ pubkey: market, isSigner: false, isWritable: true },
Expand All @@ -529,7 +535,7 @@ export class DexInstructions {
keys,
programId,
data: encodeInstruction({
prune: {},
prune: { limit },
}),
});
}
Expand Down
5 changes: 5 additions & 0 deletions packages/serum/src/market-proxy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,11 @@ export class MarketProxyInstruction {
public prune(
openOrders: PublicKey,
openOrdersOwner: PublicKey,
limit?: number,
): TransactionInstruction {
if (!limit) {
limit = 65535;
}
const ix = DexInstructions.prune({
market: this._market.address,
bids: this._market.decoded.bids,
Expand All @@ -195,6 +199,7 @@ export class MarketProxyInstruction {
openOrders,
openOrdersOwner,
programId: this._proxyProgramId,
limit,
});
this._middlewares.forEach((mw) => mw.prune(ix));
return this.proxy(ix);
Expand Down
2 changes: 1 addition & 1 deletion packages/serum/src/market-proxy/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export class Logger implements Middleware {
prune(ix: TransactionInstruction) {
console.log('Proxying prune', this.ixToDisplay(ix));
}
ixToDisplay(ix: TransactionInstruction): Object {
ixToDisplay(ix: TransactionInstruction): any {
const keys = ix.keys.map((i) => {
return { ...i, pubkey: i.pubkey.toString() };
});
Expand Down
2 changes: 2 additions & 0 deletions packages/serum/src/market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ export const MARKET_STATE_LAYOUT_V3 = struct([
publicKeyLayout('authority'),
publicKeyLayout('pruneAuthority'),

blob(1024),

blob(7),
]);

Expand Down
6 changes: 3 additions & 3 deletions packages/serum/src/tokens_and_markets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import TokenMints from './token-mints.json';

export const PROGRAM_LAYOUT_VERSIONS = {
'4ckmDgGdxQoPDLUkDT3vHgSAkzA3QRdNq5ywwY4sUSJn': 1,
'BJ3jrUzddfuSrZHXSCxMUUQsjKEyLmuuyZebkcaFp2fg': 1,
'EUqojwWA2rd19FZrzeBncJsm38Jm1hEhE3zsmX3bRc2o': 2,
BJ3jrUzddfuSrZHXSCxMUUQsjKEyLmuuyZebkcaFp2fg: 1,
EUqojwWA2rd19FZrzeBncJsm38Jm1hEhE3zsmX3bRc2o: 2,
'9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin': 3,
}
};

export function getLayoutVersion(programId: PublicKey) {
return PROGRAM_LAYOUT_VERSIONS[programId.toString()] || 3;
Expand Down

0 comments on commit 5f7ceaf

Please sign in to comment.