-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pricing: allow micro suffixes, scientific notation
- Loading branch information
Showing
8 changed files
with
93 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ profiles: | |
attributes: | ||
region: us-west | ||
pricing: | ||
web: .002000 | ||
web: 2e-3 | ||
|
||
deployment: | ||
web: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ profiles: | |
attributes: | ||
region: us-west | ||
pricing: | ||
web: .000100 | ||
web: 100u | ||
|
||
deployment: | ||
web: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ profiles: | |
attributes: | ||
region: sjc | ||
pricing: | ||
web: 0.000025 | ||
web: 25u | ||
|
||
deployment: | ||
webapp: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ profiles: | |
attributes: | ||
region: us-west | ||
pricing: | ||
web: 0.005000 | ||
web: 5e-3 | ||
|
||
deployment: | ||
web: | ||
|