cake_wallet/lib/exchange/trade.dart

119 lines
2.7 KiB
Dart
Raw Normal View History

2020-01-04 19:31:52 +00:00
import 'package:hive/hive.dart';
2021-12-24 12:37:24 +00:00
import 'package:cw_core/crypto_currency.dart';
2020-09-21 11:50:26 +00:00
import 'package:cake_wallet/exchange/exchange_provider_description.dart';
import 'package:cake_wallet/exchange/trade_state.dart';
2021-12-24 12:37:24 +00:00
import 'package:cw_core/format_amount.dart';
2020-01-04 19:31:52 +00:00
part 'trade.g.dart';
2021-01-15 17:41:30 +00:00
@HiveType(typeId: Trade.typeId)
2020-01-04 19:31:52 +00:00
class Trade extends HiveObject {
2020-01-08 12:26:34 +00:00
Trade(
2022-10-12 17:09:57 +00:00
{required this.id,
required this.amount,
ExchangeProviderDescription? provider,
CryptoCurrency? from,
CryptoCurrency? to,
TradeState? state,
2020-01-08 12:26:34 +00:00
this.createdAt,
this.expiredAt,
this.inputAddress,
this.extraId,
this.outputTransaction,
this.refundAddress,
2022-10-12 17:09:57 +00:00
this.walletId}) {
if (provider != null) {
providerRaw = provider.raw;
}
if (from != null) {
fromRaw = from.raw;
}
if (to != null) {
toRaw = to.raw;
}
if (state != null) {
stateRaw = state.raw;
}
}
2020-01-08 12:26:34 +00:00
2021-01-15 17:41:30 +00:00
static const typeId = 3;
2020-01-04 19:31:52 +00:00
static const boxName = 'Trades';
2020-09-21 11:50:26 +00:00
static const boxKey = 'tradesBoxKey';
2020-01-04 19:31:52 +00:00
@HiveField(0)
String id;
@HiveField(1)
2022-10-12 17:09:57 +00:00
late int providerRaw;
2020-01-04 19:31:52 +00:00
ExchangeProviderDescription get provider =>
ExchangeProviderDescription.deserialize(raw: providerRaw);
@HiveField(2)
2022-10-12 17:09:57 +00:00
late int fromRaw;
2020-01-04 19:31:52 +00:00
CryptoCurrency get from => CryptoCurrency.deserialize(raw: fromRaw);
@HiveField(3)
2022-10-12 17:09:57 +00:00
late int toRaw;
2020-01-04 19:31:52 +00:00
CryptoCurrency get to => CryptoCurrency.deserialize(raw: toRaw);
@HiveField(4)
2022-10-12 17:09:57 +00:00
late String stateRaw;
2020-01-04 19:31:52 +00:00
TradeState get state => TradeState.deserialize(raw: stateRaw);
@HiveField(5)
2022-10-12 17:09:57 +00:00
DateTime? createdAt;
2020-01-04 19:31:52 +00:00
@HiveField(6)
2022-10-12 17:09:57 +00:00
DateTime? expiredAt;
2020-01-04 19:31:52 +00:00
@HiveField(7)
String amount;
@HiveField(8)
2022-10-12 17:09:57 +00:00
String? inputAddress;
2020-01-04 19:31:52 +00:00
@HiveField(9)
2022-10-12 17:09:57 +00:00
String? extraId;
2020-01-04 19:31:52 +00:00
@HiveField(10)
2022-10-12 17:09:57 +00:00
String? outputTransaction;
2020-01-04 19:31:52 +00:00
@HiveField(11)
2022-10-12 17:09:57 +00:00
String? refundAddress;
2020-01-04 19:31:52 +00:00
@HiveField(12)
2022-10-12 17:09:57 +00:00
String? walletId;
2020-01-04 19:31:52 +00:00
2022-10-12 17:09:57 +00:00
static Trade fromMap(Map<String, Object?> map) {
2020-01-04 19:31:52 +00:00
return Trade(
2020-01-08 12:26:34 +00:00
id: map['id'] as String,
provider: ExchangeProviderDescription.deserialize(
raw: map['provider'] as int),
from: CryptoCurrency.deserialize(raw: map['input'] as int),
to: CryptoCurrency.deserialize(raw: map['output'] as int),
2020-01-04 19:31:52 +00:00
createdAt: map['date'] != null
2020-01-08 12:26:34 +00:00
? DateTime.fromMillisecondsSinceEpoch(map['date'] as int)
2020-01-04 19:31:52 +00:00
: null,
2020-01-08 12:26:34 +00:00
amount: map['amount'] as String,
walletId: map['wallet_id'] as String);
2020-01-04 19:31:52 +00:00
}
Map<String, dynamic> toMap() {
2020-01-08 12:26:34 +00:00
return <String, dynamic>{
2020-01-04 19:31:52 +00:00
'id': id,
'provider': provider.serialize(),
'input': from.serialize(),
'output': to.serialize(),
2022-10-12 17:09:57 +00:00
'date': createdAt != null ? createdAt!.millisecondsSinceEpoch : null,
2020-01-04 19:31:52 +00:00
'amount': amount,
'wallet_id': walletId
};
}
String amountFormatted() => formatAmount(amount);
2020-01-04 19:31:52 +00:00
}