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
This commit is contained in:
Adegoke David 2023-12-30 16:28:33 +01:00 committed by GitHub
parent 914565eb72
commit f612516be0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 6 deletions

View file

@ -61,7 +61,8 @@ class Erc20Token extends CryptoCurrency with HiveObjectMixin {
static const typeId = ERC20_TOKEN_TYPE_ID; static const typeId = ERC20_TOKEN_TYPE_ID;
static const boxName = 'Erc20Tokens'; static const boxName = 'Erc20Tokens';
static const polygonBoxName = ' PolygonErc20Tokens'; static const ethereumBoxName = 'EthereumErc20Tokens';
static const polygonBoxName = 'PolygonErc20Tokens';
@override @override
bool operator ==(other) => bool operator ==(other) =>

View file

@ -75,6 +75,8 @@ abstract class EthereumWalletBase
late final Box<Erc20Token> erc20TokensBox; late final Box<Erc20Token> erc20TokensBox;
late final Box<Erc20Token> ethereumErc20TokensBox;
late final EthPrivateKey _ethPrivateKey; late final EthPrivateKey _ethPrivateKey;
EthPrivateKey get ethPrivateKey => _ethPrivateKey; EthPrivateKey get ethPrivateKey => _ethPrivateKey;
@ -102,7 +104,8 @@ abstract class EthereumWalletBase
Completer<SharedPreferences> _sharedPrefs = Completer(); Completer<SharedPreferences> _sharedPrefs = Completer();
Future<void> init() async { Future<void> init() async {
erc20TokensBox = await CakeHive.openBox<Erc20Token>(Erc20Token.boxName); await movePreviousErc20BoxConfigsToNewBox();
await walletAddresses.init(); await walletAddresses.init();
await transactionHistory.init(); await transactionHistory.init();
_ethPrivateKey = await getPrivateKey( _ethPrivateKey = await getPrivateKey(
@ -114,6 +117,33 @@ abstract class EthereumWalletBase
await save(); await save();
} }
/// Majorly for backward compatibility for previous configs that have been set.
Future<void> movePreviousErc20BoxConfigsToNewBox() async {
// Opens a box specific to this wallet
ethereumErc20TokensBox = await CakeHive.openBox<Erc20Token>(
"${walletInfo.name.replaceAll(" ", "_")}_${Erc20Token.ethereumBoxName}");
//Open the previous token configs box
erc20TokensBox = await CakeHive.openBox<Erc20Token>(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 @override
int calculateEstimatedFee(TransactionPriority priority, int? amount) { int calculateEstimatedFee(TransactionPriority priority, int? amount) {
try { try {
@ -378,7 +408,7 @@ abstract class EthereumWalletBase
} }
Future<void> _fetchErc20Balances() async { Future<void> _fetchErc20Balances() async {
for (var token in erc20TokensBox.values) { for (var token in ethereumErc20TokensBox.values) {
try { try {
if (token.enabled) { if (token.enabled) {
balance[token] = await _client.fetchERC20Balances( balance[token] = await _client.fetchERC20Balances(
@ -413,7 +443,7 @@ abstract class EthereumWalletBase
Future<void>? updateBalance() async => await _updateBalance(); Future<void>? updateBalance() async => await _updateBalance();
List<Erc20Token> get erc20Currencies => erc20TokensBox.values.toList(); List<Erc20Token> get erc20Currencies => ethereumErc20TokensBox.values.toList();
Future<void> addErc20Token(Erc20Token token) async { Future<void> addErc20Token(Erc20Token token) async {
String? iconPath; String? iconPath;
@ -433,7 +463,7 @@ abstract class EthereumWalletBase
iconPath: iconPath, iconPath: iconPath,
); );
await erc20TokensBox.put(_token.contractAddress, _token); await ethereumErc20TokensBox.put(_token.contractAddress, _token);
if (_token.enabled) { if (_token.enabled) {
balance[_token] = await _client.fetchERC20Balances( balance[_token] = await _client.fetchERC20Balances(
@ -463,7 +493,7 @@ abstract class EthereumWalletBase
void addInitialTokens() { void addInitialTokens() {
final initialErc20Tokens = DefaultErc20Tokens().initialErc20Tokens; final initialErc20Tokens = DefaultErc20Tokens().initialErc20Tokens;
initialErc20Tokens.forEach((token) => erc20TokensBox.put(token.contractAddress, token)); initialErc20Tokens.forEach((token) => ethereumErc20TokensBox.put(token.contractAddress, token));
} }
@override @override