Skip to content

Commit

Permalink
Added option to stake validator
Browse files Browse the repository at this point in the history
  • Loading branch information
iamrz1 committed Aug 21, 2022
1 parent 36a8c5d commit ae5d7c2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 35 deletions.
69 changes: 49 additions & 20 deletions cmd/stakepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package cmd

import (
"fmt"
"log"

"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/0chain/gosdk/zcncore"
"github.com/0chain/zboxcli/util"
"log"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -77,7 +76,6 @@ var spInfo = &cobra.Command{
Long: `Stake pool information.`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {

var (
flags = cmd.Flags()
blobberID string
Expand Down Expand Up @@ -147,23 +145,36 @@ var spLock = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {

var (
flags = cmd.Flags()
blobberID string
tokens float64
fee float64
err error
flags = cmd.Flags()
providerID string
providerType sdk.ProviderType
tokens float64
fee float64
err error
)

if flags.Changed("blobber_id") {
if blobberID, err = flags.GetString("blobber_id"); err != nil {
if providerID, err = flags.GetString("blobber_id"); err != nil {
log.Fatalf("invalid 'blobber_id' flag: %v", err)
} else {
providerType = sdk.ProviderBlobber
}
} else if flags.Changed("validator_id") {
if providerID, err = flags.GetString("validator_id"); err != nil {
log.Fatalf("invalid 'validator_id' flag: %v", err)
} else {
providerType = sdk.ProviderValidator
}
}

if !flags.Changed("tokens") {
log.Fatal("missing required 'tokens' flag")
}

if providerType == 0 || providerID == "" {
log.Fatal("missing flag: one of 'blobber_id' or 'validator_id' is required")
}

if tokens, err = flags.GetFloat64("tokens"); err != nil {
log.Fatal("invalid 'tokens' flag: ", err)
}
Expand All @@ -179,7 +190,7 @@ var spLock = &cobra.Command{
}

var poolID string
poolID, _, err = sdk.StakePoolLock(blobberID,
poolID, _, err = sdk.StakePoolLock(providerType, providerID,
zcncore.ConvertToValue(tokens), zcncore.ConvertToValue(fee))
if err != nil {
log.Fatalf("Failed to lock tokens in stake pool: %v", err)
Expand All @@ -197,18 +208,32 @@ var spUnlock = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {

var (
flags = cmd.Flags()
blobberID, poolID string
fee float64
err error
flags = cmd.Flags()
poolID string
providerID string
providerType sdk.ProviderType
fee float64
err error
)

if flags.Changed("blobber_id") {
if blobberID, err = flags.GetString("blobber_id"); err != nil {
if providerID, err = flags.GetString("blobber_id"); err != nil {
log.Fatalf("invalid 'blobber_id' flag: %v", err)
} else {
providerType = sdk.ProviderBlobber
}
} else if flags.Changed("validator_id") {
if providerID, err = flags.GetString("validator_id"); err != nil {
log.Fatalf("invalid 'validator_id' flag: %v", err)
} else {
providerType = sdk.ProviderValidator
}
}

if providerType == 0 || providerID == "" {
log.Fatal("missing flag: one of 'blobber_id' or 'validator_id' is required")
}

if !flags.Changed("pool_id") {
log.Fatal("missing required 'pool_id' flag")
}
Expand All @@ -223,7 +248,8 @@ var spUnlock = &cobra.Command{
}
}

unstake, _, err := sdk.StakePoolUnlock(blobberID, poolID, zcncore.ConvertToValue(fee))
unstake, _, err := sdk.StakePoolUnlock(providerType, providerID,
poolID, zcncore.ConvertToValue(fee))
// an error
if err != nil {
log.Fatalf("Failed to unlock tokens in stake pool: %v", err)
Expand All @@ -239,7 +265,7 @@ var spUnlock = &cobra.Command{
}

// success
fmt.Println("tokens has unlocked, pool deleted")
fmt.Println("tokens unlocked, pool deleted")
},
}

Expand All @@ -256,17 +282,20 @@ func init() {
spUserInfo.PersistentFlags().Bool("json", false, "pass this option to print response as json data")

spLock.PersistentFlags().String("blobber_id", "",
"for given blobber, default is current client")
"for given blobber")
spLock.PersistentFlags().String("validator_id", "",
"for given validator")
spLock.PersistentFlags().Float64("tokens", 0.0,
"tokens to lock, required")
spLock.PersistentFlags().Float64("fee", 0.0,
"transaction fee, default 0")

spLock.MarkFlagRequired("tokens")
spLock.MarkFlagRequired("blobber_id")

spUnlock.PersistentFlags().String("blobber_id", "",
"for given blobber, default is current client")
"for given blobber")
spUnlock.PersistentFlags().String("validator_id", "",
"for given validator")
spUnlock.PersistentFlags().String("pool_id", "",
"pool id to unlock")
spUnlock.PersistentFlags().Float64("fee", 0.0,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/0chain/errors v1.0.3
github.com/0chain/gosdk v1.8.7-0.20220816024056-c4e075c69694
github.com/0chain/gosdk v1.8.7-0.20220820184840-22e91ed63c42
github.com/icza/bitio v1.1.0
github.com/olekukonko/tablewriter v0.0.5
github.com/spf13/cobra v1.5.0
Expand Down
16 changes: 2 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,8 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/0chain/errors v1.0.3 h1:QQZPFxTfnMcRdt32DXbzRQIfGWmBsKoEdszKQDb0rRM=
github.com/0chain/errors v1.0.3/go.mod h1:xymD6nVgrbgttWwkpSCfLLEJbFO6iHGQwk/yeSuYkIc=
github.com/0chain/gosdk v1.8.7-0.20220805044835-6091b1c4aed5 h1:E5QvqW7JT4it1bZZtrcPKO8YDggiu9JMUzcct2y0MAU=
github.com/0chain/gosdk v1.8.7-0.20220805044835-6091b1c4aed5/go.mod h1:ZdbSM2r3G9wekID5HHqJFiP2bYrzyheLRWz9DDzmFYw=
github.com/0chain/gosdk v1.8.7-0.20220807080924-1aff002bd1b0 h1:jsJQK8YlR+7Uexxp+VKNogq2Wpj0TtpDKEWDs+nFkuE=
github.com/0chain/gosdk v1.8.7-0.20220807080924-1aff002bd1b0/go.mod h1:ZdbSM2r3G9wekID5HHqJFiP2bYrzyheLRWz9DDzmFYw=
github.com/0chain/gosdk v1.8.7-0.20220809161948-fd05936d0b70 h1:CGTiH+/lSTrsfcF6N5oUFXGEMxO6CM9qhqLLG55jwyo=
github.com/0chain/gosdk v1.8.7-0.20220809161948-fd05936d0b70/go.mod h1:ZdbSM2r3G9wekID5HHqJFiP2bYrzyheLRWz9DDzmFYw=
github.com/0chain/gosdk v1.8.7-0.20220810014712-78a2669c99cf h1:IdVxllVdH9N15FwQfQO6K8M1M3dhnyvSnS8Dh213LE0=
github.com/0chain/gosdk v1.8.7-0.20220810014712-78a2669c99cf/go.mod h1:ZdbSM2r3G9wekID5HHqJFiP2bYrzyheLRWz9DDzmFYw=
github.com/0chain/gosdk v1.8.7-0.20220811084901-819b86c6922a h1:1+6ASxrfE4PJLwH6IK8y2X4gh5LnZrWV/Hq8YwTsPsU=
github.com/0chain/gosdk v1.8.7-0.20220811084901-819b86c6922a/go.mod h1:ZdbSM2r3G9wekID5HHqJFiP2bYrzyheLRWz9DDzmFYw=
github.com/0chain/gosdk v1.8.7-0.20220815002611-96240cfb56cc h1:AyYZ8npC53+SWJrW3s0QMNGQ0ZcvkM5DjUGQfq9nWbY=
github.com/0chain/gosdk v1.8.7-0.20220815002611-96240cfb56cc/go.mod h1:ZQU/M42RUPryspHYEHZkBzKyFOUF7B1GSCwvFItjFyA=
github.com/0chain/gosdk v1.8.7-0.20220816024056-c4e075c69694 h1:l9KyZBQQxPe6U7QjkuFVpY0UQJ8LtWu/l/6y1dckWaE=
github.com/0chain/gosdk v1.8.7-0.20220816024056-c4e075c69694/go.mod h1:ZQU/M42RUPryspHYEHZkBzKyFOUF7B1GSCwvFItjFyA=
github.com/0chain/gosdk v1.8.7-0.20220820184840-22e91ed63c42 h1:GlBi0m0b8RVoUiNCxJK0oE3DzLPVn4XKV0rOZl+yEsA=
github.com/0chain/gosdk v1.8.7-0.20220820184840-22e91ed63c42/go.mod h1:ZQU/M42RUPryspHYEHZkBzKyFOUF7B1GSCwvFItjFyA=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/VictoriaMetrics/fastcache v1.6.0 h1:C/3Oi3EiBCqufydp1neRZkqcwmEiuRT9c3fqvvgKm5o=
Expand Down

0 comments on commit ae5d7c2

Please sign in to comment.