LogoLogo
  • Introduction
    • 🔴Welcome to IntentX
    • 💢The Omnichain DEX
    • ⚡User Experience Focus
    • 🌐The Meta Front-End
  • On-Chain Derivatives Overview
    • ❓What Problem Does IntentX Solve?
    • 📊Current On-Chain Derivatives Landscape
    • 💡IntentX Solution & Architecture Overview
    • ✅Comparison and Advantages of IntentX
  • IntentX Platform
    • 📈Trading on IntentX
      • 📖Trading Basics
      • 🏫Trading Tutorials
        • 🏫Web3 Wallet
        • 🏫Account Abstracted Wallet
      • 🌡️Liquidations, Margin Management (CVA), and Account Health
      • 📊Instant 1-Click Trading
      • 🛑Take Profit and Stop Loss
      • 💵Collateral & Cross-Margin Accounts
      • 💸Understanding Funding Rates
      • 💰Pricing Data and the Role of Oracles
      • ↕️Unrealized Profit and Loss (uPNL)
      • 📏Open Interest (OI) and Market Activity
      • 🔐Withdrawal Process and Security Measures
      • 📈Advanced Charts by TradingView
    • 🔢Pair List
    • 🌀IntentX Solver Network
      • 🌀Example Solver Order Flow
    • 🫂Referral Program
    • 📱Mobile and Progressive Web App
  • Trading API
    • Introduction
    • Rest API V1
    • Typescript SDK
      • Examples
        • Opening Positions
        • Closing Positions
        • Cancel Positions
        • List Subaccounts
        • List Positions
        • Private Key Authentication
        • Enable Instant Actions
  • Tokenomics
    • 🪙INTX Token and xINTX Staking
    • 🔄Trade & Earn xINTX
    • 📅Token Allocation and Release Schedule
  • Technical Docs (WIP)
    • ⚙️Infrastructure Overview
      • 🔩Technical Docs
      • 🌐Sample Solver Docs
  • Additional Information
    • ❓FAQ
    • 🌐Official Links
    • 📃Terms & Conditions
    • 🔐Security & Audits
    • 🌟Brand & Media Assets
    • 💾Contracts
Powered by GitBook
On this page
  1. Trading API
  2. Typescript SDK
  3. Examples

Cancel Positions

  import { fromWei, SupportedChainId, toBN } from "@intentx/core";
import { CancelRequestHooks, TradingSDK } from "@intentx/trading-sdk";
import * as dotenv from "dotenv";

dotenv.config();

async function cancelPositionExample() {
  const tradingSDK = new TradingSDK({
    apiKey: process.env.API_KEY,
    baseUrl: process.env.API_BASE_URL ?? undefined,
  });

  const positionIds = [128773, 128775, 128777, 128778];

  const positionDetailsPromises = positionIds.map((positionId) =>
    tradingSDK.tradeManager.getPositionInfo(
      positionId.toString(),
      SupportedChainId.BASE
    )
  );

  const positionDetails = await Promise.all(positionDetailsPromises);

  const cancelRequests = await Promise.all(
    positionDetails.map(async (position, index) => {
      const amountToClose = toBN(fromWei(position.quantity))
        .minus(position.closedQuantity)
        .toString();

      const cancelRequest =
        await tradingSDK.tradeManager.createCancelQuoteRequest({
          quoteId: positionIds[index].toString(),
          chainId: SupportedChainId.BASE,
        });

      cancelRequest.on(CancelRequestHooks.SUCCESS, (state, justification) => {
        console.log(
          `Position ${positionIds[index]} cancelled`,
          state,
          justification
        );
      });

      cancelRequest.on(CancelRequestHooks.FAILED, (state, justification) => {
        console.log(
          `Position ${positionIds[index]} cancellation failed`,
          state,
          justification
        );
      });

      return cancelRequest;
    })
  );

  await Promise.all(
    cancelRequests.map((request) => request.waitForCompletion())
  );
}

cancelPositionExample()
  .then(() => {
    console.log("All positions cancelled");
  })
  .catch((error) => {
    console.error("Error cancelling positions", error.message, error.code);
  });
PreviousClosing PositionsNextList Subaccounts

Last updated 1 month ago