From f612516be06f353c92d946d5df709651926cf504 Mon Sep 17 00:00:00 2001 From: Adegoke David <64401859+Blazebrain@users.noreply.github.com> Date: Sat, 30 Dec 2023 16:28:33 +0100 Subject: [PATCH] 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 --- cw_core/lib/erc20_token.dart | 3 ++- cw_ethereum/lib/ethereum_wallet.dart | 40 ++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/cw_core/lib/erc20_token.dart b/cw_core/lib/erc20_token.dart index 830ce383b..f8c2afc06 100644 --- a/cw_core/lib/erc20_token.dart +++ b/cw_core/lib/erc20_token.dart @@ -61,7 +61,8 @@ class Erc20Token extends CryptoCurrency with HiveObjectMixin { static const typeId = ERC20_TOKEN_TYPE_ID; static const boxName = 'Erc20Tokens'; - static const polygonBoxName = ' PolygonErc20Tokens'; + static const ethereumBoxName = 'EthereumErc20Tokens'; + static const polygonBoxName = 'PolygonErc20Tokens'; @override bool operator ==(other) => diff --git a/cw_ethereum/lib/ethereum_wallet.dart b/cw_ethereum/lib/ethereum_wallet.dart index 5c6cc4448..cd4bd84cc 100644 --- a/cw_ethereum/lib/ethereum_wallet.dart +++ b/cw_ethereum/lib/ethereum_wallet.dart @@ -75,6 +75,8 @@ abstract class EthereumWalletBase late final Box erc20TokensBox; + late final Box ethereumErc20TokensBox; + late final EthPrivateKey _ethPrivateKey; EthPrivateKey get ethPrivateKey => _ethPrivateKey; @@ -102,7 +104,8 @@ abstract class EthereumWalletBase Completer _sharedPrefs = Completer(); Future init() async { - erc20TokensBox = await CakeHive.openBox(Erc20Token.boxName); + await movePreviousErc20BoxConfigsToNewBox(); + await walletAddresses.init(); await transactionHistory.init(); _ethPrivateKey = await getPrivateKey( @@ -114,6 +117,33 @@ abstract class EthereumWalletBase await save(); } + /// Majorly for backward compatibility for previous configs that have been set. + Future movePreviousErc20BoxConfigsToNewBox() async { + // Opens a box specific to this wallet + ethereumErc20TokensBox = await CakeHive.openBox( + "${walletInfo.name.replaceAll(" ", "_")}_${Erc20Token.ethereumBoxName}"); + + //Open the previous token configs box + erc20TokensBox = await CakeHive.openBox(Erc20Token.boxName); + + // Check if it's empty, if it is, we stop the flow and return. + if (erc20TokensBox.isEmpty) { + // If it's empty, but the new wallet specific box is also empty, + // we load the initial tokens to the new box. + if (ethereumErc20TokensBox.isEmpty) addInitialTokens(); + return; + } + + final allValues = erc20TokensBox.values.toList(); + + // Clear and delete the old token box + await erc20TokensBox.clear(); + await erc20TokensBox.deleteFromDisk(); + + // Add all the previous tokens with configs to the new box + ethereumErc20TokensBox.addAll(allValues); + } + @override int calculateEstimatedFee(TransactionPriority priority, int? amount) { try { @@ -378,7 +408,7 @@ abstract class EthereumWalletBase } Future _fetchErc20Balances() async { - for (var token in erc20TokensBox.values) { + for (var token in ethereumErc20TokensBox.values) { try { if (token.enabled) { balance[token] = await _client.fetchERC20Balances( @@ -413,7 +443,7 @@ abstract class EthereumWalletBase Future? updateBalance() async => await _updateBalance(); - List get erc20Currencies => erc20TokensBox.values.toList(); + List get erc20Currencies => ethereumErc20TokensBox.values.toList(); Future addErc20Token(Erc20Token token) async { String? iconPath; @@ -433,7 +463,7 @@ abstract class EthereumWalletBase iconPath: iconPath, ); - await erc20TokensBox.put(_token.contractAddress, _token); + await ethereumErc20TokensBox.put(_token.contractAddress, _token); if (_token.enabled) { balance[_token] = await _client.fetchERC20Balances( @@ -463,7 +493,7 @@ abstract class EthereumWalletBase void addInitialTokens() { final initialErc20Tokens = DefaultErc20Tokens().initialErc20Tokens; - initialErc20Tokens.forEach((token) => erc20TokensBox.put(token.contractAddress, token)); + initialErc20Tokens.forEach((token) => ethereumErc20TokensBox.put(token.contractAddress, token)); } @override