UNPKG

@d8x/perpetuals-sdk

Version:

Node TypeScript SDK for D8X Perpetual Futures

158 lines (157 loc) 7.9 kB
import { BigNumberish, ContractTransactionResponse, Overrides, Signer, TransactionResponse } from "ethers"; import { PayableOverrides } from "./contracts/common"; import type { NodeSDKConfig, PriceFeedSubmission } from "./nodeSDKTypes"; import WriteAccessHandler from "./writeAccessHandler"; /** * Functions to liquidate traders. This class requires a private key * and executes smart-contract interactions that require gas-payments. * @extends WriteAccessHandler */ export default class LiquidatorTool extends WriteAccessHandler { /** * Constructs a LiquidatorTool instance for a given configuration and private key. * @param {NodeSDKConfig} config Configuration object, see PerpetualDataHandler. * readSDKConfig. * @example * import { LiquidatorTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk'; * async function main() { * console.log(LiquidatorTool); * // load configuration for Polygon zkEVM (tesnet) * const config = PerpetualDataHandler.readSDKConfig("cardona"); * // LiquidatorTool (authentication required, PK is an environment variable with a private key) * const pk: string = <string>process.env.PK; * let lqudtrTool = new LiquidatorTool(config, pk); * // Create a proxy instance to access the blockchain * await lqudtrTool.createProxyInstance(); * } * main(); * * @param {string | Signer} signer Private key or ethers Signer of the account */ constructor(config: NodeSDKConfig, signer: string | Signer); updateOracles(symbol: string, priceUpdates?: PriceFeedSubmission, overrides?: PayableOverrides & { rpcURL?: string; }): Promise<ContractTransactionResponse>; /** * Liquidate a trader. * @param {string} symbol Symbol of the form ETH-USD-MATIC. * @param {string} traderAddr Address of the trader to be liquidated. * @param {string=} liquidatorAddr Address to be credited if the liquidation succeeds. * @param {PriceFeedSubmission} priceFeedData optional. VAA and timestamps for oracle. If not provided will query from REST API. * Defaults to the wallet used to execute the liquidation. * @example * import { LiquidatorTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk'; * async function main() { * console.log(LiquidatorTool); * // Setup (authentication required, PK is an environment variable with a private key) * const config = PerpetualDataHandler.readSDKConfig("cardona"); * const pk: string = <string>process.env.PK; * let lqudtrTool = new LiquidatorTool(config, pk); * await lqudtrTool.createProxyInstance(); * // liquidate trader * let liqAmount = await lqudtrTool.liquidateTrader("ETH-USD-MATIC", * "0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B"); * console.log(liqAmount); * } * main(); * * @returns Transaction object. */ liquidateTrader(symbol: string, traderAddr: string, liquidatorAddr?: string, submission?: PriceFeedSubmission, overrides?: PayableOverrides & { rpcURL?: string; splitTx?: boolean; maxGasLimit?: BigNumberish; }): Promise<TransactionResponse>; /** * Check if the collateral of a trader is above the maintenance margin ("maintenance margin safe"). * If not, the position can be liquidated. * @param {string} symbol Symbol of the form ETH-USD-MATIC. * @param {string} traderAddr Address of the trader whose position you want to assess. * @param {number[]} indexPrices optional, index price S2/S3 for which we test * @example * import { LiquidatorTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk'; * async function main() { * console.log(LiquidatorTool); * // Setup (authentication required, PK is an environment variable with a private key) * const config = PerpetualDataHandler.readSDKConfig("cardona"); * const pk: string = <string>process.env.PK; * let lqudtrTool = new LiquidatorTool(config, pk); * await lqudtrTool.createProxyInstance(); * // check if trader can be liquidated * let safe = await lqudtrTool.isMaintenanceMarginSafe("ETH-USD-MATIC", * "0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B"); * console.log(safe); * } * main(); * * @returns {boolean} True if the trader is maintenance margin safe in the perpetual. * False means that the trader's position can be liquidated. */ isMaintenanceMarginSafe(symbol: string, traderAddr: string, indexPrices?: [number, number], overrides?: Overrides): Promise<boolean>; static maintenanceMarginPredMkts(maintMgnRateBase: number, pos: number, s3: number, markPx: number): number; /** * Total number of active accounts for this symbol, i.e. accounts with positions that are currently open. * @param {string} symbol Symbol of the form ETH-USD-MATIC. * @example * import { LiquidatorTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk'; * async function main() { * console.log(LiquidatorTool); * // Setup (authentication required, PK is an environment variable with a private key) * const config = PerpetualDataHandler.readSDKConfig("cardona"); * const pk: string = <string>process.env.PK; * let lqudtrTool = new LiquidatorTool(config, pk); * await lqudtrTool.createProxyInstance(); * // get number of active accounts * let accounts = await lqudtrTool.countActivePerpAccounts("ETH-USD-MATIC"); * console.log(accounts); * } * main(); * * @returns {number} Number of active accounts. */ countActivePerpAccounts(symbol: string, overrides?: Overrides): Promise<number>; /** * Get addresses of active accounts by chunks. * @param {string} symbol Symbol of the form ETH-USD-MATIC. * @param {number} from From which account we start counting (0-indexed). * @param {number} to Until which account we count, non inclusive. * @example * import { LiquidatorTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk'; * async function main() { * console.log(LiquidatorTool); * // Setup (authentication required, PK is an environment variable with a private key) * const config = PerpetualDataHandler.readSDKConfig("cardona"); * const pk: string = <string>process.env.PK; * let lqudtrTool = new LiquidatorTool(config, pk); * await lqudtrTool.createProxyInstance(); * // get all active accounts in chunks * let accounts = await lqudtrTool.getActiveAccountsByChunks("ETH-USD-MATIC", 0, 4); * console.log(accounts); * } * main(); * * @returns {string[]} Array of addresses at locations 'from', 'from'+1 ,..., 'to'-1. */ getActiveAccountsByChunks(symbol: string, from: number, to: number, overrides?: Overrides): Promise<string[]>; /** * Addresses for all the active accounts in this perpetual symbol. * @param {string} symbol Symbol of the form ETH-USD-MATIC. * @example * import { LiquidatorTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk'; * async function main() { * console.log(LiquidatorTool); * // Setup (authentication required, PK is an environment variable with a private key) * const config = PerpetualDataHandler.readSDKConfig("cardona"); * const pk: string = <string>process.env.PK; * let lqudtrTool = new LiquidatorTool(config, pk); * await lqudtrTool.createProxyInstance(); * // get all active accounts * let accounts = await lqudtrTool.getAllActiveAccounts("ETH-USD-MATIC"); * console.log(accounts); * } * main(); * * @returns {string[]} Array of addresses. */ getAllActiveAccounts(symbol: string, overrides?: Overrides): Promise<string[]>; }