Skip to content

Latest commit

 

History

History
40 lines (29 loc) · 1.54 KB

File metadata and controls

40 lines (29 loc) · 1.54 KB

Swap (Exchange) Feature

Swap Preview

The Swap feature allows users to instantly exchange between the internal Token and supported Cryptocurrencies, or between different Cryptocurrencies.

💱 Mechanics

Supported Pairs

  1. Token to Coin: Convert earnings to crypto (e.g., Token -> BTC).
  2. Coin to Token: Convert crypto deposits to internal tokens (e.g., for purchasing Mining Plans).
  3. Coin to Coin: Exchange between distinct cryptocurrencies (e.g., BTC -> LTC).

Pricing & Fees

  • Rate Source: Real-time prices fetched via PriceService (CoinGecko/Binance API) and stored in coins table.
  • Fee: Fixed 2.0% protocol fee on all swaps.
  • Minimum: Transactions must have a value of at least $1.00 USD.

Calculation Formula

ValueUSD = Amount * SourcePrice
FeeUSD = ValueUSD * 0.02
NetValueUSD = ValueUSD - FeeUSD
TargetAmount = NetValueUSD / TargetPrice

🔒 Security

  1. Atomic Swaps:

    • Uses a single database transaction to Deduct Source and Credit Target.
    • Ensures funds are never lost in transit; if one step fails, the entire swap rolls back.
  2. Validation:

    • Checks: isActive and isSwapEnabled flags on coins.
    • Balance checks using FOR UPDATE (implicit in scalar updates) or pre-checks.

🛠️ Implementation

  • Action: app/(protected)/swap/actions.ts -> performSwap.
  • Models: swaps (History), wallets, coins.