Skip to main content

Swap Tokens

Swap Tokens

To exchange one token for another, use the swapTokens() method. This method swaps the token with the contract address specified in the currencyIn parameter for the token with the contract address specified in the currencyOut parameter at the current market price.

Additionally, this method relays the transaction and covers the gas fees for the user, so they don't need to worry about those fees.

The method requires a TradeRequestBody parameter with the following properties:

ParameterTypeDescription
currencyInaddressThe contract address of the token to be swapped.
currencyOutaddressThe contract address of the token to swap for.
amountInnumberThe amount of currencyIn to swap.
recipientaddressThe recipient address for the swapped tokens.

To ensure your transfer request is successful, subscribe to the transactionStarted, transactionHash, transactionSucceeded, and transactionFailed events of the FuseWalletSDK instance. This will allow you to monitor the transaction as it progresses through the network and handle any errors that may occur.

final TradeRequestBody tradeRequestBody = TradeRequestBody(
currencyIn: 'CURRENCY_IN',
currencyOut: 'CURRENCY_OUT',
amountIn: 'AMOUNT',
recipient: 'RECIPIENT_ADDRESS',
);

final res = await fuseSDK.swapTokens(
tradeRequestBody,
);

print('UserOpHash: ${res.userOpHash}');

print('Waiting for transaction...');
final ev = await res.wait();
print('Transaction hash: ${ev?.transactionHash}');

Code example

For more code examples on using the Fuse Wallet SDK for Flutter for Token Swap, you can check out the official Flutter SDK repository.

Was this page helpful?