UNPKG

@cks-systems/manifest-sdk

Version:
323 lines (322 loc) 13.5 kB
import { bignum } from '@metaplex-foundation/beet'; import { PublicKey, Connection, Keypair, TransactionInstruction, TransactionSignature, GetProgramAccountsResponse } from '@solana/web3.js'; import { OrderType, SwapParams } from './manifest/types'; import { Market } from './market'; import { Wrapper } from './wrapperObj'; import { WrapperCancelOrderParams } from './wrapper'; import { Global } from './global'; export interface SetupData { setupNeeded: boolean; instructions: TransactionInstruction[]; wrapperKeypair: Keypair | null; } export declare class ManifestClient { connection: Connection; wrapper: Wrapper | null; market: Market; private payer; private baseMint; private quoteMint; baseGlobal: Global | null; quoteGlobal: Global | null; isBase22: boolean; isQuote22: boolean; private constructor(); /** * fetches all user wrapper accounts and returns the first or null if none are found * * @param connection Connection * @param payerPub PublicKey of the trader * * @returns Promise<GetProgramAccountsResponse> */ private static fetchFirstUserWrapper; /** * list all Manifest markets using getProgramAccounts. caution: this is a heavy call. * * @param connection Connection * @returns PublicKey[] */ static listMarketPublicKeys(connection: Connection): Promise<PublicKey[]>; /** * List all Manifest markets that match base and quote mint. If useApi, then * this call uses the manifest stats server instead of the heavy * getProgramAccounts RPC call. * * @param connection Connection * @param baseMint PublicKey * @param quoteMint PublicKey * @param useApi boolean * @returns PublicKey[] */ static listMarketsForMints(connection: Connection, baseMint: PublicKey, quoteMint: PublicKey, useApi?: boolean): Promise<PublicKey[]>; /** * Get all market program accounts. This is expensive RPC load.. * * @param connection Connection * @returns GetProgramAccountsResponse */ static getMarketProgramAccounts(connection: Connection): Promise<GetProgramAccountsResponse>; /** * Create a new client which creates a wrapper and claims seat if needed. * * @param connection Connection * @param marketPk PublicKey of the market * @param payerKeypair Keypair of the trader * * @returns ManifestClient */ static getClientForMarket(connection: Connection, marketPk: PublicKey, payerKeypair: Keypair): Promise<ManifestClient>; /** * generate ixs which need to be executed in order to run a manifest client for a given market. `{ setupNeeded: false }` means all good. * this function should be used before getClientForMarketNoPrivateKey for UI cases where `Keypair`s cannot be directly passed in. * * @param connection Connection * @param marketPk PublicKey of the market * @param trader PublicKey of the trader * * @returns Promise<SetupData> */ static getSetupIxs(connection: Connection, marketPk: PublicKey, trader: PublicKey): Promise<SetupData>; /** * Create a new client. throws if setup ixs are needed. Call ManifestClient.getSetupIxs to check if ixs are needed. * This is the way to create a client without directly passing in `Keypair` types (for example when building a UI). * * @param connection Connection * @param marketPk PublicKey of the market * @param trader PublicKey of the trader * * @returns ManifestClient */ static getClientForMarketNoPrivateKey(connection: Connection, marketPk: PublicKey, trader: PublicKey): Promise<ManifestClient>; /** * Create a new client that is read only. Cannot send transactions or generate instructions. * * @param connection Connection * @param marketPk PublicKey of the market * @param trader PublicKey for trader whose wrapper to fetch * * @returns ManifestClient */ static getClientReadOnly(connection: Connection, marketPk: PublicKey, trader?: PublicKey): Promise<ManifestClient>; /** * Initializes a ReadOnlyClient for each Market the trader has a seat on. * This has been optimized to be as light on the RPC as possible but it is * still using getProgramAccounts. caution: this is a heavy call. * * @param connection Connection * @param trader PublicKey * @returns ManifestClient[] */ static getClientsReadOnlyForAllTraderSeats(connection: Connection, trader: PublicKey): Promise<ManifestClient[]>; /** * Reload the market and wrapper and global objects. */ reload(): Promise<void>; /** * CreateMarket instruction. Assumes the account is already funded onchain. * * @param payer PublicKey of the trader * @param baseMint PublicKey of the baseMint * @param quoteMint PublicKey of the quoteMint * @param market PublicKey of the market that will be created. Private key * will need to be a signer. * * @returns TransactionInstruction */ private static createMarketIx; /** * Deposit instruction * * @param payer PublicKey of the trader * @param mint PublicKey for deposit mint. Must be either the base or quote * @param amountTokens Number of tokens to deposit. * * @returns TransactionInstruction */ depositIx(payer: PublicKey, mint: PublicKey, amountTokens: number): TransactionInstruction; /** * Withdraw instruction * * @param payer PublicKey of the trader * @param mint PublicKey for withdraw mint. Must be either the base or quote * @param amountTokens Number of tokens to withdraw. * * @returns TransactionInstruction */ withdrawIx(payer: PublicKey, mint: PublicKey, amountTokens: number): TransactionInstruction; /** * Withdraw All instruction. Withdraws all available base and quote tokens * * @returns TransactionInstruction[] */ withdrawAllIx(): TransactionInstruction[]; /** * PlaceOrder instruction * * @param params WrapperPlaceOrderParamsExternal | WrapperPlaceOrderReverseParamsExternal * including all the information for placing an order like amount, price, * ordertype, ... This is called external because to avoid conflicts with the * autogenerated version which has problems with expressing some of the * parameters. The reverse type has a spreadBps field instead of lastValidSlot. * * @returns TransactionInstruction */ placeOrderIx(params: WrapperPlaceOrderParamsExternal | WrapperPlaceOrderReverseParamsExternal): TransactionInstruction; /** * PlaceOrderWithRequiredDeposit instruction. Only deposits the appropriate base * or quote tokens if not in the withdrawable balances. * * @param payer PublicKey of the trader * @param params WrapperPlaceOrderParamsExternal | WrapperPlaceOrderReverseParamsExternal * including all the information for placing an order like amount, price, * ordertype, ... This is called external because to avoid conflicts with the * autogenerated version which has problems with expressing some of the * parameters. The reverse type has a spreadBps field instead of lastValidSlot. * * @returns TransactionInstruction[] */ placeOrderWithRequiredDepositIxs(payer: PublicKey, params: WrapperPlaceOrderParamsExternal | WrapperPlaceOrderReverseParamsExternal): Promise<TransactionInstruction[]>; /** * Swap instruction * * Optimized swap for routers and arb bots. Normal traders should compose * depost/withdraw/placeOrder to get limit orders. Does not go through the * wrapper. * * @param payer PublicKey of the trader * @param params SwapParams * * @returns TransactionInstruction */ swapIx(payer: PublicKey, params: SwapParams): TransactionInstruction; /** * CancelOrder instruction * * @param params WrapperCancelOrderParams includes the clientOrderId of the * order to cancel. * * @returns TransactionInstruction */ cancelOrderIx(params: WrapperCancelOrderParams): TransactionInstruction; /** * BatchUpdate instruction * * @param placeParams (WrapperPlaceOrderParamsExternal | WrapperPlaceOrderReverseParamsExternal)[] * including all the information for placing an order like amount, price, * ordertype, ... This is called external because to avoid conflicts with the * autogenerated version which has problems with expressing some of the * parameters. The reverse type has a spreadBps field instead of lastValidSlot. * @param params WrapperCancelOrderParams[] includes the clientOrderId of the * order to cancel. * * @returns TransactionInstruction */ batchUpdateIx(placeParams: (WrapperPlaceOrderParamsExternal | WrapperPlaceOrderReverseParamsExternal)[], cancelParams: WrapperCancelOrderParams[], cancelAll: boolean): TransactionInstruction; /** * CancelAll instruction. Cancels all orders on a market. This is discouraged * outside of circuit breaker usage because it is less efficient and does not * cancel global cleanly. Use batchUpdate instead. This also does not cancel * any orders not placed through the wrapper, which includes reverse orders * that were reversed. * * @returns TransactionInstruction */ cancelAllIx(): TransactionInstruction; /** * CancelAllOnCore instruction. Cancels all orders on a market directly on the core program, * including reverse orders and global orders with rent prepayment. * * @returns TransactionInstruction[] */ cancelAllOnCoreIx(): Promise<TransactionInstruction[]>; /** * killSwitchMarket transactions. Pulls all orders * and withdraws all balances from the market in two transactions * * @param payer PublicKey of the trader * * @returns TransactionSignatures[] */ killSwitchMarket(payerKeypair: Keypair): Promise<TransactionSignature[]>; /** * CreateGlobalCreate instruction. Creates the global account. Should be used only once per mint. * * @param connection Connection to pull mint info * @param payer PublicKey of the trader * @param globalMint PublicKey of the globalMint * * @returns Promise<TransactionInstruction> */ private static createGlobalCreateIx; /** * CreateGlobalAddTrader instruction. Adds a new trader to the global account. * Static because it does not require a wrapper. * * @param payer PublicKey of the trader * @param globalMint PublicKey of the globalMint * * @returns TransactionInstruction */ static createGlobalAddTraderIx(payer: PublicKey, globalMint: PublicKey): TransactionInstruction; /** * Global deposit instruction. Static because it does not require a wrapper. * * @param connection Connection to pull mint info * @param payer PublicKey of the trader * @param globalMint PublicKey for global mint deposit. * @param amountTokens Number of tokens to deposit. * * @returns Promise<TransactionInstruction> */ static globalDepositIx(connection: Connection, payer: PublicKey, globalMint: PublicKey, amountTokens: number): Promise<TransactionInstruction>; /** * Global withdraw instruction. Static because it does not require a wrapper. * * @param connection Connection to pull mint info * @param payer PublicKey of the trader * @param globalMint PublicKey for global mint withdraw. * @param amountTokens Number of tokens to withdraw. * * @returns Promise<TransactionInstruction> */ static globalWithdrawIx(connection: Connection, payer: PublicKey, globalMint: PublicKey, amountTokens: number): Promise<TransactionInstruction>; } /** * Same as the autogenerated WrapperPlaceOrderParams except price here is a number. */ export type WrapperPlaceOrderParamsExternal = { /** Number of base tokens in the order. */ numBaseTokens: number; /** Price as float in quote tokens per base tokens. */ tokenPrice: number; /** Boolean for whether this order is on the bid side. */ isBid: boolean; /** Last slot before this order is invalid and will be removed. */ lastValidSlot: number; /** Type of order (Limit, PostOnly, ...). */ orderType: OrderType; /** Client order id used for cancelling orders. Does not need to be unique. */ clientOrderId: bignum; }; /** * Same as the autogenerated WrapperPlaceOrderParamsExternal except lastValidSlot is spread. */ export type WrapperPlaceOrderReverseParamsExternal = { /** Number of base tokens in the order. */ numBaseTokens: number; /** Price as float in quote tokens per base tokens. */ tokenPrice: number; /** Boolean for whether this order is on the bid side. */ isBid: boolean; /** Spread in bps. Can be between 0 and 6553 in increments of .1 */ spreadBps: number; /** Type of order (Limit, PostOnly, ...). */ orderType: OrderType; /** Client order id used for cancelling orders. Does not need to be unique. */ clientOrderId: bignum; }; export declare function toMantissaAndExponent(input: number): { priceMantissa: number; priceExponent: number; };