Orders API

Manage orders and check order status

Orders API

The Orders API provides functionality for creating orders and checking order status.

Access

const dflow = new DFlowClient();
const orders = dflow.orders;

Methods

getOrder

Get an order quote and transaction for execution.

import { USDC_MINT } from 'dflow-sdk';
 
const order = await dflow.orders.getOrder({
  inputMint: USDC_MINT,
  outputMint: market.accounts.usdc.yesMint,
  amount: 1000000, // 1 USDC (6 decimals)
  slippageBps: 50, // 0.5%
  userPublicKey: wallet.publicKey.toBase58(),
  platformFeeBps: 10, // Optional: platform fee
  platformFeeAccount: 'fee-account-address', // Optional
});

Parameters:

NameTypeRequiredDescription
inputMintstringYesInput token mint address
outputMintstringYesOutput token mint address
amountnumberYesAmount in smallest units
slippageBpsnumberNoSlippage tolerance in basis points
userPublicKeystringYesUser's wallet public key
platformFeeBpsnumberNoPlatform fee in basis points
platformFeeAccountstringNoAccount to receive platform fees

Returns:

interface OrderResponse {
  transaction: string; // Base64 encoded transaction
  inAmount: string;
  outAmount: string;
  executionMode: 'sync' | 'async';
  priceImpactPct?: number;
}

getOrderStatus

Check the status of an order by transaction signature.

const status = await dflow.orders.getOrderStatus(signature);
 
console.log(`Order status: ${status.status}`);

Returns:

interface OrderStatusResponse {
  status: 'open' | 'closed' | 'failed' | 'pendingClose';
  signature: string;
  inAmount?: string;
  outAmount?: string;
  fills?: OrderFill[];
  error?: string;
}
 
interface OrderFill {
  inputMint: string;
  outputMint: string;
  inAmount: string;
  outAmount: string;
  price: number;
  timestamp: string;
}

Status meanings:

StatusDescription
openOrder is still processing
closedOrder completed successfully
failedOrder failed to execute
pendingCloseOrder is being finalized

Examples

Create and Execute an Order

import { Connection, Keypair } from '@solana/web3.js';
import { DFlowClient, USDC_MINT, signSendAndConfirm } from 'dflow-sdk';
 
const dflow = new DFlowClient();
const connection = new Connection('https://api.mainnet-beta.solana.com');
const keypair = Keypair.fromSecretKey(/* your key */);
 
// Get the market
const market = await dflow.markets.getMarket('BTCD-25DEC0313-T92749.99');
const account = market.accounts.usdc;
 
// Create order
const order = await dflow.orders.getOrder({
  inputMint: USDC_MINT,
  outputMint: account.yesMint,
  amount: 1000000, // 1 USDC
  slippageBps: 50,
  userPublicKey: keypair.publicKey.toBase58(),
});
 
console.log(`Expected output: ${order.outAmount}`);
console.log(`Price impact: ${order.priceImpactPct}%`);
 
// Execute the order
const result = await signSendAndConfirm(
  connection,
  order.transaction,
  keypair,
  'confirmed'
);
 
console.log(`Order executed: ${result.signature}`);

Poll for Order Completion

async function waitForOrder(
  dflow: DFlowClient,
  signature: string,
  maxAttempts = 30
): Promise<OrderStatus> {
  for (let i = 0; i < maxAttempts; i++) {
    const status = await dflow.orders.getOrderStatus(signature);
    
    if (status.status === 'closed' || status.status === 'failed') {
      return status;
    }
    
    // Wait 1 second before checking again
    await new Promise(resolve => setTimeout(resolve, 1000));
  }
  
  throw new Error('Order status check timed out');
}
 
// Usage
const status = await waitForOrder(dflow, signature);
if (status.status === 'closed') {
  console.log('Order completed successfully!');
} else {
  console.log('Order failed');
}

Add Platform Fees

Platform fees allow you to monetize trades through your application. The fee is deducted from the trade and sent to your specified account.

const order = await dflow.orders.getOrder({
  inputMint: USDC_MINT,
  outputMint: account.yesMint,
  amount: 1000000,
  slippageBps: 50,
  userPublicKey: keypair.publicKey.toBase58(),
  platformFeeBps: 25, // 0.25% fee
  platformFeeAccount: 'your-fee-account-public-key',
});

On this page