github.com/iotexproject/iotex-core@v1.14.1-rc1/action/protocol/staking/ethabi/stake_candidatebyname.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 _candidateByNameInterfaceABI = `[
    16  	{
    17  		"inputs": [
    18  			{
    19  				"internalType": "string",
    20  				"name": "candName",
    21  				"type": "string"
    22  			}
    23  		],
    24  		"name": "candidateByName",
    25  		"outputs": [
    26  			{
    27  				"components": [
    28  					{
    29  						"internalType": "address",
    30  						"name": "ownerAddress",
    31  						"type": "address"
    32  					},
    33  					{
    34  						"internalType": "address",
    35  						"name": "operatorAddress",
    36  						"type": "address"
    37  					},
    38  					{
    39  						"internalType": "address",
    40  						"name": "rewardAddress",
    41  						"type": "address"
    42  					},
    43  					{
    44  						"internalType": "string",
    45  						"name": "name",
    46  						"type": "string"
    47  					},
    48  					{
    49  						"internalType": "uint256",
    50  						"name": "totalWeightedVotes",
    51  						"type": "uint256"
    52  					},
    53  					{
    54  						"internalType": "uint64",
    55  						"name": "selfStakeBucketIdx",
    56  						"type": "uint64"
    57  					},
    58  					{
    59  						"internalType": "uint256",
    60  						"name": "selfStakingTokens",
    61  						"type": "uint256"
    62  					}
    63  				],
    64  				"internalType": "struct IStaking.Candidate",
    65  				"name": "",
    66  				"type": "tuple"
    67  			}
    68  		],
    69  		"stateMutability": "view",
    70  		"type": "function"
    71  	}
    72  ]`
    73  
    74  var _candidateByNameMethod abi.Method
    75  
    76  func init() {
    77  	_candidateByNameMethod = abiutil.MustLoadMethod(_candidateByNameInterfaceABI, "candidateByName")
    78  }
    79  
    80  // CandidateByNameStateContext context for CandidateByName
    81  type CandidateByNameStateContext struct {
    82  	*protocol.BaseStateContext
    83  }
    84  
    85  func newCandidateByNameStateContext(data []byte) (*CandidateByNameStateContext, error) {
    86  	paramsMap := map[string]interface{}{}
    87  	ok := false
    88  	if err := _candidateByNameMethod.Inputs.UnpackIntoMap(paramsMap, data); err != nil {
    89  		return nil, err
    90  	}
    91  	var candName string
    92  	if candName, ok = paramsMap["candName"].(string); !ok {
    93  		return nil, errDecodeFailure
    94  	}
    95  
    96  	method := &iotexapi.ReadStakingDataMethod{
    97  		Method: iotexapi.ReadStakingDataMethod_CANDIDATE_BY_NAME,
    98  	}
    99  	methodBytes, err := proto.Marshal(method)
   100  	if err != nil {
   101  		return nil, err
   102  	}
   103  	arguments := &iotexapi.ReadStakingDataRequest{
   104  		Request: &iotexapi.ReadStakingDataRequest_CandidateByName_{
   105  			CandidateByName: &iotexapi.ReadStakingDataRequest_CandidateByName{
   106  				CandName: candName,
   107  			},
   108  		},
   109  	}
   110  	argumentsBytes, err := proto.Marshal(arguments)
   111  	if err != nil {
   112  		return nil, err
   113  	}
   114  	return &CandidateByNameStateContext{
   115  		&protocol.BaseStateContext{
   116  			Parameter: &protocol.Parameters{
   117  				MethodName: methodBytes,
   118  				Arguments:  [][]byte{argumentsBytes},
   119  			},
   120  		},
   121  	}, nil
   122  }
   123  
   124  // EncodeToEth encode proto to eth
   125  func (r *CandidateByNameStateContext) EncodeToEth(resp *iotexapi.ReadStateResponse) (string, error) {
   126  	var result iotextypes.CandidateV2
   127  	if err := proto.Unmarshal(resp.Data, &result); err != nil {
   128  		return "", err
   129  	}
   130  
   131  	cand, err := encodeCandidateToEth(&result)
   132  	if err != nil {
   133  		return "", err
   134  	}
   135  
   136  	data, err := _candidateByNameMethod.Outputs.Pack(cand)
   137  	if err != nil {
   138  		return "", nil
   139  	}
   140  	return hex.EncodeToString(data), nil
   141  }