Skip to content

Commit

Permalink
Merge pull request #259 from hypha-dao/fix_transactions
Browse files Browse the repository at this point in the history
Fix transactions
  • Loading branch information
gguijarro-c-chwy authored Sep 22, 2023
2 parents 2b5097c + d3d92ad commit 1e5e192
Show file tree
Hide file tree
Showing 10 changed files with 331 additions and 63 deletions.
43 changes: 22 additions & 21 deletions lib/core/network/api/services/token_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class TokenSymbolScope {
final String symbol;
final String scope;
final String tokenContract;

TokenSymbolScope({required this.symbol, required this.scope, required this.tokenContract});
}

Expand All @@ -31,12 +32,12 @@ class TokenService {
}) async {
LogHelper.d('getTokenBalance: $tokenContract');
try {
final requestBody = '''
{
"account": "${user.accountName}",
"code": "$tokenContract",
"symbol": "$symbol",
}''';
final requestBody = {
'account': user.accountName,
'code': tokenContract,
'symbol': symbol,
};

final Response<List> res = await user.network.manager.post<List>(
Endpoints.getCurrencyBalance,
data: requestBody,
Expand Down Expand Up @@ -64,15 +65,15 @@ class TokenService {
required String tokenContract,
}) async {
try {
final requestBody = '''
{
"code": "$tokenContract",
"table": "stat",
"lower_bound": "",
"upper_bound": "",
"limit": 1000,
"reverse": false,
}''';
final requestBody = {
'code': tokenContract,
'table': 'stat',
'lower_bound': '',
'upper_bound': '',
'limit': 1000,
'reverse': false,
};

final res = await networkingManager.post(Endpoints.getTableScopes, data: requestBody);
final List<Map<String, dynamic>> list = List<Map<String, dynamic>>.from(res.data['rows']);
final tokenSymbolScopes = List<TokenSymbolScope>.from(list.map((e) {
Expand All @@ -96,12 +97,12 @@ class TokenService {
required String symbol,
}) async {
try {
final requestBody = '''
{
"json": true,
"code": "$tokenContract",
"symbol": "$symbol"
}''';
final requestBody = {
'json': true,
'code': tokenContract,
'symbol': symbol
};

final res = await networkingManager.post(Endpoints.getCurrencyStats, data: requestBody);
final json = res.data;
// flutter: res: {HYPHA: {supply: 47738747.41 HYPHA, max_supply: -1.00 HYPHA, issuer: dao.hypha}}
Expand Down
18 changes: 10 additions & 8 deletions lib/core/network/api/services/user_account_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ class UserAccountService {
required String publicKey,
required String network,
}) async {
final requestBody = '''
{
"code": "$code",
"accountName": "$accountName",
"publicKey": "$publicKey",
"network": "$network"
}''';
final requestBody = {
'code': code,
'accountName': accountName,
'publicKey': publicKey,
'network': network
};

final url = remoteConfigService.accountCreatorEndpoint + Endpoints.createAccount;
try {
// ignore: unused_local_variable
Expand All @@ -46,7 +46,9 @@ class UserAccountService {
}

Future<bool> isUserAccountAvailable(String accountName, Network network) async {
final requestBody = '{ "account_name": "$accountName" }';
final requestBody = {
'account_name': accountName
};
try {
// ignore: unused_local_variable
final res = await network.manager.post(Endpoints.getAccount, data: requestBody);
Expand Down
20 changes: 8 additions & 12 deletions lib/core/network/models/transaction_model.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import 'package:equatable/equatable.dart';

const _daoAccount = 'dao.hypha';
const _systemTokenAccount = 'eosio.token';
const _hyphaTokenAccount = 'hypha.hypha';
const _hyphaWrapTokenAccount = 'whypha.hypha';
const _eosioLoginAccount = 'eosio.login';

sealed class TransactionModel extends Equatable {
Expand All @@ -26,7 +23,7 @@ sealed class TransactionModel extends Equatable {
});

@override
List<Object?> get props => [actionName, data, account, timestamp, blockNumber];
List<Object?> get props => [actionName, data, account, timestamp, blockNumber, actor, transactionId];

factory TransactionModel.parseFromParams({
required timestamp,
Expand All @@ -38,9 +35,7 @@ sealed class TransactionModel extends Equatable {
required Map<String, dynamic> data,
}) {
return switch (actionName) {
'transfer'
when account == _hyphaTokenAccount || account == _hyphaWrapTokenAccount || account == _systemTokenAccount =>
TransactionTransfer(
'transfer' => TransactionTransfer(
account: account,
actionName: actionName,
blockNumber: blockNumber,
Expand Down Expand Up @@ -187,10 +182,10 @@ class TransactionTransfer extends TransactionModel {
String get to => data['to'];

/// 3175.00
num get amount => data['amount'];
String get amount => (data['quantity'] as String).split(' ').first;

/// HUSD
String get symbol => data['symbol'];
String get symbol => (data['quantity'] as String).split(' ').last;

const TransactionTransfer({
required super.data,
Expand All @@ -208,10 +203,11 @@ class TransactionRedeem extends TransactionModel {

String get requestor => data['requestor'];

/// 3175.00 HUSD Beware of this...
String get amount => (data['amount'] as String).split(' ').first;
/// 3175.00
String get amount => (data['quantity'] as String).split(' ').first;

String get symbol => (data['amount'] as String).split(' ')[1];
/// HUSD
String get symbol => (data['quantity'] as String).split(' ').last;

const TransactionRedeem({
required super.data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ class TransactionHistoryRepository {

TransactionHistoryRepository({required this.service});

Future<Result<List<TransactionModel>, HyphaError>> getTransactions(UserProfileData user, bool transferOnly,
{useV1History = false}) async {
Future<Result<List<TransactionModel>, HyphaError>> getTransactions(
UserProfileData user,
bool transferOnly, {
useV1History = false,
}) async {
try {
final Response response = useV1History
? await service.getAllTransactionsV1History(user)
Expand Down
8 changes: 6 additions & 2 deletions lib/ui/token/token_details/interactor/token_details_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ import 'package:hypha_wallet/ui/wallet/data/wallet_token_data.dart';
import 'package:hypha_wallet/ui/wallet/usecases/get_transaction_history_data_use_case.dart';

part 'page_command.dart';

part 'token_details_bloc.freezed.dart';

part 'token_details_event.dart';

part 'token_details_state.dart';

class TokenDetailsBloc extends Bloc<TokenDetailsEvent, TokenDetailsState> {
Expand Down Expand Up @@ -44,8 +47,9 @@ class TokenDetailsBloc extends Bloc<TokenDetailsEvent, TokenDetailsState> {
FutureOr<void> _initial(_Initial event, Emitter<TokenDetailsState> emit) async {
emit(state.copyWith(loadingTransaction: true, loadingTokenBalance: true));


await _getTransactionHistoryDataUseCase.run(true).then((result) {
await _getTransactionHistoryDataUseCase
.getTransferTransactionsForToken(contract: state.token.contract, symbol: state.token.symbol)
.then((result) {
if (result.isValue) {
emit(state.copyWith(
pageState: PageState.success,
Expand Down
20 changes: 17 additions & 3 deletions lib/ui/wallet/components/recent_transactions_widget.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:get/get.dart';
import 'package:hypha_wallet/design/buttons/hypha_app_button.dart';
import 'package:hypha_wallet/design/hypha_card.dart';
import 'package:hypha_wallet/design/hypha_colors.dart';
import 'package:hypha_wallet/design/progress_indicator/hypha_progress_indicator.dart';
import 'package:hypha_wallet/design/themes/extensions/theme_extension_provider.dart';
import 'package:hypha_wallet/ui/shared/listview_with_all_separators.dart';
import 'package:hypha_wallet/ui/wallet/components/wallet_transaction_tile.dart';
import 'package:hypha_wallet/ui/wallet/interactor/wallet_bloc.dart';

class RecentTransactionsWidget extends StatelessWidget {
final bool loadingTransaction;
Expand Down Expand Up @@ -49,9 +52,20 @@ class RecentTransactionsWidget extends StatelessWidget {
child: HyphaCard(
child: Padding(
padding: const EdgeInsets.all(24),
child: Text(
'You haven’t done any transaction yet',
style: context.hyphaTextTheme.ralMediumSmallNote,
child: Column(
children: [
Text(
'You haven’t done any transaction yet',
style: context.hyphaTextTheme.ralMediumSmallNote,
),
const SizedBox(height: 8),
HyphaAppButton(
title: 'Refresh',
onPressed: () {
context.read<WalletBloc>().add(const WalletEvent.onRefresh());
},
)
],
),
),
),
Expand Down
40 changes: 28 additions & 12 deletions lib/ui/wallet/interactor/wallet_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:hypha_wallet/core/error_handler/error_handler_manager.dart';
import 'package:hypha_wallet/core/error_handler/model/hypha_error.dart';
import 'package:hypha_wallet/ui/architecture/interactor/page_states.dart';
import 'package:hypha_wallet/ui/architecture/result/result.dart';
import 'package:hypha_wallet/ui/wallet/components/wallet_transaction_tile.dart';
Expand All @@ -26,26 +27,24 @@ class WalletBloc extends Bloc<WalletEvent, WalletState> {
WalletBloc(
this._errorHandlerManager,
this._getUserTokensUseCase,
this._getTransactionHistoryDataUseCase,
this._getTransactionHistoryDataUseCase,
) : super(const WalletState()) {
on<_Initial>(_initial);
on<_OnTransactionsChanged>(_onTransactionsChanged);
on<_OnRefresh>(_onRefresh);
on<_ClearPageCommand>((_, emit) => emit(state.copyWith(command: null)));
}

Future<void> _initial(_Initial event, Emitter<WalletState> emit) async {
emit(state.copyWith(pageState: PageState.success, loadingTransaction: true));

unawaited(_getTransactionHistoryDataUseCase.run(true).then((result) {
if (result.isValue && !isClosed) {
emit(state.copyWith(
pageState: PageState.success,
recentTransactions: result.valueOrCrash,
loadingTransaction: false,
));
} else if (!isClosed){
// ignore: unawaited_futures
_errorHandlerManager.handlerError(result.asError!.error);
emit(state.copyWith(loadingTransaction: false));
unawaited(_getTransactionHistoryDataUseCase.getTransferTransactionsForTokens().then((result) {
if (isClosed) {
return;
}

if (result.isValue) {
add(WalletEvent.onTransactionsChanged(result));
}
}));

Expand All @@ -54,4 +53,21 @@ class WalletBloc extends Bloc<WalletEvent, WalletState> {
return state.copyWith(pageState: PageState.success, tokens: data);
});
}

FutureOr<void> _onTransactionsChanged(_OnTransactionsChanged event, Emitter<WalletState> emit) {
if (event.value.isValue) {
emit(state.copyWith(
pageState: PageState.success,
recentTransactions: event.value.asValue!.value,
loadingTransaction: false,
));
} else {
_errorHandlerManager.handlerError(event.value.asError!.error);
emit(state.copyWith(loadingTransaction: false));
}
}

FutureOr<void> _onRefresh(_OnRefresh event, Emitter<WalletState> emit) {
add(const WalletEvent.initial());
}
}
Loading

0 comments on commit 1e5e192

Please sign in to comment.