From fcf7e3dd6bea8c6579b8261bac724ac8ba54f28f Mon Sep 17 00:00:00 2001 From: Aaron Fiore Date: Mon, 17 Jun 2024 21:20:37 -0700 Subject: [PATCH] php: fetch: prices: refactor 'symbol' as 'asset' or 'ticker' Clarity through specificity. --- .../src/finance/lib/internal/fetch/fetch.php | 2 +- .../internal/fetch/prices/internal/base.php | 66 +++++++++---------- .../fetch/prices/internal/prices/crypto.php | 2 +- 3 files changed, 35 insertions(+), 35 deletions(-) diff --git a/container/src/finance/lib/internal/fetch/fetch.php b/container/src/finance/lib/internal/fetch/fetch.php index 5f17011..b679830 100644 --- a/container/src/finance/lib/internal/fetch/fetch.php +++ b/container/src/finance/lib/internal/fetch/fetch.php @@ -66,7 +66,7 @@ namespace docker_finance $env->set_env('API_PRICES_PATH', getenv('API_PRICES_PATH')); // Master price file $env->set_env('API_PRICES_API', getenv('API_PRICES_API')); // Price API to use $env->set_env('API_PRICES_KEY', getenv('API_PRICES_KEY')); // Price API key - $env->set_env('API_PRICES_SYMBOLS', getenv('API_PRICES_SYMBOLS')); // User-provided symbols + $env->set_env('API_PRICES_ASSETS', getenv('API_PRICES_ASSETS')); // User-provided assets // Exchanges $env->set_env('API_KEY', getenv('API_KEY')); // Exchange or blockchain scanner API key diff --git a/container/src/finance/lib/internal/fetch/prices/internal/base.php b/container/src/finance/lib/internal/fetch/prices/internal/base.php index 5db2500..adc4dbb 100644 --- a/container/src/finance/lib/internal/fetch/prices/internal/base.php +++ b/container/src/finance/lib/internal/fetch/prices/internal/base.php @@ -47,11 +47,11 @@ namespace docker_finance\prices\internal public function getter(array $asset, string $timestamp): array; /** - * @brief Prepare price data for given symbols - * @param string $symbols Unparsed envrionment assets (e.g., 'id/ticker,id/ticker,blockchain:id/ticker,...') - * @return array, array> Prices for all given symbols + * @brief Prepare price data for given assets + * @param string $assets Unparsed envrionment assets (e.g., 'id/ticker,id/ticker,blockchain:id/ticker,...') + * @return array, array> Prices for all given assets */ - public function reader(string $symbols): array; + public function reader(string $assets): array; /** * @brief Write price data to master prices journal @@ -90,15 +90,15 @@ namespace docker_finance\prices\internal /** * @brief Parse environment assets - * @param string $symbols Expected format: 'id/ticker,id/ticker,blockchain:id/ticker,...' e.g., 'bitcoin/BTC,ethereum/ETH,avalanche:0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e/USDC,...' + * @param string $assets Expected format: 'id/ticker,id/ticker,blockchain:id/ticker,...' e.g., 'bitcoin/BTC,ethereum/ETH,avalanche:0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e/USDC,...' * @details * * Caveats: * - * 1. There are multiple reasons why asset must be passed with symbol - * instead of symbol alone: + * 1. There are multiple reasons why asset must be passed with ticker + * instead of ticker alone: * - * a. CoinGecko uses same symbol for multiple asset ID's: + * a. CoinGecko uses same ticker for multiple asset ID's: * * ltc = binance-peg-litecoin * ltc = litecoin @@ -108,25 +108,25 @@ namespace docker_finance\prices\internal * b. Mobula will often require blockchain along with asset ID * NOTE: with Mobula, ID can also consist of a contract address * - * 2. Ticker-symbol comes *AFTER* ID because hledger's prices are: + * 2. Ticker comes *AFTER* ID because hledger's prices are: * * a. *CASE SENSITIVE* * * b. Will not understand the difference between (for example): - * aGUSD and AGUSD (CoinGecko will return lowercase symbol) + * aGUSD and AGUSD (CoinGecko will return lowercase ticker) * * @return array> Parsed environment entries */ - protected function parse_symbols(string $symbols): array + protected function parse_assets(string $assets): array { - utils\CLI::print_debug($symbols); + utils\CLI::print_debug($assets); - if (!str_contains($this->get_env()->get_env('API_PRICES_SYMBOLS'), '/')) { - utils\CLI::throw_fatal("malformed symbols format"); + if (!str_contains($this->get_env()->get_env('API_PRICES_ASSETS'), '/')) { + utils\CLI::throw_fatal("malformed assets format"); } $parsed = []; - $csv = explode(',', $symbols); + $csv = explode(',', $assets); for ($i = 0; $i < count($csv); $i++) { list($asset, $ticker) = explode('/', $csv[$i]); @@ -277,17 +277,17 @@ namespace docker_finance\prices\internal * array[1][1] = price
* * ...etc. - * @return array Date and price single-line entries without ID or symbol + * @return array Date and price single-line entries without ID or ticker */ abstract protected function parse_prices(array $prices): array; /** * @brief Create data for master price journal file - * @param string $symbol Given symbol associated with ID + * @param string $ticker Given ticker associated with ID * @param array $prices Array of [N][timestamp, price] for given year(s) * @return array Master price journal file data */ - protected function make_master(string $symbol, array $prices): array + protected function make_master(string $ticker, array $prices): array { $stack = []; // Final journal entries $average = 0; // Purely for printing @@ -295,7 +295,7 @@ namespace docker_finance\prices\internal foreach ($prices as $date => $price) { // Price journal entry line - $line = 'P ' . $date . ' ' . $symbol . ' ' . sprintf('%.8f', $price) . "\n"; + $line = 'P ' . $date . ' ' . $ticker . ' ' . sprintf('%.8f', $price) . "\n"; array_push($stack, $line); // Always push a placeholder $/USD for hledger calculations. @@ -311,19 +311,19 @@ namespace docker_finance\prices\internal // HACKS to get USD amount of unsupported upstream coins // - if ($symbol == 'AAVE') { + if ($ticker == 'AAVE') { $line = 'P ' . $date . ' ' . 'stkAAVE' . ' ' . sprintf('%.8f', $price) . "\n"; array_push($stack, $line); } // Hack for array('paxos-standard'=>'USDP') - if ($symbol == 'PAX') { + if ($ticker == 'PAX') { $line = 'P ' . $date . ' ' . 'USDP' . ' ' . sprintf('%.8f', $price) . "\n"; array_push($stack, $line); } // CGLD was changed to CELO at some point - if ($symbol == 'CGLD') { + if ($ticker == 'CGLD') { $line = 'P ' . $date . ' ' . 'CELO' . ' ' . sprintf('%.8f', $price) . "\n"; array_push($stack, $line); } @@ -332,9 +332,9 @@ namespace docker_finance\prices\internal $average = $price; } - // Print symbol and most recent price parsed + // Print ticker and most recent price parsed utils\CLI::print_custom(" \e[32m│\e[0m\n"); - utils\CLI::print_custom(" \e[32m├─\e[34m\e[1;3m $symbol\e[0m\n"); + utils\CLI::print_custom(" \e[32m├─\e[34m\e[1;3m {$ticker}\e[0m\n"); utils\CLI::print_custom(" \e[32m│ └─\e[37;2m " . $average . "\e[0m\n"); return $stack; @@ -380,17 +380,17 @@ namespace docker_finance\prices\internal return $response; } - public function reader(string $symbols): array + public function reader(string $assets): array { - // Prepared price journal for given symbols + // Prepared price journal for given assets $stack = []; // Timestamp based on given year $timestamp = $this->make_timestamp($this->get_env()->get_env('API_FETCH_YEAR')); - utils\CLI::print_normal(" ─ Symbols"); + utils\CLI::print_normal(" ─ Assets"); - foreach ($this->parse_symbols($symbols) as $asset) { + foreach ($this->parse_assets($assets) as $asset) { $parsed = $this->parse_prices($this->getter($asset, $timestamp)); $master = $this->make_master($asset['ticker'], $this->make_average($parsed)); @@ -405,12 +405,12 @@ namespace docker_finance\prices\internal public function writer(mixed $data, string $path): void { - // Cohesive array of all fetched symbols + // Cohesive array of all fetched assets $stack = []; - // Each element is an array of symbol prices - foreach($data as $symbol) { - foreach($symbol as $price) { + // Each element is an array of ticker prices + foreach($data as $ticker) { + foreach($ticker as $price) { array_push($stack, $price); } } @@ -420,7 +420,7 @@ namespace docker_finance\prices\internal public function fetcher(): void { - $master = $this->reader($this->get_env()->get_env('API_PRICES_SYMBOLS')); + $master = $this->reader($this->get_env()->get_env('API_PRICES_ASSETS')); $this->writer($master, $this->get_env()->get_env('API_PRICES_PATH')); } } diff --git a/container/src/finance/lib/internal/fetch/prices/internal/prices/crypto.php b/container/src/finance/lib/internal/fetch/prices/internal/prices/crypto.php index d1cfcb1..a0dbec0 100644 --- a/container/src/finance/lib/internal/fetch/prices/internal/prices/crypto.php +++ b/container/src/finance/lib/internal/fetch/prices/internal/prices/crypto.php @@ -193,7 +193,7 @@ namespace docker_finance\prices\internal\prices\crypto /** * @brief Parse fetched date and prices * @param array $prices Fetched prices [N]([timestamp][price])for given year(s) - * @return array Date and prices without ID or symbol + * @return array Date and prices without ID or ticker */ protected function parse_prices(array $prices): array {