Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

!fix(bidengine): pass price to shell bid strategy #132

Merged
merged 1 commit into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bidengine/pricing.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,8 @@ type dataForScriptElement struct {
EndpointQuantity int `json:"endpoint_quantity"`
IPLeaseQuantity uint `json:"ip_lease_quantity"`
}

type dataForScript struct {
Resources []dataForScriptElement `json:"resources"`
Price string `json:"price"`
}
18 changes: 9 additions & 9 deletions bidengine/pricing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,19 +663,19 @@ func Test_ScriptPricingWritesJsonToStdin(t *testing.T) {
_ = fin.Close()
}()
decoder := json.NewDecoder(fin)
data := make([]dataForScriptElement, 0)
data := dataForScript{}
err = decoder.Decode(&data)
require.NoError(t, err)

require.Len(t, data, len(gspec.Resources))
require.Len(t, data.Resources, len(gspec.Resources))

for i, r := range gspec.Resources {
require.Equal(t, r.Resources.CPU.Units.Val.Uint64(), data[i].CPU)
require.Equal(t, r.Resources.Memory.Quantity.Val.Uint64(), data[i].Memory)
require.Equal(t, r.Resources.Storage[0].Quantity.Val.Uint64(), data[i].Storage[0].Size)
require.Equal(t, r.Count, data[i].Count)
require.Equal(t, len(r.Resources.Endpoints), data[i].EndpointQuantity)
require.Equal(t, util.GetEndpointQuantityOfResourceUnits(r.Resources, atypes.Endpoint_LEASED_IP), data[i].IPLeaseQuantity)
require.Equal(t, r.Resources.CPU.Units.Val.Uint64(), data.Resources[i].CPU)
require.Equal(t, r.Resources.Memory.Quantity.Val.Uint64(), data.Resources[i].Memory)
require.Equal(t, r.Resources.Storage[0].Quantity.Val.Uint64(), data.Resources[i].Storage[0].Size)
require.Equal(t, r.Count, data.Resources[i].Count)
require.Equal(t, len(r.Resources.Endpoints), data.Resources[i].EndpointQuantity)
require.Equal(t, util.GetEndpointQuantityOfResourceUnits(r.Resources, atypes.Endpoint_LEASED_IP), data.Resources[i].IPLeaseQuantity)
}
}

