Class for interacting with Synthetix Perps V3 contracts Provides methods for creating and managing accounts, depositing and withdrawing collateral, committing and settling orders, and liquidating accounts.

Use get methods to fetch information about accounts, markets, and orders:: const markets = await sdk.perps.getMarkets() const openPositions = await sdk.perps.getOpenPositions() Other methods prepare transactions, and submit them to your RPC:: const createTxHash = await sdk.perps.createAccount(submit=True) const collateralTxHash = await sdk.perps.modifyCollateral(amount=1000, market_name='sUSD', submit=True) const orderTxHash = await sdk.perps.commitOrder(size=10, market_name='ETH', desired_fill_price=2000, submit=True) An instance of this module is available as sdk.perps. If you are using a network without perps deployed, the contracts will be unavailable and the methods will raise an error. The following contracts are required:

  • PerpsMarketProxy
  • PerpsAccountProxy
  • PythERC7412Wrapper

An instance of the Synthetix class

Constructors

Properties

accountIds: bigint[]
defaultAccountId?: bigint
isErc7412Enabled: boolean = true
isMulticollateralEnabled: boolean = false
marketMetadata: Map<number, MarketMetadata>
marketsById: Map<number, MarketData>
marketsByName: Map<string, MarketData>
marketsBySymbol: Map<string, MarketData>

