Skip to content

Commit

Permalink
prevision optional
Browse files Browse the repository at this point in the history
made token model explicit in token data
  • Loading branch information
n13 committed Jun 22, 2024
1 parent ac72252 commit e545f09
Show file tree
Hide file tree
Showing 19 changed files with 71 additions and 51 deletions.
2 changes: 1 addition & 1 deletion lib/components/amount_entry/amount_entry_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class AmountEntryWidget extends StatelessWidget {
DecimalTextInputFormatter(
decimalRange: state.currentCurrencyInput == CurrencyInput.fiat
? state.fiatAmount?.precision ?? 0
: state.tokenAmount.precision,
: state.tokenAmount.precision ?? 4,
)
],
),
Expand Down
9 changes: 5 additions & 4 deletions lib/datasource/local/models/amount_data_model.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
abstract class AmountDataModel {
final double amount;
final String symbol;
final int precision;
final int? precision;
final int defaultPrecision = 4;

AmountDataModel({
required this.amount,
required this.symbol,
this.precision = 4,
this.precision,
});

// full precision formatted string, can be used for chain calls, example "10.0000 SEEDS"
String asFormattedString() {
return "${amount.toStringAsFixed(precision)} $symbol";
return "${amount.toStringAsFixed(precision ?? defaultPrecision)} $symbol";
}

// full precision string without symbol, e.g. "10.0000"
String asFixedString() {
return amount.toStringAsFixed(precision);
return amount.toStringAsFixed(precision ?? defaultPrecision);
}
}
8 changes: 5 additions & 3 deletions lib/datasource/local/models/token_data_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import 'package:seeds/utils/rate_states_extensions.dart';
class TokenDataModel extends AmountDataModel {
String? id;
final TokenModel token;
TokenDataModel(double amount, {TokenModel this.token = seedsToken})
TokenDataModel(double amount, {required this.token})
: super(
amount: amount,
symbol: token.symbol,
precision: token.precision,
) { id = token.id; }
) {
id = token.id;
}

static TokenDataModel? from(double? amount, {TokenModel token = seedsToken}) =>
static TokenDataModel? from(double? amount, {required TokenModel token}) =>
amount != null ? TokenDataModel(amount, token: token) : null;

