From bf5bec438a47d1778ee0f02ab11036a9ab33ba7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Zaremba?= Date: Tue, 1 Nov 2022 21:54:10 +0100 Subject: [PATCH 1/2] Fix gofer ignoring errors --- exec/source-gofer | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/exec/source-gofer b/exec/source-gofer index 4c11d25..8d38a5d 100755 --- a/exec/source-gofer +++ b/exec/source-gofer @@ -2,15 +2,23 @@ set -eo pipefail if [[ -n $OMNIA_DEBUG ]]; then set -x; fi -gofer price --config "$GOFER_CONFIG" --format ndjson "$1" \ -| jq -c '{ - asset: (.base+"/"+.quote), - median: .price, - sources: ( - [ .. - | select(type == "object" and .type == "origin" and .error == null) - | {(.base+"/"+.quote+"@"+.params.origin): (.price|tostring)} - ] - | add - ) -}' \ No newline at end of file +cd "$(cd "${0%/*/*}" && pwd)/lib" +. ./log.sh + +if _goferData="$(gofer price --config "$GOFER_CONFIG" --format ndjson "$1")" +then + jq -c '{ + asset: (.base+"/"+.quote), + median: .price, + sources: ( + [ .. + | select(type == "object" and .type == "origin" and .error == null) + | {(.base+"/"+.quote+"@"+.params.origin): (.price|tostring)} + ] + | add + ) + }' <<<"$_goferData" +else + error "could not get price" "$(jq -r '.error' <<<"$_goferData")" + exit 1 +fi From 833332e53f99ba6cb00d6cb495ca5117be27cc1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Zaremba?= Date: Wed, 2 Nov 2022 10:24:42 +0100 Subject: [PATCH 2/2] Explicitly read return data from sources --- exec/source-setzer | 27 +++++++++++++++++---------- lib/feed.sh | 29 +++++++++++++++++++---------- version | 2 +- 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/exec/source-setzer b/exec/source-setzer index baaf8aa..28bdab8 100755 --- a/exec/source-setzer +++ b/exec/source-setzer @@ -22,6 +22,7 @@ _mapSetzer() { '{($s):$p}' else error "failed to get asset price" "asset=$_assetPair" "source=$_source" + return 1 fi } @@ -32,19 +33,25 @@ readSourcesWithSetzer() { _setzerAssetPair="${_setzerAssetPair,,}" local _prices - _prices=$(setzer sources "$_setzerAssetPair" \ - | while IFS= read -r _src; do _mapSetzer "$_setzerAssetPair" "$_src"; done) + _prices="$(while IFS= read -r _src; do _mapSetzer "$_setzerAssetPair" "$_src"; done < <(setzer sources "$_setzerAssetPair"))" - # Check minimal available prices for setzer - _length=$(jq 'add|tonumber' <<<"$_prices" | jq -s 'length') - if [[ $_length -lt $SETZER_MIN_MEDIAN ]]; then - error "Error: not enough sources to provide a median: ${#_prices[@]} < $SETZER_MIN_MEDIAN" - return 1 - fi + # Check minimal available prices for setzer + local _length + _length="$(jq 'add|tonumber' <<<"$_prices" | jq -s 'length')" + if [[ $_length -lt $SETZER_MIN_MEDIAN ]]; then + error "Error: not enough sources to provide a median: ${#_prices[@]} < $SETZER_MIN_MEDIAN" + return 1 + fi local _median - _median=$(jq 'add|tonumber' <<<"$_prices" \ - | jq -s 'sort | if length == 0 then null elif length % 2 == 0 then (.[length/2] + .[length/2-1])/2 else .[length/2|floor] end') + case "$_setzerAssetPair" in + rethusd) + _median="$(setzer price "$_setzerAssetPair")" + ;; + *) + _median="$(jq 'add|tonumber' <<<"$_prices" | jq -s 'sort | if length == 0 then null elif length % 2 == 0 then (.[length/2] + .[length/2-1])/2 else .[length/2|floor] end')" + ;; + esac local _output _output="$(jq -cs \ diff --git a/lib/feed.sh b/lib/feed.sh index 3cbadfa..9911f61 100644 --- a/lib/feed.sh +++ b/lib/feed.sh @@ -17,8 +17,7 @@ readSourcesAndBroadcastAllPriceMessages() { break fi - readSource "$_src" "${!_unpublishedPairs[@]}" \ - | while IFS= read -r _json + while IFS= read -r _json do if [[ -z "$_json" ]]; then continue @@ -43,7 +42,7 @@ readSourcesAndBroadcastAllPriceMessages() { unset _unpublishedPairs["$_assetPair"] transportPublish "$_assetPair" "$_message" || error "all transports failed" "asset=$_assetPair" - done + done < <(readSource "$_src" "${!_unpublishedPairs[@]}") done } @@ -51,20 +50,30 @@ readSource() { local _src="${1,,}" local _assetPairs=("${@:2}") - verbose --list "readSource" "src=$_src" "${_assetPairs[@]}" + verbose --list "read source" "src=$_src" "${_assetPairs[@]}" case "$_src" in setzer|gofer) for _assetPair in "${_assetPairs[@]}"; do - log "Querying price and calculating median" "source=$_src" "asset=${_assetPair}" - - "source-$_src" "$_assetPair" \ - | tee >(_data="$(cat)"; [[ -z "$_data" ]] || verbose --raw "source-$_src" "$(jq -sc <<<"$_data")") \ - || error "Failed to get price" "app=source-$_src" "asset=$_assetPair" + log "querying price and calculating median" "source=$_src" "asset=${_assetPair}" + + local _data + if _data="$("source-$_src" "$_assetPair")" + then + if [[ -n "$_data" ]] + then + verbose --raw "source-$_src" "$(jq -sc <<<"$_data")" + echo "$_data" + else + error "no data from source" "app=source-$_src" "asset=$_assetPair" + fi + else + error "failed to get price" "app=source-$_src" "asset=$_assetPair" + fi done ;; *) - error "Unknown Feed Source: $_src" + error "unknown Feed Source: $_src" return 1 ;; esac diff --git a/version b/version index b50dd27..61ce01b 100644 --- a/version +++ b/version @@ -1 +1 @@ -1.13.1 +1.13.2