78 lines
2.3 KiB
Dart
78 lines
2.3 KiB
Dart
import 'dart:async';
|
||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||
import '../../domain/entities/asset_display.dart';
|
||
import '../../domain/repositories/trading_repository.dart';
|
||
import '../../core/di/injection.dart';
|
||
import 'trading_providers.dart';
|
||
|
||
/// 我的资产显示 Provider (需要登录,使用 JWT token)
|
||
final myAssetProvider = FutureProvider<AssetDisplay?>((ref) async {
|
||
final repository = ref.watch(tradingRepositoryProvider);
|
||
final result = await repository.getMyAsset();
|
||
|
||
ref.keepAlive();
|
||
final timer = Timer(const Duration(minutes: 2), () {
|
||
ref.invalidateSelf();
|
||
});
|
||
ref.onDispose(() => timer.cancel());
|
||
|
||
return result.fold(
|
||
(failure) => throw Exception(failure.message),
|
||
(asset) => asset,
|
||
);
|
||
});
|
||
|
||
/// 指定账户资产显示 Provider (public API)
|
||
final accountAssetProvider = FutureProvider.family<AssetDisplay?, String>(
|
||
(ref, accountSequence) async {
|
||
if (accountSequence.isEmpty) return null;
|
||
|
||
final repository = ref.watch(tradingRepositoryProvider);
|
||
final result = await repository.getAccountAsset(accountSequence);
|
||
|
||
ref.keepAlive();
|
||
final timer = Timer(const Duration(minutes: 2), () {
|
||
ref.invalidateSelf();
|
||
});
|
||
ref.onDispose(() => timer.cancel());
|
||
|
||
return result.fold(
|
||
(failure) => throw Exception(failure.message),
|
||
(asset) => asset,
|
||
);
|
||
},
|
||
);
|
||
|
||
/// 资产显示值计算器 - 用于实时显示资产估值
|
||
/// displayAssetValue = (账户积分股 + 账户积分股 × 倍数) × 积分股价
|
||
class AssetValueCalculator {
|
||
final String shareBalance;
|
||
final String burnMultiplier;
|
||
final String currentPrice;
|
||
|
||
AssetValueCalculator({
|
||
required this.shareBalance,
|
||
required this.burnMultiplier,
|
||
required this.currentPrice,
|
||
});
|
||
|
||
/// 计算有效积分股 = 余额 × (1 + 倍数)
|
||
double get effectiveShares {
|
||
final balance = double.tryParse(shareBalance) ?? 0;
|
||
final multiplier = double.tryParse(burnMultiplier) ?? 0;
|
||
return balance * (1 + multiplier);
|
||
}
|
||
|
||
/// 计算资产显示值 = 有效积分股 × 价格
|
||
double get displayValue {
|
||
final price = double.tryParse(currentPrice) ?? 0;
|
||
return effectiveShares * price;
|
||
}
|
||
|
||
/// 计算每秒增长值
|
||
static double calculateGrowthPerSecond(String dailyAllocation) {
|
||
final daily = double.tryParse(dailyAllocation) ?? 0;
|
||
return daily / 24 / 60 / 60;
|
||
}
|
||
}
|