| 1 | import 'dart:convert'; |
| 2 | |
| 3 | import 'package:cw_core/crypto_currency.dart'; |
| 4 | import 'package:cw_core/hive_type_ids.dart'; |
| 5 | import 'package:hive/hive.dart'; |
| 6 | |
| 7 | part 'zano_asset.part.dart'; |
| 8 | |
| 9 | // @HiveType(typeId: ZanoAsset.typeId) |
| 10 | class ZanoAsset extends CryptoCurrency with HiveObjectMixin { |
| 11 | // @HiveField(0) |
| 12 | final String fullName; |
| 13 | // @HiveField(1) |
| 14 | final String ticker; |
| 15 | // @HiveField(2) |
| 16 | final String assetId; |
| 17 | // @HiveField(3) |
| 18 | final int decimalPoint; |
| 19 | // @HiveField(4, defaultValue: true) |
| 20 | bool _enabled; |
| 21 | // @HiveField(5) |
| 22 | final String? iconPath; |
| 23 | |
| 24 | // @HiveField(6) |
| 25 | // final String? tag; |
| 26 | // @HiveField(6) |
| 27 | final String owner; |
| 28 | // @HiveField(7) |
| 29 | final String metaInfo; |
| 30 | // @HiveField(8) |
| 31 | final BigInt currentSupply; |
| 32 | // @HiveField(9) |
| 33 | final bool hiddenSupply; |
| 34 | // @HiveField(10) |
| 35 | final BigInt totalMaxSupply; |
| 36 | // @HiveField(11) |
| 37 | final bool isInGlobalWhitelist; |
| 38 | // @HiveField(12, defaultValue: null) |
| 39 | final Map<String, dynamic>? info; |
| 40 | |
| 41 | bool get enabled => _enabled; |
| 42 | |
| 43 | set enabled(bool value) => _enabled = value; |
| 44 | |
| 45 | ZanoAsset({ |
| 46 | this.fullName = '', |
| 47 | this.ticker = '', |
| 48 | required this.assetId, |
| 49 | this.decimalPoint = 12, |
| 50 | bool enabled = true, |
| 51 | this.iconPath, |
| 52 | this.owner = defaultOwner, |
| 53 | this.metaInfo = '', |
| 54 | required this.currentSupply, |
| 55 | this.hiddenSupply = false, |
| 56 | required this.totalMaxSupply, |
| 57 | this.isInGlobalWhitelist = false, |
| 58 | this.info, |
| 59 | }) : _enabled = enabled, |
| 60 | super( |
| 61 | name: fullName, |
| 62 | title: ticker.toUpperCase(), |
| 63 | fullName: fullName, |
| 64 | tag: 'ZANO', |
| 65 | iconPath: iconPath, |
| 66 | decimals: decimalPoint, |
| 67 | ); |
| 68 | |
| 69 | ZanoAsset.copyWith(ZanoAsset other, {String? assetId, bool enabled = true}) |
| 70 | : this.fullName = other.fullName, |
| 71 | this.ticker = other.ticker, |
| 72 | this.assetId = assetId ?? other.assetId, |
| 73 | this.decimalPoint = other.decimalPoint, |
| 74 | this._enabled = enabled && other.enabled, |
| 75 | this.iconPath = other.iconPath, |
| 76 | this.currentSupply = other.currentSupply, |
| 77 | this.hiddenSupply = other.hiddenSupply, |
| 78 | this.metaInfo = other.metaInfo, |
| 79 | this.owner = other.owner, |
| 80 | this.totalMaxSupply = other.totalMaxSupply, |
| 81 | this.isInGlobalWhitelist = other.isInGlobalWhitelist, |
| 82 | this.info = other.info, |
| 83 | super( |
| 84 | name: other.name, |
| 85 | title: other.ticker.toUpperCase(), |
| 86 | fullName: other.name, |
| 87 | tag: 'ZANO', |
| 88 | iconPath: other.iconPath, |
| 89 | decimals: other.decimalPoint, |
| 90 | enabled: enabled, |
| 91 | ); |
| 92 | |
| 93 | factory ZanoAsset.fromJson(Map<String, dynamic> json, {bool isInGlobalWhitelist = false}) { |
| 94 | Map<String, dynamic>? info; |
| 95 | try { |
| 96 | info = jsonDecode((json['meta_info'] as String?) ?? '{}') as Map<String, dynamic>?; |
| 97 | } catch (_) {} |
| 98 | |
| 99 | return ZanoAsset( |
| 100 | assetId: json['asset_id'] as String? ?? '', |
| 101 | currentSupply: bigIntFromDynamic(json['current_supply']), |
| 102 | decimalPoint: json['decimal_point'] as int? ?? 12, |
| 103 | fullName: json['full_name'] as String? ?? '', |
| 104 | hiddenSupply: json['hidden_supply'] as bool? ?? false, |
| 105 | metaInfo: json['meta_info'] as String? ?? '', |
| 106 | owner: json['owner'] as String? ?? '', |
| 107 | ticker: json['ticker'] as String? ?? '', |
| 108 | iconPath: info?['logo_url'] as String? ?? '', |
| 109 | totalMaxSupply: bigIntFromDynamic(json['total_max_supply']), |
| 110 | isInGlobalWhitelist: isInGlobalWhitelist, |
| 111 | info: info, |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | static const typeId = ZANO_ASSET_TYPE_ID; |
| 116 | static const zanoAssetsBoxName = 'zanoAssetsBox'; |
| 117 | static const defaultOwner = '0000000000000000000000000000000000000000000000000000000000000000'; |
| 118 | } |
| 119 | |
| 120 | BigInt bigIntFromDynamic(dynamic d) { |
| 121 | if (d is int) { |
| 122 | return BigInt.from(d); |
| 123 | } else if (d is BigInt) { |
| 124 | return d; |
| 125 | } else if (d == null) { |
| 126 | return BigInt.zero; |
| 127 | } else { |
| 128 | throw 'cannot cast value of type ${d.runtimeType} to BigInt'; |
| 129 | //return BigInt.zero; |
| 130 | } |
| 131 | } |