Options
All
  • Public
  • Public/Protected
  • All
Menu

The margin account is a PDA generated using

javascript seeds=[userWalletKey, stateKey, "marginv1"] .

Hierarchy

  • default
    • Margin

Index

Constructors

Properties

Accessors

Methods

Constructors

  • new Margin(program: Program<Zo>, pubkey: PublicKey, data: MarginClassSchema, control: Control, specialOrdersAccount: null | default, state: State, heimdallKey: PublicKey, owner?: PublicKey, commitment?: Commitment, simulate?: boolean): Margin
  • Parameters

    • program: Program<Zo>
    • pubkey: PublicKey
    • data: MarginClassSchema
    • control: Control
    • specialOrdersAccount: null | default
    • state: State
    • heimdallKey: PublicKey
    • Optional owner: PublicKey
    • Optional commitment: Commitment
    • simulate: boolean = false

    Returns Margin

Properties

_balances: {} = {}

Type declaration

  • [key: string]: Num
backupSubscriberChannel: undefined | number
commitment: Commitment = "processed"
control: Control
data: Readonly<MarginClassSchema>
eventEmitter: null | EventEmitter<UpdateEvents, ChangeEvent<any>> = null
lastAccountSnapshotTime: number = ...
mainSubscriberChannel: undefined | number
orders: OrderInfo[] = []
owner?: PublicKey
positions: PositionInfo[] = []
pubkey: PublicKey
simulate: boolean = false
slot: number = -1
specialOrders: SpecialOrderInfo[] = []
specialOrdersAccount: null | default
state: State
subLock: AsyncLock = ...
subscribeLastUpdate: number = ...
subscribeTimeLimit: number = 0

Accessors

  • get _ooInfos(): {}
  • get balances(): {}
  • get borrowLendingTiedCollateralValue(): Decimal
  • get borrowPositionNotionalValue(): Decimal
  • get collateralInitialMarginInfo(): [Decimal, Decimal]
  • get collateralMaintenanceMarginInfo(): [Decimal, Decimal]
  • get connection(): Connection
  • get cumulativeUnrealizedPnL(): Decimal
  • get freeCollateralValue(): Decimal
  • get funding(): Decimal
  • get isBankrupt(): boolean
  • get isLiquidatable(): boolean
  • get isPerpLiquidation(): boolean
  • get largestBalanceSymbol(): string
  • get largestWeightedBorrow(): { symbol: string; weightedBorrow: Decimal }
  • get largestWeightedPosition(): { symbol: string; weightedPosition: Decimal }
  • get maintenanceMarginFraction(): Decimal
  • get marginFraction(): Decimal
  • get openMarginFraction(): Decimal
  • get positionsTiedCollateral(): Decimal
  • get program(): Program<Zo>
  • get provider(): default
  • get publicKey(): PublicKey
  • get realizedPnL(): Decimal
  • get rpc(): any
  • get tiedCollateral(): Decimal
  • get totalOpenPositionNotional(): Decimal
  • get totalPositionNotional(): Decimal
  • get unweightedAccountValue(): Decimal
  • get unweightedCollateralValue(): Decimal
  • get weightedAccountValue(): Decimal
  • get weightedCollateralValue(): Decimal

