Skip to content

Commit

Permalink
Update coins_ci.json and refactor affected files
Browse files Browse the repository at this point in the history
  • Loading branch information
takenagain committed Dec 14, 2023
1 parent cbdb54a commit 47c3b40
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 34 deletions.
13 changes: 13 additions & 0 deletions assets/coins_ci.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"bundled_coins_repo_commit": "8cb5983ab858dde915e8de3175c15cd0f5572356",
"coins_repo_url": "https://raw.githubusercontent.com/KomodoPlatform/coins",
"coins_repo_branch": "master",
"runtime_updates_enabled": true,
"mapped_files": {
"assets/coins_config.json": "utils/coins_config.json",
"assets/coins.json": "coins"
},
"mapped_folders": {
"assets/coin-icons/": "icons/"
}
}
3 changes: 0 additions & 3 deletions coins_ci.json

This file was deleted.

39 changes: 21 additions & 18 deletions fetch_coin_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,37 @@
import shutil
import concurrent.futures
import argparse
from dataclasses import dataclass

# Constants
COINS_REPO_OWNER = "KomodoPlatform"
COINS_REPO_NAME = "coins"
COINS_REPO_ICONS_PATH = "icons"
COINS_URL = "https://raw.githubusercontent.com/KomodoPlatform/coins"
COINS_CI_PATH = "./coins_ci.json"
COINS_CI_PATH = "assets/coins_ci.json"
COINS_PATH = "./assets/coins.json"
COINS_CONFIG_PATH = "./assets/coins_config.json"
COIN_ICONS_PATH = "./assets/coin-icons"
ICON_DOWNLOAD_TIMEOUT_SECONDS = 10
CONCURRENCY_LIMIT = 4


@dataclass
class CoinData:
bundled_coins_repo_commit: str
coins_repo_url: str
coins_repo_branch: str
runtime_updates_enabled: bool
mapped_files: dict[str, str]
mapped_folders: dict[str, str]

@staticmethod
def load_from_file(path: str = COINS_CI_PATH):
with open(path, "r") as f:
data = json.load(f)
return CoinData(**data) # type: ignore


def main() -> None:
parser = init_argparse()
args = parser.parse_args()
Expand All @@ -33,8 +51,8 @@ def main() -> None:
print("Coin configs and icons already exist. Skipping download.")
return

coins_repo_commit = load_coins_commit()
download_coin_configs(coins_repo_commit)
coins_data = CoinData.load_from_file()
download_coin_configs(coins_data.bundled_coins_repo_commit)

