flutter pub add dynamic_sdk_web3dart
dependencies:
  dynamic_sdk_web3dart: 1.0.0
Future<String> signMessage({
  required BaseWallet wallet,
  required String message,
}) async {
  final signedMessage = await DynamicSDK.instance.wallets.signMessage(
    wallet: wallet,
    message: message,
  );
  return signedMessage;
}
Future<String> sendTransaction({
  required BaseWallet wallet,
  required String recipientAddress,
  required String amount,
}) async {
  final to = recipientAddress.trim();
  if (to.isEmpty) throw Exception('Recipient address is required');
  
  final amountStr = amount.trim();
  if (amountStr.isEmpty) throw Exception('Amount is required');
  final amountInWei =
      (double.parse(amountStr) * BigInt.from(10).pow(18).toDouble())
          .toInt();
  final tx = Transaction(
    from: EthereumAddress.fromHex(wallet.address),
    to: EthereumAddress.fromHex(to),
    value: EtherAmount.inWei(BigInt.from(amountInWei)),
  );
  final txHash = await DynamicSDK.instance.web3dart.sendTransaction(
    transaction: tx,
    wallet: wallet,
  );
  return txHash;
}
Future<String> interactWithContract({
  required BaseWallet wallet,
  required String message,
}) async {
  final network = await DynamicSDK.instance.wallets.getNetwork(
    wallet: wallet,
  );
  final client = DynamicSDK.instance.web3dart.createPublicClient(
    chainId: network.intValue()!,
  );
  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(
      '[{"constant":false,"inputs":[{"name":"newMessage","type":"string"}],"name":"update","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"message","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"initMessage","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]',
      '',
    ),
    EthereumAddress.fromHex('0x8b211dfebf490a648f6de859dfbed61fa22f35e0'),
  );
  final updateFunction = contract.function('update');
  final transaction = Transaction.callContract(
    contract: contract,
    maxFeePerGas: EtherAmount.inWei(maxFeePerGas),
    maxPriorityFeePerGas: EtherAmount.inWei(maxPriorityFeePerGas),
    function: updateFunction,
    parameters: [message],
  );
  final transactionHash = await DynamicSDK.instance.web3dart
      .sendTransaction(transaction: transaction, wallet: wallet);
  return transactionHash;
}
Was this page helpful?