Traders

Traders use the SDK component AccountTrade to post orders.

AccountTrade ⇐ WriteAccessHandler

Functions to create, submit and cancel orders on the exchange. This class requires a private key and executes smart-contract interactions that require gas-payments.

Kind: global class Extends: WriteAccessHandler

  • AccountTradeWriteAccessHandler

    • new AccountTrade(config, signer)

    • .cancelOrder(symbol, orderId)ContractTransaction

    • .order(order)ContractTransaction

    • .queryExchangeFee(poolSymbolName, [brokerAddr])

    • .getCurrentTraderVolume(poolSymbolName)number

    • .getOrderIds(symbol)Array.<string>

    • .addCollateral(symbol, amount)

    • .removeCollateral(symbol, amount)

new AccountTrade(config, signer)

Constructor

ParamTypeDescription

config

NodeSDKConfig

Configuration object, see PerpetualDataHandler. readSDKConfig.

signer

string | Signer

Private key or ethers Signer of the account

Example

import { AccountTrade, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
async function main() {
  console.log(AccountTrade);
  // load configuration for Polygon zkEVM Tesnet
  const config = PerpetualDataHandler.readSDKConfig("cardona");
  // AccountTrade (authentication required, PK is an environment variable with a private key)
  const pk: string = <string>process.env.PK;
  let accTrade = new AccountTrade(config, pk);
  // Create a proxy instance to access the blockchain
  await accTrade.createProxyInstance();
}
main();

accountTrade.cancelOrder(symbol, orderId) ⇒ ContractTransaction

Cancels an existing order on the exchange.

Kind: instance method of AccountTrade Returns: ContractTransaction -

Contract Transaction (containing events).

ParamTypeDescription

symbol

string

Symbol of the form ETH-USD-MATIC.

orderId

string

ID of the order to be cancelled.

Example

import { AccountTrade, PerpetualDataHandler, Order } from '@d8x/perpetuals-sdk';
async function main() {
   console.log(AccountTrade);
   // 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 accTrade = new AccountTrade(config, pk);
   await accTrade.createProxyInstance();
   // cancel order
   let cancelTransaction = accTrade.cancelOrder("MATIC-USD-MATIC",
       "0x4639061a58dcf34f4c9c703f49f1cb00d6a4fba490d62c0eb4a4fb06e1c76c19")
   console.log(cancelTransaction);
 }
 main();

accountTrade.order(order) ⇒ ContractTransaction

Submits an order to the exchange.

Kind: instance method of AccountTrade Returns: ContractTransaction -

Contract Transaction (containing events).

ParamTypeDescription

order

Order

Order structure. As a minimum the structure needs to specify symbol, side, type and quantity.

Example

import { AccountTrade, PerpetualDataHandler, Order } from '@d8x/perpetuals-sdk';
async function main() {
   console.log(AccountTrade);
   // setup (authentication required, PK is an environment variable with a private key)
   const config = PerpetualDataHandler.readSDKConfig("cardona");
   const pk: string = <string>process.env.PK;
   const accTrade = new AccountTrade(config, pk);
   await accTrade.createProxyInstance();
   // set allowance
   await accTrade.setAllowance("MATIC");
   // set an order
   const order: Order = {
       symbol: "MATIC-USD-MATIC",
       side: "BUY",
       type: "MARKET",
       quantity: 100,
       leverage: 2,
       executionTimestamp: Date.now()/1000,
   };
   const orderTransaction = await accTrade.order(order);
   console.log(orderTransaction);
 }
 main();

accountTrade.queryExchangeFee(poolSymbolName, [brokerAddr]) ⇒

Fee charged by the exchange for trading any perpetual on a given pool. It accounts for the current trader's fee tier (based on the trader's D8X balance and trading volume). If trading with a broker, it also accounts for the selected broker's fee tier. Note that this result only includes exchange fees, additional broker fees are not included.

Kind: instance method of AccountTrade Returns:

Exchange fee, in decimals (i.e. 0.1% is 0.001).

ParamTypeDescription

poolSymbolName

string

Pool symbol name (e.g. MATIC, USDC, etc).

[brokerAddr]

string

Optional address of a broker this trader may use to trade under.

Example

import { AccountTrade, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
async function main() {
  console.log(AccountTrade);
  // 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 accTrade = new AccountTrade(config, pk);
  await accTrade.createProxyInstance();
  // query exchange fee
  let fees = await accTrade.queryExchangeFee("MATIC");
  console.log(fees);
}
main();

accountTrade.getCurrentTraderVolume(poolSymbolName) ⇒ number

Exponentially weighted EMA of the total USD trading volume of all trades performed by this trader. The weights are chosen so that in average this coincides with the 30 day volume.

Kind: instance method of AccountTrade Returns: number -

Current trading volume for this trader, in USD.

ParamTypeDescription

poolSymbolName

string

Pool symbol name (e.g. MATIC, USDC, etc).

Example

import { AccountTrade, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
async function main() {
  console.log(AccountTrade);
  // 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 accTrade = new AccountTrade(config, pk);
  await accTrade.createProxyInstance();
  // query 30 day volume
  let vol = await accTrade.getCurrentTraderVolume("MATIC");
  console.log(vol);
}
main();

accountTrade.getOrderIds(symbol) ⇒ Array.<string>

Kind: instance method of AccountTrade Returns: Array.<string> -

Array of Ids for all the orders currently open by this trader.

ParamDescription

symbol

Symbol of the form ETH-USD-MATIC.

Example

import { AccountTrade, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
async function main() {
  console.log(AccountTrade);
  // 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 accTrade = new AccountTrade(config, pk);
  await accTrade.createProxyInstance();
  // get order IDs
  let orderIds = await accTrade.getOrderIds("MATIC-USD-MATIC");
  console.log(orderIds);
}
main();

accountTrade.addCollateral(symbol, amount)

Kind: instance method of AccountTrade

ParamTypeDescription

symbol

string

Symbol of the form ETH-USD-MATIC.

amount

number

How much collateral to add, in units of collateral currency, e.g. MATIC

Example

import { AccountTrade, PerpetualDataHandler } from '@d8x/perpetuals-sdk';

async function main() {
  // 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 accTrade = new AccountTrade(config, pk);
  await accTrade.createProxyInstance();
  // add collateral to margin account
  const tx = await accTrade.addCollateral("MATIC-USD-MATIC", 10.9);
  console.log(orderIds);
}

main();

accountTrade.removeCollateral(symbol, amount)

Kind: instance method of AccountTrade

ParamTypeDescription

symbol

string

Symbol of the form ETH-USD-MATIC.

amount

number

How much collateral to remove, in units of collateral currency, e.g. MATIC

Example

import { AccountTrade, PerpetualDataHandler } from '@d8x/perpetuals-sdk';

async function main() {
  // 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 accTrade = new AccountTrade(config, pk);
  await accTrade.createProxyInstance();
  // remove collateral from margin account
  const tx = await accTrade.removeCollateral("MATIC-USD-MATIC", 3.14);
  console.log(orderIds);
}

main();