Skip to content

Commit

Permalink
feat: update token list source (#1191)
Browse files Browse the repository at this point in the history
Co-authored-by: Kirill Bubochkin <ookami.kb@gmail.com>
justinenerio and ookami-kb authored Dec 30, 2023
1 parent 7415667 commit c74df4b
Showing 6 changed files with 31 additions and 10,843 deletions.
Original file line number Diff line number Diff line change
@@ -75,5 +75,7 @@ const _popularTokensCoinGeckoId = [
'usd-coin',
'tether',
'bonk',
'jito-staked-sol',
'jito-governance-token',
'neon',
'helium',
];
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
{
"name": "Testing Token List",
"logoURI": "",
"keywords": [
"solana",
"spl"
],
"tags": {
"stablecoin": {
"name": "stablecoin",
"description": "Tokens that are fixed to an external asset, e.g. the US dollar"
}
},
"timestamp": "2021-03-03T19:57:21+0000",
"tokens": [
{
@@ -52,10 +40,5 @@
"coingeckoId": "solana"
}
}
],
"version": {
"major": 0,
"minor": 0,
"patch": 0
}
]
}
10,772 changes: 1 addition & 10,771 deletions packages/espressocash_app/lib/features/tokens/solana.tokenlist.json

Large diffs are not rendered by default.

26 changes: 1 addition & 25 deletions packages/espressocash_app/lib/features/tokens/token.dart
Original file line number Diff line number Diff line change
@@ -172,37 +172,13 @@ class Extensions {
@JsonSerializable(createToJson: false)
class ParsedContent {
const ParsedContent({
required this.name,
required this.logoURI,
required this.keywords,
required this.timestamp,
required this.tokens,
required this.version,
});

factory ParsedContent.fromJson(Map<String, dynamic> json) =>
_$ParsedContentFromJson(json);

final String name;
final String? logoURI;
final List<String> keywords;
final DateTime timestamp;
final List<Token> tokens;
final Version version;
}

@JsonSerializable(createToJson: false)
class Version {
const Version({
required this.major,
required this.minor,
required this.patch,
});

factory Version.fromJson(Map<String, dynamic> data) =>
_$VersionFromJson(data);

final int major;
final int minor;
final int patch;
final DateTime timestamp;
}
4 changes: 2 additions & 2 deletions packages/espressocash_app/lib/features/tokens/token_list.dart
Original file line number Diff line number Diff line change
@@ -62,8 +62,8 @@ class TokenList {
tokens
.singleWhereOrNull(
(t) =>
t.symbol.toLowerCase() == symbol &&
t.coingeckoId == coingeckoId,
t.symbol.toLowerCase() == symbol?.toLowerCase() &&
t.coingeckoId?.toLowerCase() == coingeckoId?.toLowerCase(),
)
.ifNull(
() => _createStubToken(
49 changes: 23 additions & 26 deletions packages/espressocash_app/tool/update_token_list.dart
Original file line number Diff line number Diff line change
@@ -45,11 +45,21 @@ Future<_CoinMap> _fetchCoins() async {
.let(Map.fromEntries);
}

Future<_Json> _matchTokens(_CoinMap coins) async {
Future<_Json> _fetchTokenList() async {
final response = await http.get(Uri.parse(_tokenListUrl));
final coingecko = json.decode(response.body) as _Json;
if (response.statusCode == 200) {
return {
'tokens':
(json.decode(response.body) as List).map((e) => e as _Json).toList(),
};
}
throw Exception('Failed to load tokens');
}

return coingecko
Future<_Json> _matchTokens(_CoinMap coins) async {
final tokenList = await _fetchTokenList();

return tokenList
..update(
'tokens',
(tokens) => (tokens as List)
@@ -76,36 +86,23 @@ Future<void> _writeToFile(_Json coingecko) async {
..update(
'tokens',
(tokens) => (tokens as List)..addAll(nonMainnetTokens ?? <_Json>[]),
);
)
..['timestamp'] = DateTime.now().toIso8601String();

await file.writeAsString(jsonEncode(allTokens));
}

extension on _Json {
_Json updateToken(_CoinMap coins) {
final address = this['address'] as String;
final coinData = coins[address];

final coingeckoId = coinData?.coingeckoId;
final isStablecoin = coinData?.isStablecoin ?? false;

if (coingeckoId != null) this['extensions'] = {'coingeckoId': coingeckoId};
if (isStablecoin) this['tags'] = const ['stablecoin'];

return this
..update('chainId', (_) => _mainnetChainId)
..update('logoURI', (it) => it is String ? it.updateLogoUri() : '');
}
}

extension on String {
String updateLogoUri() {
final uri = Uri.parse(this);
final path = uri.path.replaceFirst('thumb', 'large');
final coin = coins[this['address'] as String];

uri.replace(path: path);
if (coin != null) {
this['extensions'] = {'coingeckoId': coin.coingeckoId};
if (coin.isStablecoin) this['tags'] = const ['stablecoin'];
this['chainId'] = _mainnetChainId;
}

return uri.toString();
return this;
}
}

@@ -125,6 +122,6 @@ const _mainnetChainId = 101;

const _stablecoinsUrl =
'https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&category=stablecoins';
const _tokenListUrl = 'https://tokens.coingecko.com/solana/all.json';
const _tokenListUrl = 'https://token.jup.ag/strict';
const _coinsUrl =
'https://api.coingecko.com/api/v3/coins/list?include_platform=true';

0 comments on commit c74df4b

Please sign in to comment.