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