Function Signature
Description
Execute a token swap on supported chains. This method handles the entire swap process including finding the best route and executing the transaction.Parameters
Returns
PortalSwapResult - An object containing:
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Execute a token swap.
Future<PortalSwapResult> swap({
required String swapsApiKey,
required String chainId,
required String buyToken,
required String sellToken,
required String amount,
})
| Parameter | Type | Required | Description |
|---|---|---|---|
swapsApiKey | String | Yes | Your swaps API key |
chainId | String | Yes | The chain ID in CAIP-2 format |
buyToken | String | Yes | Token address to buy |
sellToken | String | Yes | Token address to sell |
amount | String | Yes | Amount to sell (in smallest unit) |
PortalSwapResult - An object containing:
| Property | Type | Description |
|---|---|---|
success | bool | Whether the swap was successful |
transactionHash | String? | The transaction hash |
import 'package:portal_flutter/portal_flutter.dart';
final portal = Portal();
final result = await portal.swap(
swapsApiKey: 'YOUR_SWAPS_API_KEY',
chainId: 'eip155:8453', // Base
sellToken: '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE', // ETH
buyToken: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913', // USDC
amount: '1000000000000000', // 0.001 ETH in wei
);
if (result.success) {
print('Swap successful: ${result.transactionHash}');
}
0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE
| Code | Description |
|---|---|
NOT_INITIALIZED | Portal was not initialized |
SWAP_FAILED | The swap operation failed |
INSUFFICIENT_BALANCE | Not enough tokens to swap |
INVALID_API_KEY | The swaps API key is invalid |
Was this page helpful?