transact method

Future<void> transact({
  1. required String privateKey,
  2. String? value,
  3. String? from,
  4. String? to,
})

Implementation

Future<void> transact({
  required String privateKey,
  String? value,
  String? from,
  String? to,
}) async {
  try {
    web3.EtherAmount? val = null;
    if (value != null) {
      val = web3.EtherAmount.fromBase10String(web3.EtherUnit.wei, value);
    }
    web3.EthereumAddress? fromAddress = null;
    if (from != null) {
      fromAddress = web3.EthereumAddress.fromHex(from);
    }
    web3.EthereumAddress? toAddress = null;
    if (to != null) {
      toAddress = web3.EthereumAddress.fromHex(to);
    }

    final credentials = web3.EthPrivateKey.fromHex(privateKey);
    var gasPrice = await this.client.getGasPrice();
    var chainId = await this.client.getNetworkId();

    final transaction = web3.Transaction(
        from: fromAddress, to: toAddress, value: val, gasPrice: gasPrice);

    final response = await this.client.sendTransaction(
          credentials,
          transaction,
          chainId: chainId,
        );

    print(response);
  } catch (e) {
    print(e);
  }
}