Skip to content

Commit 332941f

Browse files
committed
pricing: allow micro suffixes, scientific notation
1 parent 1474fe3 commit 332941f

File tree

8 files changed

+93
-17
lines changed

8 files changed

+93
-17
lines changed

_docs/deployment.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,16 @@ profiles:
7474
attributes:
7575
region: us-west
7676
pricing:
77-
web: 0.000010
78-
db: 0.000050
79-
db-pool: 0.000030
77+
web: 10u
78+
db: 50u
79+
db-pool: 30u
8080
eastcoast:
8181
attributes:
8282
region: us-east
8383
pricing:
84-
web: 0.000030
85-
db: 0.000060
86-
db-pool: 0.000040
84+
web: 30u
85+
db: 60u
86+
db-pool: 40u
8787

8888
deployment:
8989

_docs/sdl.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,22 @@ westcoast:
143143
attributes:
144144
region: us-west
145145
pricing:
146-
web: 8
147-
db: 15
146+
web: 8u
147+
db: 100u
148148
```
149149

150150
This defines a profile named `westcoast` having required attributes `{region="us-west"}`, and with a max price for
151-
the `web` and `db` [compute profiles](#profilescompute) of 8 and 15 tokens an hour, respectively.
151+
the `web` and `db` [compute profiles](#profilescompute) of 8 and 15 _micro_ (10^-6) tokens per block, respectively.
152+
153+
Pricing may be expressed in decimal or scientific notation for Akash units, or may be suffixed with `mu`,`µ`, or `u` to represent _micro_ Akash.
154+
155+
Examples:
156+
157+
| Value | Micro Akash Tokens |
158+
| --- | --- |
159+
| `1` | 1000000 |
160+
| `1e-4` | 100 |
161+
| `20u` | 20 |
152162

153163
### deployment
154164

_run/kube/deployment.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ profiles:
2424
attributes:
2525
region: us-west
2626
pricing:
27-
web: .002000
27+
web: 2e-3
2828

2929
deployment:
3030
web:

_run/multi/deployment.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ profiles:
2424
attributes:
2525
region: us-west
2626
pricing:
27-
web: .000100
27+
web: 100u
2828

2929
deployment:
3030
web:

denom/denom.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,41 @@
11
package denom
22

33
import (
4+
"math/big"
45
"strconv"
6+
"strings"
57
)
68

79
const (
8-
microAkashPerAkash = 1000000
10+
Mega = 1000000
11+
)
12+
13+
var (
14+
microSuffixes = []string{
15+
"mu",
16+
"µ",
17+
"u",
18+
}
919
)
1020

1121
// ToBase converts a unit of currency to its equivalent value in base denomination
1222
func ToBase(sval string) (uint64, error) {
13-
akash, err := strconv.ParseFloat(sval, 64)
23+
24+
for _, suffix := range microSuffixes {
25+
if !strings.HasSuffix(sval, suffix) {
26+
continue
27+
}
28+
return strconv.ParseUint(strings.TrimSuffix(sval, suffix), 10, 64)
29+
}
30+
31+
fval, _, err := big.ParseFloat(sval, 10, 64, big.ToNearestAway)
1432
if err != nil {
1533
return 0, err
1634
}
17-
amount := akash * microAkashPerAkash
18-
return uint64(amount), nil
35+
36+
mval := new(big.Float).
37+
Mul(fval, new(big.Float).SetUint64(Mega))
38+
39+
val, _ := mval.Uint64()
40+
return val, nil
1941
}

denom/denom_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package denom_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/ovrclk/akash/denom"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestToBase(t *testing.T) {
11+
12+
tests := []struct {
13+
sval string
14+
val uint64
15+
ok bool
16+
}{
17+
{"1", 1 * denom.Mega, true},
18+
{"100", 100 * denom.Mega, true},
19+
{"0.000001", 1, true},
20+
{"0.000100", 100, true},
21+
{"1.000001", denom.Mega + 1, true},
22+
{"0.0000001", 0, true},
23+
{"1e-6", 1, true},
24+
{"3e2", 300 * denom.Mega, true},
25+
26+
{"100u", 100, true},
27+
{"35µ", 35, true},
28+
{"200mu", 200, true},
29+
30+
{"0x100", 0, false},
31+
{"abcd", 0, false},
32+
{"u", 0, false},
33+
{"fu", 0, false},
34+
}
35+
36+
for _, test := range tests {
37+
val, err := denom.ToBase(test.sval)
38+
if test.ok {
39+
assert.Equal(t, test.val, val, test.sval)
40+
continue
41+
}
42+
assert.Error(t, err, test.sval)
43+
}
44+
}

sdl/_testdata/profile-svc-name-mismatch.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ profiles:
2121
attributes:
2222
region: sjc
2323
pricing:
24-
web: 0.000025
24+
web: 25u
2525

2626
deployment:
2727
webapp:

sdl/_testdata/simple.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ profiles:
2222
attributes:
2323
region: us-west
2424
pricing:
25-
web: 0.005000
25+
web: 5e-3
2626

2727
deployment:
2828
web:

0 commit comments

Comments
 (0)