2023-01-26 18:08:12 +00:00
|
|
|
import 'dart:math';
|
2023-01-20 17:24:19 +00:00
|
|
|
|
2023-01-25 16:08:27 +00:00
|
|
|
import 'package:bip32/bip32.dart' as bip32;
|
|
|
|
import 'package:bip39/bip39.dart' as bip39;
|
|
|
|
import "package:hex/hex.dart";
|
2023-01-20 17:24:19 +00:00
|
|
|
|
2023-01-27 12:32:05 +00:00
|
|
|
class AddressTransaction {
|
2023-01-20 17:24:19 +00:00
|
|
|
final String message;
|
|
|
|
final List<dynamic> result;
|
|
|
|
final String status;
|
|
|
|
|
2023-01-27 12:32:05 +00:00
|
|
|
const AddressTransaction({
|
2023-01-20 17:24:19 +00:00
|
|
|
required this.message,
|
|
|
|
required this.result,
|
|
|
|
required this.status,
|
|
|
|
});
|
|
|
|
|
2023-01-27 12:32:05 +00:00
|
|
|
factory AddressTransaction.fromJson(Map<String, dynamic> json) {
|
|
|
|
return AddressTransaction(
|
2023-01-20 17:24:19 +00:00
|
|
|
message: json['message'] as String,
|
|
|
|
result: json['result'] as List<dynamic>,
|
|
|
|
status: json['status'] as String,
|
|
|
|
);
|
|
|
|
}
|
2023-02-14 17:43:48 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
String toString() {
|
|
|
|
return "AddressTransaction: {"
|
|
|
|
"\n\t message: $message,"
|
|
|
|
"\n\t status: $status,"
|
|
|
|
"\n\t result: $result,"
|
|
|
|
"\n}";
|
|
|
|
}
|
2023-01-20 17:24:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-26 18:08:12 +00:00
|
|
|
class GasTracker {
|
2023-01-27 12:32:05 +00:00
|
|
|
final double average;
|
|
|
|
final double fast;
|
|
|
|
final double slow;
|
|
|
|
// final Map<String, dynamic> data;
|
2023-01-26 18:08:12 +00:00
|
|
|
|
|
|
|
const GasTracker({
|
2023-01-27 12:32:05 +00:00
|
|
|
required this.average,
|
|
|
|
required this.fast,
|
|
|
|
required this.slow,
|
2023-01-26 18:08:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
factory GasTracker.fromJson(Map<String, dynamic> json) {
|
|
|
|
return GasTracker(
|
2023-01-27 12:32:05 +00:00
|
|
|
average: json['average'] as double,
|
|
|
|
fast: json['fast'] as double,
|
|
|
|
slow: json['slow'] as double,
|
2023-01-26 18:08:12 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-14 17:43:48 +00:00
|
|
|
const hdPathEthereum = "m/44'/60'/0'/0";
|
2023-01-25 16:08:27 +00:00
|
|
|
|
2023-02-14 17:43:48 +00:00
|
|
|
String getPrivateKey(String mnemonic, String mnemonicPassphrase) {
|
2023-01-25 16:08:27 +00:00
|
|
|
final isValidMnemonic = bip39.validateMnemonic(mnemonic);
|
|
|
|
if (!isValidMnemonic) {
|
|
|
|
throw 'Invalid mnemonic';
|
2023-01-20 17:24:19 +00:00
|
|
|
}
|
2023-01-25 16:08:27 +00:00
|
|
|
|
2023-02-14 17:43:48 +00:00
|
|
|
final seed = bip39.mnemonicToSeed(mnemonic, passphrase: mnemonicPassphrase);
|
2023-01-25 16:08:27 +00:00
|
|
|
final root = bip32.BIP32.fromSeed(seed);
|
|
|
|
const index = 0;
|
2023-02-14 17:43:48 +00:00
|
|
|
final addressAtIndex = root.derivePath("$hdPathEthereum/$index");
|
2023-01-25 16:08:27 +00:00
|
|
|
|
|
|
|
return HEX.encode(addressAtIndex.privateKey as List<int>);
|
2023-01-20 17:24:19 +00:00
|
|
|
}
|
2023-01-26 18:08:12 +00:00
|
|
|
|
|
|
|
double estimateFee(int feeRate, int gasLimit, int decimals) {
|
|
|
|
final gweiAmount = feeRate / (pow(10, 9));
|
|
|
|
final fee = gasLimit * gweiAmount;
|
|
|
|
|
|
|
|
//Convert gwei to ETH
|
|
|
|
final feeInWei = fee * (pow(10, 9));
|
|
|
|
final ethAmount = feeInWei / (pow(10, decimals));
|
|
|
|
return ethAmount;
|
|
|
|
}
|
|
|
|
|
|
|
|
BigInt amountToBigInt(num amount, int decimal) {
|
|
|
|
final amountToSendinDecimal = amount * (pow(10, decimal));
|
|
|
|
return BigInt.from(amountToSendinDecimal);
|
|
|
|
}
|