Skip to content

๐Ÿงช Code Examples โ€‹

Ready-to-run TypeScript examples for common Fleet SDK operations

Getting Started โ€‹

All examples are in the /examples directory and can be run individually:

bash
# Install dependencies
pnpm install

# Run any example
pnpm tsx examples/01-basic-transfer.ts

Example Overview โ€‹

#ExampleDescriptionDifficulty
01Basic TransferSend ERG to an address๐ŸŸข Easy
02Multi-OutputBatch payments to multiple recipients๐ŸŸข Easy
03Token TransferSend native tokens๐ŸŸก Medium
04NFT MintingCreate EIP-4 NFTs๐ŸŸก Medium
05Contract InteractionCompile & use ErgoScript๐ŸŸ  Advanced
06Multi-Sig Wallet2-of-3 signature scheme๐ŸŸ  Advanced
07Oracle DataRead on-chain oracle prices๐ŸŸ  Advanced
08DeFi SwapAMM token swaps๐Ÿ”ด Expert

๐ŸŸข Basic Examples โ€‹

01 - Basic Transfer โ€‹

The simplest transaction: send ERG from one address to another.

Key Concepts:

  • TransactionBuilder basics
  • Input selection
  • Output creation
  • Fee handling
  • Change address
typescript
import { TransactionBuilder, OutputBuilder } from "@fleet-sdk/core";

const tx = new TransactionBuilder(currentHeight)
  .from(inputs)
  .to(new OutputBuilder(1_000_000_000n, recipientAddress))
  .sendChangeTo(senderAddress)
  .payFee(RECOMMENDED_MIN_FEE_VALUE)
  .build();

View Full Example โ†’


02 - Multi-Output Transaction โ€‹

Send to multiple recipients in a single transaction.

Key Concepts:

  • Multiple OutputBuilders
  • Batch processing
  • Gas efficiency
typescript
const recipients = [
  { address: "addr1...", amount: 1_000_000_000n },
  { address: "addr2...", amount: 2_000_000_000n },
  { address: "addr3...", amount: 500_000_000n }
];

const outputs = recipients.map(r => 
  new OutputBuilder(r.amount, r.address)
);

const tx = new TransactionBuilder(height)
  .from(inputs)
  .to(outputs)
  .sendChangeTo(sender)
  .payFee(fee)
  .build();

View Full Example โ†’


๐ŸŸก Token Examples โ€‹

03 - Token Transfer โ€‹

Send native Ergo tokens alongside ERG.

Key Concepts:

  • Token handling
  • .addTokens() method
  • Token change management
typescript
const tokenOutput = new OutputBuilder(SAFE_MIN_BOX_VALUE, recipient)
  .addTokens({
    tokenId: "03faf2cb...",
    amount: 100n
  });

View Full Example โ†’


04 - NFT Minting โ€‹

Create EIP-4 compliant NFTs with metadata.

Key Concepts:

  • EIP-4 standard
  • Register encoding (R4-R9)
  • NFT artwork types
typescript
import { first } from "@fleet-sdk/common";

const nftOutput = new OutputBuilder(SAFE_MIN_BOX_VALUE, minter)
  .mintToken({
    amount: 1n,
    name: "My NFT"
  })
  .setAdditionalRegisters({
    R4: SColl(SByte, utf8.decode("NFT Name")).toHex(),
    R5: SColl(SByte, utf8.decode("Description")).toHex(),
    R6: SColl(SByte, utf8.decode("0")).toHex(),  // Decimals
    R7: SColl(SByte, Buffer.from([1, 2])).toHex(), // Type
    R8: SColl(SByte, utf8.decode("ipfs://...")).toHex()
  });

View Full Example โ†’


๐ŸŸ  Advanced Examples โ€‹

05 - Contract Interaction โ€‹

Compile ErgoScript and interact with smart contracts.

Key Concepts:

  • ErgoScript compilation
  • Contract address derivation
  • Spending from contracts
typescript
import { compile } from "@fleet-sdk/compiler";

const script = `{
  val ownerPk = PK("9f...")
  sigmaProp(HEIGHT > 1500000L && ownerPk)
}`;

const compiled = compile(script);
const contractAddress = compiled.address;

View Full Example โ†’


06 - Multi-Sig Wallet โ€‹

Implement 2-of-3 multi-signature wallets.

Key Concepts:

  • Multi-sig ErgoScript
  • Aggregated signatures
  • Team treasury management
typescript
const multiSigScript = `{
  val alice = PK("9fAAA...")
  val bob = PK("9fBBB...")
  val charlie = PK("9fCCC...")
  
  sigmaProp(atLeast(2, Coll(alice, bob, charlie)))
}`;

View Full Example โ†’


07 - Oracle Data Fetch โ€‹

Read price data from on-chain oracles.

Key Concepts:

  • Oracle pool mechanics
  • Register decoding
  • Price feed integration
typescript
// Oracle boxes store price in R4
const priceData = oracleBox.additionalRegisters.R4;
const decodedPrice = decode(priceData);

View Full Example โ†’


๐Ÿ”ด Expert Examples โ€‹

08 - DeFi Swap โ€‹

Interact with AMM DEXes for token swaps.

Key Concepts:

  • Constant product formula
  • Slippage calculation
  • Swap transaction structure
typescript
// x * y = k (constant product)
function calculateOutput(
  inputAmount: bigint,
  inputReserve: bigint,
  outputReserve: bigint,
  feePercent: bigint = 3n
): bigint {
  const inputWithFee = inputAmount * (1000n - feePercent);
  const numerator = inputWithFee * outputReserve;
  const denominator = inputReserve * 1000n + inputWithFee;
  return numerator / denominator;
}

View Full Example โ†’


Running Examples โ€‹

Prerequisites โ€‹

bash
# Node.js 18+
node --version

# pnpm (recommended)
npm install -g pnpm

Quick Start โ€‹

bash
# Clone and install
git clone <repo>
cd fleet-sdk-tutorial
pnpm install

# Run an example
pnpm tsx examples/01-basic-transfer.ts

# Run with watch mode (auto-reload)
pnpm tsx watch examples/01-basic-transfer.ts

Testnet Usage โ€‹

All examples are configured for testnet. Get testnet ERG from:


Common Issues โ€‹

Having trouble? Check our Troubleshooting Guide for solutions to common problems.

Released under the MIT License.