Skip to content

Commit

Permalink
Merge pull request #1394 from ardriveapp/PE-4694
Browse files Browse the repository at this point in the history
PE-4694: Credit update issue during snapshot creation purchase
  • Loading branch information
matibat authored Oct 4, 2023
2 parents 9b8bfdd + dfb546d commit faf9687
Show file tree
Hide file tree
Showing 13 changed files with 319 additions and 97 deletions.
74 changes: 62 additions & 12 deletions lib/blocs/create_snapshot/create_snapshot_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import 'package:ardrive/entities/entities.dart';
import 'package:ardrive/entities/snapshot_entity.dart';
import 'package:ardrive/entities/string_types.dart';
import 'package:ardrive/models/daos/daos.dart';
import 'package:ardrive/services/arweave/arweave.dart';
import 'package:ardrive/services/config/app_config.dart';
import 'package:ardrive/services/pst/pst.dart';
import 'package:ardrive/services/services.dart';
import 'package:ardrive/turbo/services/payment_service.dart';
import 'package:ardrive/turbo/services/upload_service.dart';
import 'package:ardrive/turbo/turbo.dart';
Expand Down Expand Up @@ -42,7 +40,7 @@ class CreateSnapshotCubit extends Cubit<CreateSnapshotState> {
final TabVisibilitySingleton _tabVisibility;
final TurboBalanceRetriever turboBalanceRetriever;
final ArDriveAuth auth;
final AppConfig appConfig;
final ConfigService configService;
final TurboUploadService turboService;
@visibleForTesting
bool throwOnDataComputingForTesting;
Expand Down Expand Up @@ -71,11 +69,12 @@ class CreateSnapshotCubit extends Cubit<CreateSnapshotState> {
bool _sufficentCreditsBalance = false;
bool _sufficientArBalance = false;
bool _isFreeThanksToTurbo = false;
bool _wasSnapshotDataComputingCanceled = false;

bool get _useTurboUpload =>
_uploadMethod == UploadMethod.turbo || _isFreeThanksToTurbo;

bool _wasSnapshotDataComputingCanceled = false;
AppConfig get appConfig => configService.config;

SnapshotItemToBeCreated? _itemToBeCreated;
SnapshotEntity? _snapshotEntity;
Expand All @@ -89,7 +88,7 @@ class CreateSnapshotCubit extends Cubit<CreateSnapshotState> {
required TabVisibilitySingleton tabVisibility,
required this.turboBalanceRetriever,
required this.auth,
required this.appConfig,
required this.configService,
required this.turboService,
this.throwOnDataComputingForTesting = false,
this.throwOnSignTxForTesting = false,
Expand Down Expand Up @@ -404,15 +403,65 @@ class CreateSnapshotCubit extends Cubit<CreateSnapshotState> {
);
}

Future<void> _computeBalanceEstimate() async {
Future<void> refreshTurboBalance() async {
final profileState = _profileCubit.state as ProfileLoggedIn;
final wallet = profileState.wallet;

final turboBalance =
final BigInt? fakeTurboCredits = appConfig.fakeTurboCredits;

/// necessary to wait for backend update the balance
await Future.delayed(const Duration(seconds: 2));

final BigInt turboBalance = fakeTurboCredits ??
await turboBalanceRetriever.getBalance(wallet).catchError((e) {
logger.e('Error while retrieving turbo balance', e);
return BigInt.zero;
});

logger.d('Balance after topping up: $turboBalance');

_turboBalance = turboBalance;
_hasNoTurboBalance = turboBalance == BigInt.zero;
_turboCredits = convertCreditsToLiteralString(turboBalance);
_sufficentCreditsBalance = _costEstimateTurbo.totalCost <= _turboBalance;
_computeIsTurboEnabled();
_computeIsButtonEnabled();

if (state is ConfirmingSnapshotCreation) {
final stateAsConfirming = state as ConfirmingSnapshotCreation;

logger.d('Refreshing turbo balance');
logger.d('Turbo balance: $_turboCredits');
logger.d('Has no turbo balance: $_hasNoTurboBalance');
logger
.d('Sufficient balance to pay with turbo: $_sufficentCreditsBalance');
logger.d('Upload method: $_uploadMethod');

emit(
stateAsConfirming.copyWith(
turboCredits: _turboCredits,
hasNoTurboBalance: _hasNoTurboBalance,
sufficientBalanceToPayWithTurbo: _sufficentCreditsBalance,
uploadMethod: _uploadMethod,
isButtonToUploadEnabled: _isButtonToUploadEnabled,
),
);
}
}

Future<void> _computeBalanceEstimate() async {
final ProfileLoggedIn profileState = _profileCubit.state as ProfileLoggedIn;
final Wallet wallet = profileState.wallet;

final BigInt? fakeTurboCredits = appConfig.fakeTurboCredits;

final BigInt turboBalance = fakeTurboCredits ??
await turboBalanceRetriever.getBalance(wallet).catchError((e) {
logger.e('Error while retrieving turbo balance', e);
return BigInt.zero;
});
logger.e('Error while retrieving turbo balance', e);
return BigInt.zero;
});

logger.d('Balance before topping up: $turboBalance');

_turboBalance = turboBalance;
_hasNoTurboBalance = turboBalance == BigInt.zero;
Expand All @@ -439,9 +488,10 @@ class CreateSnapshotCubit extends Cubit<CreateSnapshotState> {

void _computeIsFreeThanksToTurbo() {
final allowedDataItemSizeForTurbo = appConfig.allowedDataItemSizeForTurbo;
final forceNoFreeThanksToTurbo = appConfig.forceNoFreeThanksToTurbo;
final isFreeThanksToTurbo =
_snapshotEntity!.data!.length <= allowedDataItemSizeForTurbo;
_isFreeThanksToTurbo = isFreeThanksToTurbo;
_isFreeThanksToTurbo = isFreeThanksToTurbo && !forceNoFreeThanksToTurbo;
}

Future<void> _emitConfirming({required int dataSize}) async {
Expand Down
5 changes: 2 additions & 3 deletions lib/components/create_snapshot_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Future<void> promptToCreateSnapshot(
turboBalanceRetriever: TurboBalanceRetriever(
paymentService: context.read<PaymentService>(),
),
appConfig: context.read<ConfigService>().config,
configService: context.read<ConfigService>(),
turboService: context.read<TurboUploadService>(),
),
child: CreateSnapshotDialog(
Expand Down Expand Up @@ -416,8 +416,7 @@ Widget _confirmDialog(
state.sufficientBalanceToPayWithTurbo,
isFreeThanksToTurbo: false,
onTurboTopupSucess: () {
createSnapshotCubit
.setUploadMethod(UploadMethod.turbo);
createSnapshotCubit.refreshTurboBalance();
},
onArSelect: () {
createSnapshotCubit.setUploadMethod(UploadMethod.ar);
Expand Down
Loading

0 comments on commit faf9687

Please sign in to comment.