diff --git a/bidengine/pricing.go b/bidengine/pricing.go index 5c19c268..4d37a86c 100644 --- a/bidengine/pricing.go +++ b/bidengine/pricing.go @@ -106,8 +106,10 @@ func MakeScalePricing( return result, nil } -var ErrBidQuantityInvalid = errors.New("A bid quantity is invalid") -var ErrBidZero = errors.New("A bid of zero was produced") +var ( + ErrBidQuantityInvalid = errors.New("A bid quantity is invalid") + ErrBidZero = errors.New("A bid of zero was produced") +) func ceilBigRatToBigInt(v *big.Rat) *big.Int { numerator := v.Num() @@ -345,5 +347,5 @@ type dataForScriptElement struct { type dataForScript struct { Resources []dataForScriptElement `json:"resources"` - Price string `json:"price"` + Price sdk.DecCoin `json:"price"` } diff --git a/bidengine/pricing_test.go b/bidengine/pricing_test.go index 19ad3ebd..f0331be9 100644 --- a/bidengine/pricing_test.go +++ b/bidengine/pricing_test.go @@ -17,8 +17,6 @@ import ( "testing" "time" - "github.com/akash-network/provider/cluster/util" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/shopspring/decimal" "github.com/stretchr/testify/require" @@ -28,6 +26,8 @@ import ( atypes "github.com/akash-network/akash-api/go/node/types/v1beta3" "github.com/akash-network/node/sdl" "github.com/akash-network/node/testutil" + + "github.com/akash-network/provider/cluster/util" ) func Test_ScalePricingRejectsAllZero(t *testing.T) { @@ -414,7 +414,7 @@ func Test_ScriptPricingFailsWhenScriptExitsWithoutWritingResultToStdout(t *testi } _, err = pricing.CalculatePrice(context.Background(), req) - require.Equal(t, io.EOF, errors.Unwrap(err)) + require.ErrorIs(t, err, io.EOF) } func Test_ScriptPricingFailsWhenScriptWritesZeroResult(t *testing.T) { @@ -498,7 +498,7 @@ func Test_ScriptPricingFailsWhenScriptWritesOverflowResult(t *testing.T) { scriptPath := path.Join(tempdir, "test_script.sh") fout, err := os.OpenFile(scriptPath, os.O_WRONLY|os.O_CREATE, os.ModePerm) require.NoError(t, err) - // Write the maximum value, followed by zero so it is 10x + // Write the maximum value, followed by zero, so it is 10x _, err = fmt.Fprintf(fout, "#!/bin/sh\necho %s0\nexit 0", sdk.MaxSortableDec.String()) require.NoError(t, err) err = fout.Close() @@ -514,7 +514,7 @@ func Test_ScriptPricingFailsWhenScriptWritesOverflowResult(t *testing.T) { } _, err = pricing.CalculatePrice(context.Background(), req) - require.Equal(t, ErrBidQuantityInvalid, err) + require.ErrorIs(t, err, ErrBidQuantityInvalid) } func Test_ScriptPricingReturnsResultFromScript(t *testing.T) { @@ -682,7 +682,7 @@ func Test_ScriptPricingWritesJsonToStdin(t *testing.T) { func Test_ScriptPricingFromScript(t *testing.T) { const ( mockAPIResponse = `{"akash-network":{"usd":3.57}}` - expectedPrice = 67843138 + expectedPrice = "67843137.254901960492134094" ) server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -711,7 +711,10 @@ func Test_ScriptPricingFromScript(t *testing.T) { price, err := pricing.CalculatePrice(context.Background(), req) require.NoError(t, err) - require.Equal(t, sdk.NewDecCoin("uakt", sdk.NewInt(expectedPrice)).String(), price.String()) + amount, err := sdk.NewDecFromStr(expectedPrice) + require.NoError(t, err) + + require.Equal(t, sdk.NewDecCoinFromDec("uakt", amount).String(), price.String()) } func TestRationalToIntConversion(t *testing.T) { diff --git a/bidengine/shellscript.go b/bidengine/shellscript.go index 5f4136dc..a6eee445 100644 --- a/bidengine/shellscript.go +++ b/bidengine/shellscript.go @@ -5,6 +5,7 @@ import ( "context" "encoding/json" "fmt" + "io" "os" "os/exec" "strings" @@ -118,7 +119,7 @@ func (ssp shellScriptPricing) CalculatePrice(ctx context.Context, req Request) ( dataForScript := &dataForScript{ Resources: make([]dataForScriptElement, len(req.GSpec.Resources)), - Price: req.GSpec.Price().String(), + Price: req.GSpec.Price(), } // iterate over everything & sum it up @@ -182,18 +183,14 @@ func (ssp shellScriptPricing) CalculatePrice(ctx context.Context, req Request) ( } // Decode the result - decoder := json.NewDecoder(outputBuf) - decoder.UseNumber() - - var priceNumber json.Number - err = decoder.Decode(&priceNumber) - if err != nil { - return sdk.DecCoin{}, fmt.Errorf("%w: script failure %s", err, stderrBuf.String()) + valueStr := strings.TrimSpace(outputBuf.String()) + if valueStr == "" { + return sdk.DecCoin{}, fmt.Errorf("bid script must return amount:%w%w", io.EOF, ErrBidQuantityInvalid) } - price, err := sdk.NewDecFromStr(priceNumber.String()) + price, err := sdk.NewDecFromStr(valueStr) if err != nil { - return sdk.DecCoin{}, ErrBidQuantityInvalid + return sdk.DecCoin{}, fmt.Errorf("%w%w", err, ErrBidQuantityInvalid) } if price.IsZero() { @@ -204,9 +201,5 @@ func (ssp shellScriptPricing) CalculatePrice(ctx context.Context, req Request) ( return sdk.DecCoin{}, ErrBidQuantityInvalid } - if !price.LTE(sdk.MaxSortableDec) { - return sdk.DecCoin{}, ErrBidQuantityInvalid - } - return sdk.NewDecCoinFromDec(denom, price), nil } diff --git a/script/usd_pricing_oracle.sh b/script/usd_pricing_oracle.sh index 6ac28cbd..d6651b01 100755 --- a/script/usd_pricing_oracle.sh +++ b/script/usd_pricing_oracle.sh @@ -144,8 +144,4 @@ fi total_cost_akt=$(bc -l <<<"${total_cost_usd}/${usd_per_akt}") total_cost_uakt=$(bc -l <<<"${total_cost_akt}*1000000") -# Round upwards to get an integer -total_cost_uakt=$(echo "$total_cost_uakt" | jq 'def ceil: if . | floor == . then . else . + 1.0 | floor end; .|ceil') - -# return the price in uAKT -echo "$total_cost_uakt" +printf "%.18f" "$total_cost_uakt"