From 655fe5a813dd399d2cdb3b0d5d47ef9d1160db68 Mon Sep 17 00:00:00 2001 From: NIK Date: Sat, 21 Oct 2023 20:29:28 +0800 Subject: [PATCH 1/4] checkDateParse bugfix --- lib/core/crypto/eosdart/src/serialize.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/core/crypto/eosdart/src/serialize.dart b/lib/core/crypto/eosdart/src/serialize.dart index ce10b957..f3453cfa 100644 --- a/lib/core/crypto/eosdart/src/serialize.dart +++ b/lib/core/crypto/eosdart/src/serialize.dart @@ -471,7 +471,7 @@ int checkRange(int orig, int converted) { } DateTime checkDateParse(String date) { - var result = DateTime.parse(date + 'Z'); + var result = DateTime.parse(date.endsWith('Z') ? date : date + 'Z'); return result; } From c7d597728e6bfbf4459c644c71007bb9cbdaba68 Mon Sep 17 00:00:00 2001 From: NIK Date: Sat, 21 Oct 2023 20:29:36 +0800 Subject: [PATCH 2/4] add getAccount --- lib/core/network/api/eos_service.dart | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/core/network/api/eos_service.dart b/lib/core/network/api/eos_service.dart index 3a88435d..c0394279 100644 --- a/lib/core/network/api/eos_service.dart +++ b/lib/core/network/api/eos_service.dart @@ -8,8 +8,10 @@ import 'package:hypha_wallet/core/crypto/seeds_esr/eos_transaction.dart'; import 'package:hypha_wallet/core/local/models/user_auth_data.dart'; import 'package:hypha_wallet/core/local/services/secure_storage_service.dart'; import 'package:hypha_wallet/core/logging/log_helper.dart'; +import 'package:hypha_wallet/core/network/api/endpoints.dart'; import 'package:hypha_wallet/core/network/api/services/remote_config_service.dart'; import 'package:hypha_wallet/core/network/models/network.dart'; +import 'package:hypha_wallet/core/network/models/network_extension.dart'; import 'package:hypha_wallet/core/network/models/token_value.dart'; import 'package:hypha_wallet/core/network/models/user_profile_data.dart'; @@ -224,6 +226,17 @@ class EOSService { return action; } + + Future> getAccount(String accountName, Network network) async { + final requestBody = {'account_name': accountName}; + try { + final res = await network.manager.post(Endpoints.getAccount, data: requestBody); + return Result.value(Account.fromJson(res.data)); + } catch (error) { + LogHelper.e('getAccount Error', error: error); + return Result.error('getAccount Error: $error'); + } + } } class EosError extends Error { From 7b439c2a17595cc6514fa12115035d51350e3418 Mon Sep 17 00:00:00 2001 From: NIK Date: Sat, 21 Oct 2023 20:30:49 +0800 Subject: [PATCH 3/4] getFreeCpuAction update newer accounts created by us can use free cpu action also never fail with the getFreeCPU action - it should never prevent the popup from appearing. It is a bonus feature. --- .../network/api/services/pay_cpu_service.dart | 41 +++++++++++++------ 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/lib/core/network/api/services/pay_cpu_service.dart b/lib/core/network/api/services/pay_cpu_service.dart index 474ed577..25268831 100644 --- a/lib/core/network/api/services/pay_cpu_service.dart +++ b/lib/core/network/api/services/pay_cpu_service.dart @@ -26,23 +26,38 @@ class PayForCpuService { Future> getFreeCpuAction(UserProfileData user, Network network) async { LogHelper.d('buildFreeTransaction'); - // pay cpu is under feature flag for each network - if (!remoteConfigService.isPayCpuEnabled(network)) { - return Result.value(null); - } + try { + // pay cpu is under feature flag for each network + if (!remoteConfigService.isPayCpuEnabled(network)) { + return Result.value(null); + } + + // if user is DAO member, we can pay for CPU + final daos = await daoService.getDaos(user: user); - // if user is DAO member, we can pay for CPU - final daos = await daoService.getDaos(user: user); - if (daos.isValue) { - if (daos.asValue!.value.isNotEmpty) { - final action = payCpuAction(account: user.accountName, network: network); - return Result.value(action); + if (daos.isValue) { + if (daos.asValue!.value.isNotEmpty) { + final action = payCpuAction(account: user.accountName, network: network); + return Result.value(action); + } else { + // if user was created by us and recently, we also cover CPU + // This covers EOS users that create a new account + final result = await eosService.getAccount(user.accountName, network); + final freshHours = remoteConfigService.newAccountFreshnessHours; + if (result.isValue) { + if (DateTime.now().difference(result.asValue!.value.created!).inHours < freshHours) { + return Result.value(payCpuAction(account: user.accountName, network: network)); + } + } + return Result.value(null); + } } else { + LogHelper.e('Error retrieving DAOs (ignored): ${daos.asError?.error}'); return Result.value(null); } - } else { - LogHelper.e('Error retrieving DAOs: ${daos.asError?.error}'); - return Result.error(HyphaError.fromError(daos.asError)); + } catch (error) { + LogHelper.e('ignored payCPUActionError: $error'); + return Result.value(null); } } From 6c035796a2fee30ab751c0eec6efcb42ff5b85b8 Mon Sep 17 00:00:00 2001 From: NIK Date: Sat, 21 Oct 2023 20:31:10 +0800 Subject: [PATCH 4/4] added payCpuEnabledNetwork default value and newAccountFreshnessHours --- lib/core/network/api/services/remote_config_service.dart | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/core/network/api/services/remote_config_service.dart b/lib/core/network/api/services/remote_config_service.dart index d5c3eadf..0135845b 100644 --- a/lib/core/network/api/services/remote_config_service.dart +++ b/lib/core/network/api/services/remote_config_service.dart @@ -89,6 +89,8 @@ class RemoteConfigService { bool get isSignUpEnabled => FirebaseRemoteConfig.instance.getBool('signUpEnabled'); String get signUpLinkUrl => FirebaseRemoteConfig.instance.getString('signUpLinkUrl'); + int get newAccountFreshnessHours => FirebaseRemoteConfig.instance.getInt('newAccountFreshnessHours'); + bool get isWalletEnabled => FirebaseRemoteConfig.instance.getBool('walletEnabled'); bool isPayCpuEnabled(Network network) => _getMap('payCpuEnabledNetwork')[network.name] ?? false; @@ -207,6 +209,8 @@ class RemoteConfigService { 'signUpEnabled': true, "signUpLinkUrl": "https://dao.hypha.earth/hypha/login", 'walletEnabled': false, + 'newAccountFreshnessHours': 48, + 'payCpuEnabledNetwork': json.encode({"telos": false, "telosTestnet": true, "eos": true, "eosTestnet": true}) }); FirebaseRemoteConfig.instance.onConfigUpdated.listen((event) async { // Side note: This does not seem to work reliably on simulator but it works in the app.