2023-04-05 16:01:20 +00:00
|
|
|
import 'dart:typed_data';
|
|
|
|
|
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';
|
2023-03-31 18:41:56 +00:00
|
|
|
import 'package:web3dart/crypto.dart';
|
2023-01-04 14:51:23 +00:00
|
|
|
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
|
|
|
|
2023-01-13 17:10:38 +00:00
|
|
|
bool connect(Node node) {
|
2023-01-04 14:51:23 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-05 16:01:20 +00:00
|
|
|
Future<EtherAmount> getBalance(EthereumAddress address) async {
|
|
|
|
return await _client!.getBalance(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 17:10:38 +00:00
|
|
|
final result = await Future.wait(EthereumTransactionPriority.all.map(
|
|
|
|
(priority) => _client!.estimateGas(
|
2023-03-31 18:41:56 +00:00
|
|
|
// maxPriorityFeePerGas: EtherAmount.fromUnitAndValue(EtherUnit.gwei, priority.tip),
|
|
|
|
// maxFeePerGas: EtherAmount.fromUnitAndValue(EtherUnit.gwei, 100),
|
|
|
|
),
|
2023-01-13 17:10:38 +00:00
|
|
|
));
|
2023-01-11 02:57:59 +00:00
|
|
|
|
|
|
|
return result.map((e) => e.toInt()).toList();
|
|
|
|
}
|
2023-03-16 17:24:21 +00:00
|
|
|
|
2023-04-05 17:20:25 +00:00
|
|
|
Future<TransactionInformation> signTransaction(
|
2023-04-05 16:01:20 +00:00
|
|
|
EthPrivateKey privateKey,
|
|
|
|
String toAddress,
|
|
|
|
String amount,
|
|
|
|
int fee,
|
|
|
|
) async {
|
2023-03-31 18:41:56 +00:00
|
|
|
final transaction = Transaction(
|
2023-04-05 16:01:20 +00:00
|
|
|
from: privateKey.address,
|
2023-03-31 18:41:56 +00:00
|
|
|
to: EthereumAddress.fromHex(toAddress),
|
2023-04-05 16:01:20 +00:00
|
|
|
value: EtherAmount.fromUnitAndValue(EtherUnit.ether, amount),
|
|
|
|
// maxPriorityFeePerGas: EtherAmount.inWei(BigInt.from(fee)),
|
2023-03-31 18:41:56 +00:00
|
|
|
);
|
|
|
|
|
2023-04-05 17:20:25 +00:00
|
|
|
final Uint8List signedTransactionRaw = await _client!.signTransaction(privateKey, transaction);
|
2023-03-31 18:41:56 +00:00
|
|
|
|
2023-04-05 17:20:25 +00:00
|
|
|
final transactionHash = bytesToHex(signedTransactionRaw);
|
|
|
|
|
|
|
|
final signedTransaction = await _client!.getTransactionByHash(transactionHash);
|
|
|
|
|
|
|
|
return signedTransaction;
|
2023-03-31 18:41:56 +00:00
|
|
|
}
|
|
|
|
|
2023-04-05 17:20:25 +00:00
|
|
|
Future<String> sendTransaction(
|
|
|
|
EthPrivateKey privateKey, TransactionInformation transactionInformation) async {
|
2023-03-16 17:24:21 +00:00
|
|
|
final transaction = Transaction(
|
2023-04-05 17:20:25 +00:00
|
|
|
from: transactionInformation.from,
|
|
|
|
to: transactionInformation.to,
|
|
|
|
value: transactionInformation.value,
|
|
|
|
gasPrice: transactionInformation.gasPrice,
|
|
|
|
data: transactionInformation.input,
|
2023-03-16 17:24:21 +00:00
|
|
|
);
|
|
|
|
|
2023-04-05 17:20:25 +00:00
|
|
|
return await _client!.sendTransaction(privateKey, transaction);
|
2023-03-16 17:24:21 +00:00
|
|
|
}
|
2023-01-11 02:57:59 +00:00
|
|
|
}
|