Expand Down Expand Up @@ -703,7 +703,7 @@ func Test_ScriptPricingFromScript(t *testing.T) {
require.NotNil(t, pricing)

gspec := defaultGroupSpec()
gspec.Resources[0].Resources.Endpoints = make([]atypes.Endpoint, 7)
gspec.Resources[0].Resources.Endpoints = make(atypes.Endpoints, 7)
req := Request{
Owner: testutil.AccAddress(t).String(),
GSpec: gspec,
Expand Down
7 changes: 5 additions & 2 deletions bidengine/shellscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ func parseStorage(resource atypes.Volumes) []storageElement {
func (ssp shellScriptPricing) CalculatePrice(ctx context.Context, req Request) (sdk.DecCoin, error) {
buf := &bytes.Buffer{}

dataForScript := make([]dataForScriptElement, len(req.GSpec.Resources))
dataForScript := &dataForScript{
Resources: make([]dataForScriptElement, len(req.GSpec.Resources)),
Price: req.GSpec.Price().String(),
}

// iterate over everything & sum it up
for i, group := range req.GSpec.Resources {
Expand All @@ -128,7 +131,7 @@ func (ssp shellScriptPricing) CalculatePrice(ctx context.Context, req Request) (
storageQuantity := parseStorage(group.Resources.Storage)
endpointQuantity := len(group.Resources.Endpoints)

dataForScript[i] = dataForScriptElement{
dataForScript.Resources[i] = dataForScriptElement{
CPU: cpuQuantity,
GPU: gpuQuantity,
Memory: memoryQuantity,
Expand Down
19 changes: 14 additions & 5 deletions script/usd_pricing_oracle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ fi
# * curl

if ! command -v jq &> /dev/null ; then
echo "jq could not be found"
echo "jq could not be found" >&2
exit 1
fi

if ! command -v bc &> /dev/null ; then
echo "bc could not be found"
echo "bc could not be found" >&2
exit 1
fi

if ! command -v curl &> /dev/null ; then
echo "curl could not be found"
echo "curl could not be found" >&2
exit 1
fi

Expand All @@ -39,6 +39,7 @@ DEFAULT_API_URL="https://api.coingecko.com/api/v3/simple/price?ids=akash-network

# These are the variables one can modify to change the USD scale for each resource kind
CPU_USD_SCALE=0.10
GPU_USD_SCALE=0.50
MEMORY_USD_SCALE=0.02
ENDPOINT_USD_SCALE=0.02

Expand All @@ -56,14 +57,15 @@ MAX_INT64=9223372036854775807
# local variables used for calculation
memory_total=0
cpu_total=0
gpu_total=0
endpoint_total=0
storage_cost_usd=0

# read the JSON in `stdin` into $script_input
read -r script_input

# iterate over all the groups and calculate total quantity of each resource
for group in $(jq -c '.[]' <<<"$script_input"); do
for group in $(jq -c '.resources[]' <<<"$script_input"); do
count=$(jq '.count' <<<"$group")

memory_quantity=$(jq '.memory' <<<"$group")
Expand All @@ -74,6 +76,10 @@ for group in $(jq -c '.[]' <<<"$script_input"); do
cpu_quantity=$((cpu_quantity * count))
cpu_total=$((cpu_total + cpu_quantity))

gpu_quantity=$(jq '.gpu.units' <<<"$group")
gpu_quantity=$((gpu_quantity * count))
gpu_total=$((gpu_total + gpu_quantity))

for storage in $(jq -c '.storage[]' <<<"$group"); do
storage_size=$(jq -r '.size' <<<"$storage")
# jq has to be with -r to not quote value
Expand All @@ -95,11 +101,13 @@ done

# calculate the total cost in USD for each resource
cpu_cost_usd=$(bc -l <<<"${cpu_total}*${CPU_USD_SCALE}")
gpu_cost_usd=$(bc -l <<<"${gpu_total}*${GPU_USD_SCALE}")
memory_cost_usd=$(bc -l <<<"${memory_total}*${MEMORY_USD_SCALE}")
endpoint_cost_usd=$(bc -l <<<"${endpoint_total}*${ENDPOINT_USD_SCALE}")

# validate the USD cost for each resource
if [ 1 -eq "$(bc <<<"${cpu_cost_usd}<0")" ] || [ 0 -eq "$(bc <<<"${cpu_cost_usd}<=${MAX_INT64}")" ] ||
[ 1 -eq "$(bc <<<"${gpu_cost_usd}<0")" ] || [ 0 -eq "$(bc <<<"${gpu_cost_usd}<=${MAX_INT64}")" ] ||
[ 1 -eq "$(bc <<<"${memory_cost_usd}<0")" ] || [ 0 -eq "$(bc <<<"${memory_cost_usd}<=${MAX_INT64}")" ] ||
[ 1 -eq "$(bc <<<"${storage_cost_usd}<0")" ] || [ 0 -eq "$(bc <<<"${storage_cost_usd}<=${MAX_INT64}")" ] ||
[ 1 -eq "$(bc <<<"${endpoint_cost_usd}<0")" ] || [ 0 -eq "$(bc <<<"${endpoint_cost_usd}<=${MAX_INT64}")" ]; then
Expand All @@ -108,7 +116,7 @@ if [ 1 -eq "$(bc <<<"${cpu_cost_usd}<0")" ] || [ 0 -eq "$(bc <<<"${cpu_cost_usd}
fi

# finally, calculate the total cost in USD of all resources and validate it
total_cost_usd=$(bc -l <<<"${cpu_cost_usd}+${memory_cost_usd}+${storage_cost_usd}+${endpoint_cost_usd}")
total_cost_usd=$(bc -l <<<"${cpu_cost_usd}+${gpu_cost_usd}+${memory_cost_usd}+${storage_cost_usd}+${endpoint_cost_usd}")
if [ 1 -eq "$(bc <<<"${total_cost_usd}<0")" ] || [ 0 -eq "$(bc <<<"${total_cost_usd}<=${MAX_INT64}")" ]; then
echo "invalid total cost $total_cost_usd" >&2
exit 1
Expand All @@ -128,6 +136,7 @@ usd_per_akt=$(jq '."akash-network"."usd"' <<<"$API_RESPONSE")

# validate the current USD price per AKT is not zero
if [ 1 -eq "$(bc <<< "${usd_per_akt}==0")" ]; then
echo "invalid akt to usd price, cannot be 0" >&2
exit 1
fi

Expand Down
Loading