stack_wallet/lib/models/isar/exchange_cache/currency.dart

171 lines
4.1 KiB
Dart
Raw Normal View History

2023-05-26 21:21:16 +00:00
/*
* This file is part of Stack Wallet.
*
* Copyright (c) 2023 Cypher Stack
* All Rights Reserved.
* The code is distributed under GPLv3 license, see LICENSE file for details.
* Generated by Cypher Stack on 2023-05-26
*
*/
import 'package:isar/isar.dart';
import 'package:stackwallet/models/isar/exchange_cache/pair.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
part 'currency.g.dart';
@Collection(accessor: "currencies")
2022-08-26 08:11:35 +00:00
class Currency {
Id? id;
@Index()
final String exchangeName;
2022-08-26 08:11:35 +00:00
/// Currency ticker
@Index(composite: [
CompositeIndex("exchangeName"),
CompositeIndex("name"),
])
2022-08-26 08:11:35 +00:00
final String ticker;
/// Currency name
final String name;
2022-10-02 19:37:11 +00:00
/// Currency network
final String network;
2022-08-26 08:11:35 +00:00
/// Currency logo url
final String image;
2022-10-02 19:37:11 +00:00
/// external id if it exists
final String? externalId;
2022-08-26 08:11:35 +00:00
/// Indicates if a currency is a fiat currency (EUR, USD)
final bool isFiat;
/// Indicates if a currency is available on a fixed-rate flow
@enumerated
final SupportedRateType rateType;
2022-08-26 08:11:35 +00:00
/// (Optional - based on api call) Indicates whether the pair is
/// currently supported by change now
final bool? isAvailable;
@Index()
final bool isStackCoin;
final String? tokenContract;
@ignore
bool get supportsFixedRate =>
rateType == SupportedRateType.fixed || rateType == SupportedRateType.both;
@ignore
bool get supportsEstimatedRate =>
rateType == SupportedRateType.estimated ||
rateType == SupportedRateType.both;
2022-08-26 08:11:35 +00:00
Currency({
required this.exchangeName,
2022-08-26 08:11:35 +00:00
required this.ticker,
required this.name,
2022-10-02 19:37:11 +00:00
required this.network,
2022-08-26 08:11:35 +00:00
required this.image,
2022-10-02 19:37:11 +00:00
this.externalId,
2022-08-26 08:11:35 +00:00
required this.isFiat,
required this.rateType,
2022-08-26 08:11:35 +00:00
this.isAvailable,
required this.isStackCoin,
required this.tokenContract,
2022-08-26 08:11:35 +00:00
});
factory Currency.fromJson(
Map<String, dynamic> json, {
required String exchangeName,
required SupportedRateType rateType,
}) {
2022-08-26 08:11:35 +00:00
try {
final ticker = (json["ticker"] as String).toUpperCase();
2022-08-26 08:11:35 +00:00
return Currency(
exchangeName: exchangeName,
ticker: ticker,
2022-08-26 08:11:35 +00:00
name: json["name"] as String,
2022-10-03 00:49:12 +00:00
network: json["network"] as String? ?? "",
2022-08-26 08:11:35 +00:00
image: json["image"] as String,
2022-10-02 19:37:11 +00:00
externalId: json["externalId"] as String?,
2022-08-26 08:11:35 +00:00
isFiat: json["isFiat"] as bool,
rateType: rateType,
2022-08-26 08:11:35 +00:00
isAvailable: json["isAvailable"] as bool?,
isStackCoin:
json["isStackCoin"] as bool? ?? Currency.checkIsStackCoin(ticker),
tokenContract: json["tokenContract"] as String?,
)..id = json["id"] as int?;
2022-08-26 08:11:35 +00:00
} catch (e) {
rethrow;
}
}
Map<String, dynamic> toJson() {
final map = {
"id": id,
"exchangeName": exchangeName,
2022-08-26 08:11:35 +00:00
"ticker": ticker,
"name": name,
2022-10-02 19:37:11 +00:00
"network": network,
2022-08-26 08:11:35 +00:00
"image": image,
2022-10-02 19:37:11 +00:00
"externalId": externalId,
2022-08-26 08:11:35 +00:00
"isFiat": isFiat,
"rateType": rateType,
"isAvailable": isAvailable,
"isStackCoin": isStackCoin,
"tokenContract": tokenContract,
2022-08-26 08:11:35 +00:00
};
return map;
}
Currency copyWith({
Id? id,
String? exchangeName,
2022-08-26 08:11:35 +00:00
String? ticker,
String? name,
2022-10-02 19:37:11 +00:00
String? network,
2022-08-26 08:11:35 +00:00
String? image,
2022-10-02 19:37:11 +00:00
String? externalId,
2022-08-26 08:11:35 +00:00
bool? isFiat,
SupportedRateType? rateType,
2022-08-26 08:11:35 +00:00
bool? isAvailable,
bool? isStackCoin,
String? tokenContract,
2022-08-26 08:11:35 +00:00
}) {
return Currency(
exchangeName: exchangeName ?? this.exchangeName,
2022-08-26 08:11:35 +00:00
ticker: ticker ?? this.ticker,
name: name ?? this.name,
2022-10-02 19:37:11 +00:00
network: network ?? this.network,
2022-08-26 08:11:35 +00:00
image: image ?? this.image,
2022-10-02 19:37:11 +00:00
externalId: externalId ?? this.externalId,
2022-08-26 08:11:35 +00:00
isFiat: isFiat ?? this.isFiat,
rateType: rateType ?? this.rateType,
2022-08-26 08:11:35 +00:00
isAvailable: isAvailable ?? this.isAvailable,
isStackCoin: isStackCoin ?? this.isStackCoin,
tokenContract: tokenContract ?? this.tokenContract,
)..id = id ?? this.id;
2022-08-26 08:11:35 +00:00
}
@override
String toString() {
return "Currency: ${toJson()}";
}
static bool checkIsStackCoin(String ticker) {
try {
coinFromTickerCaseInsensitive(ticker);
return true;
} catch (_) {
return false;
}
}
2022-08-26 08:11:35 +00:00
}