github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/util/chain_api.go (about)

     1  package util
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/iotexproject/iotex-proto/golang/iotexapi"
     7  	"github.com/iotexproject/iotex-proto/golang/iotextypes"
     8  	"github.com/pkg/errors"
     9  	"google.golang.org/protobuf/proto"
    10  )
    11  
    12  const (
    13  	_stakingProtocol = "staking"
    14  )
    15  
    16  // GetStakingCandidates returns staking candidates
    17  func GetStakingCandidates(chainClient iotexapi.APIServiceClient, offset, limit uint32) (candidateList *iotextypes.CandidateListV2, err error) {
    18  	methodName, err := proto.Marshal(&iotexapi.ReadStakingDataMethod{
    19  		Method: iotexapi.ReadStakingDataMethod_CANDIDATES,
    20  	})
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  	arg, err := proto.Marshal(&iotexapi.ReadStakingDataRequest{
    25  		Request: &iotexapi.ReadStakingDataRequest_Candidates_{
    26  			Candidates: &iotexapi.ReadStakingDataRequest_Candidates{
    27  				Pagination: &iotexapi.PaginationParam{
    28  					Offset: offset,
    29  					Limit:  limit,
    30  				},
    31  			},
    32  		},
    33  	})
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  	readStateRequest := &iotexapi.ReadStateRequest{
    38  		ProtocolID: []byte(_stakingProtocol),
    39  		MethodName: methodName,
    40  		Arguments:  [][]byte{arg},
    41  	}
    42  	readStateRes, err := chainClient.ReadState(context.Background(), readStateRequest)
    43  	if err != nil {
    44  		return
    45  	}
    46  	candidateList = &iotextypes.CandidateListV2{}
    47  	if err := proto.Unmarshal(readStateRes.GetData(), candidateList); err != nil {
    48  		return nil, errors.Wrap(err, "failed to unmarshal VoteBucketList")
    49  	}
    50  	return
    51  }