UNPKG

@d8x/perpetuals-sdk

Version:

Node TypeScript SDK for D8X Perpetual Futures

212 lines (211 loc) 9.36 kB
import { ContractTransactionResponse, Overrides, Signer } from "ethers"; import MarketData from "./marketData"; import type { ClientOrder, NodeSDKConfig, Order, SmartContractOrder } from "./nodeSDKTypes"; import TraderDigests from "./traderDigests"; /** * Interface that can be used by front-end that wraps all private functions * so that signatures can be handled in frontend via wallet * @extends MarketData */ export default class TraderInterface extends MarketData { digestTool: TraderDigests; protected gasLimit: number; /** * Constructor * @param {NodeSDKConfig} config Configuration object, see * PerpetualDataHandler.readSDKConfig. */ constructor(config: NodeSDKConfig); /** * Get the fee that is charged to the trader for a given broker (can be ZERO-address), * without broker fee * @param poolSymbolName pool currency (e.g. MATIC) * @param traderAddr address of trader * @param brokerAddr address of broker * @returns fee (in decimals) that is charged by exchange (without broker) * @example * import { TraderInterface, PerpetualDataHandler } from '@d8x/perpetuals-sdk'; * async function main() { * console.log(TraderInterface); * const config = PerpetualDataHandler.readSDKConfig("cardona"); * let traderAPI = new TraderInterface(config); * await traderAPI.createProxyInstance(); * // query exchange fee * let fees = await traderAPI.queryExchangeFee("MATIC"); * console.log(fees); * } * main(); * */ queryExchangeFee(poolSymbolName: string, traderAddr: string, brokerAddr: string, overrides?: Overrides): Promise<number>; /** * * @param poolSymbolName pool symbol, e.g. MATIC * @param traderAddr address of the trader * @returns volume in USD * @example * import { TraderInterface, PerpetualDataHandler } from '@d8x/perpetuals-sdk'; * async function main() { * console.log(TraderInterface); * const config = PerpetualDataHandler.readSDKConfig("cardona"); * let traderAPI = new TraderInterface(config); * await traderAPI.createProxyInstance(); * // query volume * let vol = await traderAPI.getCurrentTraderVolume("MATIC", "0xmyAddress"); * console.log(vol); * } * main(); * */ getCurrentTraderVolume(poolSymbolName: string, traderAddr: string, overrides?: Overrides): Promise<number>; /** * Get digest to cancel an order. Digest needs to be signed and submitted via * orderBookContract.cancelOrder(orderId, signature); * @param symbol * @param orderId * @returns tuple of digest which the trader needs to sign and address of order book contract * @example * import { TraderInterface, PerpetualDataHandler } from '@d8x/perpetuals-sdk'; * async function main() { * console.log(TraderInterface); * const config = PerpetualDataHandler.readSDKConfig("x1"); * let traderAPI = new TraderInterface(config); * await traderAPI.createProxyInstance(); * // submit order * let resp = await accTrade.order(order, undefined, { gasLimit: 800_000 }); * await resp.tx.wait(); * // cancel what we just submitted * let d = await traderAPI.cancelOrderDigest("ETH-USDC-USDC", resp.orderId); * console.log(d); * } * main(); */ cancelOrderDigest(symbol: string, orderId: string, overrides?: Overrides): Promise<{ digest: string; OBContractAddr: string; }>; /** * Get the order book address for a perpetual * @param symbol symbol (e.g. MATIC-USD-MATIC) * @returns order book address for the perpetual * @example * import { TraderInterface, PerpetualDataHandler } from '@d8x/perpetuals-sdk'; * async function main() { * console.log(TraderInterface); * const config = PerpetualDataHandler.readSDKConfig("cardona"); * let traderAPI = new TraderInterface(config); * await traderAPI.createProxyInstance(); * // get order book address * let ob = traderAPI.getOrderBookAddress("BTC-USD-MATIC"); * console.log(ob); * } * main(); */ getOrderBookAddress(symbol: string): string; /** * createSmartContractOrder from user-friendly order * @param order order struct * @param traderAddr address of trader * @returns Smart contract type order struct */ createSmartContractOrder(order: Order, traderAddr: string): SmartContractOrder; /** * Create smart contract order and digest that the trader signs. * await orderBookContract.postOrder(scOrder, signature, { gasLimit: gasLimit }); * Order must contain broker fee and broker address if there is supposed to be a broker. * @param scOrder smart contract order struct (get from order via createSCOrder) * @returns digest that the trader has to sign */ orderDigest(scOrder: SmartContractOrder): string; /** * Get the ABI of a method in the proxy contract. Throws if non-existent * @param method Name of the method * @returns ABI as a single string */ getProxyABI(method: string): string; /** * Get the ABI of a method in the Limit Order Book contract corresponding to a given symbol. * @param symbol Symbol of the form MATIC-USD-MATIC * @param method Name of the method * @returns ABI as a single string */ getOrderBookABI(symbol: string, method: string): string; /** * Takes up to three orders and designates the first one as "parent" of the others. * E.g. the first order opens a position, and the other two are take-profit and/or stop-loss orders. * @param orders 1, 2 or 3 smart contract orders * @param ids order ids * @returns client orders with dependency info filled in */ static chainOrders(orders: SmartContractOrder[], ids: string[]): ClientOrder[]; /** * Add liquidity to the PnL participant fund via signer. The address gets pool shares in return. * @param {Signer} signer Signer that will deposit liquidity * @param {string} poolSymbolName Name of pool symbol (e.g. MATIC) * @param {number} amountCC Amount in pool-collateral currency * @return Transaction object * @example * import { TraderInterface, PerpetualDataHandler } from '@d8x/perpetuals-sdk'; * async function main() { * console.log(TraderInterface); * const config = PerpetualDataHandler.readSDKConfig("cardona"); * const signer = // ethers Signer, e.g. from Metamask * let traderAPI = new TraderInterface(config); * await traderAPI.createProxyInstance(); * // add liquidity * let respAddLiquidity = await traderAPI.addLiquidity(signer, "MATIC", 0.1); * console.log(respAddLiquidity); * } * main(); * */ addLiquidity(signer: Signer, poolSymbolName: string, amountCC: number, overrides?: Overrides): Promise<ContractTransactionResponse>; /** * Initiates a liquidity withdrawal from the pool * It triggers a time-delayed unlocking of the given number of pool shares. * The amount of pool shares to be unlocked is fixed by this call, but not their value in pool currency. * @param {Signer} signer Signer that will initiate liquidity withdrawal * @param {string} poolSymbolName Name of pool symbol (e.g. MATIC). * @param {string} amountPoolShares Amount in pool-shares, removes everything if > available amount. * * @return Transaction object. * @example * import { TraderInterface, PerpetualDataHandler } from '@d8x/perpetuals-sdk'; * async function main() { * console.log(TraderInterface); * const config = PerpetualDataHandler.readSDKConfig("cardona"); * const signer = // ethers Signer, e.g. from Metamask * let traderAPI = new TraderInterface(config); * await traderAPI.createProxyInstance(); * // submit txn * let tx = await traderAPI.initiateLiquidityWithdrawal(signer, "MATIC", 10.2); * console.log(tx); * } * main(); * */ initiateLiquidityWithdrawal(signer: Signer, poolSymbolName: string, amountPoolShares: number, overrides?: Overrides): Promise<ContractTransactionResponse>; /** * Withdraws as much liquidity as there is available after a call to initiateLiquidityWithdrawal. * The address loses pool shares in return. * @param {Signer} signer Signer that will execute the liquidity withdrawal * @param poolSymbolName * * @returns Transaction object. * @example * import { TraderInterface, PerpetualDataHandler } from '@d8x/perpetuals-sdk'; * async function main() { * console.log(TraderInterface); * const config = PerpetualDataHandler.readSDKConfig("cardona"); * const signer = // ethers Signer, e.g. from Metamask * let traderAPI = new TraderInterface(config); * await traderAPI.createProxyInstance(); * // submit txn * let tx = await traderAPI.executeLiquidityWithdrawal(signer, "MATIC"); * console.log(tx); * } * main(); * */ executeLiquidityWithdrawal(signer: Signer, poolSymbolName: string, overrides?: Overrides): Promise<ContractTransactionResponse>; }