Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/pay cpu 2 0 #268

Merged
merged 4 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/core/crypto/eosdart/src/serialize.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
13 changes: 13 additions & 0 deletions lib/core/network/api/eos_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -224,6 +226,17 @@ class EOSService {

return action;
}

Future<Result<Account>> 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 {
Expand Down
41 changes: 28 additions & 13 deletions lib/core/network/api/services/pay_cpu_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,38 @@ class PayForCpuService {
Future<Result<EOSAction?, HyphaError>> 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make the created field in the account non billable?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non nullable right? the problem is this is the old eosdart code and I didn't want to touch that.

created is always defined

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

Expand Down
4 changes: 4 additions & 0 deletions lib/core/network/api/services/remote_config_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to add these to firebase

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

payCpuEnabledNetwork had been only in firebase, adding the other one

});
FirebaseRemoteConfig.instance.onConfigUpdated.listen((event) async {
// Side note: This does not seem to work reliably on simulator but it works in the app.
Expand Down
Loading