CW-545-Overriding-Erc20-tokens-list (#1249)
* fix: Erc20 tokens configs list overriding each other in different ethereum wallets * feat: Add backward compatibility for wallets that already use the previous erc20tokens box
Adegoke David committed
Dec 30, 2023 at 16:28 UTC
f612516be06f353c92d946d5df709651926cf504
2 files changed
+37
-6
cw_core/lib/erc20_token.dart
+2
-1
@@ -61,7 +61,8 @@ class Erc20Token extends CryptoCurrency with HiveObjectMixin {
61
62
static const typeId = ERC20_TOKEN_TYPE_ID;
63
static const boxName = 'Erc20Tokens';
64
- static const polygonBoxName = ' PolygonErc20Tokens';
64
+ static const ethereumBoxName = 'EthereumErc20Tokens';
65
+ static const polygonBoxName = 'PolygonErc20Tokens';
66
67
@override
68
bool operator ==(other) =>
cw_ethereum/lib/ethereum_wallet.dart
+35
-5
@@ -75,6 +75,8 @@ abstract class EthereumWalletBase
75
76
late final Box<Erc20Token> erc20TokensBox;
77
78
+ late final Box<Erc20Token> ethereumErc20TokensBox;
79
+
80
late final EthPrivateKey _ethPrivateKey;
81
82
EthPrivateKey get ethPrivateKey => _ethPrivateKey;
@@ -102,7 +104,8 @@ abstract class EthereumWalletBase
104
Completer<SharedPreferences> _sharedPrefs = Completer();
105
106
Future<void> init() async {
105
- erc20TokensBox = await CakeHive.openBox<Erc20Token>(Erc20Token.boxName);
107
+ await movePreviousErc20BoxConfigsToNewBox();
108
+
109
await walletAddresses.init();
110
await transactionHistory.init();
111
_ethPrivateKey = await getPrivateKey(
@@ -114,6 +117,33 @@ abstract class EthereumWalletBase
117
await save();
118
}
119
120
+ /// Majorly for backward compatibility for previous configs that have been set.
121
+ Future<void> movePreviousErc20BoxConfigsToNewBox() async {
122
+ // Opens a box specific to this wallet
123
+ ethereumErc20TokensBox = await CakeHive.openBox<Erc20Token>(
124
+ "${walletInfo.name.replaceAll(" ", "_")}_${Erc20Token.ethereumBoxName}");
125
+
126
+ //Open the previous token configs box
127
+ erc20TokensBox = await CakeHive.openBox<Erc20Token>(Erc20Token.boxName);
128
+
129
+ // Check if it's empty, if it is, we stop the flow and return.
130
+ if (erc20TokensBox.isEmpty) {
131
+ // If it's empty, but the new wallet specific box is also empty,
132
+ // we load the initial tokens to the new box.
133
+ if (ethereumErc20TokensBox.isEmpty) addInitialTokens();
134
+ return;
135
+ }
136
+
137
+ final allValues = erc20TokensBox.values.toList();
138
+
139
+ // Clear and delete the old token box
140
+ await erc20TokensBox.clear();
141
+ await erc20TokensBox.deleteFromDisk();
142
+
143
+ // Add all the previous tokens with configs to the new box
144
+ ethereumErc20TokensBox.addAll(allValues);
145
+ }
146
+
147
@override
148
int calculateEstimatedFee(TransactionPriority priority, int? amount) {
149
try {
@@ -378,7 +408,7 @@ abstract class EthereumWalletBase
408
}
409
410
Future<void> _fetchErc20Balances() async {
381
- for (var token in erc20TokensBox.values) {
411
+ for (var token in ethereumErc20TokensBox.values) {
412
try {
413
if (token.enabled) {
414
balance[token] = await _client.fetchERC20Balances(
@@ -413,7 +443,7 @@ abstract class EthereumWalletBase
443
444
Future<void>? updateBalance() async => await _updateBalance();
445
416
- List<Erc20Token> get erc20Currencies => erc20TokensBox.values.toList();
446
+ List<Erc20Token> get erc20Currencies => ethereumErc20TokensBox.values.toList();
447
448
Future<void> addErc20Token(Erc20Token token) async {
449
String? iconPath;
@@ -433,7 +463,7 @@ abstract class EthereumWalletBase
463
iconPath: iconPath,
464
);
465
436
- await erc20TokensBox.put(_token.contractAddress, _token);
466
+ await ethereumErc20TokensBox.put(_token.contractAddress, _token);
467
468
if (_token.enabled) {
469
balance[_token] = await _client.fetchERC20Balances(
@@ -463,7 +493,7 @@ abstract class EthereumWalletBase
493
void addInitialTokens() {
494
final initialErc20Tokens = DefaultErc20Tokens().initialErc20Tokens;
495
466
- initialErc20Tokens.forEach((token) => erc20TokensBox.put(token.contractAddress, token));
496
+ initialErc20Tokens.forEach((token) => ethereumErc20TokensBox.put(token.contractAddress, token));
497
}
498
499
@override