LCOV - code coverage report
Current view: top level - domain/entities - coin_entity.dart (source / functions) Coverage Total Hit
Test: lcov.info Lines: 30.1 % 146 44
Test Date: 2025-06-07 01:20:49 Functions: - 0 0

            Line data    Source code
       1              : import 'dart:math';
       2              : import 'package:walletkit_dart/src/domain/predefined_assets.dart';
       3              : import 'package:walletkit_dart/walletkit_dart.dart';
       4              : 
       5              : const CoinEntity nullToken = CoinEntity(
       6              :   name: '',
       7              :   symbol: '',
       8              :   decimals: 0,
       9              : );
      10              : 
      11              : class CoinEntity {
      12              :   final String name;
      13              :   final String symbol;
      14              :   final int decimals;
      15              : 
      16           80 :   const CoinEntity({
      17              :     required this.name,
      18              :     required this.symbol,
      19              :     required this.decimals,
      20              :   });
      21              : 
      22            0 :   num get subunits => pow(10, decimals);
      23              : 
      24            0 :   String get identifier => "$name:$symbol:$decimals";
      25              : 
      26            0 :   ERC20Entity? get asEthBased => this is ERC20Entity ? this as ERC20Entity : null;
      27              : 
      28            0 :   ERC1155Entity? get asERC1155 => this is ERC1155Entity ? this as ERC1155Entity : null;
      29              : 
      30            0 :   bool get isEvm => this is EvmCoinEntity;
      31            0 :   bool get isERC20 => this is ERC20Entity;
      32            0 :   bool get isERC1155 => this is ERC1155Entity;
      33            1 :   bool get isUTXO => switch (this) {
      34            5 :         btcCoin || ltcCoin || zeniqCoin || bchCoin || ec8Coin => true,
      35              :         _ => false,
      36              :       };
      37              : 
      38            9 :   @override
      39           72 :   int get hashCode => name.hashCode ^ symbol.hashCode ^ decimals.hashCode;
      40              : 
      41           14 :   @override
      42              :   bool operator ==(Object other) {
      43              :     if (identical(this, other)) return true;
      44              : 
      45           13 :     return other is CoinEntity &&
      46           13 :         other is! EvmCoinEntity &&
      47           39 :         other.name == name &&
      48            0 :         other.symbol == symbol &&
      49            0 :         other.decimals == decimals;
      50              :   }
      51              : 
      52            0 :   @override
      53              :   String toString() {
      54            0 :     return '$name ($symbol)';
      55              :   }
      56              : 
      57            1 :   Map<String, dynamic> toJson() {
      58              :     return switch (this) {
      59            1 :       ERC20Entity ethBasedToken => {
      60            0 :           'name': ethBasedToken.name,
      61            0 :           'symbol': ethBasedToken.symbol,
      62            0 :           'decimals': ethBasedToken.decimals,
      63            0 :           'chainID': ethBasedToken.chainID,
      64            0 :           'contractAddress': ethBasedToken.contractAddress,
      65            0 :           'allowDeletion': ethBasedToken.allowDeletion,
      66              :         },
      67            1 :       ERC1155Entity erc1155Token => {
      68            0 :           'name': erc1155Token.name,
      69            0 :           'symbol': erc1155Token.symbol,
      70            0 :           'contractAddress': erc1155Token.contractAddress,
      71            0 :           'tokenId': erc1155Token.tokenId.toString(),
      72            0 :           'allowDeletion': erc1155Token.allowDeletion,
      73            0 :           'chainID': erc1155Token.chainID,
      74              :         },
      75            1 :       EvmCoinEntity evmEntity => {
      76            0 :           'name': evmEntity.name,
      77            0 :           'symbol': evmEntity.symbol,
      78            0 :           'decimals': evmEntity.decimals,
      79            0 :           'chainID': evmEntity.chainID,
      80              :         },
      81            2 :       CoinEntity tokenEntity => {
      82            1 :           'name': tokenEntity.name,
      83            1 :           'symbol': tokenEntity.symbol,
      84            1 :           'decimals': tokenEntity.decimals,
      85              :         },
      86              :     };
      87              :   }
      88              : 
      89            1 :   factory CoinEntity.fromJson(Map json) {
      90              :     return switch (json) {
      91              :       {
      92            2 :         'name': String name,
      93            2 :         'symbol': String symbol,
      94            2 :         'decimals': int decimals,
      95            1 :         'chainID': int chainID,
      96            1 :         'contractAddress': String contractAddress,
      97              :       } =>
      98            0 :         ERC20Entity(
      99              :           name: name,
     100              :           symbol: symbol,
     101              :           decimals: decimals,
     102              :           chainID: chainID,
     103              :           contractAddress: contractAddress,
     104            0 :           allowDeletion: json['allowDeletion'] ?? true,
     105              :         ),
     106              :       {
     107            1 :         "name": String name,
     108            1 :         "symbol": String symbol,
     109            0 :         "contractAddress": String contractAddress,
     110            0 :         "tokenId": String tokenId,
     111            0 :         "chainID": int chainID,
     112              :       } =>
     113            0 :         ERC1155Entity(
     114              :           name: name,
     115              :           symbol: symbol,
     116              :           contractAddress: contractAddress,
     117            0 :           tokenId: BigInt.parse(tokenId),
     118              :           chainID: chainID,
     119            0 :           allowDeletion: json['allowDeletion'] ?? true,
     120              :         ),
     121              :       {
     122            1 :         'name': String name,
     123            1 :         'symbol': String symbol,
     124            1 :         'decimals': int decimals,
     125            0 :         'chainID': int chainID,
     126              :       } =>
     127            0 :         EvmCoinEntity(
     128              :           name: name,
     129              :           symbol: symbol,
     130              :           decimals: decimals,
     131              :           chainID: chainID,
     132              :         ),
     133              :       {
     134            1 :         'name': String name,
     135            1 :         'symbol': String symbol,
     136            1 :         'decimals': int decimals,
     137              :       } =>
     138            1 :         CoinEntity(
     139              :           name: name,
     140              :           symbol: symbol,
     141              :           decimals: decimals,
     142              :         ),
     143            0 :       _ => throw Exception("Invalid TokenEntity JSON"),
     144              :     };
     145              :   }
     146              : }
     147              : 
     148              : class EvmCoinEntity extends CoinEntity {
     149              :   final int chainID;
     150              :   final bool? allowDeletion;
     151              : 
     152            0 :   @override
     153            0 :   String get identifier => "$name:$symbol:$decimals:$chainID";
     154              : 
     155            0 :   @override
     156            0 :   int get hashCode => chainID.hashCode;
     157              : 
     158            1 :   @override
     159              :   bool operator ==(Object other) {
     160              :     if (identical(this, other)) return true;
     161              : 
     162            0 :     return other is EvmCoinEntity &&
     163            0 :         other is! ERC20Entity &&
     164            0 :         other is! ERC721Entity &&
     165            0 :         other is! ERC1155Entity &&
     166            0 :         other.chainID == chainID;
     167              :   }
     168              : 
     169           40 :   const EvmCoinEntity({
     170              :     required super.name,
     171              :     required super.symbol,
     172              :     required super.decimals,
     173              :     required this.chainID,
     174              :     this.allowDeletion = false,
     175              :   });
     176              : 
     177            0 :   EvmCoinEntity copyWith({
     178              :     String? name,
     179              :     String? symbol,
     180              :     int? decimals,
     181              :     int? chainID,
     182              :     bool? allowDeletion,
     183              :   }) {
     184            0 :     return EvmCoinEntity(
     185            0 :       name: name ?? this.name,
     186            0 :       symbol: symbol ?? this.symbol,
     187            0 :       decimals: decimals ?? this.decimals,
     188            0 :       chainID: chainID ?? this.chainID,
     189            0 :       allowDeletion: allowDeletion ?? this.allowDeletion,
     190              :     );
     191              :   }
     192              : }
     193              : 
     194              : class ERC1155Entity extends EvmCoinEntity {
     195              :   final String contractAddress;
     196              :   final BigInt tokenId;
     197              : 
     198            0 :   const ERC1155Entity({
     199              :     required super.name,
     200              :     required super.symbol,
     201              :     required super.chainID,
     202              :     required this.contractAddress,
     203              :     required this.tokenId,
     204              :     super.allowDeletion,
     205            0 :   }) : super(decimals: 0);
     206              : 
     207            0 :   String get lowerCaseAddress => contractAddress.toLowerCase();
     208              : 
     209            0 :   @override
     210            0 :   int get hashCode => lowerCaseAddress.hashCode ^ chainID.hashCode ^ tokenId.hashCode;
     211              : 
     212            0 :   @override
     213              :   bool operator ==(Object other) {
     214              :     if (identical(this, other)) return true;
     215              : 
     216            0 :     return other is ERC1155Entity &&
     217            0 :         other.lowerCaseAddress == lowerCaseAddress &&
     218            0 :         other.chainID == chainID &&
     219            0 :         other.tokenId == tokenId;
     220              :   }
     221              : 
     222            0 :   ERC1155Entity copyWith({
     223              :     String? name,
     224              :     String? symbol,
     225              :     int? decimals,
     226              :     String? contractAddress,
     227              :     BigInt? tokenId,
     228              :     bool? allowDeletion,
     229              :     int? chainID,
     230              :   }) {
     231            0 :     return ERC1155Entity(
     232            0 :       name: name ?? this.name,
     233            0 :       symbol: symbol ?? this.symbol,
     234            0 :       contractAddress: contractAddress ?? this.contractAddress,
     235            0 :       tokenId: tokenId ?? this.tokenId,
     236            0 :       allowDeletion: allowDeletion ?? this.allowDeletion,
     237            0 :       chainID: chainID ?? this.chainID,
     238              :     );
     239              :   }
     240              : 
     241            0 :   factory ERC1155Entity.fromJson(
     242              :     Map<String, dynamic> json, {
     243              :     required bool allowDeletion,
     244              :     required int chainID,
     245              :   }) {
     246            0 :     return ERC1155Entity(
     247            0 :       name: json['name'],
     248            0 :       symbol: json['symbol'],
     249            0 :       contractAddress: json['contractAddress'],
     250            0 :       tokenId: BigInt.parse(json['tokenId']),
     251              :       allowDeletion: allowDeletion,
     252              :       chainID: chainID,
     253              :     );
     254              :   }
     255              : }
     256              : 
     257              : class ERC20Entity extends EvmCoinEntity {
     258              :   final String contractAddress;
     259              : 
     260           40 :   const ERC20Entity({
     261              :     required super.name,
     262              :     required super.symbol,
     263              :     required super.decimals,
     264              :     required super.chainID,
     265              :     required this.contractAddress,
     266              :     super.allowDeletion,
     267              :   });
     268              : 
     269            6 :   String get lowerCaseAddress => contractAddress.toLowerCase();
     270              : 
     271            0 :   @override
     272            0 :   String get identifier => "$name:$symbol:$decimals:$contractAddress:$chainID";
     273              : 
     274            1 :   @override
     275            5 :   int get hashCode => lowerCaseAddress.hashCode ^ chainID.hashCode;
     276              : 
     277            1 :   @override
     278              :   bool operator ==(Object other) {
     279              :     if (identical(this, other)) return true;
     280              : 
     281            1 :     return other is ERC20Entity &&
     282            3 :         other.lowerCaseAddress == lowerCaseAddress &&
     283            3 :         other.chainID == chainID;
     284              :   }
     285              : 
     286            0 :   factory ERC20Entity.fromJson(
     287              :     Map<String, dynamic> json, {
     288              :     required bool allowDeletion,
     289              :     required int chainID,
     290              :   }) {
     291            0 :     return ERC20Entity(
     292            0 :       name: json['name'],
     293            0 :       symbol: json['symbol'],
     294            0 :       decimals: json['decimals'],
     295            0 :       contractAddress: json['contractAddress'],
     296              :       allowDeletion: allowDeletion,
     297              :       chainID: chainID,
     298              :     );
     299              :   }
     300              : 
     301            0 :   ERC20Entity copyWith({
     302              :     String? name,
     303              :     String? symbol,
     304              :     int? decimals,
     305              :     String? contractAddress,
     306              :     bool? allowDeletion,
     307              :     int? chainID,
     308              :   }) {
     309            0 :     return ERC20Entity(
     310            0 :       name: name ?? this.name,
     311            0 :       symbol: symbol ?? this.symbol,
     312            0 :       decimals: decimals ?? this.decimals,
     313            0 :       contractAddress: contractAddress ?? this.contractAddress,
     314            0 :       allowDeletion: allowDeletion ?? this.allowDeletion,
     315            0 :       chainID: chainID ?? this.chainID,
     316              :     );
     317              :   }
     318              : }
     319              : 
     320              : class ERC721Entity extends EvmCoinEntity {
     321              :   final String contractAddress;
     322              :   final BigInt tokenId;
     323              : 
     324            1 :   const ERC721Entity({
     325              :     required super.name,
     326              :     required super.symbol,
     327              :     required super.chainID,
     328              :     required this.contractAddress,
     329              :     required this.tokenId,
     330            1 :   }) : super(decimals: 0);
     331              : 
     332            0 :   String get lowerCaseAddress => contractAddress.toLowerCase();
     333              : 
     334            0 :   @override
     335            0 :   int get hashCode => lowerCaseAddress.hashCode ^ chainID.hashCode ^ tokenId.hashCode;
     336              : 
     337            0 :   @override
     338              :   bool operator ==(Object other) {
     339              :     if (identical(this, other)) return true;
     340              : 
     341            0 :     return other is ERC721Entity &&
     342            0 :         other.lowerCaseAddress == lowerCaseAddress &&
     343            0 :         other.chainID == chainID &&
     344            0 :         other.tokenId == tokenId;
     345              :   }
     346              : }
        

Generated by: LCOV version 2.0-1