github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/bc/bcbucketlist.go (about) 1 // Copyright (c) 2022 IoTeX Foundation 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package bc 7 8 import ( 9 "strconv" 10 "strings" 11 12 "github.com/iotexproject/iotex-proto/golang/iotexapi" 13 "github.com/iotexproject/iotex-proto/golang/iotextypes" 14 "github.com/pkg/errors" 15 "github.com/spf13/cobra" 16 17 "github.com/iotexproject/iotex-core/ioctl" 18 "github.com/iotexproject/iotex-core/ioctl/config" 19 ) 20 21 // constants 22 const ( 23 MethodVoter = "voter" 24 MethodCandidate = "cand" 25 ) 26 27 // Multi-language support 28 var ( 29 _bcBucketListCmdShorts = map[config.Language]string{ 30 config.English: "Get bucket list with method and arg(s) on IoTeX blockchain", 31 config.Chinese: "根据方法和参数在IoTeX区块链上读取投票列表", 32 } 33 _bcBucketListCmdUses = map[config.Language]string{ 34 config.English: "bucketlist <method> [arguments]", 35 config.Chinese: "bucketlist <方法> [参数]", 36 } 37 _bcBucketListCmdLongs = map[config.Language]string{ 38 config.English: "Read bucket list\nValid methods: [" + 39 MethodVoter + ", " + MethodCandidate + "]", 40 config.Chinese: "根据方法和参数在IoTeX区块链上读取投票列表\n可用方法有:" + 41 MethodVoter + "," + MethodCandidate, 42 } 43 ) 44 45 // NewBCBucketListCmd represents the bc bucketlist command 46 func NewBCBucketListCmd(client ioctl.Client) *cobra.Command { 47 use, _ := client.SelectTranslation(_bcBucketListCmdUses) 48 short, _ := client.SelectTranslation(_bcBucketListCmdShorts) 49 long, _ := client.SelectTranslation(_bcBucketListCmdLongs) 50 51 return &cobra.Command{ 52 Use: use, 53 Short: short, 54 Long: long, 55 Args: cobra.MinimumNArgs(2), 56 Example: `ioctl bc bucketlist voter [VOTER_ADDRESS] [OFFSET] [LIMIT] 57 ioctl bc bucketlist cand [CANDIDATE_NAME] [OFFSET] [LIMIT]`, 58 RunE: func(cmd *cobra.Command, args []string) error { 59 cmd.SilenceUsage = true 60 61 var ( 62 bl *iotextypes.VoteBucketList 63 address string 64 err error 65 ) 66 67 offset, limit := uint32(0), uint32(1000) 68 method, addr, s := args[0], args[1], args[2:] 69 70 if len(s) > 0 { 71 val, err := strconv.ParseUint(s[0], 10, 32) 72 if err != nil { 73 return errors.Wrap(err, "invalid offset") 74 } 75 offset = uint32(val) 76 } 77 if len(s) > 1 { 78 val, err := strconv.ParseUint(s[1], 10, 32) 79 if err != nil { 80 return errors.Wrap(err, "invalid limit") 81 } 82 limit = uint32(val) 83 } 84 85 switch method { 86 case MethodVoter: 87 address, err = client.AddressWithDefaultIfNotExist(addr) 88 if err != nil { 89 return err 90 } 91 bl, err = getBucketListByVoterAddress(client, address, offset, limit) 92 case MethodCandidate: 93 bl, err = getBucketListByCandidateName(client, addr, offset, limit) 94 default: 95 return errors.New("unknown <method>") 96 } 97 if err != nil { 98 return err 99 } 100 101 var lines []string 102 if len(bl.Buckets) == 0 { 103 lines = append(lines, "Empty bucketlist with given address") 104 } else { 105 for _, b := range bl.Buckets { 106 bucket, err := newBucket(b) 107 if err != nil { 108 return err 109 } 110 lines = append(lines, bucket.String()) 111 } 112 } 113 cmd.Println(strings.Join(lines, "\n")) 114 return nil 115 }, 116 } 117 } 118 119 func getBucketListByVoterAddress(client ioctl.Client, addr string, offset, limit uint32) (*iotextypes.VoteBucketList, error) { 120 readStakingdataRequest := &iotexapi.ReadStakingDataRequest{ 121 Request: &iotexapi.ReadStakingDataRequest_BucketsByVoter{ 122 BucketsByVoter: &iotexapi.ReadStakingDataRequest_VoteBucketsByVoter{ 123 VoterAddress: addr, 124 Pagination: &iotexapi.PaginationParam{ 125 Offset: offset, 126 Limit: limit, 127 }, 128 }, 129 }, 130 } 131 return GetBucketList(client, iotexapi.ReadStakingDataMethod_BUCKETS_BY_VOTER, readStakingdataRequest) 132 } 133 134 func getBucketListByCandidateName(client ioctl.Client, candName string, offset, limit uint32) (*iotextypes.VoteBucketList, error) { 135 readStakingDataRequest := &iotexapi.ReadStakingDataRequest{ 136 Request: &iotexapi.ReadStakingDataRequest_BucketsByCandidate{ 137 BucketsByCandidate: &iotexapi.ReadStakingDataRequest_VoteBucketsByCandidate{ 138 CandName: candName, 139 Pagination: &iotexapi.PaginationParam{ 140 Offset: offset, 141 Limit: limit, 142 }, 143 }, 144 }, 145 } 146 return GetBucketList(client, iotexapi.ReadStakingDataMethod_BUCKETS_BY_CANDIDATE, readStakingDataRequest) 147 }