๐งช 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:
# Install dependencies
pnpm install
# Run any example
pnpm tsx examples/01-basic-transfer.tsExample Overview โ
| # | Example | Description | Difficulty |
|---|---|---|---|
| 01 | Basic Transfer | Send ERG to an address | ๐ข Easy |
| 02 | Multi-Output | Batch payments to multiple recipients | ๐ข Easy |
| 03 | Token Transfer | Send native tokens | ๐ก Medium |
| 04 | NFT Minting | Create EIP-4 NFTs | ๐ก Medium |
| 05 | Contract Interaction | Compile & use ErgoScript | ๐ Advanced |
| 06 | Multi-Sig Wallet | 2-of-3 signature scheme | ๐ Advanced |
| 07 | Oracle Data | Read on-chain oracle prices | ๐ Advanced |
| 08 | DeFi Swap | AMM 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
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();02 - Multi-Output Transaction โ
Send to multiple recipients in a single transaction.
Key Concepts:
- Multiple OutputBuilders
- Batch processing
- Gas efficiency
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();๐ก Token Examples โ
03 - Token Transfer โ
Send native Ergo tokens alongside ERG.
Key Concepts:
- Token handling
.addTokens()method- Token change management
const tokenOutput = new OutputBuilder(SAFE_MIN_BOX_VALUE, recipient)
.addTokens({
tokenId: "03faf2cb...",
amount: 100n
});04 - NFT Minting โ
Create EIP-4 compliant NFTs with metadata.
Key Concepts:
- EIP-4 standard
- Register encoding (R4-R9)
- NFT artwork types
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()
});๐ Advanced Examples โ
05 - Contract Interaction โ
Compile ErgoScript and interact with smart contracts.
Key Concepts:
- ErgoScript compilation
- Contract address derivation
- Spending from contracts
import { compile } from "@fleet-sdk/compiler";
const script = `{
val ownerPk = PK("9f...")
sigmaProp(HEIGHT > 1500000L && ownerPk)
}`;
const compiled = compile(script);
const contractAddress = compiled.address;06 - Multi-Sig Wallet โ
Implement 2-of-3 multi-signature wallets.
Key Concepts:
- Multi-sig ErgoScript
- Aggregated signatures
- Team treasury management
const multiSigScript = `{
val alice = PK("9fAAA...")
val bob = PK("9fBBB...")
val charlie = PK("9fCCC...")
sigmaProp(atLeast(2, Coll(alice, bob, charlie)))
}`;07 - Oracle Data Fetch โ
Read price data from on-chain oracles.
Key Concepts:
- Oracle pool mechanics
- Register decoding
- Price feed integration
// Oracle boxes store price in R4
const priceData = oracleBox.additionalRegisters.R4;
const decodedPrice = decode(priceData);๐ด Expert Examples โ
08 - DeFi Swap โ
Interact with AMM DEXes for token swaps.
Key Concepts:
- Constant product formula
- Slippage calculation
- Swap transaction structure
// 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;
}Running Examples โ
Prerequisites โ
# Node.js 18+
node --version
# pnpm (recommended)
npm install -g pnpmQuick Start โ
# 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.tsTestnet Usage โ
All examples are configured for testnet. Get testnet ERG from:
Common Issues โ
Having trouble? Check our Troubleshooting Guide for solutions to common problems.