Build a Limit Order Bot With Javascript
Jupiter Limit Order provides users with the simplest way to place limit orders on Solana and receive tokens directly in your wallet when the order is filled!
Query user open order, order history and trade history APIs
GEThttps://jup.ag/api/limit/v1/openOrders
https://jup.ag/api/limit/v1/openOrders
GEThttps://jup.ag/api/limit/v1/orderHistory
https://jup.ag/api/limit/v1/orderHistory
GEThttps://jup.ag/api/limit/v1/tradeHistory
https://jup.ag/api/limit/v1/tradeHistory
Cancel order
POSThttps://jup.ag/api/limit/v1/cancelOrders
https://jup.ag/api/limit/v1/cancelOrders
Create Limit Order (Code Example)
1. Install the libraries
Running this example requires a minimum of NodeJS 16. In your command line terminal, install the libraries.
npm i @solana/web3.js
npm i cross-fetch
npm i @project-serum/anchor
npm i bs58
2. Import from libraries and setup connection
Next you can copy the following code snippets to a javascript file jupiter-api-example.js. And when you are ready to run the code, just type: node jupiter-api-example.js
import { Connection, Keypair, Transaction } from "@solana/web3.js";
import fetch from "cross-fetch";
import { Wallet } from "@project-serum/anchor";
import bs58 from "bs58";
// This RPC endpoint is only for demonstration purposes so it may not work.
const connection = new Connection(
"https://neat-hidden-sanctuary.solana-mainnet.discover.quiknode.pro/2af5315d336f9ae920028bbb90a73b724dc1bbed/"
);
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.
3. Setup your wallet
In this example, you can paste in your private key for testing purposes but this is not recommended for production applications.
const wallet = new Wallet(
Keypair.fromSecretKey(bs58.decode(process.env.PRIVATE_KEY || ""))
);
4. Get the serialized transactions to perform the limit order
// Base key are used to generate a unique order id
const base = Keypair.generate();
// get serialized transactions
const { tx } = await (
await fetch('https://jup.ag/api/limit/v1/createOrder', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
owner: wallet.publicKey.toString(),
inAmount: 100000, // 1000000 => 1 USDC if inputToken.address is USDC mint
outAmount: 100000,
inputMint: inputMint.toString(),
outputMint: outputMint.toString(),
expiredAt: null, // new Date().valueOf() / 1000,
base: base.publicKey.toString(),
// referralAccount and name are both optional.
// Please provide both to get referral fees.
// More details in the section below.
// referralAccount: referralPublicKey,
// referralName: "Referral Name"
})
})
).json();
expiredAt - Can be either null or Unix timestamp in seconds.
Execute transaction
5. Deserialize and sign the transaction
// deserialize the transaction
const transactionBuf = Buffer.from(tx, "base64");
var transaction = Transaction.deserialize(transactionBuf);
// sign the transaction using the required key
// for create order, wallet and base key are required.
transaction.sign([wallet.payer, base]);
6. Execute the transaction
// get the latest block hash
const latestBlockHash = await connection.getLatestBlockhash();
// Execute the transaction
const rawTransaction = transaction.serialize();
const txid = await connection.sendRawTransaction(rawTransaction, {
skipPreflight: true,
maxRetries: 2,
});
await connection.confirmTransaction({
blockhash: latestBlockHash.blockhash,
lastValidBlockHeight: latestBlockHash.lastValidBlockHeight,
signature: txid
});
console.log(`https://solscan.io/tx/${txid}`);
Whole code snippet
Deserialize, sign and execute the transaction from the response like here.
Due to the transaction size limit, the maximum cancellation order in a batch is 10.
The Jupiter Limit Order's project account for the Referral Program is 45ruCyfdRkWpRNGEqWzjCiXRHkZs8WXCLQ67Pnpye7Hp
.
Referral
Check out the referral program for Limit Order.