php: fetch: prices: refactor 'symbol' as 'asset' or 'ticker'
Clarity through specificity.
This commit is contained in:
@@ -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_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_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_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
|
// Exchanges
|
||||||
$env->set_env('API_KEY', getenv('API_KEY')); // Exchange or blockchain scanner API key
|
$env->set_env('API_KEY', getenv('API_KEY')); // Exchange or blockchain scanner API key
|
||||||
|
|||||||
@@ -47,11 +47,11 @@ namespace docker_finance\prices\internal
|
|||||||
public function getter(array $asset, string $timestamp): array;
|
public function getter(array $asset, string $timestamp): array;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Prepare price data for given symbols
|
* @brief Prepare price data for given assets
|
||||||
* @param string $symbols Unparsed envrionment assets (e.g., 'id/ticker,id/ticker,blockchain:id/ticker,...')
|
* @param string $assets Unparsed envrionment assets (e.g., 'id/ticker,id/ticker,blockchain:id/ticker,...')
|
||||||
* @return array<int<0, max>, array<string>> Prices for all given symbols
|
* @return array<int<0, max>, array<string>> 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
|
* @brief Write price data to master prices journal
|
||||||
@@ -90,15 +90,15 @@ namespace docker_finance\prices\internal
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Parse environment assets
|
* @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
|
* @details
|
||||||
*
|
*
|
||||||
* Caveats:
|
* Caveats:
|
||||||
*
|
*
|
||||||
* 1. There are multiple reasons why asset must be passed with symbol
|
* 1. There are multiple reasons why asset must be passed with ticker
|
||||||
* instead of symbol alone:
|
* 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 = binance-peg-litecoin
|
||||||
* ltc = litecoin
|
* ltc = litecoin
|
||||||
@@ -108,25 +108,25 @@ namespace docker_finance\prices\internal
|
|||||||
* b. Mobula will often require blockchain along with asset ID
|
* b. Mobula will often require blockchain along with asset ID
|
||||||
* NOTE: with Mobula, ID can also consist of a contract address
|
* 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*
|
* a. *CASE SENSITIVE*
|
||||||
*
|
*
|
||||||
* b. Will not understand the difference between (for example):
|
* 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<array<string>> Parsed environment entries
|
* @return array<array<string>> 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'), '/')) {
|
if (!str_contains($this->get_env()->get_env('API_PRICES_ASSETS'), '/')) {
|
||||||
utils\CLI::throw_fatal("malformed symbols format");
|
utils\CLI::throw_fatal("malformed assets format");
|
||||||
}
|
}
|
||||||
|
|
||||||
$parsed = [];
|
$parsed = [];
|
||||||
$csv = explode(',', $symbols);
|
$csv = explode(',', $assets);
|
||||||
|
|
||||||
for ($i = 0; $i < count($csv); $i++) {
|
for ($i = 0; $i < count($csv); $i++) {
|
||||||
list($asset, $ticker) = explode('/', $csv[$i]);
|
list($asset, $ticker) = explode('/', $csv[$i]);
|
||||||
@@ -277,17 +277,17 @@ namespace docker_finance\prices\internal
|
|||||||
* array[1][1] = price<br>
|
* array[1][1] = price<br>
|
||||||
*
|
*
|
||||||
* ...etc.
|
* ...etc.
|
||||||
* @return array<string> Date and price single-line entries without ID or symbol
|
* @return array<string> Date and price single-line entries without ID or ticker
|
||||||
*/
|
*/
|
||||||
abstract protected function parse_prices(array $prices): array;
|
abstract protected function parse_prices(array $prices): array;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Create data for master price journal file
|
* @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<mixed> $prices Array of [N][timestamp, price] for given year(s)
|
* @param array<mixed> $prices Array of [N][timestamp, price] for given year(s)
|
||||||
* @return array<string> Master price journal file data
|
* @return array<string> 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
|
$stack = []; // Final journal entries
|
||||||
$average = 0; // Purely for printing
|
$average = 0; // Purely for printing
|
||||||
@@ -295,7 +295,7 @@ namespace docker_finance\prices\internal
|
|||||||
foreach ($prices as $date => $price) {
|
foreach ($prices as $date => $price) {
|
||||||
|
|
||||||
// Price journal entry line
|
// Price journal entry line
|
||||||
$line = 'P ' . $date . ' ' . $symbol . ' ' . sprintf('%.8f', $price) . "\n";
|
$line = 'P ' . $date . ' ' . $ticker . ' ' . sprintf('%.8f', $price) . "\n";
|
||||||
array_push($stack, $line);
|
array_push($stack, $line);
|
||||||
|
|
||||||
// Always push a placeholder $/USD for hledger calculations.
|
// 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
|
// HACKS to get USD amount of unsupported upstream coins
|
||||||
//
|
//
|
||||||
|
|
||||||
if ($symbol == 'AAVE') {
|
if ($ticker == 'AAVE') {
|
||||||
$line = 'P ' . $date . ' ' . 'stkAAVE' . ' ' . sprintf('%.8f', $price) . "\n";
|
$line = 'P ' . $date . ' ' . 'stkAAVE' . ' ' . sprintf('%.8f', $price) . "\n";
|
||||||
array_push($stack, $line);
|
array_push($stack, $line);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hack for array('paxos-standard'=>'USDP')
|
// Hack for array('paxos-standard'=>'USDP')
|
||||||
if ($symbol == 'PAX') {
|
if ($ticker == 'PAX') {
|
||||||
$line = 'P ' . $date . ' ' . 'USDP' . ' ' . sprintf('%.8f', $price) . "\n";
|
$line = 'P ' . $date . ' ' . 'USDP' . ' ' . sprintf('%.8f', $price) . "\n";
|
||||||
array_push($stack, $line);
|
array_push($stack, $line);
|
||||||
}
|
}
|
||||||
|
|
||||||
// CGLD was changed to CELO at some point
|
// CGLD was changed to CELO at some point
|
||||||
if ($symbol == 'CGLD') {
|
if ($ticker == 'CGLD') {
|
||||||
$line = 'P ' . $date . ' ' . 'CELO' . ' ' . sprintf('%.8f', $price) . "\n";
|
$line = 'P ' . $date . ' ' . 'CELO' . ' ' . sprintf('%.8f', $price) . "\n";
|
||||||
array_push($stack, $line);
|
array_push($stack, $line);
|
||||||
}
|
}
|
||||||
@@ -332,9 +332,9 @@ namespace docker_finance\prices\internal
|
|||||||
$average = $price;
|
$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[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");
|
utils\CLI::print_custom(" \e[32m│ └─\e[37;2m " . $average . "\e[0m\n");
|
||||||
|
|
||||||
return $stack;
|
return $stack;
|
||||||
@@ -380,17 +380,17 @@ namespace docker_finance\prices\internal
|
|||||||
return $response;
|
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 = [];
|
$stack = [];
|
||||||
|
|
||||||
// Timestamp based on given year
|
// Timestamp based on given year
|
||||||
$timestamp = $this->make_timestamp($this->get_env()->get_env('API_FETCH_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));
|
$parsed = $this->parse_prices($this->getter($asset, $timestamp));
|
||||||
$master = $this->make_master($asset['ticker'], $this->make_average($parsed));
|
$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
|
public function writer(mixed $data, string $path): void
|
||||||
{
|
{
|
||||||
// Cohesive array of all fetched symbols
|
// Cohesive array of all fetched assets
|
||||||
$stack = [];
|
$stack = [];
|
||||||
|
|
||||||
// Each element is an array of symbol prices
|
// Each element is an array of ticker prices
|
||||||
foreach($data as $symbol) {
|
foreach($data as $ticker) {
|
||||||
foreach($symbol as $price) {
|
foreach($ticker as $price) {
|
||||||
array_push($stack, $price);
|
array_push($stack, $price);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -420,7 +420,7 @@ namespace docker_finance\prices\internal
|
|||||||
|
|
||||||
public function fetcher(): void
|
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'));
|
$this->writer($master, $this->get_env()->get_env('API_PRICES_PATH'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -193,7 +193,7 @@ namespace docker_finance\prices\internal\prices\crypto
|
|||||||
/**
|
/**
|
||||||
* @brief Parse fetched date and prices
|
* @brief Parse fetched date and prices
|
||||||
* @param array<mixed> $prices Fetched prices [N]([timestamp][price])for given year(s)
|
* @param array<mixed> $prices Fetched prices [N]([timestamp][price])for given year(s)
|
||||||
* @return array<string> Date and prices without ID or symbol
|
* @return array<string> Date and prices without ID or ticker
|
||||||
*/
|
*/
|
||||||
protected function parse_prices(array $prices): array
|
protected function parse_prices(array $prices): array
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user