2023-01-04 14:51:23 +00:00
|
|
|
import 'package:cw_core/node.dart';
|
2023-01-11 02:57:59 +00:00
|
|
|
import 'package:cw_ethereum/ethereum_transaction_priority.dart';
|
2023-01-04 14:51:23 +00:00
|
|
|
import 'package:http/http.dart';
|
|
|
|
import 'package:web3dart/web3dart.dart';
|
|
|
|
|
|
|
|
class EthereumClient {
|
2023-01-13 01:47:24 +00:00
|
|
|
Web3Client? _client;
|
2023-01-04 14:51:23 +00:00
|
|
|
|
|
|
|
Future<bool> connect(Node node) async {
|
|
|
|
try {
|
2023-01-13 01:47:24 +00:00
|
|
|
_client = Web3Client(node.uri.toString(), Client());
|
2023-01-04 14:51:23 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<EtherAmount> getBalance(String privateKey) async {
|
|
|
|
final private = EthPrivateKey.fromHex(privateKey);
|
|
|
|
|
2023-01-13 01:47:24 +00:00
|
|
|
return _client!.getBalance(private.address);
|
2023-01-04 14:51:23 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 02:57:59 +00:00
|
|
|
Future<int> getGasUnitPrice() async {
|
2023-01-13 01:47:24 +00:00
|
|
|
final gasPrice = await _client!.getGasPrice();
|
2023-01-11 02:57:59 +00:00
|
|
|
return gasPrice.getInWei.toInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<int>> getEstimatedGasForPriorities() async {
|
2023-01-13 01:47:24 +00:00
|
|
|
final result = await Future.wait(EthereumTransactionPriority.all.map((priority) => _client!
|
|
|
|
.estimateGas(
|
2023-01-12 13:53:20 +00:00
|
|
|
maxPriorityFeePerGas: EtherAmount.fromUnitAndValue(EtherUnit.gwei, priority.tip))));
|
2023-01-11 02:57:59 +00:00
|
|
|
|
|
|
|
return result.map((e) => e.toInt()).toList();
|
|
|
|
}
|
|
|
|
}
|