From 0b86b5e93c5958157feaf9a5562547e7e0aa96f9 Mon Sep 17 00:00:00 2001 From: amela Date: Wed, 22 May 2024 16:37:04 +0200 Subject: [PATCH] feat(cmd/paratime/denom): Add cmd denom set, set-native and remove --- cmd/paratime/denomination/denomination.go | 17 +++++ cmd/paratime/denomination/remove.go | 51 +++++++++++++++ cmd/paratime/denomination/set-native.go | 49 ++++++++++++++ cmd/paratime/denomination/set.go | 64 +++++++++++++++++++ cmd/paratime/paratime.go | 3 + docs/paratime.md | 29 +++++++++ examples/paratime-denom/00-denom-set.in | 1 + examples/paratime-denom/00-denom-set.out | 0 .../paratime-denom/01-denom-set-native.in | 1 + .../paratime-denom/01-denom-set-native.out | 0 examples/paratime-denom/02-denom-remove.in | 1 + examples/paratime-denom/02-denom-remove.out | 0 12 files changed, 216 insertions(+) create mode 100644 cmd/paratime/denomination/denomination.go create mode 100644 cmd/paratime/denomination/remove.go create mode 100644 cmd/paratime/denomination/set-native.go create mode 100644 cmd/paratime/denomination/set.go create mode 100644 examples/paratime-denom/00-denom-set.in create mode 100644 examples/paratime-denom/00-denom-set.out create mode 100644 examples/paratime-denom/01-denom-set-native.in create mode 100644 examples/paratime-denom/01-denom-set-native.out create mode 100644 examples/paratime-denom/02-denom-remove.in create mode 100644 examples/paratime-denom/02-denom-remove.out diff --git a/cmd/paratime/denomination/denomination.go b/cmd/paratime/denomination/denomination.go new file mode 100644 index 00000000..9ec60ec7 --- /dev/null +++ b/cmd/paratime/denomination/denomination.go @@ -0,0 +1,17 @@ +package denomination + +import ( + "github.com/spf13/cobra" +) + +var Cmd = &cobra.Command{ + Use: "denomination", + Short: "Denomination operations", + Aliases: []string{"denom"}, +} + +func init() { + Cmd.AddCommand(setDenomCmd) + Cmd.AddCommand(setNativeDenomCmd) + Cmd.AddCommand(removeDenomCmd) +} diff --git a/cmd/paratime/denomination/remove.go b/cmd/paratime/denomination/remove.go new file mode 100644 index 00000000..e5d8e293 --- /dev/null +++ b/cmd/paratime/denomination/remove.go @@ -0,0 +1,51 @@ +package denomination + +import ( + "fmt" + "strings" + + "github.com/spf13/cobra" + + "github.com/oasisprotocol/oasis-sdk/client-sdk/go/config" + + cliConfig "github.com/oasisprotocol/cli/config" +) + +var removeDenomCmd = &cobra.Command{ + Use: "remove ", + Short: "Remove denomination", + Args: cobra.ExactArgs(3), + Run: func(cmd *cobra.Command, args []string) { + cfg := cliConfig.Global() + networkArg, ptArg, denomArg := args[0], args[1], args[2] + + if denomArg == config.NativeDenominationKey { + cobra.CheckErr(fmt.Errorf("you cannot delete native denomination")) + return + } + + net := cfg.Networks.All[networkArg] + if net == nil { + cobra.CheckErr(fmt.Errorf("network '%s' does not exist", networkArg)) + return + } + + pt := net.ParaTimes.All[ptArg] + if pt == nil { + cobra.CheckErr(fmt.Errorf("pratime '%s' does not exist", ptArg)) + return + } + + denomArg = strings.ToLower(denomArg) + _, ok := pt.Denominations[denomArg] + if ok { + delete(pt.Denominations, denomArg) + } else { + cobra.CheckErr(fmt.Errorf("denomination '%s' does not exist", denomArg)) + return + } + + err := cfg.Save() + cobra.CheckErr(err) + }, +} diff --git a/cmd/paratime/denomination/set-native.go b/cmd/paratime/denomination/set-native.go new file mode 100644 index 00000000..40ea4887 --- /dev/null +++ b/cmd/paratime/denomination/set-native.go @@ -0,0 +1,49 @@ +package denomination + +import ( + "fmt" + "strconv" + + "github.com/spf13/cobra" + + "github.com/oasisprotocol/oasis-sdk/client-sdk/go/config" + + cliConfig "github.com/oasisprotocol/cli/config" +) + +var setNativeDenomCmd = &cobra.Command{ + Use: "set-native ", + Short: "Set native denomination", + Args: cobra.ExactArgs(4), + Run: func(cmd *cobra.Command, args []string) { + cfg := cliConfig.Global() + networkArg, ptArg, symbolArg, decimalsArg := args[0], args[1], args[2], args[3] + + decimalsInt, err := strconv.Atoi(decimalsArg) + if err != nil { + cobra.CheckErr(fmt.Errorf("number of decimals '%s' cannot be converted to integer", decimalsArg)) + return + } + + net := cfg.Networks.All[networkArg] + if net == nil { + cobra.CheckErr(fmt.Errorf("network '%s' does not exist", networkArg)) + return + } + + pt := net.ParaTimes.All[ptArg] + if pt == nil { + cobra.CheckErr(fmt.Errorf("pratime '%s' does not exist", ptArg)) + return + } + + denomInfo := &config.DenominationInfo{ + Symbol: symbolArg, + Decimals: uint8(decimalsInt), + } + pt.Denominations[config.NativeDenominationKey] = denomInfo + + err = cfg.Save() + cobra.CheckErr(err) + }, +} diff --git a/cmd/paratime/denomination/set.go b/cmd/paratime/denomination/set.go new file mode 100644 index 00000000..07fa0220 --- /dev/null +++ b/cmd/paratime/denomination/set.go @@ -0,0 +1,64 @@ +package denomination + +import ( + "fmt" + "strconv" + + "github.com/spf13/cobra" + flag "github.com/spf13/pflag" + + "github.com/oasisprotocol/oasis-sdk/client-sdk/go/config" + + cliConfig "github.com/oasisprotocol/cli/config" +) + +var ( + symbol string + + setDenomCmd = &cobra.Command{ + Use: "set [--symbol ]", + Short: "Set denomination", + Args: cobra.ExactArgs(4), + Run: func(cmd *cobra.Command, args []string) { + cfg := cliConfig.Global() + networkArg, ptArg, denomArg, decimalsArg := args[0], args[1], args[2], args[3] + + if symbol == "" { + symbol = denomArg + } + + decimalsInt, err := strconv.Atoi(decimalsArg) + if err != nil { + cobra.CheckErr(fmt.Errorf("number of decimals '%s' cannot be converted to integer", decimalsArg)) + return + } + + net := cfg.Networks.All[networkArg] + if net == nil { + cobra.CheckErr(fmt.Errorf("network '%s' does not exist", networkArg)) + return + } + + pt := net.ParaTimes.All[ptArg] + if pt == nil { + cobra.CheckErr(fmt.Errorf("pratime '%s' does not exist", ptArg)) + return + } + + denomInfo := &config.DenominationInfo{ + Symbol: symbol, + Decimals: uint8(decimalsInt), + } + pt.Denominations[denomArg] = denomInfo + + err = cfg.Save() + cobra.CheckErr(err) + }, + } +) + +func init() { + symbolFlag := flag.NewFlagSet("", flag.ContinueOnError) + symbolFlag.StringVar(&symbol, "symbol", "", "Denomination symbol") + setDenomCmd.Flags().AddFlagSet(symbolFlag) +} diff --git a/cmd/paratime/paratime.go b/cmd/paratime/paratime.go index 73c7f3ee..8c6420f4 100644 --- a/cmd/paratime/paratime.go +++ b/cmd/paratime/paratime.go @@ -2,6 +2,8 @@ package paratime import ( "github.com/spf13/cobra" + + "github.com/oasisprotocol/cli/cmd/paratime/denomination" ) var Cmd = &cobra.Command{ @@ -18,4 +20,5 @@ func init() { Cmd.AddCommand(setDefaultCmd) Cmd.AddCommand(showCmd) Cmd.AddCommand(statsCmd) + Cmd.AddCommand(denomination.Cmd) } diff --git a/docs/paratime.md b/docs/paratime.md index cdfc9b2e..7effa74a 100644 --- a/docs/paratime.md +++ b/docs/paratime.md @@ -140,6 +140,35 @@ encrypted: ![code](../examples/paratime-show/show-tx-encrypted.out.static) +## Set information about a denomination {#denom-set} + +To set information about a denomination on the specific network and paratime use +`paratime denom set +--symbol `. To use this command a denomination must already exist in the +actual paratime. + +![code shell](../examples/paratime-denom/00-denom-set.in) + +## Set information about the native denomination {#denom-set-native} + +To set information about the native denomination on the specific network and +paratime use `paratime denom set-native +`. + +The native denomination is already mandatory in the [`paratime add`](#add) +command. + +![code shell](../examples/paratime-denom/01-denom-set-native.in) + +## Remove denomination {#denom-remove} + +To remove an existing denomination on the specific network and paratime use +`paratime denom remove `. + +The native denomination cannot be removed. + +![code shell](../examples/paratime-denom/02-denom-remove.in) + ## Advanced ### Register a New ParaTime {#register} diff --git a/examples/paratime-denom/00-denom-set.in b/examples/paratime-denom/00-denom-set.in new file mode 100644 index 00000000..1627fa39 --- /dev/null +++ b/examples/paratime-denom/00-denom-set.in @@ -0,0 +1 @@ +oasis paratime denom set mainnet sapphire TESTTEST 16 diff --git a/examples/paratime-denom/00-denom-set.out b/examples/paratime-denom/00-denom-set.out new file mode 100644 index 00000000..e69de29b diff --git a/examples/paratime-denom/01-denom-set-native.in b/examples/paratime-denom/01-denom-set-native.in new file mode 100644 index 00000000..900853c1 --- /dev/null +++ b/examples/paratime-denom/01-denom-set-native.in @@ -0,0 +1 @@ +oasis paratime denom set-native testnet cipher TEST 9 diff --git a/examples/paratime-denom/01-denom-set-native.out b/examples/paratime-denom/01-denom-set-native.out new file mode 100644 index 00000000..e69de29b diff --git a/examples/paratime-denom/02-denom-remove.in b/examples/paratime-denom/02-denom-remove.in new file mode 100644 index 00000000..fad7d240 --- /dev/null +++ b/examples/paratime-denom/02-denom-remove.in @@ -0,0 +1 @@ +oasis paratime denom remove mainnet sapphire TESTTEST diff --git a/examples/paratime-denom/02-denom-remove.out b/examples/paratime-denom/02-denom-remove.out new file mode 100644 index 00000000..e69de29b