The Dynamic SDK Web3Dart is a dart package that provides a few methods for interacting with the Ethereum blockchain. It connects to an Ethereum through web3dart node to send transactions, interact with smart contracts and much more!

Installation

Simply run the following in your terminal:

flutter pub add dynamic_sdk_web3dart

This will add a line like this to your package’s pubspec.yaml (and run an implicit flutter pub get):

dependencies:
  dynamic_sdk_web3dart: ^0.0.1-alpha.2

Main Features

1. Sign Message

Future<String> signMessage({
  required BaseWallet wallet,
  required String message,
}) async {
  
  final requestChannel = RequestChannel(
    DynamicSDK.instance.messageTransport,
  );
  
  final signedMessage = await DynamicCredential.fromWallet(
    requestChannel: requestChannel,
    wallet: wallet,
  ).signMessage(
    payload: Uint8List.fromList(
      message.codeUnits,
    ),
  );

  return signedMessage;
}

2. Send Transaction

Future<String> web3dartSendTransaction({
  required BaseWallet wallet,
  required String recipientAddress,
  required String amount,
}) async {
  final recipient = EthereumAddress.fromHex(
    recipientAddress,
  );

  final network = await DynamicSDK.instance.wallets.getNetwork(
    wallet: wallet,
  );

  final service = DynamicRpcService(
    requestChannel: RequestChannel(
      DynamicSDK.instance.messageTransport,
    ),
    chainId: network.intValue() ?? 0,
  );

  final client = Web3Client.custom(service);

  final gasPrice = await client.getGasPrice();

  final maxFeePerGas =
      gasPrice.getValueInUnitBI(EtherUnit.wei) * BigInt.from(2);

  final maxPriorityFeePerGas = gasPrice.getValueInUnitBI(EtherUnit.wei);

  final amountInWei =
      (double.parse(amount) * BigInt.from(10).pow(18).toDouble()).toInt();

  final transaction = Transaction(
    to: recipient,
    maxGas: 21000,
    gasPrice: gasPrice,
    maxFeePerGas: EtherAmount.inWei(
      maxFeePerGas,
    ),
    maxPriorityFeePerGas: EtherAmount.inWei(
      maxPriorityFeePerGas,
    ),
    value: EtherAmount.inWei(
      BigInt.from(amountInWei),
    ),
  );

  final requestChannel = RequestChannel(
    DynamicSDK.instance.messageTransport,
  );

  final dynamicCredential = DynamicCredential.fromWallet(
    requestChannel: requestChannel,
    wallet: wallet,
  );

  final signedTransaction = await dynamicCredential.sendTransaction(
    transaction,
  );

  return signedTransaction;
}

3. Sign Contract

Future<String> writeContract({
  required BaseWallet wallet,
  required String message,
}) async {
  final requestChannel = RequestChannel(
    DynamicSDK.instance.messageTransport,
  );

  final dynamicCredential = DynamicCredential.fromWallet(
    requestChannel: requestChannel,
    wallet: wallet,
  );

  final network = await DynamicSDK.instance.wallets.getNetwork(
    wallet: wallet,
  );

  final service = DynamicRpcService(
    requestChannel: RequestChannel(
      DynamicSDK.instance.messageTransport,
    ),
    chainId: network.intValue() ?? 0,
  );

  final client = Web3Client.custom(service);

  final gasPrice = await client.getGasPrice();

  final maxFeePerGas =
      gasPrice.getValueInUnitBI(EtherUnit.wei) * BigInt.from(2);

  final maxPriorityFeePerGas =
      gasPrice.getValueInUnitBI(EtherUnit.wei) * BigInt.from(2);

  final contract = DeployedContract(
    ContractAbi.fromJson(TestContract.contractAbi, ''),
    TestContract.deployedAddress,
  );

  final updateFunction = contract.function('update');

  final transaction = Transaction.callContract(
    contract: contract,
    maxFeePerGas: EtherAmount.inWei(
      maxFeePerGas,
    ),
    maxPriorityFeePerGas: EtherAmount.inWei(
      maxPriorityFeePerGas,
    ),
    function: updateFunction,
    parameters: [
      message,
    ],
  );

  final signedContract = await dynamicCredential.sendTransaction(
    transaction,
  );

  return signedContract;
}

You can read more about our client package here.