balances.dart 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import 'package:mobx/mobx.dart';
  2. import 'package:polkawallet_sdk/api/types/balanceData.dart';
  3. part 'balances.g.dart';
  4. class BalancesStore = BalancesStoreBase with _$BalancesStore;
  5. abstract class BalancesStoreBase with Store {
  6. @observable
  7. BalanceData? native;
  8. @observable
  9. List<TokenBalanceData> tokens = [];
  10. @observable
  11. bool isTokensFromCache = false;
  12. @observable
  13. List<ExtraTokenData>? extraTokens;
  14. @action
  15. void setBalance(BalanceData data) {
  16. native = data;
  17. }
  18. @action
  19. void setTokens(List<TokenBalanceData> ls, {bool isFromCache = false}) {
  20. final data = ls;
  21. if (!isFromCache) {
  22. tokens.toList().forEach((old) {
  23. final newDataIndex = ls.indexWhere((token) {
  24. if (old.tokenNameId == null) {
  25. // check by token.symbol with old data cache
  26. return token.symbol == old.symbol || token.symbol == old.id;
  27. } else {
  28. // or check by tokenNameId with new data
  29. return token.tokenNameId == old.tokenNameId;
  30. }
  31. });
  32. if (newDataIndex < 0) {
  33. data.add(old);
  34. }
  35. });
  36. }
  37. tokens = data;
  38. isTokensFromCache = isFromCache;
  39. }
  40. @action
  41. void setExtraTokens(List<ExtraTokenData> ls) {
  42. extraTokens = ls;
  43. }
  44. }
  45. class ExtraTokenData {
  46. ExtraTokenData({this.title, this.tokens});
  47. final String? title;
  48. final List<TokenBalanceData>? tokens;
  49. }
  50. /// none-native token data
  51. /// 1. [id] foreign asset id (Acala tokens module).
  52. /// 2. [name] <kUSD> for Karura USD.
  53. /// 3. [symbol] <KUSD> for Karura USD.
  54. /// 4. [fullName] <Karura US Dollar> for Karura USD.
  55. /// 5. [type] <Token | DexShare | ForeignAsset> for Karura tokens.
  56. /// 6. [tokenNameId] acala.js formatted tokenNameId, <fa://0> for {ForeignAsset: 0}.
  57. /// 6. [currencyId] acala currencyId type, {ForeignAsset: 0} for <fa://0>.
  58. class TokenBalanceData {
  59. TokenBalanceData(
  60. {this.id,
  61. this.name,
  62. this.tokenNameId,
  63. this.symbol,
  64. this.type = 'Token',
  65. this.currencyId,
  66. this.src,
  67. this.minBalance,
  68. this.fullName,
  69. this.decimals,
  70. this.amount,
  71. this.locked,
  72. this.reserved,
  73. this.detailPageRoute,
  74. this.price,
  75. this.isCacheChange = false});
  76. final String? id;
  77. final String? name;
  78. final String? tokenNameId;
  79. final String? symbol;
  80. final String type;
  81. final Map? currencyId;
  82. final Map? src;
  83. final String? minBalance;
  84. final String? fullName;
  85. final int? decimals;
  86. String? amount;
  87. final String? locked;
  88. final String? reserved;
  89. String? detailPageRoute;
  90. final double? price;
  91. bool isCacheChange;
  92. }