diff --git a/container/src/finance/lib/internal/lib_fetch.bash b/container/src/finance/lib/internal/lib_fetch.bash index 33ff3cb..a3b0a7f 100644 --- a/container/src/finance/lib/internal/lib_fetch.bash +++ b/container/src/finance/lib/internal/lib_fetch.bash @@ -61,10 +61,12 @@ function lib_fetch::__parse_args() all${global_arg_delim_2} - Ticker symbols (see documentation): + Asset values (see documentation): price${global_arg_delim_2} + api${global_arg_delim_2} + Support account(s): account${global_arg_delim_2} @@ -80,27 +82,33 @@ function lib_fetch::__parse_args() \e[32mExamples:\e[0m \e[37;2m#\e[0m - \e[37;2m# All\e[0m + \e[37;2m# All (account, price)\e[0m \e[37;2m#\e[0m - \e[37;2m# Fetch all fetch types for default (current) year\e[0m + \e[37;2m# Fetch all types for current (default) year\e[0m $ $global_usage all${global_arg_delim_2}all - \e[37;2m# Fetch all configured prices for all years\e[0m + \e[37;2m# Fetch all accounts for current (default) year\e[0m + $ $global_usage all${global_arg_delim_2}account + + \e[37;2m# Fetch all historical daily average prices and accounts for current (default) year\e[0m + $ $global_usage all${global_arg_delim_2}price${global_arg_delim_3}account + + \e[37;2m# Fetch all historical daily average prices of assets in configuration (since genesis)\e[0m $ $global_usage all${global_arg_delim_2}price year${global_arg_delim_2}all - \e[37;2m# Fetch historical prices and accounts for default (current) year\e[0m - $ $global_usage all${global_arg_delim_2}price${global_arg_delim_3}account + \e[37;2m# Fetch all historical daily average prices of assets in configuration (since genesis) using Mobula API\e[0m + $ $global_usage all${global_arg_delim_2}price year${global_arg_delim_2}all api${global_arg_delim_2}mobula \e[37;2m#\e[0m \e[37;2m# Price\e[0m \e[37;2m#\e[0m - \e[37;2m# Fetch all historical average prices for only Bitcoin and Litecoin\e[0m - $ $global_usage price${global_arg_delim_2}bitcoin${global_arg_delim_1}BTC${global_arg_delim_3}litecoin${global_arg_delim_1}LTC year${global_arg_delim_2}all + \e[37;2m# Fetch historical daily average prices since genesis for only Bitcoin (using default aggregator)\e[0m + $ $global_usage price${global_arg_delim_2}bitcoin${global_arg_delim_1}BTC year${global_arg_delim_2}all - \e[37;2m# Fetch only 2022 historical average prices for Bitcoin and Ethereum\e[0m - $ $global_usage price${global_arg_delim_2}bitcoin${global_arg_delim_1}BTC${global_arg_delim_3}ethereum${global_arg_delim_1}ETH year${global_arg_delim_2}2023 + \e[37;2m# Fetch only current year historical daily average prices for Bitcoin and Ethereum from CoinGecko\e[0m + $ $global_usage price${global_arg_delim_2}bitcoin${global_arg_delim_1}BTC${global_arg_delim_3}ethereum${global_arg_delim_1}ETH api${global_arg_delim_2}coingecko \e[37;2m#\e[0m \e[37;2m# Account\e[0m @@ -139,6 +147,7 @@ function lib_fetch::__parse_args() for _arg in "$@"; do [[ ! "$_arg" =~ ^all${global_arg_delim_2} ]] \ && [[ ! "$_arg" =~ ^price[s]?${global_arg_delim_2} ]] \ + && [[ ! "$_arg" =~ ^api[s]?${global_arg_delim_2} ]] \ && [[ ! "$_arg" =~ ^account[s]?${global_arg_delim_2} ]] \ && [[ ! "$_arg" =~ ^(^block)?chain[s]?${global_arg_delim_2} ]] \ && [[ ! "$_arg" =~ ^year[s]?${global_arg_delim_2} ]] \ @@ -165,6 +174,11 @@ function lib_fetch::__parse_args() [ -z "$_arg_price" ] && lib_utils::die_usage "$_usage" fi + if [[ "$_key" =~ ^api[s]?$ ]]; then + local _arg_api="${_arg:${_len}}" + [ -z "$_arg_api" ] && lib_utils::die_usage "$_usage" + fi + if [[ "$_key" =~ ^account[s]?$ ]]; then local _arg_account="${_arg:${_len}}" [ -z "$_arg_account" ] && lib_utils::die_usage "$_usage" @@ -202,6 +216,14 @@ function lib_fetch::__parse_args() fi fi + # Arg: api + if [ ! -z "$_arg_api" ]; then + # Need a valid arg + if [[ -z "$_arg_all" && -z "$_arg_price" ]]; then + lib_utils::die_usage "$_usage" + fi + fi + # Arg: account if [ ! -z "$_arg_account" ]; then if [[ ! -z "$_arg_all" ]]; then @@ -253,6 +275,16 @@ function lib_fetch::__parse_args() declare -gr global_arg_price=("${_read[@]}") fi + # Arg: api + if [ ! -z "$_arg_api" ]; then + if [[ ! "$_arg_api" =~ ^coingecko$|^mobula$ ]]; then + lib_utils::die_usage "$_usage" + fi + declare -gr global_arg_api="$_arg_api" + else + declare -gr global_arg_api="mobula" + fi + # Arg: account if [ ! -z "$_arg_account" ]; then read -ra _read <<<"$_arg_account" @@ -509,21 +541,48 @@ function lib_fetch::__fetch_account() function lib_fetch::__fetch_price() { - [[ $(lib_fetch::__parse_yaml "get-type" "price") != "sequence" ]] \ - && lib_utils::die_fatal "Invalid price sequence!" + # + # Test configuration for supported members + # + + lib_fetch::__parse_yaml "get-value" "price.${global_arg_api}" &>/dev/null \ + || lib_utils::die_fatal "'${global_arg_api}' is unavailable" + + local -r _members=("key" "asset") + for _member in "${_members[@]}"; do + if _value=$(lib_fetch::__parse_yaml "get-value" "price.${global_arg_api}.${_member}"); then + + if [[ "$_member" == "key" ]]; then + local _api_key="$_value" + [ -z "$_api_key" ] \ + && lib_utils::print_warning "${global_arg_api} has empty ${_member}" + fi + + if [[ "$_member" == "asset" ]]; then + [[ $(lib_fetch::__parse_yaml "get-type" "price.${global_arg_api}.${_member}") != "sequence" ]] \ + && lib_utils::die_fatal "Invalid asset sequence!" + fi + else + lib_utils::die_fatal "$_member is unavailable" + fi + done + + # + # Prepare given assets and filesystem layout + # # Get symbols from configuration if [ -z "${global_arg_price[*]}" ]; then # Parse out each symbol while read _line; do local _symbols+=("$_line") - done < <(lib_fetch::__parse_yaml "get-values" "price") + done < <(lib_fetch::__parse_yaml "get-values" "price.${global_arg_api}.asset") else # Get symbols from CLI local _symbols=("${global_arg_price[*]}") fi - # Set base dir for price files, Subdirs will be by years + # Set base dir for price files (subdirs will be by year) local -r _prices_dir="${global_child_profile_flow}/prices" [ ! -d "$_prices_dir" ] && mkdir -p "$_prices_dir" @@ -535,10 +594,26 @@ function lib_fetch::__fetch_price() "Fetching prices in year '${global_arg_year}' for '${global_parent_profile}/${global_child_profile}' ..." lib_utils::print_custom "\n" - API_FETCH_YEAR="$global_arg_year" \ - API_PRICES_PATH="$_master_prices" \ - API_PRICES_SYMBOLS="${_symbols[*]}" \ - lib_fetch::__fetch_exec "prices" "crypto" + # + # Execute fetch for master price file creation + # + + declare -x API_FETCH_YEAR="$global_arg_year" + lib_utils::print_debug "$API_FETCH_YEAR" + + declare -x API_PRICES_PATH="$_master_prices" + lib_utils::print_debug "$API_PRICES_PATH" + + declare -x API_PRICES_API="$global_arg_api" + lib_utils::print_debug "$API_PRICES_API" + + declare -x API_PRICES_KEY="$_api_key" + lib_utils::print_debug "$API_PRICES_KEY" + + declare -x API_PRICES_SYMBOLS="${_symbols[*]}" + lib_utils::print_debug "$API_PRICES_SYMBOLS" + + lib_fetch::__fetch_exec "prices" "crypto" if [ $? -eq 0 ]; then lib_utils::print_custom "\n" @@ -548,7 +623,7 @@ function lib_fetch::__fetch_price() fi # - # Line format: + # Master prices line format: # # P 2023/01/01 USD 1 #