Skip to content

Flutter / Dart Integration

How to call Comet Engine from a Flutter/Dart app (e.g. FusionFy).

API Client

dart
import 'dart:convert';
import 'package:http/http.dart' as http;

class CometClient {
  final String baseUrl;
  final String apiKey;
  final String apiSecret;

  CometClient({
    required this.baseUrl,
    required this.apiKey,
    required this.apiSecret,
  });

  Map<String, String> get _headers => {
    'Content-Type': 'application/json',
    'X-API-Key': apiKey,
    'X-API-Secret': apiSecret,
  };

  Future<Map<String, dynamic>> _get(String path, [Map<String, String>? params]) async {
    final uri = Uri.parse('$baseUrl$path').replace(queryParameters: params);
    final resp = await http.get(uri, headers: _headers);
    return jsonDecode(resp.body);
  }

  Future<Map<String, dynamic>> _post(String path, Map<String, dynamic> body) async {
    final resp = await http.post(
      Uri.parse('$baseUrl$path'),
      headers: _headers,
      body: jsonEncode(body),
    );
    return jsonDecode(resp.body);
  }

  // Wallets (tenant is inferred from API key)
  Future<Map<String, dynamic>> createWallet(int userId) =>
    _post('/api/v1/wallets/create', {'userId': userId});

  Future<Map<String, dynamic>> getBalance(int userId) =>
    _get('/api/v1/wallets/balance', {'userId': userId.toString()});

  // Assets
  Future<Map<String, dynamic>> mintTokens({
    required String symbol,
    required int userId,
    required String amountBase,
    required String externalId,
  }) => _post('/api/v1/assets/${symbol.toLowerCase()}/mint', {
    'userId': userId,
    'amountBase': amountBase,
    'externalId': externalId,
  });

  // AMM
  Future<Map<String, dynamic>> swapQuote(String from, String to, String amountIn) =>
    _get('/api/v1/swap/quote', {'from': from, 'to': to, 'amountIn': amountIn});

  Future<Map<String, dynamic>> swapTokens({
    required int userId,
    required String from,
    required String to,
    required String amountIn,
  }) => _post('/api/v1/swap/tokens', {
    'userId': userId,
    'from': from,
    'to': to,
    'amountIn': amountIn,
  });

  Future<Map<String, dynamic>> listPools() =>
    _get('/api/v1/swap/pools');
}

Usage

dart
final comet = CometClient(
  baseUrl: 'http://localhost:8100',
  apiKey: 'your-api-key',
  apiSecret: 'your-api-secret',
);

// Create wallet (tenant is inferred from API key)
final wallet = await comet.createWallet(12345);

// Get balance
final balance = await comet.getBalance(12345);
print('USDT: ${balance['data']['usdtBaseUnits']}');

// Swap USDT → IMC
final swap = await comet.swapTokens(
  userId: 12345,
  from: 'USDT',
  to: 'IMC',
  amountIn: '1000000',
);
print('Swap tx: ${swap['data']['txHash']}');

FusionFy Integration

In FusionFy, the swap screen detects on-chain token pairs and routes through the AMM endpoint automatically:

dart
// lib/services/wallet_service.dart
static const walletSwapAMMEndpoint = '/wallet/swap-amm';

Future<Map<String, dynamic>> swapCurrencies({
  required String fromCurrency,
  required String toCurrency,
  required int amount,
  // ...
}) async {
  // Check if this is an on-chain token pair (USDT, USDC, IMC)
  final isOnChainFrom = fromCurrency == 'USDT' || fromCurrency == 'USDC' || fromCurrency == 'IMC';
  final isOnChainTo = toCurrency == 'USDT' || toCurrency == 'USDC' || toCurrency == 'IMC';

  if (isOnChainFrom && isOnChainTo) {
    // Route through AMM
    return _post(walletSwapAMMEndpoint, {
      'currency': fromCurrency,
      'to': toCurrency,
      'amount': amount,
      // ...
    });
  }
  // Otherwise use regular swap
}

Released under the MIT License.