Skip to main content

🎮 Examples & Patterns

Learn how to build real applications on Cedra through practical examples and proven patterns.

Navigation

📍 You are here: Examples & Patterns ⬅️ Previous: Transaction Guide 🏠 Back to: TypeScript SDK

📚 Learn by Building​

The best way to master the Cedra TypeScript SDK is by building real projects. These guides walk you through complete implementations:

Getting Started​

Token Development​

DeFi Applications​

  • Build a DEX - Implement a decentralized exchange with liquidity pools and swapping
  • Fee Splitter Contract - Automatically distribute payments among multiple recipients
  • Escrow System - Create secure, time-locked payments for marketplaces

Each guide includes complete TypeScript code using the SDK, explanations of key concepts, and best practices.

🔄 Common Patterns​

Connection Management​

When building applications, you'll want to reuse the same client connection throughout your app. Instead of creating multiple instances, establish one connection and share it:

// Initialize once in your app
import { Cedra, CedraConfig, Network } from "@cedra-labs/ts-sdk";

const config = new CedraConfig({ network: Network.TESTNET });
export const client = new Cedra(config);

This pattern prevents connection overhead and ensures consistent configuration across your application. For production apps, consider implementing connection pooling and retry logic.

Error Handling​

Blockchain operations can fail for various reasons - insufficient funds, network issues, or smart contract errors. Good error handling is crucial for user experience.

Instead of letting errors crash your app, anticipate common failures:

  • Check balances before transfers
  • Simulate transactions before submission
  • Provide clear error messages to users
  • Implement retry logic for network failures

The SDK provides detailed error messages that help identify issues quickly. Always wrap blockchain operations in try-catch blocks and provide fallback behavior.

Gas Optimization​

Gas costs can vary significantly based on network congestion and transaction complexity. To optimize costs:

  1. Simulate first - Always simulate to estimate costs before submission
  2. Batch operations - Combine multiple operations when possible
  3. Time your transactions - Submit during low-congestion periods
  4. Set appropriate limits - Don't overpay with excessive gas limits

Remember that simple transfers are cheap (5-10 gas units) while complex DeFi operations can use thousands.