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