Blog
    Guides6 min read

    Credit Swaps: Building Multi-Currency Economies

    How to use credit swaps to enable users to exchange between different credit types, creating rich in-app economies.

    By cred.diy Team

    Credit Swaps: Building Multi-Currency Economies

    Most applications start with a single credit type. But as your platform grows, you may need multiple currencies – compute credits, storage credits, premium tokens. Credit swaps let users exchange between these types, creating a dynamic internal economy.

    Why Multiple Credit Types?

    Use Cases

    • AI Platform: Separate credits for text generation, image creation, and embeddings
    • Gaming: Gold coins, gems, and energy tokens with different acquisition paths
    • Marketplace: Seller credits, buyer credits, and promotional tokens
    • SaaS: API credits, storage credits, and support tokens

    Benefits

    1. Granular pricing – charge differently for different resource types
    2. Promotional flexibility – give free credits for one type without affecting others
    3. Revenue optimization – premium credit types can have higher margins
    4. User choice – let users allocate resources to what matters to them

    Setting Up Swap Pairs

    Create Your Credit Types

    First, define the credit types you'll support:

    const creditTypes = [
      { name: "Compute Credits", symbol: "CC", exchange_rate: 0.01 },
      { name: "Storage Credits", symbol: "SC", exchange_rate: 0.005 },
      { name: "Premium Tokens", symbol: "PT", exchange_rate: 0.05 }
    ];
    

    Configure Swap Pairs

    Create swap pairs in Dashboard → Swaps: pick the source and target credit types, the exchange rate, an optional fee, minimum and maximum amounts, and whether the pair works in both directions. Your app then discovers them through the API:

    const listSwapPairs = async (sourceType) => {
      const response = await fetch(
        `https://eutdgemlrpvnxfkkpvlu.supabase.co/functions/v1/credits-api/swap-pairs?source_credit_type_id=${sourceType}`,
        {
          headers: { 'x-api-key': process.env.CRED_API_KEY }
        }
      );
      const { swap_pairs } = await response.json();
      return swap_pairs;
      // [{ id, exchange_rate, fee_percent, min_swap_amount, max_swap_amount, is_bidirectional, ... }]
    };
    

    Exchange Rate Types

    TypeDescriptionBest For
    FixedConstant rate (e.g., 1:2)Stable, predictable economies
    DynamicRates adjust based on supply/demandGaming, marketplaces

    Executing Swaps

    Basic Swap

    const executeSwap = async (userId, swapPairId, sourceAmount) => {
      const response = await fetch(
        'https://eutdgemlrpvnxfkkpvlu.supabase.co/functions/v1/credits-api/swap',
        {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
            'x-api-key': process.env.CRED_API_KEY
          },
          body: JSON.stringify({
            user_id: userId,
            swap_pair_id: swapPairId,
            source_amount: sourceAmount
          })
        }
      );
      return response.json();
      // { success: true, source_amount: 100, target_amount: 195, exchange_rate: 2, fee_amount: 5, ... }
    };;
    

    Preview Swap (Get Quote)

    Let users see what they'll receive before confirming:

    // No API call needed: the pair from GET /swap-pairs has everything
    const previewSwap = (pair, sourceAmount) => {
      const gross = sourceAmount * pair.exchange_rate;
      const fee = gross * (pair.fee_percent / 100);
      return { you_receive: gross - fee, fee_amount: fee };
      // previewSwap({ exchange_rate: 2, fee_percent: 2.5 }, 100)
      // → { you_receive: 195, fee_amount: 5 }
    };;
    

    Designing Your Economy

    Setting Exchange Rates

    Consider the real cost behind each credit type:

    Compute Credits: $0.01 each (backed by GPU costs)
    Storage Credits: $0.005 each (backed by disk costs)
    Exchange rate: 1 CC = 2 SC (reflecting cost ratio)
    

    Swap Fees

    Fees serve multiple purposes:

    • Revenue generation: Earn on every swap transaction
    • Economy stabilization: Discourage excessive arbitrage
    • Behavioral nudging: Higher fees on premium→basic swaps

    Recommended Fee Ranges

    Swap DirectionFeeReasoning
    Basic → Basic1-3%Encourage flexibility
    Basic → Premium3-5%Moderate friction
    Premium → Basic5-10%Discourage downgrading

    Building a Swap UI

    Key Components

    1. Source selector: Choose which credits to swap from
    2. Amount input: How many credits to swap
    3. Rate display: Show the current exchange rate
    4. Fee breakdown: Transparent fee calculation
    5. Target preview: Show expected received amount
    6. Confirm button: Execute the swap

    UX Best Practices

    • Show real-time balance updates
    • Display the exchange rate prominently
    • Warn when swapping will deplete a balance
    • Add a confirmation step for large swaps
    • Show transaction history for past swaps

    Monitoring Your Economy

    Track these metrics to keep your economy healthy:

    1. Swap volume: Total swaps per day/week
    2. Popular pairs: Which swaps happen most
    3. Fee revenue: Income from swap fees
    4. Balance distribution: How users allocate across types
    5. Arbitrage detection: Unusual circular swap patterns

    Conclusion

    Credit swaps transform a simple credit system into a rich, flexible economy. By letting users exchange between credit types, you create a more engaging platform while generating additional revenue through swap fees.

    Set up your first swap pair in the dashboard.

    Try it on a real project.