Methods

  • _subscribe(accountName: string, withBackup?: boolean, programPassed?: Program<any>): Promise<EventEmitter<string | symbol, any>>
  • Parameters

    • accountName: string
    • withBackup: boolean = false
    • Optional programPassed: Program<any>

    Returns Promise<EventEmitter<string | symbol, any>>

  • _unsubscribe(): Promise<void>
  • cancelPerpOrder(__namedParameters: { clientId?: any; isLong?: boolean; orderId?: any; symbol: string }): Promise<any>
  • Cancels an order on the orderbook for a given market either by orderId or by clientId.

    Parameters

    • __namedParameters: { clientId?: any; isLong?: boolean; orderId?: any; symbol: string }
      • Optional clientId?: any
      • Optional isLong?: boolean
      • Optional orderId?: any
      • symbol: string

    Returns Promise<any>

  • cancelSpecialOrder(id: number): Promise<string>
  • closePosition(symbol: string): Promise<any>
  • collateralWithdrawable(assetKey: string): Decimal
  • collateralWithdrawableWithBorrow(assetKey: string): Decimal
  • createPerpOpenOrders(symbol: string): Promise<any>
  • User must create a perp OpenOrders account for every perpetual market(future and or options) they intend to trade on.

    Parameters

    • symbol: string

      The market symbol. Ex: ("BTC-PERP")

    Returns Promise<any>

  • deposit(mintOrSymbol: string | PublicKey, size: number, repayOnly: boolean, tokenAccountProvided?: PublicKey): Promise<any>
  • Deposits a given amount of collateral into the Margin account from the associated token account.

    Parameters

    • mintOrSymbol: string | PublicKey
    • size: number

      The amount of tokens to deposit, in big units. (ex: 1.5 SOL, or 0.5 BTC)

    • repayOnly: boolean

      If true, will only deposit up to the amount borrowed. If true, amount parameter can be set to an arbitrarily large number to ensure that any outstanding borrow is fully repaid.

    • Optional tokenAccountProvided: PublicKey

      optional param to provide the token account to use it for deposits

    Returns Promise<any>

  • depositRaw(tokenAccount: PublicKey, vault: PublicKey, amount: BN, repayOnly: boolean): Promise<any>
  • Deposits a given amount of collateral into the Margin account. Raw implementation of the instruction.

    Parameters

    • tokenAccount: PublicKey

      The user's token account where tokens will be subtracted from.

    • vault: PublicKey

      The state vault where tokens will be deposited into.

    • amount: BN

      The amount of tokens to deposit, in native quantity. (ex: lamports for SOL, satoshis for BTC)

    • repayOnly: boolean

      If true, will only deposit up to the amount borrowed. If true, amount parameter can be set to an arbitrarily large number to ensure that any outstanding borrow is fully repaid.

    Returns Promise<any>

  • depositSol(vault: PublicKey, amount: BN, repayOnly: boolean): Promise<any>
  • Deposits a given amount of SOL collateral into the Margin account. Raw implementation of the instruction.

    Parameters

    • vault: PublicKey

      The state vault where tokens will be deposited into.

    • amount: BN

      The amount of tokens to deposit, in native quantity. (ex: lamports for SOL, satoshis for BTC)

    • repayOnly: boolean

      If true, will only deposit up to the amount borrowed. If true, amount parameter can be set to an arbitrarily large number to ensure that any outstanding borrow is fully repaid.

    Returns Promise<any>

  • getDexProgram(): PublicKey
  • getMaxSpotReducibleAsset(symbolBorrowed: string, symbolSupplied: string): Decimal
  • getOpenOrdersInfoBySymbol(symbol: string, create?: boolean): Promise<null | TypeDef<never & never & never & never & never & never & never & ({ name: "OpenOrdersInfo"; type: { fields: [{ name: "key"; type: "publicKey" }, { name: "nativePcTotal"; type: "i64" }, { name: "posSize"; type: "i64" }, { name: "realizedPnl"; type: "i64" }, { name: "coinOnBids"; type: "u64" }, { name: "coinOnAsks"; type: "u64" }, { name: "orderCount"; type: "u8" }, { name: "fundingIndex"; type: "i128" }]; kind: "struct" } } & { name: "OpenOrdersInfo" }) & never & never & never & never & never & never & never & never & never & never & never & never & never, Record<string, never>>>
  • Parameters

    • symbol: string

      The market symbol. Ex: ("BTC-PERP")

    • create: boolean = false

      If true, creates the OpenOrders account if it doesn't exist.

    Returns Promise<null | TypeDef<never & never & never & never & never & never & never & ({ name: "OpenOrdersInfo"; type: { fields: [{ name: "key"; type: "publicKey" }, { name: "nativePcTotal"; type: "i64" }, { name: "posSize"; type: "i64" }, { name: "realizedPnl"; type: "i64" }, { name: "coinOnBids"; type: "u64" }, { name: "coinOnAsks"; type: "u64" }, { name: "orderCount"; type: "u8" }, { name: "fundingIndex"; type: "i128" }]; kind: "struct" } } & { name: "OpenOrdersInfo" }) & never & never & never & never & never & never & never & never & never & never & never & never & never, Record<string, never>>>

    The OpenOrdersInfo for the given market.

  • getOpenOrdersKeyBySymbol(symbol: string): Promise<[PublicKey, number]>
  • Parameters

    • symbol: string

      The market symbol. Ex: ("BTC-PERP")

    Returns Promise<[PublicKey, number]>

    The OpenOrders account key for the given market.

  • getOrderByOrderId(orderId: any): null | OrderInfo
  • getWeightedBorrow(marketKey: string): { weight: Decimal; weightedBorrow: Decimal }
  • Get the value of the weighted borrow

    Parameters

    • marketKey: string

    Returns { weight: Decimal; weightedBorrow: Decimal }

    • weight: Decimal
    • weightedBorrow: Decimal
  • initialMarginFraction(trade?: any): Decimal
  • initialMarginInfo(trade?: any): [Decimal, Decimal]
  • isLiquidatableWithTolerance(tolerance: number): boolean
  • longOrderSize(marketKey: string): Decimal
  • makeCancelPerpOrderIx(__namedParameters: { clientId?: any; isLong?: boolean; orderId?: any; symbol: string }): Promise<TransactionInstruction>
  • Creates the instruction for cancelling a perp order.

    Parameters

    • __namedParameters: { clientId?: any; isLong?: boolean; orderId?: any; symbol: string }
      • Optional clientId?: any
      • Optional isLong?: boolean
      • Optional orderId?: any
      • symbol: string

    Returns Promise<TransactionInstruction>

  • makePlacePerpOrderIx(__namedParameters: Readonly<{ clientId?: number; isLong: boolean; limit?: number; orderType: OrderType | OrderTypeName; price: number; size: number; symbol: string }>): Promise<TransactionInstruction>
  • Creates the instruction for placing a perp order on the orderbook. Creates an Open orders account if does not exist, in the same transaction.

    Parameters

    • __namedParameters: Readonly<{ clientId?: number; isLong: boolean; limit?: number; orderType: OrderType | OrderTypeName; price: number; size: number; symbol: string }>

    Returns Promise<TransactionInstruction>

  • maxCollateralSpendable(trade: TradeInfo, marketKey: string): number
  • maxContractsPurchaseable(trade: TradeInfo, marketKey: string): number
  • openSize(marketKey: string): Decimal
  • placePerpOrder(__namedParameters: Readonly<{ clientId?: number; isLong: boolean; limit?: number; maxTs?: number; orderType: OrderType | OrderTypeName; price: number; size: number; symbol: string }>): Promise<any>
  • Places a perp order on the orderbook. Creates an Open orders account if does not exist, in the same transaction.

    Parameters

    • __namedParameters: Readonly<{ clientId?: number; isLong: boolean; limit?: number; maxTs?: number; orderType: OrderType | OrderTypeName; price: number; size: number; symbol: string }>

    Returns Promise<any>

  • placePerpOrderRaw(__namedParameters: Readonly<{ clientId?: any; isLong: boolean; limit?: number; limitPrice: BN; maxBaseQty: BN; maxQuoteQty: BN; orderType: OrderType; symbol: string }>): Promise<any>
  • Raw implementation of the instruction rpc call. Places an order on the orderbook for a given market, using lot sizes for limit and base quantity, and native units for quote quantity. Assumes an open orders account has been created already.

    Parameters

    • __namedParameters: Readonly<{ clientId?: any; isLong: boolean; limit?: number; limitPrice: BN; maxBaseQty: BN; maxQuoteQty: BN; orderType: OrderType; symbol: string }>

    Returns Promise<any>

  • placeSpecialOrder(__namedParameters: Readonly<{ isLong: boolean; limitPrice?: number; size: number; specialOrderType: SpecialOrderType; symbol: string; triggerPrice: number }>): Promise<string>
  • Parameters

    • __namedParameters: Readonly<{ isLong: boolean; limitPrice?: number; size: number; specialOrderType: SpecialOrderType; symbol: string; triggerPrice: number }>

    Returns Promise<string>

  • positionPnLBasedOnMarkPrice(position: PositionInfo): Decimal
  • positionWeighted(marketKey: string): { posNotional: Decimal; positionWeighted: Decimal }
  • Position weighted

    Parameters

    • marketKey: string

    Returns { posNotional: Decimal; positionWeighted: Decimal }

    • posNotional: Decimal
    • positionWeighted: Decimal
  • refresh(refreshState?: boolean, refreshMarginData?: boolean): Promise<void>
  • Refreshes the data on the Margin, state, cache and control accounts.

    Parameters

    • refreshState: boolean = true
    • refreshMarginData: boolean = true

    Returns Promise<void>

  • settleFunds(symbol: string): Promise<any>
  • Settles unrealized funding and realized PnL into the margin account for a given market.

    Parameters

    • symbol: string

      Market symbol (ex: BTC-PERP).

    Returns Promise<any>

  • shortOrderSize(marketKey: string): Decimal
  • subscribe(withBackup?: boolean, stateLimit?: number, cacheLimit?: number): Promise<void>
  • Refreshes the data on the Margin, state, cache and control accounts.

    Parameters

    • withBackup: boolean = false

      use a backup confirmed listener

    • stateLimit: number = 1000

      minimum time between state updates

    • cacheLimit: number = 5000

      minimum time between cache updates

    Returns Promise<void>

  • subscribeToSpecialOrdersAccount(withBackup: boolean): Promise<void>
  • swap(__namedParameters: Readonly<{ allowBorrow: boolean; buy: boolean; fromSize: number; serumMarket: PublicKey; slippage: number; toSize: number; tokenMint: PublicKey }>): Promise<any>
  • Swaps between USDC and a given Token B (or vice versa) on the Serum Spot DEX. This is a direct IOC trade that instantly settles. Note that the token B needs to be swappable, as enabled by the 01 program.

    Parameters

    • __namedParameters: Readonly<{ allowBorrow: boolean; buy: boolean; fromSize: number; serumMarket: PublicKey; slippage: number; toSize: number; tokenMint: PublicKey }>

    Returns Promise<any>

  • toString(): string
  • unsubscribe(): Promise<void>
  • updateAccountOnChange(processUpdate: any, that: default<T>): (account: any, snapshotTime?: any) => void
  • Parameters

    • processUpdate: any
    • that: default<T>

    Returns (account: any, snapshotTime?: any) => void

      • (account: any, snapshotTime?: any): void
      • Parameters

        • account: any
        • Optional snapshotTime: any

        Returns void

  • updateControlFromAccountInfo(accountInfo: AccountInfo<Buffer>, withOrders?: boolean): Promise<void>
  • Parameters

    • accountInfo: AccountInfo<Buffer>
    • withOrders: boolean = true

    Returns Promise<void>

  • updateState(state: State): void
  • updateWithAccountInfo(accountInfo: AccountInfo<Buffer>, withOrders?: boolean): Promise<void>
  • withdraw(mintOrSymbol: string | PublicKey, size: number, allowBorrow: boolean): Promise<any>
  • Withdraws a given amount of collateral from the Margin account to a specified token account. If withdrawing more than the amount deposited, then account will be borrowing.

    Parameters

    • mintOrSymbol: string | PublicKey
    • size: number

      The amount of tokens to withdraw, in big units. (ex: 1.5 SOL, or 0.5 BTC)

    • allowBorrow: boolean

      If false, will only be able to withdraw up to the amount deposited. If false, amount parameter can be set to an arbitrarily large number to ensure that all deposits are fully withdrawn.

    Returns Promise<any>

  • withdrawRaw(tokenAccount: PublicKey, vault: PublicKey, amount: BN, allowBorrow: boolean, preInstructions: undefined | TransactionInstruction[]): Promise<any>
  • Withdraws a given amount of collateral from the Margin account to a specified token account. If withdrawing more than the amount deposited, then account will be borrowing. Raw implementation of the instruction.

    Parameters

    • tokenAccount: PublicKey

      The user's token account where tokens will be withdrawn to.

    • vault: PublicKey

      The state vault where tokens will be withdrawn from.

    • amount: BN

      The amount of tokens to withdraw, in native quantity. (ex: lamports for SOL, satoshis for BTC)

    • allowBorrow: boolean

      If false, will only be able to withdraw up to the amount deposited. If false, amount parameter can be set to an arbitrarily large number to ensure that all deposits are fully withdrawn.

    • preInstructions: undefined | TransactionInstruction[]

      instructions executed before withdrawal

    Returns Promise<any>

  • withdrawSol(vault: PublicKey, amount: BN, allowBorrow: boolean): Promise<any>
  • Withdraws a given amount of collateral from the Margin account to a specified token account. If withdrawing more than the amount deposited, then account will be borrowing. Raw implementation of the instruction.

    Parameters

    • vault: PublicKey

      The state vault where tokens will be withdrawn from.

    • amount: BN

      The amount of tokens to withdraw, in native quantity. (ex: lamports for SOL, satoshis for BTC)

    • allowBorrow: boolean

      If false, will only be able to withdraw up to the amount deposited. If false, amount parameter can be set to an arbitrarily large number to ensure that all deposits are fully withdrawn.

    Returns Promise<any>

  • create(program: Program<Zo>, st: State, commitment?: Commitment): Promise<Margin>
  • Creates a margin account.

    Parameters

    • program: Program<Zo>

      The Zo Program

    • st: State

      The Zo State object, overrides the default config.

    • commitment: Commitment = "finalized"

      commitment of the transaction, finalized is used as default

    Returns Promise<Margin>

  • exists(program: Program<Zo>, st: State, owner?: PublicKey): Promise<boolean>
  • getHeimdallPda(programId: PublicKey): Promise<PublicKey>
  • getMarginKey(st: State, marginOwner: PublicKey, program: Program<Zo>): Promise<[PublicKey, number]>
  • getPda(st: State, traderKey: PublicKey, programId: PublicKey): Promise<[PublicKey, number]>
  • Gets the Margin account's PDA and bump.

    Parameters

    • st: State
    • traderKey: PublicKey
    • programId: PublicKey

    Returns Promise<[PublicKey, number]>

    An array consisting of [PDA, bump]

  • load(program: Program<Zo>, st: State, ch?: null | Cache, owner?: PublicKey, commitment?: Commitment, simulate?: boolean): Promise<Margin>
  • loadAllMarginAndControlSchemas(program: Program<Zo>): Promise<{ controlSchema: ProgramAccount<ControlSchema>; marginSchema: ProgramAccount<MarginSchema> }[]>
  • loadAllMargins(program: Program<Zo>, st: State, ch?: null | Cache, verbose?: boolean, withOrders?: boolean): Promise<Margin[]>
  • loads all margin accounts

    Parameters

    • program: Program<Zo>
    • st: State

      state

    • ch: null | Cache = null

      cache

    • verbose: boolean = false
    • withOrders: boolean = true

      load orders for each margin account

    Returns Promise<Margin[]>

  • loadFromAccountInfo(program: Program<Zo>, st: State, accountInfo: AccountInfo<Buffer>, withOrders: boolean, commitment?: Commitment): Promise<Margin>
  • loadPrefetchedWeb3(program: Program<Zo>, st: State, prefetchedMarginData: ProgramAccount<MarginSchema>, prefetchedControlData: ProgramAccount<ControlSchema>, withOrders: boolean, commitment?: Commitment, simulate?: boolean, prefetchedHeimdallKey?: PublicKey, prefetchedSpecialOrdersData?: ProgramAccount<SpecialOrdersSchema>): Promise<default>
  • Loads a new Margin object from prefetched schema;

    Parameters

    • program: Program<Zo>
    • st: State
    • prefetchedMarginData: ProgramAccount<MarginSchema>
    • prefetchedControlData: ProgramAccount<ControlSchema>
    • withOrders: boolean
    • Optional commitment: Commitment
    • simulate: boolean = false
    • Optional prefetchedHeimdallKey: PublicKey
    • Optional prefetchedSpecialOrdersData: ProgramAccount<SpecialOrdersSchema>

    Returns Promise<default>

  • loadWeb3(program: Program<Zo>, st: State, owner?: PublicKey, commitment?: Commitment, simulate?: boolean): Promise<default>
  • Loads a new Margin object.

    Parameters

    • program: Program<Zo>
    • st: State
    • Optional owner: PublicKey
    • commitment: Commitment = ...
    • simulate: boolean = false

    Returns Promise<default>

  • printMarginsLoadProgress(index: number, len: number): void

Generated using TypeDoc