code cleanup

This commit is contained in:
starlord 2024-06-28 08:30:15 -07:00
parent 1d34619e7e
commit 97c61442ca

View file

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
# current dir # current dir
dir="$(dirname $(realpath $0))" dir="$(dirname "$(realpath "$0")")"
# get files # get files
wallet="${dir}/wallet.json" wallet="${dir}/wallet.json"
@ -22,7 +22,10 @@ portfolio=$(jq -s '[ .[0] + .[1] | group_by(.token)[] | add ]' \
<(echo "$(jq '.tokens' $wallet)") <(echo "$prices")) <(echo "$(jq '.tokens' $wallet)") <(echo "$prices"))
# calculate value # calculate value
calc=$(jq '[.[] | .["value"] = .balance * .price | .["margin"] = .value - .investment | .["movement"] = .margin / .investment]' \ calc=$(jq '[.[]
| .["value"] = .balance * .price
| .["margin"] = .value - .investment
| .["movement"] = .margin / .investment]' \
<(echo "$portfolio")) <(echo "$portfolio"))
# create rows # create rows
@ -32,13 +35,30 @@ for t in "${tokens[@]}"; do
tok=$(echo $t | jq '.token' | tr -d '"'); tok=$(echo $t | jq '.token' | tr -d '"');
bal=$(echo $t | jq '.balance|tonumber'); bal=$(echo $t | jq '.balance|tonumber');
prc=$(echo $t | jq '.price|tonumber'); prc=$(echo $t | jq '.price|tonumber');
inv=$(echo $t | jq '.investment|tonumber');
val=$(echo $t | jq '.value|tonumber');
mar=$(echo $t | jq '.margin|tonumber');
mov=$(echo $t | jq '.movement|tonumber');
row=$(echo "${row}<tr><td class=\"left\">${tok}</td><td>${bal}</td><td>"$(printf "$%.2f\n" $prc)"</td><td>"$(printf "$%.2f\n" $inv)"</td><td>"$(printf "$%.2f\n" $val)"</td><td>"$(printf "$%.2f\n" $mar)"</td><td>"$(printf "%.3f\n" $mov)"</td></tr>"); if [[ -n "$prc" ]]; then
csv=$(echo "${csv}"$(echo -n $(date +",['"%F"','"%T"','"%:::z"'"))",'${tok}','${bal}','${prc}','${inv}','${val}','${mar}','${mov}']"); inv=$(echo $t | jq '.investment|tonumber');
val=$(echo $t | jq '.value|tonumber');
mar=$(echo $t | jq '.margin|tonumber');
mov=$(echo $t | jq '.movement|tonumber');
else
read -r inv val mar mov <<<$(echo 0 0 0 0);
fi
row=$(
echo "${row}<tr><td class=\"left\">${tok}</td><td>${bal}</td><td>" \
$(printf "$%.2f\n" $prc)"</td><td>" \
$(printf "$%.2f\n" $inv)"</td><td>" \
$(printf "$%.2f\n" $val)"</td><td>" \
$(printf "$%.2f\n" $mar)"</td><td>" \
$(printf "%.3f\n" $mov)"</td></tr>" \
);
csv=$(
echo "${csv}"\
$(echo -n $(date +"['"%F"','"%T"','"%:::z"'")) \
",'${tok}','${bal}','${prc}','${inv}','${val}','${mar}','${mov}']" \
);
# Sum up # Sum up
tot_inv=$(echo "${tot_inv:-0}+$inv" | bc); tot_inv=$(echo "${tot_inv:-0}+$inv" | bc);
@ -48,21 +68,31 @@ for t in "${tokens[@]}"; do
done done
# format array # format array
csv=$(echo $csv | sed 's|^,||g') csv=$(echo $csv | sed 's|\] \[|\],\[|g')
# calculate movement # calculate movement
tot_mov=$(echo "scale=3 ; $tot_mar / $tot_inv" | bc) tot_mov=$(echo "scale=3 ; $tot_mar / $tot_inv" | bc)
# fill template # fill template
page=$(cat $template | sed "s|%row%|$row|g;s|%inv%|"$(printf "$%.2f\n" $tot_inv)"|g;s|%val%|"$(printf "$%.2f\n" $tot_val)"|g;s|%mar%|"$(printf "$%.2f\n" $tot_mar)"|g;s|%mov%|"$(printf "%.4f\n" $tot_mov)"|g;s|%dts%|$dts|g;s|%csv_data%|$csv|g;s|%dts_fln%|"$(echo -n $(date '+%F_%T%:::z'))"|g") page=$(cat $template | \
sed "s|%row%|$row|g" | \
sed "s|%inv%|"$(printf "$%.2f\n" $tot_inv)"|g" | \
sed "s|%val%|"$(printf "$%.2f\n" $tot_val)"|g" | \
sed "s|%mar%|"$(printf "$%.2f\n" $tot_mar)"|g" | \
sed "s|%mov%|"$(printf "%.4f\n" $tot_mov)"|g" | \
sed "s|%dts%|$dts|g" | \
sed "s|%csv_data%|$csv|g" | \
sed "s|%dts_fln%|"$(echo -n $(date '+%F_%T%:::z'))"|g")
# encode link # encode link
itty=$(echo -n $page | lzma -9 | base64 -w0 | xargs -0 printf "https://itty.bitty.site/#Portfolio/%s\n") itty=$(echo -n $page | lzma -9 | base64 -w0 | xargs -0 printf "https://itty.bitty.site/#Portfolio/%s\n")
# send signal # get ntfy
ntfy=$(jq -r '.ntfy | "\(.server)/\(.topic)"' $wallet) ntfy=$(jq -r '.ntfy | "\(.server)/\(.topic)"' $wallet)
# send report
curl \ curl \
-H "title: Portfolio Update" \ -H "title: Portfolio Update" \
-H "tags: coin" -H "click: ${itty}" \ -H "tags: coin" \
-H "click: ${itty}" \
-d "Click Here" $ntfy -d "Click Here" $ntfy