github.com/iotexproject/iotex-core@v1.14.1-rc1/action/protocol/staking/ethabi/stake_candidates.go (about)

     1  package ethabi
     2  
     3  import (
     4  	"encoding/hex"
     5  
     6  	"github.com/ethereum/go-ethereum/accounts/abi"
     7  	"github.com/iotexproject/iotex-proto/golang/iotexapi"
     8  	"github.com/iotexproject/iotex-proto/golang/iotextypes"
     9  	"google.golang.org/protobuf/proto"
    10  
    11  	"github.com/iotexproject/iotex-core/action/protocol"
    12  	"github.com/iotexproject/iotex-core/action/protocol/abiutil"
    13  )
    14  
    15  const _candidatesInterfaceABI = `[
    16  	{
    17  		"inputs": [
    18  			{
    19  				"internalType": "uint32",
    20  				"name": "offset",
    21  				"type": "uint32"
    22  			},
    23  			{
    24  				"internalType": "uint32",
    25  				"name": "limit",
    26  				"type": "uint32"
    27  			}
    28  		],
    29  		"name": "candidates",
    30  		"outputs": [
    31  			{
    32  				"components": [
    33  					{
    34  						"internalType": "address",
    35  						"name": "ownerAddress",
    36  						"type": "address"
    37  					},
    38  					{
    39  						"internalType": "address",
    40  						"name": "operatorAddress",
    41  						"type": "address"
    42  					},
    43  					{
    44  						"internalType": "address",
    45  						"name": "rewardAddress",
    46  						"type": "address"
    47  					},
    48  					{
    49  						"internalType": "string",
    50  						"name": "name",
    51  						"type": "string"
    52  					},
    53  					{
    54  						"internalType": "uint256",
    55  						"name": "totalWeightedVotes",
    56  						"type": "uint256"
    57  					},
    58  					{
    59  						"internalType": "uint64",
    60  						"name": "selfStakeBucketIdx",
    61  						"type": "uint64"
    62  					},
    63  					{
    64  						"internalType": "uint256",
    65  						"name": "selfStakingTokens",
    66  						"type": "uint256"
    67  					}
    68  				],
    69  				"internalType": "struct IStaking.Candidate[]",
    70  				"name": "",
    71  				"type": "tuple[]"
    72  			}
    73  		],
    74  		"stateMutability": "view",
    75  		"type": "function"
    76  	}
    77  ]`
    78  
    79  var _candidatesMethod abi.Method
    80  
    81  func init() {
    82  	_candidatesMethod = abiutil.MustLoadMethod(_candidatesInterfaceABI, "candidates")
    83  }
    84  
    85  // CandidatesStateContext context for Candidates
    86  type CandidatesStateContext struct {
    87  	*protocol.BaseStateContext
    88  }
    89  
    90  func newCandidatesStateContext(data []byte) (*CandidatesStateContext, error) {
    91  	paramsMap := map[string]interface{}{}
    92  	ok := false
    93  	if err := _candidatesMethod.Inputs.UnpackIntoMap(paramsMap, data); err != nil {
    94  		return nil, err
    95  	}
    96  	var offset, limit uint32
    97  	if offset, ok = paramsMap["offset"].(uint32); !ok {
    98  		return nil, errDecodeFailure
    99  	}
   100  	if limit, ok = paramsMap["limit"].(uint32); !ok {
   101  		return nil, errDecodeFailure
   102  	}
   103  
   104  	method := &iotexapi.ReadStakingDataMethod{
   105  		Method: iotexapi.ReadStakingDataMethod_CANDIDATES,
   106  	}
   107  	methodBytes, err := proto.Marshal(method)
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  	arguments := &iotexapi.ReadStakingDataRequest{
   112  		Request: &iotexapi.ReadStakingDataRequest_Candidates_{
   113  			Candidates: &iotexapi.ReadStakingDataRequest_Candidates{
   114  				Pagination: &iotexapi.PaginationParam{
   115  					Offset: offset,
   116  					Limit:  limit,
   117  				},
   118  			},
   119  		},
   120  	}
   121  	argumentsBytes, err := proto.Marshal(arguments)
   122  	if err != nil {
   123  		return nil, err
   124  	}
   125  	return &CandidatesStateContext{
   126  		&protocol.BaseStateContext{
   127  			Parameter: &protocol.Parameters{
   128  				MethodName: methodBytes,
   129  				Arguments:  [][]byte{argumentsBytes},
   130  			},
   131  		},
   132  	}, nil
   133  }
   134  
   135  // EncodeToEth encode proto to eth
   136  func (r *CandidatesStateContext) EncodeToEth(resp *iotexapi.ReadStateResponse) (string, error) {
   137  	var result iotextypes.CandidateListV2
   138  	if err := proto.Unmarshal(resp.Data, &result); err != nil {
   139  		return "", err
   140  	}
   141  
   142  	args := make([]CandidateEth, len(result.Candidates))
   143  	for i, candidate := range result.Candidates {
   144  		cand, err := encodeCandidateToEth(candidate)
   145  		if err != nil {
   146  			return "", err
   147  		}
   148  		args[i] = *cand
   149  	}
   150  
   151  	data, err := _candidatesMethod.Outputs.Pack(args)
   152  	if err != nil {
   153  		return "", nil
   154  	}
   155  	return hex.EncodeToString(data), nil
   156  }