跳转到主要内容

前提条件

  • 一个 LaserSell API 密钥(在此获取)。
  • 一个 Solana 密钥对文件(JSON 字节数组)。
  • Node.js 18+(TypeScript 示例)或你首选语言的运行时。
使用 AI 编程?LaserSell MCP 服务器添加到你的编辑器,让你的 AI 助手能够实时搜索 LaserSell 文档。兼容 Claude Code、Claude Desktop、Cursor 和 Windsurf。

示例 1:构建、签名和发送卖出交易

此示例调用 LaserSell API 构建未签名的卖出交易,在本地签名,并通过 Helius Sender 提交。
import { readFile } from "node:fs/promises";
import { Keypair } from "@solana/web3.js";
import {
  ExitApiClient,
  sendTransaction,
  sendTargetHeliusSender,
  signUnsignedTx,
  type BuildSellTxRequest,
} from "@lasersell/lasersell-sdk";

const apiKey = process.env.LASERSELL_API_KEY!;
const keypairPath = "./keypair.json";

// Load keypair
const raw = await readFile(keypairPath, "utf8");
const keypair = Keypair.fromSecretKey(
  Uint8Array.from(JSON.parse(raw))
);

// Build unsigned tx
const client = ExitApiClient.withApiKey(apiKey);
const request: BuildSellTxRequest = {
  mint: "So11111111111111111111111111111111111111112",
  user_pubkey: keypair.publicKey.toBase58(),
  amount_tokens: 1_000_000,
  slippage_bps: 2_000,
  output: "SOL",
};
const unsignedTxB64 = await client.buildSellTxB64(request);

// Sign locally
const signedTx = signUnsignedTx(unsignedTxB64, keypair);

// Submit
const signature = await sendTransaction(
  sendTargetHeliusSender(),
  signedTx
);
console.log("Signature:", signature);

示例 2:使用退出智能流自动卖出

连接退出智能流以监控钱包,当策略阈值达到时自动执行退出。
在买入之前连接流。 流通过监控链上代币到达来检测仓位。如果你在流连接和配置之前调用 /v1/buy,仓位将不会被追踪。始终先启动流,然后提交买入交易。
import { readFile } from "node:fs/promises";
import { Keypair } from "@solana/web3.js";
import {
  StreamClient,
  StreamSession,
  sendTransaction,
  sendTargetHeliusSender,
  signUnsignedTx,
} from "@lasersell/lasersell-sdk";

const apiKey = process.env.LASERSELL_API_KEY!;
const raw = await readFile("./keypair.json", "utf8");
const signer = Keypair.fromSecretKey(Uint8Array.from(JSON.parse(raw)));

const client = new StreamClient(apiKey);
const session = await StreamSession.connect(client, {
  wallet_pubkeys: [signer.publicKey.toBase58()],
  strategy: {
    target_profit_pct: 5,
    stop_loss_pct: 1.5,
  },
  deadline_timeout_sec: 45,
  send_mode: "helius_sender",
  tip_lamports: 1000,
});

while (true) {
  const event = await session.recv();
  if (event === null) break;

  if (event.type === "exit_signal_with_tx") {
    const signed = signUnsignedTx(event.message.unsigned_tx_b64, signer);
    const sig = await sendTransaction(sendTargetHeliusSender(), signed);
    console.log("Exit submitted:", sig);
  }
}

后续步骤