// ignore: prefer_constructors_over_static_methods
Expand Down
2 changes: 1 addition & 1 deletion lib/datasource/remote/model/token_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TokenModel extends Equatable {
final String logoUrl;
final String balanceSubTitle;
final String overdraw;
final int precision;
final int? precision; // precision can be in an "unknown" state
final List<String>? usecases;

String get id => "$contract#$symbol";
Expand Down
54 changes: 30 additions & 24 deletions lib/domain-shared/shared_use_cases/get_token_models_use_case.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import 'package:collection/collection.dart';
import 'package:dynamic_parallel_queue/dynamic_parallel_queue.dart';
import 'package:seeds/datasource/remote/api/stat_repository.dart';
import 'package:seeds/datasource/remote/api/tokenmodels_repository.dart';
import 'package:seeds/datasource/remote/model/token_model.dart';
import 'package:seeds/domain-shared/base_use_case.dart';
Expand Down Expand Up @@ -79,33 +77,41 @@ class GetTokenModelsUseCase extends InputUseCase<List<TokenModel>, TokenModelSel
for (final token in tokens) {
token['usecases'] = useCaseMap[token['id']];
}
final StatRepository _statRepository = StatRepository();
List<TokenModel?> theseTokens = [];
// final StatRepository _statRepository = StatRepository();

/// verify token contract on chain and get contract precision
loadData(token) async {
TokenModel? tm = TokenModel.fromJson(token as Map<String, dynamic>);
// Note this can be made a lot nicer...
final List<TokenModel?> theseTokens = [];
for (final dynamic token in tokens) {
final TokenModel? tm = TokenModel.fromJson(token as Map<String, dynamic>);
if (tm != null) {
await _statRepository.getTokenStat(tokenContract: tm.contract, symbol: tm.symbol).then(
(stats) async {
if (stats.asValue != null) {
final supply = stats.asValue!.value.supplyString;
tm.setPrecisionFromString(supply);
theseTokens.add(tm);
print("supply: $supply");
}
},
).catchError((dynamic error) => _statRepository.mapHttpError(error));
theseTokens.add(tm);
}
}

final queue = Queue(parallel: 5);
for (final dynamic token in tokens) {
queue.add(() async {
await loadData(token);
});
}
await queue.whenComplete();
/// verify token contract on chain and get contract precision
// Future<Result?> loadData(token) async {
// final TokenModel? tm = TokenModel.fromJson(token as Map<String, dynamic>);
// if (tm != null) {
// return await _statRepository.getTokenStat(tokenContract: tm.contract, symbol: tm.symbol).then(
// (stats) async {
// if (stats.asValue != null) {
// final supply = stats.asValue!.value.supplyString;
// tm.setPrecisionFromString(supply);
// theseTokens.add(tm);
// print("supply: $supply");
// }
// },
// ).catchError((dynamic error) => _statRepository.mapHttpError(error));
// }
// }

// final queue = Queue(parallel: 5);
// for (final dynamic token in tokens) {
// queue.add(() async {
// await loadData(token);
// });
// }
// await queue.whenComplete();
rv.addAll(theseTokens.whereNotNull());

/// build a TokenModel from each selected token's metadata
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:seeds/blocs/rates/viewmodels/rates_bloc.dart';
import 'package:seeds/datasource/local/models/token_data_model.dart';
import 'package:seeds/datasource/local/settings_storage.dart';
import 'package:seeds/datasource/remote/model/token_model.dart';
import 'package:seeds/domain-shared/result_to_state_mapper.dart';
import 'package:seeds/screens/explore_screens/invite/interactor/viewmodels/invite_bloc.dart';
import 'package:seeds/screens/explore_screens/invite/invite_errors.dart';
Expand All @@ -12,7 +13,7 @@ class SeedsAmountChangeMapper extends StateMapper {
final double currentAvailable = currentState.availableBalance?.amount ?? 0;

final InviteError? alertMessage = _handleAlertMessage(currentAvailable, parsedQuantity);
final tokenAmount = TokenDataModel(parsedQuantity);
final tokenAmount = TokenDataModel(parsedQuantity, token: seedsToken);

return currentState.copyWith(
tokenAmount: tokenAmount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:seeds/blocs/rates/viewmodels/rates_bloc.dart';
import 'package:seeds/datasource/local/models/token_data_model.dart';
import 'package:seeds/datasource/local/settings_storage.dart';
import 'package:seeds/datasource/remote/model/balance_model.dart';
import 'package:seeds/datasource/remote/model/token_model.dart';
import 'package:seeds/domain-shared/page_state.dart';
import 'package:seeds/domain-shared/result_to_state_mapper.dart';
import 'package:seeds/screens/explore_screens/invite/interactor/viewmodels/invite_bloc.dart';
Expand All @@ -14,7 +15,7 @@ class UserBalanceStateMapper extends StateMapper {
return currentState.copyWith(pageState: PageState.failure, errorMessage: InviteError.errorLoadingBalance);
} else {
final BalanceModel balance = result.asValue!.value;
final availableBalance = TokenDataModel(balance.quantity);
final availableBalance = TokenDataModel(balance.quantity, token: seedsToken);
final String selectedFiat = settingsStorage.selectedFiatCurrency;

return currentState.copyWith(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class InviteState extends Equatable {
isAutoFocus: true,
fiatAmount: FiatDataModel(0),
isCreateInviteButtonEnabled: false,
tokenAmount: TokenDataModel(0),
tokenAmount: TokenDataModel(0, token: seedsToken),
showCloseDialogButton: false,
);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/screens/explore_screens/invite/invite_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:seeds/components/flat_button_long.dart';
import 'package:seeds/components/full_page_error_indicator.dart';
import 'package:seeds/components/full_page_loading_indicator.dart';
import 'package:seeds/datasource/local/models/token_data_model.dart';
import 'package:seeds/datasource/remote/model/token_model.dart';
import 'package:seeds/design/app_theme.dart';
import 'package:seeds/domain-shared/event_bus/event_bus.dart';
import 'package:seeds/domain-shared/event_bus/events.dart';
Expand Down Expand Up @@ -85,7 +86,7 @@ class InviteScreen extends StatelessWidget {
style: Theme.of(context).textTheme.titleLarge),
const SizedBox(height: 16),
AmountEntryWidget(
tokenDataModel: TokenDataModel(0),
tokenDataModel: TokenDataModel(0, token: seedsToken),
onValueChange: (value) {
BlocProvider.of<InviteBloc>(context).add(OnAmountChange(amountChanged: value));
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import 'package:seeds/blocs/rates/viewmodels/rates_bloc.dart';
import 'package:seeds/datasource/local/models/token_data_model.dart';
import 'package:seeds/datasource/local/settings_storage.dart';
import 'package:seeds/datasource/remote/model/token_model.dart';
import 'package:seeds/domain-shared/result_to_state_mapper.dart';
import 'package:seeds/screens/explore_screens/plant_seeds/interactor/viewmodels/plant_seeds_bloc.dart';
import 'package:seeds/utils/rate_states_extensions.dart';

class SeedsAmountChangeMapper extends StateMapper {
PlantSeedsState mapResultToState(PlantSeedsState currentState, RatesState rateState, String quantity) {
final double parsedQuantity = double.tryParse(quantity) ?? 0;
final tokenAmount = TokenDataModel(parsedQuantity);
final tokenAmount = TokenDataModel(parsedQuantity, token: seedsToken);
final double currentAvailable = currentState.availableBalance?.amount ?? 0;
final String selectedFiat = settingsStorage.selectedFiatCurrency;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:seeds/datasource/local/models/token_data_model.dart';
import 'package:seeds/datasource/local/settings_storage.dart';
import 'package:seeds/datasource/remote/model/balance_model.dart';
import 'package:seeds/datasource/remote/model/planted_model.dart';
import 'package:seeds/datasource/remote/model/token_model.dart';
import 'package:seeds/domain-shared/page_state.dart';
import 'package:seeds/domain-shared/result_to_state_mapper.dart';
import 'package:seeds/screens/explore_screens/plant_seeds/interactor/viewmodels/plant_seeds_bloc.dart';
Expand All @@ -22,16 +23,16 @@ class UserBalanceAndPlantedStateMapper extends StateMapper {
final PlantedModel? plantedSeeds = values.whereType<PlantedModel>().firstOrNull;
final String selectedFiat = settingsStorage.selectedFiatCurrency;

final availableBalance = TokenDataModel.from(balance?.quantity);
final tokenAmount = TokenDataModel(0);
final plantedAmount = TokenDataModel(plantedSeeds?.quantity ?? 0);
final availableBalance = TokenDataModel.from(balance?.quantity, token: seedsToken);
final tokenAmount = TokenDataModel(0, token: seedsToken);
final plantedAmount = TokenDataModel(plantedSeeds?.quantity ?? 0, token: seedsToken);

return currentState.copyWith(
pageState: PageState.success,
fiatAmount: rateState.tokenToFiat(tokenAmount, selectedFiat),
availableBalance: availableBalance,
availableBalanceFiat: availableBalance != null ? rateState.tokenToFiat(availableBalance, selectedFiat) : null,
plantedBalance: TokenDataModel.from(plantedSeeds?.quantity),
plantedBalance: TokenDataModel.from(plantedSeeds?.quantity, token: seedsToken),
plantedBalanceFiat: rateState.tokenToFiat(plantedAmount, selectedFiat),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:equatable/equatable.dart';
import 'package:seeds/blocs/rates/viewmodels/rates_bloc.dart';
import 'package:seeds/datasource/local/models/fiat_data_model.dart';
import 'package:seeds/datasource/local/models/token_data_model.dart';
import 'package:seeds/datasource/remote/model/token_model.dart';
import 'package:seeds/domain-shared/page_command.dart';
import 'package:seeds/domain-shared/page_state.dart';
import 'package:seeds/screens/explore_screens/plant_seeds/interactor/mappers/plant_seeds_result_mapper.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class PlantSeedsState extends Equatable {
pageState: PageState.initial,
ratesState: ratesState,
isAutoFocus: true,
tokenAmount: TokenDataModel(0),
tokenAmount: TokenDataModel(0, token: seedsToken),
fiatAmount: FiatDataModel(0),
isPlantSeedsButtonEnabled: false,
showAlert: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:seeds/components/flat_button_long.dart';
import 'package:seeds/components/full_page_error_indicator.dart';
import 'package:seeds/components/full_page_loading_indicator.dart';
import 'package:seeds/datasource/local/models/token_data_model.dart';
import 'package:seeds/datasource/remote/model/token_model.dart';
import 'package:seeds/domain-shared/event_bus/event_bus.dart';
import 'package:seeds/domain-shared/event_bus/events.dart';
import 'package:seeds/domain-shared/page_state.dart';
Expand Down Expand Up @@ -59,7 +60,7 @@ class PlantSeedsScreen extends StatelessWidget {
Text(context.loc.plantSeedsPlantAmount, style: Theme.of(context).textTheme.titleLarge),
const SizedBox(height: 16),
AmountEntryWidget(
tokenDataModel: TokenDataModel(0),
tokenDataModel: TokenDataModel(0, token: seedsToken),
onValueChange: (value) {
BlocProvider.of<PlantSeedsBloc>(context).add(OnAmountChange(amountChanged: value));
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:seeds/datasource/local/models/token_data_model.dart';
import 'package:seeds/datasource/local/settings_storage.dart';
import 'package:seeds/datasource/remote/model/planted_model.dart';
import 'package:seeds/datasource/remote/model/refund_model.dart';
import 'package:seeds/datasource/remote/model/token_model.dart';
import 'package:seeds/domain-shared/app_constants.dart';
import 'package:seeds/domain-shared/page_state.dart';
import 'package:seeds/domain-shared/result_to_state_mapper.dart';
Expand All @@ -21,7 +22,7 @@ class UserPlantedBalanceStateMapper extends StateMapper {
final String selectedFiat = settingsStorage.selectedFiatCurrency;

final PlantedModel? plantedSeeds = values.firstWhereOrNull((element) => element is PlantedModel);
final plantedAmount = TokenDataModel(plantedSeeds?.quantity ?? 0);
final plantedAmount = TokenDataModel(plantedSeeds?.quantity ?? 0, token: seedsToken);

final List<int> availableRequestIds = [];
final int millisecondsPerWeek = 24 * 60 * 60 * 1000 * 7;
Expand All @@ -46,11 +47,11 @@ class UserPlantedBalanceStateMapper extends StateMapper {
return currentState.copyWith(
showMinPlantedBalanceAlert: (plantedSeeds?.quantity ?? 0) <= minPlanted,
pageState: PageState.success,
plantedBalance: TokenDataModel.from(plantedSeeds?.quantity ?? 0),
plantedBalance: TokenDataModel.from(plantedSeeds?.quantity ?? 0, token: seedsToken),
plantedBalanceFiat: currentState.ratesState.tokenToFiat(plantedAmount, selectedFiat),
availableClaimBalance: TokenDataModel(availableTotalClaim),
availableClaimBalance: TokenDataModel(availableTotalClaim, token: seedsToken),
availableClaimBalanceFiat:
currentState.ratesState.tokenToFiat(TokenDataModel(availableTotalClaim), selectedFiat),
currentState.ratesState.tokenToFiat(TokenDataModel(availableTotalClaim, token: seedsToken), selectedFiat),
availableRequestIds: availableRequestIds,
isClaimButtonEnabled: enableClaimButton,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:seeds/blocs/rates/viewmodels/rates_bloc.dart';
import 'package:seeds/datasource/local/models/fiat_data_model.dart';
import 'package:seeds/datasource/local/models/token_data_model.dart';
import 'package:seeds/datasource/remote/firebase/firebase_remote_config.dart';
import 'package:seeds/datasource/remote/model/token_model.dart';
import 'package:seeds/domain-shared/app_constants.dart';
import 'package:seeds/domain-shared/page_command.dart';
import 'package:seeds/domain-shared/page_state.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class UnplantSeedsState extends Equatable {
showOverBalanceAlert: false,
showMinPlantedBalanceAlert: false,
isUnplantSeedsButtonEnabled: false,
unplantedInputAmount: TokenDataModel(0),
unplantedInputAmount: TokenDataModel(0, token: seedsToken),
showUnclaimedBalance: claimUnplantedSeedsEnabled,
isClaimButtonEnabled: false,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:seeds/components/flat_button_long.dart';
import 'package:seeds/components/full_page_error_indicator.dart';
import 'package:seeds/components/full_page_loading_indicator.dart';
import 'package:seeds/datasource/local/models/token_data_model.dart';
import 'package:seeds/datasource/remote/model/token_model.dart';
import 'package:seeds/domain-shared/event_bus/event_bus.dart';
import 'package:seeds/domain-shared/event_bus/events.dart';
import 'package:seeds/domain-shared/page_command.dart';
Expand Down Expand Up @@ -101,7 +102,7 @@ class _UnplantSeedsScreenState extends State<UnplantSeedsScreen> {
UnplantSeedsAmountEntry(
controller: _amountController,
unplantedBalanceFiat: state.unplantedInputAmountFiat,
tokenDataModel: TokenDataModel(0),
tokenDataModel: TokenDataModel(0, token: seedsToken),
onValueChange: (value) {
BlocProvider.of<UnplantSeedsBloc>(context).add(OnAmountChange(value));
},
Expand Down
3 changes: 2 additions & 1 deletion test/utils/dartesr_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:seeds/datasource/local/models/token_data_model.dart';
import 'package:seeds/datasource/local/util/seeds_esr.dart';
import 'package:seeds/datasource/remote/api/invoice_repository.dart';
import 'package:seeds/datasource/remote/firebase/firebase_remote_config.dart';
import 'package:seeds/datasource/remote/model/token_model.dart';

void main() {
group('dartesr', () {
Expand Down Expand Up @@ -93,7 +94,7 @@ void main() {

test('Encode and decode ESR code', () async {
final result = await InvoiceRepository().createInvoice(
tokenAmount: TokenDataModel(100),
tokenAmount: TokenDataModel(100, token: seedsToken),
accountName: "harvst.seeds",
tokenContract: "token.seeds",
);
Expand Down

0 comments on commit e545f09

Please sign in to comment.