Features
Initialization
The init
method is a fundamental feature of the Fuse Wallet Flutter SDK that enables you to setup a wallet & authenticate the user's provided credentials. When you send a request to the server using this method, it verifies the credentials and returns a JSON Web Token (JWT) if the credentials are valid. The JWT is then used to make authenticated API requests, which allows you to access the Fuse Wallet API securely.
// Generate a random Ethereum private key
final String privateKey = await Mnemonic.generatePrivateKey();
final EthPrivateKey credentials = EthPrivateKey.fromHex(privateKey);
final fuseSDK = await FuseSDK.init(credentials);
Access the wallet
// Create Wallet
final smartContractAddress = fuseSDK.wallet.getSender();
print('Smart contract wallet address: $smartContractAddress');
Sponsored Transactions
Sponsored transactions are the ability to pay for another user’s transaction fees. To do this, the Fuse operator must enable the sponsored feature in his project and deposit some funds into the paymaster contract. The SDK provides a middleware to check if the project is sponsored and the amount of funds available for sponsoring.
To use this feature, you must first initialize the SDK with the withPaymaster
parameter set to true
.
import 'package:fuse_wallet_sdk/fuse_wallet_sdk.dart';
final apiKey = 'YOUR_PUBLIC_API_KEY';
final privateKey = EthPrivateKey.fromHex('YOUR_PRIVATE_KEY');
final fuseSDK = await FuseSDK.init(apiKey, privateKey, withPaymaster: true);
Send transactions
Transfer ERC20 Token/ Native FUSE
To transfer an ERC20/FUSE with a relay, use the transferToken
method. This method relays the transaction and covers the gas fees for the user, so they don't need to worry about those fees.
You can also subscribe to events related to the token transfer to track its progress. The method takes the following parameters as inputs:
Parameter | Description |
---|---|
tokenAddress | The contract address of the ERC20 token or FUSE |
recipientAddress | The recipient's wallet address |
amount | The amount to transfer |
final res = await fuseSDK.transferToken(
EthereumAddress.fromHex('TOKEN_ADDRESS'), // For sending native token, use '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE'
EthereumAddress.fromHex('RECIPIENT_ADDRESS'),
BigInt.parse('AMOUNT_IN_WEI'),
);
print('UserOpHash: ${res.userOpHash}');
print('Waiting for transaction...');
final ev = await res.wait();
Non-Blocking transactions
// Approve and transfer
final res = await fuseSDK.executeBatch(
[
Call(
to: EthereumAddress.fromHex('TOKEN_ADDRESS'),
value: BigInt.zero,
data: ContractsUtils.encodeERC20ApproveCall(
EthereumAddress.fromHex('TOKEN_ADDRESS'),
EthereumAddress.fromHex('SPENDER_ADDRESS'),
BigInt.parse('AMOUNT_IN_WEI'),
),
),
Call(
to: EthereumAddress.fromHex('TOKEN_ADDRESS'),
value: BigInt.zero,
data: ContractsUtils.encodeERC20TransferCall(
EthereumAddress.fromHex('TOKEN_ADDRESS'),
EthereumAddress.fromHex('RECIPIENT_ADDRESS'),
BigInt.parse('AMOUNT_IN_WEI'),
),
),
],
FuseSDK.defaultTxOptions.copyWith(
useNonceSequence: true,
),
);
// Transfer
await fuseSDK.transferToken(
EthereumAddress.fromHex(Variables.NATIVE_TOKEN_ADDRESS),
EthereumAddress.fromHex('RECIPIENT_ADDRESS'),
BigInt.parse('AMOUNT_IN_WEI'),
FuseSDK.defaultTxOptions.copyWith(
useNonceSequence: true,
),
);
// Transfer
await fuseSDK.transferToken(
EthereumAddress.fromHex(Variables.NATIVE_TOKEN_ADDRESS),
EthereumAddress.fromHex('RECIPIENT_ADDRESS'),
BigInt.parse('AMOUNT_IN_WEI'),
FuseSDK.defaultTxOptions.copyWith(
useNonceSequence: true,
),
);
Transfer NFT
To transfer an ERC721 (NFT) asset, use the transferNFT
method. The method takes the following parameters as inputs:
Parameter | Description |
---|---|
nftContractAddress | The contract address of the ERC721 asset |
recipientAddress | The recipient's wallet address |
tokenId | The ID of the asset you wish to transfer |
final tokenId = 1;
final res = await fuseSDK.transferNFT(
EthereumAddress.fromHex('NFT_CONTRACT_ADDRESS'),
EthereumAddress.fromHex('RECIPIENT_ADDRESS'),
tokenId,
);
print('UserOpHash: ${res.userOpHash}');
print('Waiting for transaction...');
final ev = await res.wait();
print('Transaction hash: ${ev?.transactionHash}');
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:
Parameter | Description |
---|---|
currencyIn | The contract address of the token to be swapped. |
currencyOut | The contract address of the token to swap for. |
amountIn | The amount of currencyIn to swap. |
recipient | The 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}');
Data Features
Get the list of tokens owned by address
The getTokenList
function allows you to get the list of tokens owned by a specific address. You can use this function to retrieve information such as the token name, symbol, and balance. The function returns a TokenList
object that contains a list of Token
objects, each of which represents a token owned by the specified address.
final String address = 'ADDRESS';
final tokenListData = await fuseSDK.explorerModule.getTokenList(
address,
);
tokenListData.pick(
onData: (TokenList tokenList) {
// Do you magic here
},
onError: (err) {
// Handle errors
},
);
Get the historical transactions
final walletActionsResult = await fuseSDK.getWalletActions(page: 1, limit: 50);
Get token details
The getTokenDetails
function allows you to retrieve detailed information about a specific ERC20 token. This includes the token's name, symbol, decimal places, and total supply. To use the getTokenDetails
function, you need to provide the contract address of the ERC20 token that you want to retrieve information from. Once you call the function, it will return a TokenDetails
object that contains all the relevant information about the token.
final String tokenAddress = 'TOKEN_ADDRESS';
final tokenDetailsData = await fuseSDK.explorerModule.getTokenDetails(
tokenAddress,
);
tokenDetailsData.pick(
onData: (TokenDetails tokenList) {
// Do you magic here
},
onError: (err) {
// Handle errors
},
);
Get Smart Wallet’s token balance
The getTokenBalance
function allows you to retrieve the balance of a specific ERC20 token owned by a Fuse wallet. You will need to provide the contract address of the token and the address of the Fuse wallet. The function returns the balance as a BigInt
object, which you can use to perform further calculations or display the balance to the user.
final String tokenAddress = 'TOKEN_ADDRESS';
final String smartWalletAddress = fuseSDK.wallet.getSender();
final tokenBalanceData = await fuseSDK.explorerModule.getTokenBalance(
tokenAddress,
smartWalletAddress,
);
tokenBalanceData.pick(
onData: (BigInt value) {
// Do you magic here
},
onError: (err) {
// Handle errors
},
);
Staking Features
Get staking options
final stakingOptionsData = await fuseSDK.stakingModule.getStakingOptions();
stakingOptionsData.pick(
onData: (List<StakingOption> data) {
// Do you magic here
},
onError: (Exception err) {
// Handle errors
}
);
Stake
final res = await fuseSDK.stakeToken(
StakeRequestBody(
accountAddress: fuseSDK.wallet.getSender(),
tokenAmount: 'AMOUNT', // "0.01"
tokenAddress: 'TOKEN_ADDRESS', // "0x34Ef2Cc892a88415e9f02b91BfA9c91fC0bE6bD4"
),
);
print('UserOpHash: ${res.userOpHash}');
print('Waiting for transaction...');
final ev = await res.wait();
print('Transaction hash: ${ev?.transactionHash}');
Unstake
final res = await fuseSDK.unstakeToken(
UnstakeRequestBody(
accountAddress: fuseSDK.wallet.getSender(),
tokenAmount: '0.1',
tokenAddress: 'TOKEN_ADDRESS', // "0x34Ef2Cc892a88415e9f02b91BfA9c91fC0bE6bD4"
),
EthereumAddress.fromHex('0xb1DD0B683d9A56525cC096fbF5eec6E60FE79871'),
);
print('UserOpHash: ${res.userOpHash}');
print('Waiting for transaction...');
final ev = await res.wait();
print('Transaction hash: ${ev?.transactionHash}');
Get staked tokens
final smartWalletAddress = fuseSDK.wallet.getSender();
final stakedTokensData = await fuseSDK.stakingModule.getStakedTokens(smartWalletAddress);
stakedTokensData.pick(
onData: (StakedTokenResponse data) {
// Do you magic here
},
onError: (err) {
// Handle errors
}
);
Trade Features
Get token price
final tokenAddress = 'TOKEN_ADDRESS';
final priceData = await fuseSDK.tradeModule.price(tokenAddress);
priceData.pick(
onData: (String tokenPrice) {
// Do you magic here
},
onError: (err) {
// Handle errors
},
);
Get token price change in the last 24 hours
final tokenAddress = 'TOKEN_ADDRESS';
final priceChangeData = await fuseSDK.tradeModule.priceChange(tokenAddress);
priceChangeData.pick(
onData: (String priceInfo) {
// Do you magic here
},
onError: (err) {
// Handle errors
},
);
Get price change in interval
final tokenAddress = 'TOKEN_ADDRESS';
final intervalData = await fuseSDK.tradeModule.interval(tokenAddress, TimeFrame.day);
intervalData.pick(
onData: (List<IntervalStats> stats) {
// Do you magic here
},
onError: (err) {
// Handle errors
},
);
Get the list of supported tokens
final tokensData = await fuseSDK.tradeModule.fetchTokens();
tokensData.pick(
onData: (List<TokenDetails> tokens) {
// Do you magic here
},
onError: (err) {
// Handle errors
},
);
Code examples
For more code examples on using the Fuse Wallet SDK for Flutter, you can check out the official Flutter SDK repository.