new bucket list cmd
@@ -10,6 +10,7 @@ import (
"context"
"strconv"
+ "github.com/golang/protobuf/proto"
"github.com/grpc-ecosystem/go-grpc-middleware/util/metautils"
"github.com/spf13/cobra"
"google.golang.org/grpc/codes"
@@ -148,3 +149,51 @@ func GetProbationList(epochNum uint64, epochStartHeight uint64) (*iotexapi.ReadS
}
return response, nil
}
+
+// GetBucketList get bucket list
+func GetBucketList(
+ methodName iotexapi.ReadStakingDataMethod_Name,
+ readStakingDataRequest *iotexapi.ReadStakingDataRequest,
+) (*iotextypes.VoteBucketList, error) {
+ conn, err := util.ConnectToEndpoint(config.ReadConfig.SecureConnect && !config.Insecure)
+ if err != nil {
+ return nil, output.NewError(output.NetworkError, "failed to connect to endpoint", err)
+ }
+ defer conn.Close()
+ cli := iotexapi.NewAPIServiceClient(conn)
+ method := &iotexapi.ReadStakingDataMethod{Method: methodName}
+ methodData, err := proto.Marshal(method)
+ if err != nil {
+ return nil, output.NewError(output.SerializationError, "failed to marshal read staking data method", err)
+ }
+ requestData, err := proto.Marshal(readStakingDataRequest)
+ if err != nil {
+ return nil, output.NewError(output.SerializationError, "failed to marshal read staking data request", err)
+ }
+
+ request := &iotexapi.ReadStateRequest{
+ ProtocolID: []byte("staking"),
+ MethodName: methodData,
+ Arguments: [][]byte{requestData},
+ }
+
+ ctx := context.Background()
+ jwtMD, err := util.JwtAuth()
+ if err == nil {
+ ctx = metautils.NiceMD(jwtMD).ToOutgoing(ctx)
+ }
+
+ response, err := cli.ReadState(ctx, request)
+ if err != nil {
+ sta, ok := status.FromError(err)
+ if ok {
+ return nil, output.NewError(output.APIError, sta.Message(), nil)
+ }
+ return nil, output.NewError(output.NetworkError, "failed to invoke ReadState api", err)
+ }
+ bucketlist := iotextypes.VoteBucketList{}
+ if err := proto.Unmarshal(response.Data, &bucketlist); err != nil {
+ return nil, output.NewError(output.SerializationError, "failed to unmarshal response", err)
+ }
+ return &bucketlist, nil
+}
