Skip to content

Commit

Permalink
pricing: allow micro suffixes, scientific notation
Browse files Browse the repository at this point in the history
  • Loading branch information
boz committed Jul 31, 2018
1 parent 1474fe3 commit 332941f
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 17 deletions.
12 changes: 6 additions & 6 deletions _docs/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,16 @@ profiles:
attributes:
region: us-west
pricing:
web: 0.000010
db: 0.000050
db-pool: 0.000030
web: 10u
db: 50u
db-pool: 30u
eastcoast:
attributes:
region: us-east
pricing:
web: 0.000030
db: 0.000060
db-pool: 0.000040
web: 30u
db: 60u
db-pool: 40u

deployment:

Expand Down
16 changes: 13 additions & 3 deletions _docs/sdl.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,22 @@ westcoast:
attributes:
region: us-west
pricing:
web: 8
db: 15
web: 8u
db: 100u
```

This defines a profile named `westcoast` having required attributes `{region="us-west"}`, and with a max price for
the `web` and `db` [compute profiles](#profilescompute) of 8 and 15 tokens an hour, respectively.
the `web` and `db` [compute profiles](#profilescompute) of 8 and 15 _micro_ (10^-6) tokens per block, respectively.

Pricing may be expressed in decimal or scientific notation for Akash units, or may be suffixed with `mu`,`µ`, or `u` to represent _micro_ Akash.

Examples:

| Value | Micro Akash Tokens |
| --- | --- |
| `1` | 1000000 |
| `1e-4` | 100 |
| `20u` | 20 |

### deployment

Expand Down
2 changes: 1 addition & 1 deletion _run/kube/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ profiles:
attributes:
region: us-west
pricing:
web: .002000
web: 2e-3

deployment:
web:
Expand Down
2 changes: 1 addition & 1 deletion _run/multi/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ profiles:
attributes:
region: us-west
pricing:
web: .000100
web: 100u

deployment:
web:
Expand Down
30 changes: 26 additions & 4 deletions denom/denom.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
package denom

import (
"math/big"
"strconv"
"strings"
)

const (
microAkashPerAkash = 1000000
Mega = 1000000
)

var (
microSuffixes = []string{
"mu",
"µ",
"u",
}
)

// ToBase converts a unit of currency to its equivalent value in base denomination
func ToBase(sval string) (uint64, error) {
akash, err := strconv.ParseFloat(sval, 64)

for _, suffix := range microSuffixes {
if !strings.HasSuffix(sval, suffix) {
continue
}
return strconv.ParseUint(strings.TrimSuffix(sval, suffix), 10, 64)
}

fval, _, err := big.ParseFloat(sval, 10, 64, big.ToNearestAway)
if err != nil {
return 0, err
}
amount := akash * microAkashPerAkash
return uint64(amount), nil

mval := new(big.Float).
Mul(fval, new(big.Float).SetUint64(Mega))

val, _ := mval.Uint64()
return val, nil
}
44 changes: 44 additions & 0 deletions denom/denom_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package denom_test

import (
"testing"

"github.com/ovrclk/akash/denom"
"github.com/stretchr/testify/assert"
)

func TestToBase(t *testing.T) {

tests := []struct {
sval string
val uint64
ok bool
}{
{"1", 1 * denom.Mega, true},
{"100", 100 * denom.Mega, true},
{"0.000001", 1, true},
{"0.000100", 100, true},
{"1.000001", denom.Mega + 1, true},
{"0.0000001", 0, true},
{"1e-6", 1, true},
{"3e2", 300 * denom.Mega, true},

{"100u", 100, true},
{"35µ", 35, true},
{"200mu", 200, true},

{"0x100", 0, false},
{"abcd", 0, false},
{"u", 0, false},
{"fu", 0, false},
}

for _, test := range tests {
val, err := denom.ToBase(test.sval)
if test.ok {
assert.Equal(t, test.val, val, test.sval)
continue
}
assert.Error(t, err, test.sval)
}
}
2 changes: 1 addition & 1 deletion sdl/_testdata/profile-svc-name-mismatch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ profiles:
attributes:
region: sjc
pricing:
web: 0.000025
web: 25u

deployment:
webapp:
Expand Down
2 changes: 1 addition & 1 deletion sdl/_testdata/simple.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ profiles:
attributes:
region: us-west
pricing:
web: 0.005000
web: 5e-3

deployment:
web:
Expand Down

0 comments on commit 332941f

Please sign in to comment.