Skip to main content

Example 0 – Aggregator (swaps + borrow)

import { createClient } from "satsterminal-sdk";
import { ChainType } from "@satsterminal-sdk/borrow";

const { swaps, borrow } = createClient({
  apiKey: process.env.API_KEY!,
  borrow: { chain: ChainType.BASE, wallet: /* your wallet */ },
});

const quote = await swaps.swapQuote({
  amount: "0.001",
  fromToken: "BTC",
  toToken: "USDC",
  address: "bc1p...",
  protocol: "runes",
  params: {},
});

const setup = await borrow.setup();
console.log("Smart account:", setup.userStatus.smartAccountAddress);

Example 1 – Buy runes (BTC → rune)

import { SatsTerminal } from "satsterminal-sdk";
import readlineSync from "readline-sync";

const swaps = new SatsTerminal({ apiKey: process.env.API_KEY! });

const TRADE = {
  fromToken: "BTC",
  toToken: "LOBO•THE•WOLF•PUP",
  address: "bc1p...",
  publicKey: "...",
  paymentAddress: "3Pc...",
  paymentPublicKey: "...",
  amount: "0.0001",
  protocol: "runes",
};

const quote = await swaps.swapQuote({ ...TRADE, params: {} });
const psbt = await swaps.swapPSBT({
  ...TRADE,
  marketplace: quote.bestMarketplace,
  swapId: quote.swapId,
  feeRate: 5,
  slippage: 9,
  themeID: null,
});

const signedPsbts = psbt.psbts.map(({ hex }, i) => {
  console.log(`PSBT ${i + 1}:`, hex.slice(0, 60), "...");
  return readlineSync.question("Enter signed PSBT hex: ");
});

const result = await swaps.swapSubmit({
  ...TRADE,
  marketplace: quote.bestMarketplace,
  swapId: psbt.swapId,
  signedPsbts,
});

console.log("✅ Swap complete:", result.txid);

Example 2 – Sell runes (rune → BTC)

const TRADE = {
  fromToken: "DOG•GO•TO•THE•MOON",
  toToken: "BTC",
  sellAmount: "10000",
  address: "bc1p...",
  publicKey: "...",
  paymentAddress: "3Pc...",
  paymentPublicKey: "...",
  protocol: "runes",
};

const quote = await swaps.swapQuote({
  amount: TRADE.sellAmount,
  fromToken: TRADE.fromToken,
  toToken: TRADE.toToken,
  address: TRADE.address,
  protocol: TRADE.protocol,
  params: {},
});

const psbt = await swaps.swapPSBT({
  ...TRADE,
  marketplace: quote.bestMarketplace,
  swapId: quote.swapId,
  feeRate: 5,
  slippage: 9,
  themeID: null,
});

const signedPsbts = psbt.psbts.map(({ hex }) => signWithWallet(hex));

const confirmation = await swaps.swapSubmit({
  ...TRADE,
  marketplace: quote.bestMarketplace,
  swapId: psbt.swapId,
  signedPsbts,
});

console.log("✅ Sold runes for BTC:", confirmation.txid);

Example 3 – Alkanes swap (BTC → token)

const TRADE = {
  fromToken: "BTC",
  toToken: "GOLD DUST",
  address: "bc1p...",
  publicKey: "...",
  paymentAddress: "bc1q...",
  paymentPublicKey: "...",
  amount: "0.00008",
  protocol: "alkanes",
};

const quote = await swaps.swapQuote({ ...TRADE, params: {} });

if (quote.metrics) {
  Object.entries(quote.metrics).forEach(([m, metrics]) => {
    console.log(`${m}: ${metrics.percentFulfilled}% at ${metrics.averageUnitPrice}`);
  });
}

const psbt = await swaps.swapPSBT({
  ...TRADE,
  marketplace: quote.bestMarketplace,
  swapId: quote.swapId,
  feeRate: 3,
  slippage: 5,
  themeID: null,
});

const signedPsbts = psbt.psbts.map(({ hex }) => signWithWallet(hex));
const result = await swaps.swapSubmit({
  ...TRADE,
  marketplace: quote.bestMarketplace,
  swapId: psbt.swapId,
  signedPsbts,
});

console.log("🎉 Alkanes swap complete:", result.txid);

Error-handling tips

  • Network issues: check API key + connectivity; retry with backoff.
  • Slippage/amount errors: increase slippage or reduce amount.
  • Marketplace errors: try another marketplace from the quote.
  • Swap expiration: re-run swapQuote to refresh the session.