Limit Orders
The Fusion AMM SDK provides tools to manage limit orders in a FusionPool. Developers can open or close limit orders using straightforward SDK methods, as well as fetch Limit Orders by the owner.
Open Limit Order: creates a new limit order and adds the specified token amount. See
openLimitOrderInstructions,increaseLimitOrderInstructionsfunctions of the high-level SDK package.Close Limit Order: removes all ordered or swapped amount (if partially or fully swapped), collects accrued fees, pays any applicable fees, and closes the order, as detailed in Fees and Incentives. See
closeLimitOrderInstructions,decreaseLimitOrderInstructionsfunctions of the high-level SDK package.Fetch Limit Orders: fetches multiple limit orders according to input parameters. See
fetchLimitOrdersForOwnerfunction of the high-level SDK package.
Limit order amounts can also be increased or decreased independently, offering flexible management.
Examples:
1. Open Limit Order
import { openLimitOrderInstructions } from "@crypticdot/fusionamm-sdk";
import { sendTransaction } from "@crypticdot/fusionamm-tx-sender";
import { address, createSolanaRpc } from "@solana/kit";
export const rpc = createSolanaRpc("https://api.mainnet-beta.solana.com");
export const signer = await loadKeypair(); // Load your wallet
// SOL/USDC pool address
let poolAddress = address("7VuKeevbvbQQcxz6N4SNLmuq6PYy4AcGQRDssoqo4t65");
let amount = 1_000_000_000n; // The limit order input amount
let aToB = true; // The limit order direction
const open = await openLimitOrderInstructions(
rpc,
poolAddress,
amount,
{ price: 140.0 },
aToB,
signer,
);
const signature = await sendTransaction(rpc, open.instructions, signer);
console.log("Transaction ID:", signature);
console.log("Initialization cost:", open.initializationCost);
console.log("Limit order mint:", open.limitOrderMint);use fusionamm_sdk::{open_limit_order_instructions, PriceOrTickIndex};
use solana_client::nonblocking::rpc_client::RpcClient;
use solana_pubkey::pubkey;
use solana_keypair::Keypair;
use solana_signer::Signer;
#[tokio::main]
async fn main() {
let rpc = RpcClient::new("https://api.mainnet.solana.com".to_string());
let wallet = Keypair::new(); // Load your wallet here
let fusion_pool_pubkey =
pubkey!("7VuKeevbvbQQcxz6N4SNLmuq6PYy4AcGQRDssoqo4t65");
let amount = 1_000_000;
let limit_order_price = 100.5;
let a_to_b = true;
let funder = Some(wallet.pubkey());
let result = open_limit_order_instructions(
&rpc,
fusion_pool_pubkey,
amount,
PriceOrTickIndex::Price(limit_order_price),
a_to_b,
funder,
)
.await
.unwrap();
println!("Limit Order Mint: {:?}", result.limit_order_mint);
println!("Initialization Cost: {} lamports", result.initialization_cost);
}2. Close Limit Order
import { closeLimitOrderInstructions, openLimitOrderInstructions }
from "@crypticdot/fusionamm-sdk";
import { sendTransaction } from "@crypticdot/fusionamm-tx-sender";
import { address, createSolanaRpc } from "@solana/kit";
export const rpc = createSolanaRpc("https://api.mainnet-beta.solana.com");
export const signer = await loadKeypair(); // Load your wallet
let limitOrderMint = address("HqoV7Qv27REUtmd9UKSJGGmCRNx3531t33bDG1BUfo9K");
const close = await closeLimitOrderInstructions(rpc, limitOrderMint, signer);
const signature = await sendTransaction(rpc, close.instructions, signer);
console.log("Transaction ID:", signature);use fusionamm_sdk::close_limit_order_instructions;
use solana_client::nonblocking::rpc_client::RpcClient;
use solana_pubkey::pubkey;
use solana_keypair::Keypair;
use solana_signer::Signer;
#[tokio::main]
async fn main() {
let rpc = RpcClient::new("https://api.mainnet.solana.com".to_string());
let wallet = Keypair::new(); // Load your wallet here
let limit_order_mint_address =
pubkey!("HqoV7Qv27REUtmd9UKSJGGmCRNx3531t33bDG1BUfo9K");
let authority = Some(wallet.pubkey());
let result = close_limit_order_instructions(
&rpc,
limit_order_mint_address,
authority,
)
.await
.unwrap();
println!("Number of Instructions: {}", result.instructions.len());
}Last updated