Skip to content

Interacting with a Node

The JsonRPCProvider provides a minimal JSON-RPC interface for interacting with supported EVM networks, specifically for smart account-related cases. It allows you to estimate gas costs, retrieve block information, and get gas prices.

If you require the full features of a JSON-RPC client, you can use the Web3Client class from the web3dart package instead.

Setting up the JsonRPCProvider

To start using the JsonRPCProvider, you need to create an instance of it by providing the necessary chain configuration. Here's an example:

import 'package:variance_dart/variance_dart.dart';
 
final chain = Chains.getChain(Network.ethereum);
final jsonRpcProvider = JsonRPCProvider(chain); 

Properties

The JsonRPCProvider class has the following property:

  • rpc: The remote procedure call (RPC) client used to communicate with the blockchain. It is an instance of RPCBase.

Methods

estimateGas

The estimateGas method allows you to estimate the gas cost for a transaction before sending it. Here's an example:

import 'package:variance_dart/variance_dart.dart';
 
final recipient = '0x1234567890123456789012345678901234567890';
final amount = EtherAmount.fromUnitAndValue(EtherUnit.wei, 1);
 
try {
  final gasEstimate = await jsonRpcProvider.estimateGas(smartAccountAddress, Contract.execute(smartAccountAddress, to: recipient, amount: amount)); 
  print('Estimated gas cost: $gasEstimate');
} catch (e) {
  print('Error estimating gas cost: $e');
}

getBlockNumber

The getBlockNumber method allows you to retrieve the current block number of the EVM network coresponding to the configured chain. Here's an example:

try {
  final blockNumber = await jsonRpcProvider.getBlockNumber(); 
  print('Current block number: $blockNumber');
} catch (e) {
  print('Error retrieving block number: $e');
}

getBlockInformation

The getBlockInformation method allows you to retrieve information about a specific block. Here's an example:

try {
  final blockInfo = await jsonRpcProvider.getBlockInformation(blockNumber: 'latest', isContainFullObj: true); 
  print('Block information: $blockInfo');
} catch (e) {
  print('Error retrieving block information: $e');
}

In this example, we call the getBlockInformation method, specifying the block number (in this case, 'latest') and whether to include full transaction objects (isContainFullObj).

getGasPrice

The getGasPrice method allows you to retrieve the current gas prices. It tries to retrieve the gas prices according to the EIP-1559 specification first, and if that fails, it falls back to retrieving the legacy gas price. Here's an example:

try {
  final gasPrices = await jsonRpcProvider.getGasPrice(); 
  print('Max fee per gas: ${gasPrices['maxFeePerGas']}');
  print('Max priority fee per gas: ${gasPrices['maxPriorityFeePerGas']}');
} catch (e) {
  print('Error retrieving gas prices: $e');
}

You can also specify the gas rate in this case it can be low, normal or high. by passing the rate as an argument to the getGasPrice method. Here's an example:

final gasPrices = await jsonRpcProvider.getGasPrice(GasEstimation.normal); 
print('Max fee per gas: ${gasPrices['maxFeePerGas']}');
print('Max priority fee per gas: ${gasPrices['maxPriorityFeePerGas']}');

If you require the full features of a JSON-RPC client, you can consider using the Web3Client class from the web3dart package instead which contains all the methods of the eth_rpc.