if not os.path.exists(COIN_ICONS_PATH):
os.mkdir(COIN_ICONS_PATH)
Expand Down Expand Up @@ -126,21 +144,6 @@ def download_coin_icons(
future.result(timeout=timeout)


def load_coins_commit(ci_path: str = COINS_CI_PATH) -> str:
"""
Load the commit id from the coins_ci.json file
:param ci_path: path to the coins_ci.json file
:return: commit id
"""
with open(ci_path, "r") as f:
ci = json.load(f)

if "coins_repo_commit" not in ci:
raise Exception("coins_repo_commit not found in coins_ci.json")

return ci["coins_repo_commit"]


def init_argparse() -> argparse.ArgumentParser:
"""Initialize argparse
Expand Down
40 changes: 27 additions & 13 deletions lib/app_config/coins_updater.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:isolate';

import 'package:flutter/services.dart';
import 'package:http/http.dart' as http;
import 'package:komodo_dex/model/coin_ci.dart';
import 'package:komodo_dex/utils/log.dart';
import 'package:path_provider/path_provider.dart';

Expand Down Expand Up @@ -32,22 +35,19 @@ class CoinUpdater {

/// The branch of the coins repository to use.
//! QA: change branch name here and then restart twice after logging in.
static const coinsRepoBranch = 'master';

static const coinsRepoUrl =
'https://raw.githubusercontent.com/KomodoPlatform/coins';

static const isUpdateEnabled = true;

final String localAssetPathCIConfig = 'assets/coins_ci.json';
final String localAssetPathConfig = 'assets/coins_config.json';
final String localAssetPathCoins = 'assets/coins.json';

String get remotePathConfig =>
'$coinsRepoUrl/$coinsRepoBranch/utils/coins_config.json';
String get remotePathCoins => '$coinsRepoUrl/$coinsRepoBranch/coins';

String _cachedConfig;
String _cachedCoins;
CoinsCI _coinsCI;

Future<CoinsCI> _loadCoinsCIConfig() async {
final String coinsCI = await _fetchAsset(localAssetPathCIConfig);
final coinsCIResponse = jsonDecode(coinsCI);
return CoinsCI.fromJson(coinsCIResponse);
}

Future<String> _fetchAsset(String path) async {
return await rootBundle.loadString(path);
Expand All @@ -63,6 +63,7 @@ class CoinUpdater {
String remoteUrl,
String cacheName,
String cacheProperty,
bool runtimeUpdatesEnabled,
) async {
try {
if (cacheProperty != null) {
Expand All @@ -73,7 +74,7 @@ class CoinUpdater {

final cacheFileExists = await cacheFile.exists();

if (isUpdateEnabled) {
if (runtimeUpdatesEnabled) {
scheduleMicrotask(
() => _updateCacheInBackground(remoteUrl, cacheFile),
);
Expand Down Expand Up @@ -116,7 +117,8 @@ class CoinUpdater {
Log(
'CoinUpdater',
'Coin updater updated coins to latest commit on branch '
'$coinsRepoBranch from $coinsRepoUrl. \n $remoteUrl',
'${_coinsCI?.coinsRepoBranch} from ${_coinsCI?.coinsRepoUrl}. '
'\n $remoteUrl',
);
});
} catch (e) {
Expand All @@ -135,22 +137,34 @@ class CoinUpdater {
}
}

Future<String> _getAssetRemotePath(String localPath) async {
_coinsCI ??= await _loadCoinsCIConfig();
final mappedFile = _coinsCI?.mappedFiles[localPath];
return '${_coinsCI?.coinsRepoUrl}/${_coinsCI?.coinsRepoBranch}/$mappedFile';
}

Future<String> getConfig() async {
final String remotePathConfig =
await _getAssetRemotePath(localAssetPathConfig);
_cachedConfig = await _fetchOrCache(
localAssetPathConfig,
remotePathConfig,
'coins_config_cache.json',
_cachedConfig,
_coinsCI?.runtimeUpdatesEnabled ?? false,
);
return _cachedConfig;
}

Future<String> getCoins() async {
final String remotePathCoins =
await _getAssetRemotePath(localAssetPathCoins);
_cachedCoins = await _fetchOrCache(
localAssetPathCoins,
remotePathCoins,
'coins_cache.json',
_cachedCoins,
_coinsCI?.runtimeUpdatesEnabled ?? false,
);
return _cachedCoins;
}
Expand Down
28 changes: 28 additions & 0 deletions lib/model/coin_ci.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class CoinsCI {
final String bundledCoinsRepoCommit;
final String coinsRepoUrl;
final String coinsRepoBranch;
final bool runtimeUpdatesEnabled;
final Map<String, String> mappedFiles;
final Map<String, String> mappedFolders;

CoinsCI({
this.bundledCoinsRepoCommit,
this.coinsRepoUrl,
this.coinsRepoBranch,
this.runtimeUpdatesEnabled,
this.mappedFiles,
this.mappedFolders,
});

factory CoinsCI.fromJson(Map<String, dynamic> json) {
return CoinsCI(
bundledCoinsRepoCommit: json['bundled_coins_repo_commit'],
coinsRepoUrl: json['coins_repo_url'],
coinsRepoBranch: json['coins_repo_branch'],
runtimeUpdatesEnabled: json['runtime_updates_enabled'],
mappedFiles: Map<String, String>.from(json['mapped_files']),
mappedFolders: Map<String, String>.from(json['mapped_folders']),
);
}
}

0 comments on commit 47c3b40

Please sign in to comment.