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 deployedContract = DeployedContract(
ContractAbi.fromJson(
jsonEncode([{.....}]), // ABI object
'TokenName',
),
EthereumAddress.fromHex('0xaddress.......'), // Contract address
);
  1. Read from it by calling a read function
Future<void> getWalletBalance() async {
// Get balance of wallet
final result = await _appKitModal.requestReadContract(
deployedContract: deployedContract,
topic: _appKitModal.session!.topic,
chainId: _appKitModal.selectedChain!.chainId,
functionName: 'balanceOf',
parameters: [
EthereumAddress.fromHex(_appKitModal.session!.address!),
],
);
}

Future<void> getTotalSupply() async {
// Get token total supply
final result = await _appKitModal.requestReadContract(
deployedContract: deployedContract,
topic: _appKitModal.session!.topic,
chainId: _appKitModal.selectedChain!.chainId,
functionName: 'balanceOf',
);
}

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:

  1. Create a DeployedContract object (same as before)
// Create DeployedContract object using contract's ABI and address
final deployedContract = DeployedContract(
ContractAbi.fromJson(
jsonEncode([{.....}]), // ABI object
'TokenName',
),
EthereumAddress.fromHex('0xaddress.......'), // Contract address
);
  1. Write to it by calling a write function
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,
],
);
}

Future<void> writeMessage() async {
// Write a message data
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