eth gas tracker updated

This commit is contained in:
julian 2023-03-30 14:50:58 -06:00
parent bf03e6913d
commit a04223e0b7
4 changed files with 74 additions and 27 deletions

View file

@ -329,31 +329,62 @@ abstract class EthereumAPI {
}
}
static Future<GasTracker> getGasOracle() async {
final response = await get(
Uri.parse(
"https://api.etherscan.io/api?module=gastracker&action=gasoracle&apikey=EG6J7RJIQVSTP2BS59D3TY2G55YHS5F2HP",
),
);
if (response.statusCode == 200) {
final json = jsonDecode(response.body) as Map;
static Future<EthereumResponse<GasTracker>> getGasOracle() async {
try {
final response = await get(
Uri.parse(
"$stackBaseServer/gas-prices",
),
);
return GasTracker.fromJson(json["result"] as Map<String, dynamic>);
} else {
throw Exception('Failed to load gas oracle');
if (response.statusCode == 200) {
final json = jsonDecode(response.body) as Map;
if (json["success"] == true) {
return EthereumResponse(
GasTracker.fromJson(
Map<String, dynamic>.from(json["result"] as Map),
),
null,
);
} else {
throw EthApiException(
"getGasOracle() failed with response: "
"${response.body}",
);
}
} else {
throw EthApiException(
"getGasOracle() failed with status code: "
"${response.statusCode}",
);
}
} on EthApiException catch (e) {
return EthereumResponse(
null,
e,
);
} catch (e, s) {
Logging.instance.log(
"getGasOracle(): $e\n$s",
level: LogLevel.Error,
);
return EthereumResponse(
null,
EthApiException(e.toString()),
);
}
}
static Future<FeeObject> getFees() async {
GasTracker fees = await getGasOracle();
final feesFast = fees.fast * (pow(10, 9));
final feesStandard = fees.average * (pow(10, 9));
final feesSlow = fees.slow * (pow(10, 9));
final fees = (await getGasOracle()).value!;
final feesFast = fees.fast.shift(9).toBigInt();
final feesStandard = fees.average.shift(9).toBigInt();
final feesSlow = fees.slow.shift(9).toBigInt();
return FeeObject(
numberOfBlocksFast: 1,
numberOfBlocksAverage: 3,
numberOfBlocksSlow: 3,
numberOfBlocksFast: fees.numberOfBlocksFast,
numberOfBlocksAverage: fees.numberOfBlocksAverage,
numberOfBlocksSlow: fees.numberOfBlocksSlow,
fast: feesFast.toInt(),
medium: feesStandard.toInt(),
slow: feesSlow.toInt());

View file

@ -159,7 +159,7 @@ abstract class Constants {
return 60;
case Coin.ethereum:
return 60;
return 15;
case Coin.monero:
return 120;

View file

@ -17,7 +17,6 @@ import 'package:stackwallet/services/coins/particl/particl_wallet.dart'
as particl;
import 'package:stackwallet/services/coins/wownero/wownero_wallet.dart' as wow;
import 'package:stackwallet/utilities/constants.dart';
import 'dart:io' show Platform;
enum Coin {
bitcoin,

View file

@ -2,25 +2,42 @@ import 'dart:math';
import 'package:bip32/bip32.dart' as bip32;
import 'package:bip39/bip39.dart' as bip39;
import 'package:decimal/decimal.dart';
import "package:hex/hex.dart";
import 'package:stackwallet/utilities/constants.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
class GasTracker {
// gwei
final int average;
final int fast;
final int slow;
final Decimal average;
final Decimal fast;
final Decimal slow;
final int numberOfBlocksFast;
final int numberOfBlocksAverage;
final int numberOfBlocksSlow;
final int timestamp;
const GasTracker({
required this.average,
required this.fast,
required this.slow,
required this.numberOfBlocksFast,
required this.numberOfBlocksAverage,
required this.numberOfBlocksSlow,
required this.timestamp,
});
factory GasTracker.fromJson(Map<String, dynamic> json) {
final targetTime = Constants.targetBlockTimeInSeconds(Coin.ethereum);
return GasTracker(
average: int.parse(json['ProposeGasPrice'] as String),
fast: int.parse(json['FastGasPrice'] as String),
slow: int.parse(json['SafeGasPrice'] as String),
average: Decimal.parse(json["average"]["price"].toString()),
fast: Decimal.parse(json["fast"]["price"].toString()),
slow: Decimal.parse(json["slow"]["price"].toString()),
numberOfBlocksAverage: (json["average"]["time"] as int) ~/ targetTime,
numberOfBlocksFast: (json["fast"]["time"] as int) ~/ targetTime,
numberOfBlocksSlow: (json["slow"]["time"] as int) ~/ targetTime,
timestamp: json["timestamp"] as int,
);
}
}