| 1 | import "package:cw_core/crypto_currency.dart"; |
| 2 | import "package:cw_core/db/sqlite.dart"; |
| 3 | import "package:sqflite/sqflite.dart"; |
| 4 | |
| 5 | class Erc20Token extends CryptoCurrency { |
| 6 | Erc20Token({ |
| 7 | required this.name, |
| 8 | required this.symbol, |
| 9 | required this.contractAddress, |
| 10 | required this.decimal, |
| 11 | bool enabled = true, |
| 12 | this.iconPath, |
| 13 | this.tag, |
| 14 | this.isPotentialScam = false, |
| 15 | this.id = 0, |
| 16 | this.walletName, |
| 17 | this.chainId, |
| 18 | }) : _enabled = enabled, |
| 19 | super( |
| 20 | name: symbol.toLowerCase(), |
| 21 | title: symbol.toUpperCase(), |
| 22 | fullName: name, |
| 23 | tag: tag, |
| 24 | iconPath: iconPath, |
| 25 | decimals: decimal, |
| 26 | isPotentialScam: isPotentialScam, |
| 27 | ); |
| 28 | |
| 29 | Erc20Token.copyWith( |
| 30 | Erc20Token other, { |
| 31 | String? icon, |
| 32 | super.tag, |
| 33 | bool? enabled, |
| 34 | String? walletName, |
| 35 | int? chainId, |
| 36 | }) : name = other.name, |
| 37 | symbol = other.symbol, |
| 38 | contractAddress = other.contractAddress, |
| 39 | decimal = other.decimal, |
| 40 | _enabled = enabled ?? other.enabled, |
| 41 | tag = tag ?? other.tag, |
| 42 | iconPath = icon ?? other.iconPath, |
| 43 | isPotentialScam = other.isPotentialScam, |
| 44 | id = 0, |
| 45 | walletName = walletName ?? other.walletName, |
| 46 | chainId = chainId ?? other.chainId, |
| 47 | super( |
| 48 | name: other.name, |
| 49 | title: other.symbol.toUpperCase(), |
| 50 | fullName: other.name, |
| 51 | iconPath: icon, |
| 52 | decimals: other.decimal, |
| 53 | isPotentialScam: other.isPotentialScam, |
| 54 | ); |
| 55 | |
| 56 | Erc20Token.fromMap(Map<String, Object?> map) |
| 57 | : this( |
| 58 | name: map["name"] as String? ?? "", |
| 59 | symbol: map["symbol"] as String? ?? "", |
| 60 | contractAddress: map["contractAddress"] as String? ?? "", |
| 61 | decimal: (map["decimal"] ?? 0) as int, |
| 62 | enabled: _getBoolFromDB(map["enabled"], defaultValue: true), |
| 63 | iconPath: map["iconPath"] as String?, |
| 64 | tag: map["tag"] as String?, |
| 65 | isPotentialScam: _getBoolFromDB(map["isPotentialScam"]), |
| 66 | id: (map[selfIdColumn] ?? 0) as int, |
| 67 | walletName: map["walletName"] as String?, |
| 68 | chainId: map["chainId"] as int?, |
| 69 | ); |
| 70 | |
| 71 | @override |
| 72 | final String name; |
| 73 | |
| 74 | @override |
| 75 | final String symbol; |
| 76 | |
| 77 | @override |
| 78 | String? iconPath; |
| 79 | |
| 80 | @override |
| 81 | final String? tag; |
| 82 | |
| 83 | @override |
| 84 | bool isPotentialScam; |
| 85 | |
| 86 | @override |
| 87 | bool get enabled => _enabled; |
| 88 | |
| 89 | @override |
| 90 | set enabled(bool value) => _enabled = value; |
| 91 | |
| 92 | int id; |
| 93 | int? chainId; |
| 94 | bool _enabled; |
| 95 | final int decimal; |
| 96 | String? walletName; |
| 97 | final String contractAddress; |
| 98 | |
| 99 | static bool _getBoolFromDB(value, {bool? defaultValue}) { |
| 100 | if (value is bool) { |
| 101 | return value; |
| 102 | } else if (value is int) { |
| 103 | return value == 1; |
| 104 | } else { |
| 105 | return defaultValue ?? false; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | Map<String, dynamic> toMap() => { |
| 110 | selfIdColumn: id, |
| 111 | "walletName": walletName, |
| 112 | "chainId": chainId, |
| 113 | "name": name, |
| 114 | "symbol": symbol, |
| 115 | "contractAddress": contractAddress.toLowerCase(), |
| 116 | "decimal": decimal, |
| 117 | "enabled": _enabled ? 1 : 0, |
| 118 | "iconPath": iconPath, |
| 119 | "tag": tag, |
| 120 | "isPotentialScam": isPotentialScam ? 1 : 0, |
| 121 | }; |
| 122 | |
| 123 | static String get tableName => "Erc20Token"; |
| 124 | static String get selfIdColumn => "${tableName}Id"; |
| 125 | |
| 126 | Future<int> save() async { |
| 127 | if (walletName == null || chainId == null) { |
| 128 | throw StateError("Erc20Token.save() requires walletName and chainId to be set"); |
| 129 | } |
| 130 | |
| 131 | final json = toMap(); |
| 132 | if (json[selfIdColumn] == 0) { |
| 133 | json[selfIdColumn] = null; |
| 134 | } |
| 135 | id = await db!.insert(tableName, json, conflictAlgorithm: ConflictAlgorithm.replace); |
| 136 | return id; |
| 137 | } |
| 138 | |
| 139 | static Future<List<Erc20Token>> selectList( |
| 140 | String where, |
| 141 | List<dynamic> whereArgs, { |
| 142 | String? orderBy, |
| 143 | }) async { |
| 144 | orderBy ??= selfIdColumn; |
| 145 | final list = await db!.query( |
| 146 | tableName, |
| 147 | where: where.isNotEmpty ? where : "1 = 1", |
| 148 | whereArgs: whereArgs.isNotEmpty ? whereArgs : null, |
| 149 | orderBy: orderBy, |
| 150 | ); |
| 151 | return List.generate(list.length, (index) => Erc20Token.fromMap(list[index])); |
| 152 | } |
| 153 | |
| 154 | static Future<List<Erc20Token>> getAllForWallet(String walletName, int chainId) => |
| 155 | selectList("walletName = ? AND chainId = ?", [walletName, chainId]); |
| 156 | |
| 157 | static Future<Erc20Token?> getByContract( |
| 158 | String walletName, |
| 159 | int chainId, |
| 160 | String contractAddress, |
| 161 | ) async { |
| 162 | final list = await selectList( |
| 163 | "walletName = ? AND chainId = ? AND contractAddress = ?", |
| 164 | [walletName, chainId, contractAddress.toLowerCase()], |
| 165 | ); |
| 166 | return list.isEmpty ? null : list.first; |
| 167 | } |
| 168 | |
| 169 | static Future<int> deleteForWallet(String walletName, int chainId, String contractAddress) => |
| 170 | db!.delete( |
| 171 | tableName, |
| 172 | where: "walletName = ? AND chainId = ? AND contractAddress = ?", |
| 173 | whereArgs: [walletName, chainId, contractAddress.toLowerCase()], |
| 174 | ); |
| 175 | |
| 176 | static Future<int> deleteAllForWallet(String walletName) => |
| 177 | db!.delete(tableName, where: "walletName = ?", whereArgs: [walletName]); |
| 178 | |
| 179 | static Future<void> renameWallet(String oldName, String newName) async { |
| 180 | await db!.delete(tableName, where: "walletName = ?", whereArgs: [newName]); |
| 181 | await db! |
| 182 | .update(tableName, {"walletName": newName}, where: "walletName = ?", whereArgs: [oldName]); |
| 183 | } |
| 184 | |
| 185 | @override |
| 186 | bool operator ==(Object other) => other is Erc20Token && other.contractAddress == contractAddress; |
| 187 | |
| 188 | @override |
| 189 | int get hashCode => contractAddress.hashCode; |
| 190 | } |