From a04223e0b7cf959f6d4b12bfe619dae17be58252 Mon Sep 17 00:00:00 2001 From: julian <julian@cypherstack.com> Date: Thu, 30 Mar 2023 14:50:58 -0600 Subject: [PATCH] eth gas tracker updated --- lib/services/ethereum/ethereum_api.dart | 67 ++++++++++++++++++------- lib/utilities/constants.dart | 2 +- lib/utilities/enums/coin_enum.dart | 1 - lib/utilities/eth_commons.dart | 31 +++++++++--- 4 files changed, 74 insertions(+), 27 deletions(-) diff --git a/lib/services/ethereum/ethereum_api.dart b/lib/services/ethereum/ethereum_api.dart index a0f134614..9c65057f1 100644 --- a/lib/services/ethereum/ethereum_api.dart +++ b/lib/services/ethereum/ethereum_api.dart @@ -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()); diff --git a/lib/utilities/constants.dart b/lib/utilities/constants.dart index 562d44047..92e99849d 100644 --- a/lib/utilities/constants.dart +++ b/lib/utilities/constants.dart @@ -159,7 +159,7 @@ abstract class Constants { return 60; case Coin.ethereum: - return 60; + return 15; case Coin.monero: return 120; diff --git a/lib/utilities/enums/coin_enum.dart b/lib/utilities/enums/coin_enum.dart index e62203628..0490174f0 100644 --- a/lib/utilities/enums/coin_enum.dart +++ b/lib/utilities/enums/coin_enum.dart @@ -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, diff --git a/lib/utilities/eth_commons.dart b/lib/utilities/eth_commons.dart index c71aa27af..5b71e917d 100644 --- a/lib/utilities/eth_commons.dart +++ b/lib/utilities/eth_commons.dart @@ -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, ); } }