mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2024-11-17 17:57:40 +00:00
WIP: Add transaction fees
This commit is contained in:
parent
734a51f5dd
commit
ab4392b0bd
1 changed files with 44 additions and 20 deletions
|
@ -14,6 +14,7 @@ import 'package:stackwallet/services/price.dart';
|
||||||
import 'package:stackwallet/services/transaction_notification_tracker.dart';
|
import 'package:stackwallet/services/transaction_notification_tracker.dart';
|
||||||
import 'package:stackwallet/utilities/constants.dart';
|
import 'package:stackwallet/utilities/constants.dart';
|
||||||
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
||||||
|
import 'package:stackwallet/utilities/enums/fee_rate_type_enum.dart';
|
||||||
import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart';
|
import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart';
|
||||||
import 'package:stackwallet/utilities/format.dart';
|
import 'package:stackwallet/utilities/format.dart';
|
||||||
import 'package:stackwallet/utilities/prefs.dart';
|
import 'package:stackwallet/utilities/prefs.dart';
|
||||||
|
@ -72,7 +73,7 @@ class AddressTransaction {
|
||||||
|
|
||||||
class GasTracker {
|
class GasTracker {
|
||||||
final int code;
|
final int code;
|
||||||
final String data;
|
final Map<String, dynamic> data;
|
||||||
|
|
||||||
const GasTracker({
|
const GasTracker({
|
||||||
required this.code,
|
required this.code,
|
||||||
|
@ -82,13 +83,14 @@ class GasTracker {
|
||||||
factory GasTracker.fromJson(Map<String, dynamic> json) {
|
factory GasTracker.fromJson(Map<String, dynamic> json) {
|
||||||
return GasTracker(
|
return GasTracker(
|
||||||
code: json['code'] as int,
|
code: json['code'] as int,
|
||||||
data: json['data'] as String,
|
data: json['data'] as Map<String, dynamic>,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class EthereumWallet extends CoinServiceAPI {
|
class EthereumWallet extends CoinServiceAPI {
|
||||||
NodeModel? _ethNode;
|
NodeModel? _ethNode;
|
||||||
|
final _gasLimit = 21000;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
set isFavorite(bool markFavorite) {
|
set isFavorite(bool markFavorite) {
|
||||||
|
@ -197,14 +199,17 @@ class EthereumWallet extends CoinServiceAPI {
|
||||||
final gasPrice = await _client.getGasPrice();
|
final gasPrice = await _client.getGasPrice();
|
||||||
final int chainId = await _client.getNetworkId();
|
final int chainId = await _client.getNetworkId();
|
||||||
|
|
||||||
|
print("GAS PRICE IS $gasPrice");
|
||||||
|
print("AMOUNT TO SEND IS ${txData['fee']}");
|
||||||
|
|
||||||
final amount = txData['recipientAmt'];
|
final amount = txData['recipientAmt'];
|
||||||
final decimalAmount =
|
final decimalAmount =
|
||||||
Format.satoshisToAmount(amount as int, coin: Coin.ethereum);
|
Format.satoshisToAmount(amount as int, coin: Coin.ethereum);
|
||||||
final bigIntAmount = amountToBigInt(decimalAmount.toDouble());
|
final bigIntAmount = amountToBigInt(decimalAmount.toDouble());
|
||||||
final tx = Transaction.Transaction(
|
final tx = Transaction.Transaction(
|
||||||
to: EthereumAddress.fromHex(txData['address'] as String),
|
to: EthereumAddress.fromHex(txData['address'] as String),
|
||||||
gasPrice: gasPrice,
|
gasPrice: EtherAmount.fromUnitAndValue(EtherUnit.gwei, txData['fee']),
|
||||||
maxGas: 21000,
|
maxGas: _gasLimit,
|
||||||
value: EtherAmount.inWei(bigIntAmount));
|
value: EtherAmount.inWei(bigIntAmount));
|
||||||
final transaction =
|
final transaction =
|
||||||
await _client.sendTransaction(_credentials, tx, chainId: chainId);
|
await _client.sendTransaction(_credentials, tx, chainId: chainId);
|
||||||
|
@ -228,10 +233,14 @@ class EthereumWallet extends CoinServiceAPI {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<int> estimateFeeFor(int satoshiAmount, int feeRate) async {
|
Future<int> estimateFeeFor(int satoshiAmount, int feeRate) async {
|
||||||
print("CALLING ESTIMATE FEE");
|
final gweiAmount = feeRate / (pow(10, 9));
|
||||||
// TODO: implement estimateFeeFor
|
final fee = _gasLimit * gweiAmount;
|
||||||
// throw UnimplementedError();
|
|
||||||
return 1;
|
//Convert gwei to ETH
|
||||||
|
final feeInWei = fee * (pow(10, 9));
|
||||||
|
final ethAmount = feeInWei / (pow(10, 18));
|
||||||
|
return Format.decimalAmountToSatoshis(
|
||||||
|
Decimal.parse(ethAmount.toString()), coin);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -248,16 +257,14 @@ class EthereumWallet extends CoinServiceAPI {
|
||||||
|
|
||||||
Future<FeeObject> _getFees() async {
|
Future<FeeObject> _getFees() async {
|
||||||
GasTracker fees = await getGasOracle();
|
GasTracker fees = await getGasOracle();
|
||||||
if (fees.code == 200) {
|
final feesMap = fees.data;
|
||||||
print("FEES IS ${fees.data}");
|
|
||||||
}
|
|
||||||
return FeeObject(
|
return FeeObject(
|
||||||
numberOfBlocksFast: 10,
|
numberOfBlocksFast: 3,
|
||||||
numberOfBlocksAverage: 10,
|
numberOfBlocksAverage: 3,
|
||||||
numberOfBlocksSlow: 10,
|
numberOfBlocksSlow: 1,
|
||||||
fast: 1,
|
fast: feesMap['fast'] as int,
|
||||||
medium: 1,
|
medium: feesMap['standard'] as int,
|
||||||
slow: 1);
|
slow: feesMap['slow'] as int);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<GasTracker> getGasOracle() async {
|
Future<GasTracker> getGasOracle() async {
|
||||||
|
@ -410,12 +417,29 @@ class EthereumWallet extends CoinServiceAPI {
|
||||||
{required String address,
|
{required String address,
|
||||||
required int satoshiAmount,
|
required int satoshiAmount,
|
||||||
Map<String, dynamic>? args}) async {
|
Map<String, dynamic>? args}) async {
|
||||||
|
print("CALLING PREPARE SEND");
|
||||||
|
print(args);
|
||||||
|
final feeRateType = args?["feeRate"];
|
||||||
|
int fee = 0;
|
||||||
|
final feeObject = await fees;
|
||||||
|
switch (feeRateType) {
|
||||||
|
case FeeRateType.fast:
|
||||||
|
fee = feeObject.fast;
|
||||||
|
break;
|
||||||
|
case FeeRateType.average:
|
||||||
|
fee = feeObject.medium;
|
||||||
|
break;
|
||||||
|
case FeeRateType.slow:
|
||||||
|
fee = feeObject.slow;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
final feeEstimate = await estimateFeeFor(satoshiAmount, fee);
|
||||||
|
print("FEE ESTIMATE IS $feeEstimate");
|
||||||
|
|
||||||
final gasPrice = await _client.getGasPrice();
|
final gasPrice = await _client.getGasPrice();
|
||||||
|
|
||||||
Map<String, dynamic> txData = {
|
Map<String, dynamic> txData = {
|
||||||
"fee": Format.decimalAmountToSatoshis(
|
"fee": feeEstimate,
|
||||||
Decimal.parse(gasPrice.getValueInUnit(EtherUnit.ether).toString()),
|
|
||||||
coin),
|
|
||||||
"address": address,
|
"address": address,
|
||||||
"recipientAmt": satoshiAmount,
|
"recipientAmt": satoshiAmount,
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue