-
Notifications
You must be signed in to change notification settings - Fork 4
/
bid-engine-script.sh
70 lines (57 loc) · 3.18 KB
/
bid-engine-script.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash
# Function to fetch the AKT to USD exchange rate
fetch_akt_to_usd() {
for url in "https://api-osmosis.imperator.co/tokens/v2/price/AKT" \
"https://api.kraken.com/0/public/Ticker?pair=AKTUSD" \
"https://api.kucoin.com/api/v1/market/orderbook/level1?symbol=AKT-USDT"; do
usd_per_akt=$(curl -s "$url" | jq -r '.data.price // .price // .result.AKTUSD.a[0]')
if [[ -n $usd_per_akt && $usd_per_akt != "0" ]]; then
break
fi
done
if [[ -z $usd_per_akt || $usd_per_akt == "0" ]]; then
exit 1
fi
}
# Function to calculate total resource costs in USD
calculate_total_cost_usd() {
local data=$1
# Extract resource quantities
local cpu=$(jq -r '(map(.cpu * .count) | add) / 1000' <<<"$data")
local memory=$(jq -r '(map(.memory * .count) | add) / pow(1024; 3)' <<<"$data")
local ephemeral_storage=$(jq -r '[.[] | (.storage[] | select(.class == "ephemeral").size // 0) * .count] | add / pow(1024; 3)' <<<"$data")
local hdd_storage=$(jq -r '[.[] | (.storage[] | select(.class == "beta1").size // 0) * .count] | add / pow(1024; 3)' <<<"$data")
local ssd_storage=$(jq -r '[.[] | (.storage[] | select(.class == "beta2").size // 0) * .count] | add / pow(1024; 3)' <<<"$data")
local nvme_storage=$(jq -r '[.[] | (.storage[] | select(.class == "beta3").size // 0) * .count] | add / pow(1024; 3)' <<<"$data")
local endpoints=$(jq -r '(map(.endpoint_quantity // 0 * .count) | add)' <<<"$data")
local ips=$(jq -r '(map(.ip_lease_quantity // 0 * .count) | add)' <<<"$data")
local gpu_units=$(jq -r '[.[] | (.gpu.units // 0) * .count] | add' <<<"$data")
local gpu_model=$(jq -r '.[0].gpu.attributes.vendor.nvidia.model' <<<"$data")
# Define target prices for different resources
TARGET_CPU="5.00"
TARGET_MEMORY="0.75"
TARGET_EPHEMERAL_STORAGE="0.25"
TARGET_HDD_STORAGE="0.1667"
TARGET_SSD_STORAGE="0.3333"
TARGET_NVME_STORAGE="0.50"
TARGET_ENDPOINT="0.01"
TARGET_IP="2.00"
TARGET_GPU_UNIT="0"
# Total cost in USD
total_cost_usd_target=$(bc -l <<<"($cpu * $TARGET_CPU) + ($memory * $TARGET_MEMORY) + ($ephemeral_storage * $TARGET_EPHEMERAL_STORAGE) + ($hdd_storage * $TARGET_HDD_STORAGE) + ($ssd_storage * $TARGET_SSD_STORAGE) + ($nvme_storage * $TARGET_NVME_STORAGE) + ($endpoints * $TARGET_ENDPOINT) + ($ips * $TARGET_IP) + ($gpu_units * $TARGET_GPU_UNIT)")
}
# Fetch AKT to USD rate
fetch_akt_to_usd
# Read JSON input
data_in=$(jq .)
# Calculate total cost in USD
calculate_total_cost_usd "$(jq -r '.resources' <<<"$data_in")"
TARGET_MIN_USD=$(awk "BEGIN {print (($TARGET_MEMORY + $TARGET_EPHEMERAL_STORAGE + $TARGET_CPU))}") #Dynamically calculate - removed /1.25
TARGET_MIN_UAKT=$(awk "BEGIN {print (($TARGET_MIN_USD * 1000000) / ($usd_per_akt * 425940.524781341))}") #Converts the MIN_USD to uakt
total_cost_akt_target=$(awk "BEGIN {print ($total_cost_usd_target/$usd_per_akt)}")
total_cost_uakt_target=$(awk "BEGIN {print ($total_cost_akt_target*1000000)}")
cost_per_block=$(awk "BEGIN {print ($total_cost_uakt_target/425940.524781341)}")
if (($(echo "$cost_per_block < $TARGET_MIN_UAKT" | bc -l))); then
cost_per_block=$TARGET_MIN_UAKT
fi
printf "%.4f\n" "$cost_per_block"