Creating a Limit Order with the SDK
Program Address
jupoNjAxXgZ4rjzxzPMP4oxduvQsQtZzyknqvzYNrNu
Installation
Our published package can be found here at NPM.
yarn add @jup-ag/limit-order-sdk
Usage
1. Import the needed libraries
import { LimitOrderProvider } from "@jup-ag/limit-order-sdk";
2. Load limit order instance with connection
// This RPC endpoint is only for demonstration purposes so it may not work.
const SOLANA_RPC_ENDPOINT =
"https://neat-hidden-sanctuary.solana-mainnet.discover.quiknode.pro/2af5315d336f9ae920028bbb90a73b724dc1bbed/";
const connection = new Connection($SOLANA_RPC_ENDPOINT);
const limitOrder = new LimitOrderProvider(
connection,
// referralPubKey and referalName are both optional.
// Please provide both to get referral fees.
// More details in the section below.
// referralPubKey,
// referralName
);
info
Always make sure that you are using your own RPC endpoint. The RPC endpoint used by the connection object in the above example may not work anymore.
Create limit order
// Base key are used to generate a unique order id
const base = Keypair.generate();
const { tx, orderPubKey } = await limitOrder.createOrder({
owner: owner.publicKey,
inAmount: new BN(100000), // 1000000 => 1 USDC if inputToken.address is USDC mint
outAmount: new BN(100000),
inputMint: new PublicKey(inputToken.address),
outputMint: new PublicKey(outputToken.address),
expiredAt: null, // new BN(new Date().valueOf() / 1000)
base: base.publicKey,
});
await sendAndConfirmTransaction(connection, tx, [owner, base]);
expiredAt - Can be either null or Unix timestamp in seconds.
Query user order and history
import { ownerFilter } from "@jup-ag/limit-order-sdk";
import { OrderHistoryItem, TradeHistoryItem } from "@jup-ag/limit-order-sdk";
const openOrder = await limitOrder.getOrders([ownerFilter(owner.publicKey)]);
const orderHistory: OrderHistoryItem[] = await limitOrder.getOrderHistory({
wallet: owner.publicKey.toBase58(),
take: 20, // optional, default is 20, maximum is 100
// lastCursor: order.id // optional, for pagination
});
const orderHistoryCount: number = await limitOrder.getOrderHistoryCount({
wallet: owner.publicKey.toBase58(),
});
const tradeHistory: TradeHistoryItem[] = await limitOrder.getTradeHistory({
wallet: owner.publicKey.toBase58(),
take: 20, // optional, default is 20, maximum is 100
// lastCursor: order.id // optional, for pagination
});
const tradeHistoryCount: number = await limitOrder.getTradeHistoryCount({
wallet: owner.publicKey.toBase58(),
});
Cancel order
const tx = await limitOrder.cancelOrder({
owner: owner.publicKey,
orderPubKey: order.publicKey,
});
await sendAndConfirmTransaction(connection, tx, [owner]);
Batch cancel order
const tx = await limitOrder.batchCancelOrder({
owner: owner.publicKey,
ordersPubKey: batchOrdersPubKey,
});
await sendAndConfirmTransaction(connection, tx, [owner]);
info
Due to the transaction size limit, the maximum allowed cancellation order per transaction in a batch is 10.
Referral
Check out the referral program for Limit Order.