Skip to content

Commit

Permalink
Problem: base denomination can't be parsed with decimal point (fixes #16
Browse files Browse the repository at this point in the history
)

Solution:
- decimal point is something for the future: cosmos/cosmos-sdk#7113
- for the moment -- configured "human" denomination ("cro") + base ("basecro" / "carson") -- configs/tx use "basecro"
- custom commands that use "human" denomination and convert it to the base
- custom init that changes bond_denom + gov minimum deposit to use the base denomination
  • Loading branch information
tomtau committed Sep 7, 2020
1 parent 78b049b commit 06e748a
Show file tree
Hide file tree
Showing 8 changed files with 535 additions and 8 deletions.
9 changes: 9 additions & 0 deletions app/prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,21 @@ var (
ValidatorPubKeyPrefix = "crocnclpub"
ConsNodeAddressPrefix = "crocnclcons"
ConsNodePubKeyPrefix = "crocnclconspub"
HumanCoinUnit = "cro"
BaseCoinUnit = "basecro" // 10^-8 AKA "carson"
)

func SetConfig() {
config := sdk.GetConfig()
config.SetBech32PrefixForAccount(AccountAddressPrefix, AccountPubKeyPrefix)
config.SetBech32PrefixForValidator(ValidatorAddressPrefix, ValidatorPubKeyPrefix)
config.SetBech32PrefixForConsensusNode(ConsNodeAddressPrefix, ConsNodePubKeyPrefix)

croUnit := sdk.OneDec()
sdk.RegisterDenom(HumanCoinUnit, croUnit)

carsonUnit := sdk.NewDecWithPrec(1, 8) // 10^-8 (carson)
sdk.RegisterDenom(BaseCoinUnit, carsonUnit)

config.Seal()
}
42 changes: 42 additions & 0 deletions app/prefix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package app_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/crypto-com/chain-main/app"
"github.com/stretchr/testify/require"
)

func TestConversion(t *testing.T) {
app.SetConfig()

testCases := []struct {
input sdk.Coin
denom string
result sdk.Coin
expErr bool
}{
{sdk.NewCoin("foo", sdk.ZeroInt()), app.HumanCoinUnit, sdk.Coin{}, true},
{sdk.NewCoin(app.HumanCoinUnit, sdk.ZeroInt()), "foo", sdk.Coin{}, true},
{sdk.NewCoin(app.HumanCoinUnit, sdk.ZeroInt()), "FOO", sdk.Coin{}, true},

{sdk.NewCoin(app.HumanCoinUnit, sdk.NewInt(5)), app.BaseCoinUnit, sdk.NewCoin(app.BaseCoinUnit, sdk.NewInt(500000000)), false}, // cro => carson

{sdk.NewCoin(app.BaseCoinUnit, sdk.NewInt(500000000)), app.HumanCoinUnit, sdk.NewCoin(app.HumanCoinUnit, sdk.NewInt(5)), false}, // carson => cro

}

for i, tc := range testCases {
res, err := sdk.ConvertCoin(tc.input, tc.denom)
require.Equal(
t, tc.expErr, err != nil,
"unexpected error; tc: #%d, input: %s, denom: %s", i+1, tc.input, tc.denom,
)
require.Equal(
t, tc.result, res,
"invalid result; tc: #%d, input: %s, denom: %s", i+1, tc.input, tc.denom,
)
}

}
55 changes: 50 additions & 5 deletions cmd/chain-maincli/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bufio"
"fmt"
"os"
"path"
Expand All @@ -10,21 +11,24 @@ import (
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/client/lcd"
"github.com/cosmos/cosmos-sdk/client/rpc"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/auth"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
"github.com/cosmos/cosmos-sdk/x/bank"
bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli"
//"github.com/gogo/protobuf/codec"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/tendermint/go-amino"
amino "github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/libs/cli"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
"github.com/crypto-com/chain-main/app"
// this line is used by starport scaffolding
// this line is used by starport scaffolding
)

func main() {
Expand Down Expand Up @@ -99,14 +103,55 @@ func queryCmd(cdc *amino.Codec) *cobra.Command {
return queryCmd
}

// SendTxCmd will create a send tx and sign it with the given key.
// and convert CRO to carson
func CustomSendTxCmd(cdc *amino.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "send [from_key_or_address] [to_address] [amount]",
Short: "Create and sign a send tx",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
cliCtx := context.NewCLIContextWithInputAndFrom(inBuf, args[0]).WithCodec(cdc)

to, err := sdk.AccAddressFromBech32(args[1])
if err != nil {
return err
}

// parse coins trying to be sent
coins, err := sdk.ParseCoins(args[2])
if err != nil {
return err
}
for i := 0; i < len(coins); i++ {
coin, err := sdk.ConvertCoin(coins[i], app.BaseCoinUnit)
if err != nil {
return err
}
coins[i] = coin
}

// build and sign the transaction, then broadcast to Tendermint
msg := bank.NewMsgSend(cliCtx.GetFromAddress(), to, coins)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}

cmd = flags.PostCommands(cmd)[0]

return cmd
}

func txCmd(cdc *amino.Codec) *cobra.Command {
txCmd := &cobra.Command{
Use: "tx",
Short: "Transactions subcommands",
}

txCmd.AddCommand(
bankcmd.SendTxCmd(cdc),
CustomSendTxCmd(cdc),
flags.LineBreak,
authcmd.GetSignCommand(cdc),
authcmd.GetMultiSignCommand(cdc),
Expand Down Expand Up @@ -141,7 +186,7 @@ func registerRoutes(rs *lcd.RestServer) {
client.RegisterRoutes(rs.CliCtx, rs.Mux)
authrest.RegisterTxRoutes(rs.CliCtx, rs.Mux)
app.ModuleBasics.RegisterRESTRoutes(rs.CliCtx, rs.Mux)
// this line is used by starport scaffolding # 2
// this line is used by starport scaffolding # 2
}

func initConfig(cmd *cobra.Command) error {
Expand Down
18 changes: 17 additions & 1 deletion cmd/chain-maind/genaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"errors"
"fmt"

"github.com/crypto-com/chain-main/app"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/tendermint/tendermint/libs/cli"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
Expand Down Expand Up @@ -72,6 +73,13 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
if err != nil {
return fmt.Errorf("failed to parse coins: %w", err)
}
for i := 0; i < len(coins); i++ {
coin, err := sdk.ConvertCoin(coins[i], app.BaseCoinUnit)
if err != nil {
return fmt.Errorf("failed to convert coins: %w", err)
}
coins[i] = coin
}

vestingStart := viper.GetInt64(flagVestingStart)
vestingEnd := viper.GetInt64(flagVestingEnd)
Expand All @@ -80,6 +88,14 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
return fmt.Errorf("failed to parse vesting amount: %w", err)
}

for i := 0; i < len(vestingAmt); i++ {
coin, err := sdk.ConvertCoin(vestingAmt[i], app.BaseCoinUnit)
if err != nil {
return fmt.Errorf("failed to convert coins: %w", err)
}
vestingAmt[i] = coin
}

// create concrete account type based on input parameters
var genAccount authexported.GenesisAccount

Expand Down
Loading

0 comments on commit 06e748a

Please sign in to comment.