Buy (manual implementation)
This shows the complete build-sign-submit flow without using the SDK. This is exactly what sdk.buy() does internally. Use this as a reference for custom integrations or porting to other languages.
Manual Implementation
Section titled “Manual Implementation”This is the full orchestration for a buy transaction:
- Build (
POST /v1/tx/buy) → getunsignedTransactionandsubmissionToken - Sign locally (decode base64 wire → sign → encode base64 wire)
- Submit (
POST /v1/tx/submit)
For the full endpoint references, see:
import bs58 from 'bs58';import axios from 'axios';import https from 'https';import { getTransactionDecoder, createKeyPairFromBytes, signTransaction, getBase64EncodedWireTransaction,} from '@solana/kit';
// Hardcoded config (load from env in production)const API_KEY = 'api_your_key_here';const SECRET_KEY_BASE58 = 'PASTE_BASE58_64_BYTE_SECRET_KEY_HERE';
const MINT = 'TOKEN_MINT';const SOL_AMOUNT = 0.002;const SLIPPAGE = 0.05;const PRIORITY = 'fast'; // see: /api/priority/
// HTTPS agent with keepalive for connection reuseconst httpsAgent = new https.Agent({ keepAlive: true, keepAliveMsecs: 1000, maxSockets: 10, maxFreeSockets: 5, timeout: 60000,});
const api = axios.create({ baseURL: 'https://api.darkfibre.dev/v1', headers: { 'Authorization': `Bearer ${API_KEY}` }, timeout: 30000, httpsAgent,});
async function fetchBuyTx() { const res: any = await api.post('/tx/buy', { mint: MINT, solAmount: SOL_AMOUNT, slippage: SLIPPAGE, priority: PRIORITY, }); return res.data.data; // { submissionToken, unsignedTransaction, ... }}
async function submitTx(submissionToken: string, signedTransaction: string) { const res: any = await api.post('/tx/submit', { submissionToken, signedTransaction, }); return res.data.data; // { signature, status, ... }}
async function signTx(unsignedTxBase64Wire: string) { const signer: CryptoKeyPair = await createKeyPairFromBytes(bs58.decode(SECRET_KEY_BASE58));
// 1) Deserialize base64 wire → Transaction const unsignedTx = getTransactionDecoder().decode(Buffer.from(unsignedTxBase64Wire, 'base64'));
// 2) Sign const signed = await signTransaction([signer], unsignedTx as any);
// 3) Serialize Transaction → base64 wire return getBase64EncodedWireTransaction(signed);}
async function main() { // 1) Build const { submissionToken, unsignedTransaction } = await fetchBuyTx();
// 2) Sign (do it immediately; tx expires quickly) const signedTransaction = await signTx(unsignedTransaction);
// 3) Submit const { signature } = await submitTx(submissionToken, signedTransaction); console.log(`https://solscan.io/tx/${signature}`);}
main().catch(console.error);import base64import base58import requestsimport osfrom pathlib import Path
from solders.keypair import Keypairfrom solders.transaction import VersionedTransaction
#Hardcoded config (load from env in production)API_KEY = 'api_your_key_here';SECRET_KEY_BASE58 = 'PASTE_BASE58_64_BYTE_SECRET_KEY_HERE';
# ConfigAPI_BASE_URL = "https://api.darkfibre.dev/v1"
MINT = "5UUH9RTDiSpq6HKS6bp4NdU9PNJpXRXuiw6ShBTBhgH2"SOL_AMOUNT = 0.002SLIPPAGE = 0.05PRIORITY = "fast" # see: /api/priority/
def post(path: str, body: dict): r = requests.post( f"{API_BASE_URL}{path}", headers={"Authorization": f"Bearer {API_KEY}"}, json=body, timeout=30, ) r.raise_for_status() return r.json()["data"]
def sign_tx(unsigned_tx_base64_wire: str) -> str: signer = Keypair.from_bytes(base58.b58decode(SECRET_KEY_BASE58))
# 1) Deserialize base64 wire → Transaction unsigned_tx = VersionedTransaction.from_bytes(base64.b64decode(unsigned_tx_base64_wire))
# 2) Sign signed_tx = VersionedTransaction(unsigned_tx.message, [signer])
# 3) Serialize Transaction → base64 wire return base64.b64encode(bytes(signed_tx)).decode("utf-8")
def main(): # 1) Build built = post("/tx/buy", {"mint": MINT, "solAmount": SOL_AMOUNT, "slippage": SLIPPAGE, "priority": PRIORITY}) submission_token = built["submissionToken"] unsigned_transaction = built["unsignedTransaction"]
# 2) Sign signed_transaction = sign_tx(unsigned_transaction)
# 3) Submit submitted = post("/tx/submit", {"submissionToken": submission_token, "signedTransaction": signed_transaction}) print(f"https://solscan.io/tx/{submitted['signature']}")
main()- Sign fast:
expiresAtis short (~30s). Build and sign immediately before submitting. - Keep the message unchanged: the submit endpoint validates message bytes against what the server built.