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; } 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 { 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); } } 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.