stack_wallet/lib/utilities/eth_commons.dart
2023-03-29 12:49:12 -06:00

61 lines
1.7 KiB
Dart

import 'dart:math';
import 'package:bip32/bip32.dart' as bip32;
import 'package:bip39/bip39.dart' as bip39;
import "package:hex/hex.dart";
class GasTracker {
// gwei
final int average;
final int fast;
final int slow;
const GasTracker({
required this.average,
required this.fast,
required this.slow,
});
factory GasTracker.fromJson(Map<String, dynamic> json) {
return GasTracker(
average: int.parse(json['ProposeGasPrice'] as String),
fast: int.parse(json['FastGasPrice'] as String),
slow: int.parse(json['SafeGasPrice'] as String),
);
}
}
const hdPathEthereum = "m/44'/60'/0'/0";
// equal to "0x${keccak256("Transfer(address,address,uint256)".toUint8ListFromUtf8).toHex}";
const kTransferEventSignature =
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef";
String getPrivateKey(String mnemonic, String mnemonicPassphrase) {
final isValidMnemonic = bip39.validateMnemonic(mnemonic);
if (!isValidMnemonic) {
throw 'Invalid mnemonic';
}
final seed = bip39.mnemonicToSeed(mnemonic, passphrase: mnemonicPassphrase);
final root = bip32.BIP32.fromSeed(seed);
const index = 0;
final addressAtIndex = root.derivePath("$hdPathEthereum/$index");
return HEX.encode(addressAtIndex.privateKey as List<int>);
}
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);
}