Examples

End-to-end example scripts for easy implementation using the SDK V2.

Example 1: Complete Rune Purchase Flow

Below is a complete example flow for buying runes using the simplified SDK V2, including waiting for user input for signed PSBTs:

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

// Configuration
const CONFIG = {
  apiKey: 'your_api_key_here',
};

// Trade Parameters
const TRADE_PARAMS = {
  fromToken: 'BTC',
  toToken: 'LOBO•THE•WOLF•PUP',
  address: 'bc1p...',
  publicKey: '...',
  paymentAddress: '3Pc...',
  paymentPublicKey: '...',
  amount: '0.0001',
  protocol: 'runes'
};

// Initialize the SDK
const satsTerminal = new SatsTerminal(CONFIG);

// Function to get user input for signed PSBTs (V2 can have multiple)
const getSignedPSBTs = (psbts) => {
  console.log(`\nPlease sign ${psbts.length} PSBT(s):`);
  const signedPsbts = [];
  
  for (let i = 0; i < psbts.length; i++) {
    console.log(`\nPSBT ${i + 1} hex: ${psbts[i].hex}`);
    const signed = readlineSync.question(`Enter signed PSBT ${i + 1} hex: `);
    signedPsbts.push(signed);
  }
  
  return signedPsbts;
};

// Main execution
(async () => {
  try {
    // 1. Get a quote for the swap
    console.log('\n1. Fetching swap quote...');
    const quote = await satsTerminal.swapQuote({
      amount: TRADE_PARAMS.amount,
      fromToken: TRADE_PARAMS.fromToken,
      toToken: TRADE_PARAMS.toToken,
      address: TRADE_PARAMS.address,
      protocol: TRADE_PARAMS.protocol,
      params: {}
    });
  
    console.log('Best marketplace:', quote.bestMarketplace);
    console.log('Swap ID:', quote.swapId);
    console.log('From token amount:', quote.fromTokenAmount);
    console.log('To token amount:', quote.toTokenAmount);
    console.log('Available marketplaces:', Object.keys(quote.marketplaces));

    // 2. Create PSBT(s) for the swap
    console.log('\n2. Creating PSBT(s)...');
    const psbtResponse = await satsTerminal.swapPSBT({
      marketplace: quote.bestMarketplace,
      swapId: quote.swapId,
      address: TRADE_PARAMS.address,
      publicKey: TRADE_PARAMS.publicKey,
      paymentAddress: TRADE_PARAMS.paymentAddress,
      paymentPublicKey: TRADE_PARAMS.paymentPublicKey,
      protocol: TRADE_PARAMS.protocol,
      feeRate: 5,
      slippage: 9,
      themeID: null
    });
  
    console.log(`Created ${psbtResponse.psbts.length} PSBT(s)`);
    console.log('Updated swap ID:', psbtResponse.swapId);
  
    // Wait for user to sign all PSBTs
    const signedPsbts = getSignedPSBTs(psbtResponse.psbts);

    // 3. Submit the signed PSBTs
    console.log('\n3. Submitting signed PSBTs...');
    const result = await satsTerminal.swapSubmit({
      marketplace: quote.bestMarketplace,
      swapId: psbtResponse.swapId,
      address: TRADE_PARAMS.address,
      publicKey: TRADE_PARAMS.publicKey,
      paymentAddress: TRADE_PARAMS.paymentAddress,
      paymentPublicKey: TRADE_PARAMS.paymentPublicKey,
      protocol: TRADE_PARAMS.protocol,
      signedPsbts: signedPsbts
    });
  
    console.log('✅ Swap completed successfully!');
    console.log('Transaction ID:', result.txid);
    console.log('Marketplace used:', result.marketplace);

  } catch (error) {
    console.error('Error:', error.message);
  }
})();

Example 2: Selling Runes for BTC

Example 3: Alkanes Swap

Last updated