flutter pub add dynamic_sdk_web3dart
dependencies:
dynamic_sdk_web3dart: ^0.0.1-alpha.2
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;
}
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;
}
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;
}
Was this page helpful?