import {
createSolanaRpc,
pipe,
createTransactionMessage,
setTransactionMessageFeePayerSigner,
appendTransactionMessageInstructions,
setTransactionMessageLifetimeUsingBlockhash,
signTransactionMessageWithSigners,
sendAndConfirmTransactionFactory,
createSolanaRpcSubscriptions,
} from "@solana/kit";
import { DlmmProgram } from "@picon-finance/dlmm-sdk";
const rpc = createSolanaRpc("https://api.mainnet-beta.solana.com");
// Omit programAddress to target mainnet. Testing against devnet instead? Import
// DEVNET_PROGRAM_ADDRESS and pass it as the second argument here.
const dlmmProgram = new DlmmProgram(rpc);
// Fetch and cache a pool's account data, mints, and token programs in one call.
const pool = await dlmmProgram.getPool(poolAddress);
// Pool.swapExactIn() quotes internally (right-sizing exactly which bin arrays it needs) and
// returns both the quote and the matching swap instructions built against it — no separate
// quote step. Pool.swapExactOut() is the mirror image, fixing the output instead of the input.
const { quoteOutput, instructions } = await pool.swapExactIn(userSigner, {
xToY: true,
amountIn: 1_000_000n,
slippageToleranceBps: 50,
});
// Every entity method returns Instruction[] (or a single Instruction) — build, sign and
// send the transaction however you normally do with @solana/kit.
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
const transactionMessage = pipe(
createTransactionMessage({ version: 0 }),
msg => setTransactionMessageFeePayerSigner(userSigner, msg),
msg => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, msg),
msg => appendTransactionMessageInstructions(instructions, msg),
);
const signedTransaction = await signTransactionMessageWithSigners(transactionMessage);
const rpcSubscriptions = createSolanaRpcSubscriptions("wss://api.mainnet-beta.solana.com");
const sendAndConfirm = sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions });
await sendAndConfirm(signedTransaction, { commitment: "confirmed" });