Methods

  • Submit an order to the specified market. Keepers will attempt to fill the order according to the settlement strategy. If desired_fill_price is provided, the order will be filled at that price or better. If max_price_impact is provided, the desired_fill_price is calculated from the current market price and the price impact.

    Parameters

    • size: number

      The size of the order to submit

    • settlementStrategyId: number = 0

      The id of the settlement strategy to use

    • marketId: undefined | number

      The id of the market to submit the order to. If not provided, marketName must be provided

    • marketName: undefined | string

      The name of the market to submit the order to. If not provided, marketId must be provided.

    • accountId: undefined | bigint

      The id of the account to submit the order for. Defaults to defaultAccountId.

    • desiredFillPrice: undefined | number

      The max price for longs and minimum price for shorts. If not provided, one will be calculated based on maxPriceImpact

    • maxPriceImpact: undefined | number

      The maximum price impact to allow when filling the order as a percentage (1.0 = 1%). If not provided, it will inherit the default value from snx.max_price_impact

    • submit: boolean = false

      If true, submit the transaction to the blockchain

    Returns Promise<string | CallParameters>

  • Create a perps account. An account NFT is minted to the sender, who owns the account.

    Parameters

    • accountId: undefined | bigint = undefined

      Id of the account. If not passed, default Perps account ID is used

    • submit: boolean = false

      Executes the transaction if true

    Returns Promise<string | CallParameters>

    Transaction hash or transaction data

  • Parameters

    • collateralAmount: number
    • collateralMarketId: number
    • size: number
    • OptionalmarketId: number
    • OptionalmarketName: string
    • settlementStrategyId: number = 0
    • OptionalaccountId: bigint
    • OptionaldesiredFillPrice: number
    • OptionalmaxPriceImpact: number
    • submit: boolean = false

    Returns Promise<string | CallParameters>

  • Fetch a list of perps account_id owned by an address. Perps accounts are minted as an NFT to the owner's address. The account_id is the token id of the NFTs held by the address.

    Parameters

    • address: undefined | string = undefined

      The address to get accounts for. Uses connected address if not provided.

    • defaultAccountId: undefined | bigint = undefined

      The default account ID to set after fetching.

    Returns Promise<bigint[]>

    A list of account IDs owned by the address

  • Check if an accountId is eligible for liquidation.

    Parameters

    • accountId: undefined | bigint = undefined

      The id of the account to check. If not provided, the default account is used.

    Returns Promise<boolean>

  • Check if a batch of accountId's are eligible for liquidation.

    Parameters

    • accountIds: undefined | bigint[] = undefined

      An array of account ids

    Returns Promise<{
        accountId: bigint;
        canLiquidate: boolean;
    }[]>

  • Fetch the balance of each collateral type for an account.

    Parameters

    • accountId: undefined | bigint = undefined

      The id of the account to fetch the collateral balances for. If not provided, the default account is used.

    Returns Promise<void>

  • Returns the debt of the account id

    Parameters

    • accountId: undefined | bigint = undefined

      The id of the account to get the debt for. If not provided, the default account is used.

    Returns Promise<number>

    debt Account debt in ether

  • Fetch funding parameters for an array of market ids.

    Parameters

    • marketIds: number[]

      Array of marketIds to fetch settlement strategy

    Returns Promise<FundingParameters[]>

    Funding Parameters array for markets

  • Fetch information about an account's margin requirements and balances. Accounts must maintain an available_margin above the maintenance_margin_requirement to avoid liquidation. Accounts with available_margin below the initial_margin_requirement can not interact with their position unless they deposit more collateral.

    Parameters

    • accountId: undefined | bigint = undefined

      The id of the account to fetch the margin info for. If not provided, the default account is used

    Returns Promise<CollateralData>

  • Fetch the ids and summaries for all perps markets. Market summaries include information about the market's price, open interest, funding rate, and skew

    Returns Promise<{
        marketsById: Map<number, MarketData>;
        marketsByName: Map<string, MarketData>;
    }>

  • Fetch the market summaries for an array of marketIds

    Parameters

    • marketIds: number[]

      Array of market ids to fetch

    Returns Promise<MarketSummary[]>

    Summary of market ids data fetched from the contract

  • Fetch the market summary for a single market, including information about the market's price, open interest, funding rate, and skew. Provide either the marketId or marketName.

    Parameters

    • marketId: undefined | number = undefined

      Market id to fetch the summary

    • marketName: undefined | string = undefined

      Name of the market to fetch summary

    Returns Promise<MarketSummary>

    Summary of market data fetched from the contract

  • Gets the max size (in value) of an array of marketIds.

    Parameters

    • marketIds: number[]

      Array of market ids.

    Returns Promise<MaxMarketValue[]>

    Max market size in market USD value for each market

  • Fetch the position for a specified account and market. The result includes the unrealized pnl since the last interaction with this position, any accrued funding, and the position size. Provide either a marketId or a marketName::

    Parameters

    • marketId: undefined | number = undefined

      The id of the market to fetch the position for.

    • marketName: undefined | string = undefined

      The name of the market to fetch the position for.

    • accountId: undefined | bigint = undefined

      The id of the account to fetch the position for. If not provided, the default account is used.

    Returns Promise<OpenPositionData>

  • Fetch positions for an array of specified markets. The result includes the unrealized pnl since the last interaction with this position, any accrued funding, and the position size. Provide either an array of marketIds or a marketNames::

    Parameters

    • marketIds: undefined | number[] = undefined

      Array of market ids to fetch the position for.

    • marketNames: undefined | string[] = undefined

      Array of market names to fetch the position for.

    • accountId: undefined | bigint = undefined

      The id of the account to fetch the position for. If not provided, the default account is used.

    Returns Promise<OpenPositionData[]>

  • Fetches the open order for an account. Optionally fetches the settlement strategy, which can be useful for order settlement and debugging.

    Parameters

    • accountId: undefined | bigint = undefined

      The id of the account. If not provided, the default account is used

    • fetchSettlementStrategy: boolean = true

      Flag to indicate whether to fetch the settlement strategy

    Returns Promise<OrderData>

  • Gets the order fees of a market.

    Parameters

    • marketIds: number[]

      Array of market ids.

    Returns Promise<OrderFees[]>

    Order fees array for markets

  • Get a quote for the size of an order in a specified market. The quote includes the provided price and the fill price of the order after price impact. If a price is not provided, a price will be fetched from Pyth. Provide either a marketId or marketName.

    Parameters

    • size: number

      The size of the order to quote.

    • Optionalprice: number

      The price to quote the order at. If not provided, the current market price is used

    • marketId: undefined | number = undefined

      The id of the market to quote the order for

    • marketName: undefined | string = undefined

      The name of the market to quote the order for

    • accountId: undefined | bigint = undefined

      The id of the account to quote the order for. If not provided, the default account is used

    • settlementStrategyId: number = 0

      The id of the settlement strategy to use for the settlement reward calculation

    • includeRequiredMargin: boolean = true

      If true, include the required margin for the account in the quote.

    Returns Promise<OrderQuote>

  • Fetch the settlement strategies for an array of market ids. Settlement strategies describe the conditions under which an order can be settled.

    Parameters

    • marketIds: number[]

      Array of marketIds to fetch settlement strategy

    Returns Promise<SettlementStrategy[]>

    Settlement strategy array for markets

  • Fetch the settlement strategy for a market. Settlement strategies describe the conditions under which an order can be settled. Provide either a marketId or marketName

    Parameters

    • settlementStrategyId: number
    • marketId: undefined | number = undefined

      Id of the market to get settlement strategy

    • marketName: undefined | string = undefined

      Name of the market to get settlement strategy

    Returns Promise<SettlementStrategy>

    Settlement strategy for market

  • Submit a liquidation for an account, or static call the liquidation function to fetch the liquidation reward. The static call is important for accounts which have been partially liquidated. Due to the throughput limit on liquidated value, the static call returning a nonzero value means more value can be liquidated (and rewards collected). This function can not be called if submit and staticCall are true.

    Parameters

    • accountId: undefined | bigint = undefined

      The id of the account to liquidate. If not provided, the default account is used.

    • submit: boolean = false

      If true, submit the transaction to the blockchain.

    • staticCall: boolean = false

      If true, static call the liquidation function to fetch the liquidation reward.

    Returns Promise<string | number | CallParameters>

  • Move collateral in or out of a specified perps account. The market_id or market_name must be provided to specify the collateral type. Provide either a market_id or a market_name. Note that the market_id here refers to the spot market id, not the perps market id. Make sure to approve the market proxy to transfer tokens of the collateral type before calling this function.

    Parameters

    • amount: number

      The amount of collateral to move. Positive values deposit collateral, negative values withdraw collateral

    • marketId: undefined | number = undefined

      The id of the market to move collateral for

    • marketName: undefined | string = undefined

      The name of the market to move collateral for.

    • accountId: undefined | bigint = undefined

      The id of the account to move collateral for. If not provided, the default account is used.

    • submit: boolean = false

      If True, submit the transaction to the blockchain.

    Returns Promise<string | CallParameters>

  • Pay the debt of a perps account. If no amount is provided, the full debt of the account is repaid. Make sure to approve the proxy to transfer sUSD before calling this function.

    Parameters

    • amount: undefined | number = undefined

      The amount of debt to repay. If not provided, the full debt is repaid.

    • accountId: undefined | bigint = undefined

      The id of the account to repay the debt for. If not provided, the default account is used.

    • submit: boolean = false

      If true, submit the transaction to the blockchain. If not provided, transaction object is returned

    Returns Promise<string | CallParameters>

  • Prepare a call to the external node with oracle updates for the specified market names. The result can be passed as the first argument to a multicall function to improve performance of ERC-7412 calls. If no market names are provided, all markets are fetched. This is useful for read functions since the user does not pay gas for those oracle calls, and reduces RPC calls and runtime.

    Parameters

    • marketIds: number[] = []

      An array of market ids to fetch prices for. If not provided, all markets are fetched

    Returns Promise<Call3Value[]>

  • Look up the market_id and market_name for a market. If only one is provided, the other is resolved. If both are provided, they are checked for consistency.

    Parameters

    • marketId: undefined | number = undefined

      Id of the market to resolve

    • marketName: undefined | string = undefined

      Name of the market to resolve

    Returns {
        resolvedMarketId: number;
        resolvedMarketName: string;
    }

    • resolvedMarketId: number
    • resolvedMarketName: string
  • Settles an order using ERC7412 by handling OracleDataRequired errors and forming a multicall. If the order is not yet ready to be settled, this function will wait until the settlement time. If the transaction fails, this function will retry until the max number of tries is reached with a configurable delay.

    Parameters

    • accountId: undefined | bigint = undefined

      The id of the account to settle. If not provided, the default account is used.

    • submit: boolean = false

      If true, submit the transaction to the blockchain.

    • maxTxTries: number = 3

      The max number of tries to submit the transaction

    • txDelay: number = 2

      The delay in seconds between transaction submissions.

    Returns Promise<undefined | string | CallParameters>