Skip to content

Commit

Permalink
Fixed methods related to new simplified read-pool
Browse files Browse the repository at this point in the history
  • Loading branch information
iamrz1 committed May 25, 2022
1 parent 8c0b5c4 commit 0627140
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 68 deletions.
102 changes: 38 additions & 64 deletions cmd/readpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"
"log"
"time"

"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/0chain/gosdk/zcncore"
Expand All @@ -26,7 +25,14 @@ var rpCreate = &cobra.Command{
},
}

func printReadPoolStat(stat []*sdk.AllocationPoolStat) {
func printReadPoolInfo(stat *sdk.ReadPool) {
fmt.Println()
fmt.Println(" balance for own allocations: ", stat.OwnerBalance)
fmt.Println(" balance for other allocations: ", stat.VisitorBalance)
fmt.Println()
}

func printWritePoolStat(stat []*sdk.AllocationPoolStat) {
for _, st := range stat {
fmt.Println("- id: ", st.ID)
fmt.Println(" balance: ", st.Balance.String())
Expand All @@ -49,35 +55,23 @@ var rpInfo = &cobra.Command{
Long: `Read pool information.`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {

var (
flags = cmd.Flags()
allocID string
err error
)

if flags.Changed("allocation") {
if allocID, err = flags.GetString("allocation"); err != nil {
log.Fatalf("can't get 'allocation' flag: %v", err)
}
}
doJSON, _ := cmd.Flags().GetBool("json")

var info *sdk.AllocationPoolStats
if info, err = sdk.GetReadPoolInfo(""); err != nil {
info, err := sdk.GetReadPoolInfo("")
if err != nil {
log.Fatalf("Failed to get read pool info: %v", err)
}
if len(info.Pools) == 0 {

if info.OwnerBalance == 0 && info.VisitorBalance == 0 {
fmt.Println("no tokens locked")
return
}

info.AllocFilter(allocID)
if doJSON {
util.PrintJSON(info.Pools)
util.PrintJSON(info)
return
}
printReadPoolStat(info.Pools)
printReadPoolInfo(info)
},
}

Expand All @@ -90,41 +84,16 @@ var rpLock = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {

var (
flags = cmd.Flags()
duration time.Duration
allocID string // required
blobberID string // optional
tokens float64
fee float64
err error
flags = cmd.Flags()
tokens float64
fee float64
err error
)

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

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

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

if duration, err = flags.GetDuration("duration"); err != nil {
log.Fatal("invalid 'duration' flag: ", err)
}

if allocID, err = flags.GetString("allocation"); err != nil {
log.Fatal("invalid 'allocation' flag: ", err)
}

if flags.Changed("blobber") {
if blobberID, err = flags.GetString("blobber"); err != nil {
log.Fatal("invalid 'blobber' flag: ", err)
}
}

if tokens, err = flags.GetFloat64("tokens"); err != nil {
log.Fatal("invalid 'tokens' flag: ", err)
}
Expand All @@ -135,11 +104,18 @@ var rpLock = &cobra.Command{
}
}

_, _, err = sdk.ReadPoolLock(duration, allocID, blobberID,
zcncore.ConvertToValue(tokens), zcncore.ConvertToValue(fee))
isOwner := true
if flags.Changed("owner") {
if isOwner, err = flags.GetBool("owner"); err != nil {
log.Fatal("invalid 'owner' flag: ", err)
}
}

_, _, err = sdk.ReadPoolLock(zcncore.ConvertToValue(tokens), zcncore.ConvertToValue(fee), isOwner)
if err != nil {
log.Fatalf("Failed to lock tokens in read pool: %v", err)
}

fmt.Println("locked")
},
}
Expand All @@ -153,27 +129,25 @@ var rpUnlock = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {

var (
flags = cmd.Flags()
poolID string
fee float64
err error
flags = cmd.Flags()
fee float64
err error
)

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

if poolID, err = flags.GetString("pool_id"); err != nil {
log.Fatal("invalid 'pool_id' flag: ", err)
}

if flags.Changed("fee") {
if fee, err = flags.GetFloat64("fee"); err != nil {
log.Fatal("invalid 'fee' flag: ", err)
}
}

_, _, err = sdk.ReadPoolUnlock(poolID, zcncore.ConvertToValue(fee))
isOwner := true
if flags.Changed("owner") {
if isOwner, err = flags.GetBool("owner"); err != nil {
log.Fatal("invalid 'owner' flag: ", err)
}
}

_, _, err = sdk.ReadPoolUnlock(zcncore.ConvertToValue(fee), isOwner)
if err != nil {
log.Fatalf("Failed to unlock tokens in read pool: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/writepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var wpInfo = &cobra.Command{
util.PrintJSON(info.Pools)
return
}
printReadPoolStat(info.Pools)
printWritePoolStat(info.Pools)
},
}

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.4-0.20220520000521-937107c10563
github.com/0chain/gosdk v1.8.4-0.20220525202650-6f3a31801ec9
github.com/icza/bitio v1.1.0
github.com/olekukonko/tablewriter v0.0.5
github.com/spf13/cobra v1.4.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ collectd.org v0.3.0/go.mod h1:A/8DzQBkF6abtvrT2j/AU/4tiBgJWYyh0y/oB/4MlWE=
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.4-0.20220520000521-937107c10563 h1:W2UYjHKmQgA5nbifSUc4R8RAbCBcnv6KtvZpMB/2NfA=
github.com/0chain/gosdk v1.8.4-0.20220520000521-937107c10563/go.mod h1:td58TBB5wrWGrULiXHQKNqqdoAmCtXtaERGB3bFIfYo=
github.com/0chain/gosdk v1.8.4-0.20220525202650-6f3a31801ec9 h1:0EWruqZvG4IY3Wjk3IWlQHLl0ZDjXhGARs6xVOusH8I=
github.com/0chain/gosdk v1.8.4-0.20220525202650-6f3a31801ec9/go.mod h1:td58TBB5wrWGrULiXHQKNqqdoAmCtXtaERGB3bFIfYo=
github.com/Azure/azure-sdk-for-go/sdk/azcore v0.21.1/go.mod h1:fBF9PQNqB8scdgpZ3ufzaLntG0AG7C1WjPMsiFOmfHM=
github.com/Azure/azure-sdk-for-go/sdk/internal v0.8.3/go.mod h1:KLF4gFr6DcKFZwSuH8w8yEK6DpFl3LP5rhdvAb7Yz5I=
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v0.3.0/go.mod h1:tPaiy8S5bQ+S5sOiDlINkp7+Ef339+Nz5L5XO+cnOHo=
Expand Down

0 comments on commit 0627140

Please sign in to comment.