rwadurian/frontend/mining-app/lib/data/datasources/remote/trading_remote_datasource.dart

206 lines
5.5 KiB
Dart

import '../../models/trade_order_model.dart';
import '../../models/price_info_model.dart';
import '../../models/trading_account_model.dart';
import '../../models/market_overview_model.dart';
import '../../models/asset_display_model.dart';
import '../../../core/network/api_client.dart';
import '../../../core/network/api_endpoints.dart';
import '../../../core/error/exceptions.dart';
abstract class TradingRemoteDataSource {
/// 获取当前价格信息
Future<PriceInfoModel> getCurrentPrice();
/// 获取市场概览
Future<MarketOverviewModel> getMarketOverview();
/// 获取交易账户信息
Future<TradingAccountModel> getTradingAccount(String accountSequence);
/// 创建订单
Future<Map<String, dynamic>> createOrder({
required String type,
required String price,
required String quantity,
});
/// 取消订单
Future<void> cancelOrder(String orderNo);
/// 获取用户订单列表
Future<OrdersPageModel> getOrders({
int page = 1,
int pageSize = 50,
});
/// 预估卖出收益
Future<Map<String, dynamic>> estimateSell(String quantity);
/// 划入积分股 (从挖矿账户到交易账户)
Future<Map<String, dynamic>> transferIn(String amount);
/// 划出积分股 (从交易账户到挖矿账户)
Future<Map<String, dynamic>> transferOut(String amount);
/// 获取我的资产显示信息
Future<AssetDisplayModel> getMyAsset({String? dailyAllocation});
/// 获取指定账户资产显示信息
Future<AssetDisplayModel> getAccountAsset(String accountSequence, {String? dailyAllocation});
}
class TradingRemoteDataSourceImpl implements TradingRemoteDataSource {
final ApiClient client;
TradingRemoteDataSourceImpl({required this.client});
@override
Future<PriceInfoModel> getCurrentPrice() async {
try {
final response = await client.get(ApiEndpoints.currentPrice);
return PriceInfoModel.fromJson(response.data);
} catch (e) {
throw ServerException(e.toString());
}
}
@override
Future<MarketOverviewModel> getMarketOverview() async {
try {
final response = await client.get(ApiEndpoints.marketOverview);
return MarketOverviewModel.fromJson(response.data);
} catch (e) {
throw ServerException(e.toString());
}
}
@override
Future<TradingAccountModel> getTradingAccount(String accountSequence) async {
try {
final response = await client.get(ApiEndpoints.tradingAccount(accountSequence));
return TradingAccountModel.fromJson(response.data);
} catch (e) {
throw ServerException(e.toString());
}
}
@override
Future<Map<String, dynamic>> createOrder({
required String type,
required String price,
required String quantity,
}) async {
try {
final response = await client.post(
ApiEndpoints.createOrder,
data: {
'type': type,
'price': price,
'quantity': quantity,
},
);
return response.data;
} catch (e) {
throw ServerException(e.toString());
}
}
@override
Future<void> cancelOrder(String orderNo) async {
try {
await client.post(ApiEndpoints.cancelOrder(orderNo));
} catch (e) {
throw ServerException(e.toString());
}
}
@override
Future<OrdersPageModel> getOrders({
int page = 1,
int pageSize = 50,
}) async {
try {
final response = await client.get(
ApiEndpoints.orders,
queryParameters: {'page': page, 'pageSize': pageSize},
);
return OrdersPageModel.fromJson(response.data);
} catch (e) {
throw ServerException(e.toString());
}
}
@override
Future<Map<String, dynamic>> estimateSell(String quantity) async {
try {
final response = await client.get(
ApiEndpoints.estimateSell,
queryParameters: {'quantity': quantity},
);
return response.data;
} catch (e) {
throw ServerException(e.toString());
}
}
@override
Future<Map<String, dynamic>> transferIn(String amount) async {
try {
final response = await client.post(
ApiEndpoints.transferIn,
data: {'amount': amount},
);
return response.data;
} catch (e) {
throw ServerException(e.toString());
}
}
@override
Future<Map<String, dynamic>> transferOut(String amount) async {
try {
final response = await client.post(
ApiEndpoints.transferOut,
data: {'amount': amount},
);
return response.data;
} catch (e) {
throw ServerException(e.toString());
}
}
@override
Future<AssetDisplayModel> getMyAsset({String? dailyAllocation}) async {
try {
final queryParams = <String, dynamic>{};
if (dailyAllocation != null) {
queryParams['dailyAllocation'] = dailyAllocation;
}
final response = await client.get(
ApiEndpoints.myAsset,
queryParameters: queryParams.isNotEmpty ? queryParams : null,
);
return AssetDisplayModel.fromJson(response.data);
} catch (e) {
throw ServerException(e.toString());
}
}
@override
Future<AssetDisplayModel> getAccountAsset(String accountSequence, {String? dailyAllocation}) async {
try {
final queryParams = <String, dynamic>{};
if (dailyAllocation != null) {
queryParams['dailyAllocation'] = dailyAllocation;
}
final response = await client.get(
ApiEndpoints.accountAsset(accountSequence),
queryParameters: queryParams.isNotEmpty ? queryParams : null,
);
return AssetDisplayModel.fromJson(response.data);
} catch (e) {
throw ServerException(e.toString());
}
}
}