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