Skip to main content

Actions

- Listen to balance change

You can subscribe to _appKitModal.balanceNotifier to be up to date with balance.

// Example usage:
ValueListenableBuilder<String>(
valueListenable: _appKitModal.balanceNotifier,
builder: (_, balance, __) {
return Text(balance);
},
),

- Launch the current wallet

If you connected your dApp through deep linkining to a Wallet app you can launch that wallet app with the following:

_appKitModal.launchConnectedWallet();

- Launch block explorer

You can open the selected chain's block explorer easily:

_appKitModal.launchBlockExplorer();

- Send an RPC request

final bytes = utf8.encode(message);
final encodedMessage = hex.encode(bytes);

final result = await _appKitModal.request(
topic: _appKitModal.session!.topic,
chainId: _appKitModal.selectedChain!.chainId,
request: SessionRequestParams(
method: 'personal_sign',
params: [
'0x$encodedMessage',
_appKitModal.session!.address!,
],
),
);

A list of all available methods can be found in constants.dart file, which is already exported for you to use directly from AppKit package.

- List of approved chains by the connected wallet

_appKitModal.getApprovedChains();

- List of approved methods by connected wallet

_appKitModal.getApprovedMethods();

- List of approved events by the connected wallet

_appKitModal.getApprovedEvents();

- Interact with Smart Contracts

Read function:

Future<List<dynamic>> requestReadContract({
required String? topic,
required String chainId,
required DeployedContract deployedContract,
required String functionName,
EthereumAddress? sender,
List parameters = const [],
});

Usage:

  1. Create a DeployedContract object
// Create DeployedContract object using contract's ABI and address
final tetherContract = DeployedContract(
ContractAbi.fromJson(
jsonEncode([{.....}]), // ABI object
'Tether USD',
),
EthereumAddress.fromHex('0xdAC17F958D2ee523a2206206994597C13D831ec7'), // https://etherscan.io/token/0xdAC17F958D2ee523a2206206994597C13D831ec7
);
  1. Read from it by calling a read function
// Get token decimals
final decimals = await _appKitModal.requestReadContract(
topic: _appKitModal.session!.topic,
chainId: _appKitModal.selectedChain!.chainId,
deployedContract: tetherContract,
functionName: 'decimals',
);

// Get balance of wallet
final balanceOf = await _appKitModal.requestReadContract(
deployedContract: tetherContract,
topic: _appKitModal.session!.topic,
chainId: _appKitModal.selectedChain!.chainId,
functionName: 'balanceOf',
parameters: [
EthereumAddress.fromHex(_appKitModal.session!.address!),
],
);

// Get token total supply
final totalSupply = await _appKitModal.requestReadContract(
deployedContract: tetherContract,
topic: _appKitModal.session!.topic,
chainId: _appKitModal.selectedChain!.chainId,
functionName: 'totalSupply',
);

- Write function:

Future<dynamic> requestWriteContract({
required String? topic,
required String chainId,
required DeployedContract deployedContract,
required String functionName,
required Transaction transaction,
List<dynamic> parameters = const [],
String? method,
});

Usage:

Write to it by calling a write function, for example, transfer function from USDC token contract:

final decimalUnits = (decimals.first as BigInt); // decimals value from `decimals` contract function
final transferValue = _formatValue(0.23, decimals: decimalUnits); // your format value function

// Transfer USDT
Future<void> transferToken() async {
// Transfer 0.01 amount of Token using Smart Contract's transfer function
final result = await _appKitModal.requestWriteContract(
topic: _appKitModal.session!.topic,
chainId: _appKitModal.selectedChain!.chainId,
deployedContract: deployedContract,
functionName: 'transfer',
transaction: Transaction(
from: EthereumAddress.fromHex(_appKitModal.session!.address!), // sender address
),
parameters: [
EthereumAddress.fromHex('0x59e2f66C0E96803206B6486cDb39029abAE834c0'), // recipient address
transferValue, // == 0.23 USDT
],
);
}

Additional example:

Call a sayHello function of a smart contract to write a message.

// Write a message data
Future<void> writeMessage() async {
final result = await _appKitModal.requestWriteContract(
topic: _appKitModal.session!.topic,
chainId: _appKitModal.selectedChain!.chainId,
deployedContract: deployedContract,
functionName: 'sayHello',
transaction: Transaction(
from: EthereumAddress.fromHex(_appKitModal.session!.address!), // sender address
),
parameters: ['Hello world!'],
);
}

For a complete example app check out the example app for AppKit