Skip to content

Commit

Permalink
Standardize decimals (#1620)
Browse files Browse the repository at this point in the history
  • Loading branch information
containerman17 authored Oct 2, 2024
1 parent e191c88 commit 79f363e
Show file tree
Hide file tree
Showing 18 changed files with 57 additions and 41 deletions.
1 change: 0 additions & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
type Controller interface {
DatabasePath() string
Symbol() string
Decimals() uint8
GetParser(string) (chain.Parser, error)
HandleTx(*chain.Transaction, *chain.Result)
LookupBalance(address codec.Address, uri string) (uint64, error)
Expand Down
4 changes: 2 additions & 2 deletions cli/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (h *Handler) SetKey() error {
"%d) {{cyan}}address:{{/}} %s {{cyan}}balance:{{/}} %s %s\n",
i,
addrStr,
utils.FormatBalance(balance, h.c.Decimals()),
utils.FormatBalance(balance),
h.c.Symbol(),
)
}
Expand Down Expand Up @@ -73,7 +73,7 @@ func (h *Handler) Balance(checkAllChains bool) error {
utils.Outf(
"{{cyan}}address:{{/}} %s {{cyan}}balance:{{/}} %s %s\n",
addr,
utils.FormatBalance(balance, h.c.Decimals()),
utils.FormatBalance(balance),
h.c.Symbol(),
)
}
Expand Down
5 changes: 2 additions & 3 deletions cli/prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ func Asset(label string, symbol string, allowNative bool) (ids.ID, error) {

func Amount(
label string,
decimals uint8,
balance uint64,
f func(input uint64) error,
) (uint64, error) {
Expand All @@ -134,7 +133,7 @@ func Amount(
if len(input) == 0 {
return ErrInputEmpty
}
amount, err := utils.ParseBalance(input, decimals)
amount, err := utils.ParseBalance(input)
if err != nil {
return err
}
Expand All @@ -152,7 +151,7 @@ func Amount(
return 0, err
}
rawAmount = strings.TrimSpace(rawAmount)
return utils.ParseBalance(rawAmount, decimals)
return utils.ParseBalance(rawAmount)
}

func Int(
Expand Down
4 changes: 2 additions & 2 deletions cli/spam.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (h *Handler) Spam(sh SpamHelper) error {
distAmount := (balance - withholding) / uint64(numAccounts)
utils.Outf(
"{{yellow}}distributing funds to each account:{{/}} %s %s\n",
utils.FormatBalance(distAmount, h.c.Decimals()),
utils.FormatBalance(distAmount),
h.c.Symbol(),
)
accounts := make([]*PrivateKey, numAccounts)
Expand Down Expand Up @@ -455,7 +455,7 @@ func (h *Handler) Spam(sh SpamHelper) error {
}
utils.Outf(
"{{yellow}}returned funds:{{/}} %s %s\n",
utils.FormatBalance(returnedBalance, h.c.Decimals()),
utils.FormatBalance(returnedBalance),
h.c.Symbol(),
)
return nil
Expand Down
1 change: 1 addition & 0 deletions consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ const (
MaxUint64 = ^uint64(0)
MaxFloat64 = math.MaxFloat64
MillisecondsPerSecond = 1000
Decimals = 9
)
8 changes: 1 addition & 7 deletions docs/tutorials/morpheusvm/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ func GetBalanceFromState(
This function is almost identical to `getBalance()` except that we are passing
along `f` of type `ReadState` instead of `im` of type `state.Immutable`.

We also need to specify the precision of our VM token. To do this, go to
`consts/consts.go` and add the following constant:

```golang
const Decimals = 9
```

## Getting Started

Expand Down Expand Up @@ -233,7 +227,7 @@ func (cli *JSONRPCClient) WaitForBalance(
if !shouldExit {
utils.Outf(
"{{yellow}}waiting for %s balance: %s{{/}}\n",
utils.FormatBalance(min, consts.Decimals),
utils.FormatBalance(min),
addr,
)
}
Expand Down
3 changes: 1 addition & 2 deletions examples/morpheusvm/cmd/morpheus-cli/cmd/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/ava-labs/hypersdk/chain"
"github.com/ava-labs/hypersdk/cli/prompt"
"github.com/ava-labs/hypersdk/examples/morpheusvm/actions"
"github.com/ava-labs/hypersdk/examples/morpheusvm/consts"
)

var actionCmd = &cobra.Command{
Expand Down Expand Up @@ -43,7 +42,7 @@ var transferCmd = &cobra.Command{
}

// Select amount
amount, err := prompt.Amount("amount", consts.Decimals, balance, nil)
amount, err := prompt.Amount("amount", balance, nil)
if err != nil {
return err
}
Expand Down
6 changes: 1 addition & 5 deletions examples/morpheusvm/cmd/morpheus-cli/cmd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (*Handler) GetBalance(
}
utils.Outf(
"{{yellow}}balance:{{/}} %s %s\n",
utils.FormatBalance(balance, consts.Decimals),
utils.FormatBalance(balance),
consts.Symbol,
)
return balance, nil
Expand All @@ -121,10 +121,6 @@ func (*Controller) Symbol() string {
return consts.Symbol
}

func (*Controller) Decimals() uint8 {
return consts.Decimals
}

func (*Controller) GetParser(uri string) (chain.Parser, error) {
cli := vm.NewJSONRPCClient(uri)
return cli.Parser(context.TODO())
Expand Down
6 changes: 3 additions & 3 deletions examples/morpheusvm/cmd/morpheus-cli/cmd/resolutions.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func handleTx(tx *chain.Transaction, result *chain.Result) {
actor,
result.Error,
float64(result.Fee)/float64(tx.Base.MaxFee)*100,
utils.FormatBalance(result.Fee, consts.Decimals),
utils.FormatBalance(result.Fee),
consts.Symbol,
result.Units,
)
Expand All @@ -81,7 +81,7 @@ func handleTx(tx *chain.Transaction, result *chain.Result) {
var summaryStr string
switch act := action.(type) { //nolint:gocritic
case *actions.Transfer:
summaryStr = fmt.Sprintf("%s %s -> %s\n", utils.FormatBalance(act.Value, consts.Decimals), consts.Symbol, actor)
summaryStr = fmt.Sprintf("%s %s -> %s\n", utils.FormatBalance(act.Value), consts.Symbol, actor)
}
utils.Outf(
"%s {{yellow}}%s{{/}} {{yellow}}actor:{{/}} %s {{yellow}}summary (%s):{{/}} [%s] {{yellow}}fee (max %.2f%%):{{/}} %s %s {{yellow}}consumed:{{/}} [%s]\n",
Expand All @@ -91,7 +91,7 @@ func handleTx(tx *chain.Transaction, result *chain.Result) {
reflect.TypeOf(action),
summaryStr,
float64(result.Fee)/float64(tx.Base.MaxFee)*100,
utils.FormatBalance(result.Fee, consts.Decimals),
utils.FormatBalance(result.Fee),
consts.Symbol,
result.Units,
)
Expand Down
2 changes: 1 addition & 1 deletion examples/morpheusvm/cmd/morpheus-cli/cmd/spam.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (sh *SpamHelper) LookupBalance(choice int, address codec.Address) (uint64,
"%d) {{cyan}}address:{{/}} %s {{cyan}}balance:{{/}} %s %s\n",
choice,
address,
utils.FormatBalance(balance, consts.Decimals),
utils.FormatBalance(balance),
consts.Symbol,
)
return balance, err
Expand Down
2 changes: 1 addition & 1 deletion examples/morpheusvm/vm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (cli *JSONRPCClient) WaitForBalance(
if !shouldExit {
utils.Outf(
"{{yellow}}waiting for %s balance: %s{{/}}\n",
utils.FormatBalance(min, consts.Decimals),
utils.FormatBalance(min),
addr,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/ava-labs/hypersdk/cli/prompt"
"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/examples/vmwithcontracts/actions"
"github.com/ava-labs/hypersdk/examples/vmwithcontracts/consts"
"github.com/ava-labs/hypersdk/utils"
)

Expand Down Expand Up @@ -48,7 +47,7 @@ var transferCmd = &cobra.Command{
}

// Select amount
amount, err := prompt.Amount("amount", consts.Decimals, balance, nil)
amount, err := prompt.Amount("amount", balance, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -127,7 +126,7 @@ var callCmd = &cobra.Command{
}

// Select amount
amount, err := prompt.Amount("amount", consts.Decimals, balance, nil)
amount, err := prompt.Amount("amount", balance, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -174,7 +173,7 @@ var callCmd = &cobra.Command{
if err != nil {
return err
}
utils.Outf("%s\n", utils.FormatBalance(intValue, consts.Decimals))
utils.Outf("%s\n", utils.FormatBalance(intValue))
}
case "get_value":
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (*Handler) GetBalance(
}
utils.Outf(
"{{yellow}}balance:{{/}} %s %s\n",
utils.FormatBalance(balance, consts.Decimals),
utils.FormatBalance(balance),
consts.Symbol,
)
return balance, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func handleTx(tx *chain.Transaction, result *chain.Result) {
actor,
result.Error,
float64(result.Fee)/float64(tx.Base.MaxFee)*100,
utils.FormatBalance(result.Fee, consts.Decimals),
utils.FormatBalance(result.Fee),
consts.Symbol,
result.Units,
)
Expand All @@ -78,7 +78,7 @@ func handleTx(tx *chain.Transaction, result *chain.Result) {
var summaryStr string
switch act := action.(type) { //nolint:gocritic
case *actions.Transfer:
summaryStr = fmt.Sprintf("%s %s -> %s\n", utils.FormatBalance(act.Value, consts.Decimals), consts.Symbol, act.To)
summaryStr = fmt.Sprintf("%s %s -> %s\n", utils.FormatBalance(act.Value), consts.Symbol, act.To)
}
utils.Outf(
"%s {{yellow}}%s{{/}} {{yellow}}actor:{{/}} %s {{yellow}}summary (%s):{{/}} [%s] {{yellow}}fee (max %.2f%%):{{/}} %s %s {{yellow}}consumed:{{/}} [%s]\n",
Expand All @@ -88,7 +88,7 @@ func handleTx(tx *chain.Transaction, result *chain.Result) {
reflect.TypeOf(action),
summaryStr,
float64(result.Fee)/float64(tx.Base.MaxFee)*100,
utils.FormatBalance(result.Fee, consts.Decimals),
utils.FormatBalance(result.Fee),
consts.Symbol,
result.Units,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (sh *SpamHelper) LookupBalance(choice int, address codec.Address) (uint64,
"%d) {{cyan}}address:{{/}} %s {{cyan}}balance:{{/}} %s %s\n",
choice,
address,
utils.FormatBalance(balance, consts.Decimals),
utils.FormatBalance(balance),
consts.Symbol,
)
return balance, err
Expand Down
2 changes: 1 addition & 1 deletion examples/vmwithcontracts/vm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (cli *JSONRPCClient) WaitForBalance(
if !shouldExit {
utils.Outf(
"{{yellow}}waiting for %s balance: %s{{/}}\n",
utils.FormatBalance(min, consts.Decimals),
utils.FormatBalance(min),
addr,
)
}
Expand Down
8 changes: 4 additions & 4 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,16 @@ func GetPort(uri string) (string, error) {
return purl.Port(), err
}

func FormatBalance(bal uint64, decimals uint8) string {
return strconv.FormatFloat(float64(bal)/math.Pow10(int(decimals)), 'f', int(decimals), 64)
func FormatBalance(bal uint64) string {
return strconv.FormatFloat(float64(bal)/math.Pow10(int(consts.Decimals)), 'f', int(consts.Decimals), 64)
}

func ParseBalance(bal string, decimals uint8) (uint64, error) {
func ParseBalance(bal string) (uint64, error) {
f, err := strconv.ParseFloat(bal, 64)
if err != nil {
return 0, err
}
return uint64(f * math.Pow10(int(decimals))), nil
return uint64(f * math.Pow10(int(consts.Decimals))), nil
}

func Repeat[T any](v T, n int) []T {
Expand Down
29 changes: 29 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"os"
"path/filepath"
"strconv"
"testing"

"github.com/ava-labs/avalanchego/ids"
Expand Down Expand Up @@ -82,3 +83,31 @@ func TestLoadBytes(t *testing.T) {
// Remove
_ = os.Remove(fileName)
}

func TestFormatAndParseBalance(t *testing.T) {
// this test assumes that the number of decimals is 9
require := require.New(t)

testCases := []struct {
input uint64
expected string
}{
{1000000000, "1.000000000"},
{123456789, "0.123456789"},
{1234567890, "1.234567890"},
{9876543210, "9.876543210"},
{0, "0.000000000"},
}

for _, tc := range testCases {
formatted := FormatBalance(tc.input)
require.Equal(tc.expected, formatted)

parsed, err := ParseBalance(tc.expected)
require.NoError(err)
require.Equal(tc.input, parsed)
}

_, err := ParseBalance("invalid")
require.ErrorIs(err, strconv.ErrSyntax)
}

0 comments on commit 79f363e

Please sign in to comment.