CW-608-Fetch-And-Save-Icons-Of-ERC20-And-SPL-Tokens-When-Adding-Them (#1365)

* feat: Fetch and save icons of SPL tokens when adding them

* feat: Implement fetch and save icons for ERC20 tokens when adding them

* fix: Add moralisApiKey to evm secrets

* Add check to ensure decimals cannot be zero

* - Fallback to adding erc20 token from web3dart
- Wrap fetching spl token icon in a try/catch block

---------

Co-authored-by: OmarHatem <omarh.ismail1@gmail.com>
This commit is contained in:
Adegoke David 2024-04-08 23:53:14 +01:00 committed by GitHub
parent d4b317b3a0
commit 2877cc160c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
41 changed files with 182 additions and 69 deletions

View file

@ -533,4 +533,21 @@ class SolanaWalletClient {
throw Exception(e);
}
}
Future<String?> getIconImageFromTokenUri(String uri) async {
try {
final response = await httpClient.get(Uri.parse(uri));
final jsonResponse = json.decode(response.body) as Map<String, dynamic>;
if (response.statusCode >= 200 && response.statusCode < 300) {
return jsonResponse['image'];
} else {
return null;
}
} catch (e) {
print('Error occurred while fetching token image: \n${e.toString()}');
return null;
}
}
}

View file

@ -464,11 +464,17 @@ abstract class SolanaWalletBase
return null;
}
String? iconPath;
try {
iconPath = await _client.getIconImageFromTokenUri(token.uri);
} catch (_) {}
return SPLToken.fromMetadata(
name: token.name,
mint: token.mint,
symbol: token.symbol,
mintAddress: mintAddress,
iconPath: iconPath,
);
} catch (e) {
return null;

View file

@ -55,6 +55,7 @@ class SPLToken extends CryptoCurrency with HiveObjectMixin {
required String mint,
required String symbol,
required String mintAddress,
String? iconPath
}) {
return SPLToken(
name: name,
@ -62,7 +63,7 @@ class SPLToken extends CryptoCurrency with HiveObjectMixin {
mintAddress: mintAddress,
decimal: 0,
mint: mint,
iconPath: '',
iconPath: iconPath,
);
}