github.com/iotexproject/iotex-core@v1.14.1-rc1/action/protocol/staking/ethabi/stake_bucketscount.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  var _bucketsCountInterfaceABI = `[
    16  	{
    17  		"inputs": [],
    18  		"name": "bucketsCount",
    19  		"outputs": [
    20  			{
    21  				"internalType": "uint64",
    22  				"name": "total",
    23  				"type": "uint64"
    24  			},
    25  			{
    26  				"internalType": "uint64",
    27  				"name": "active",
    28  				"type": "uint64"
    29  			}
    30  		],
    31  		"stateMutability": "view",
    32  		"type": "function"
    33  	}
    34  ]`
    35  
    36  var _bucketsCountMethod abi.Method
    37  
    38  func init() {
    39  	_bucketsCountMethod = abiutil.MustLoadMethod(_bucketsCountInterfaceABI, "bucketsCount")
    40  }
    41  
    42  // BucketsCountStateContext context for BucketsCount
    43  type BucketsCountStateContext struct {
    44  	*protocol.BaseStateContext
    45  }
    46  
    47  func newBucketsCountStateContext() (*BucketsCountStateContext, error) {
    48  	method := &iotexapi.ReadStakingDataMethod{
    49  		Method: iotexapi.ReadStakingDataMethod_BUCKETS_COUNT,
    50  	}
    51  	methodBytes, err := proto.Marshal(method)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	arguments := &iotexapi.ReadStakingDataRequest{
    56  		Request: &iotexapi.ReadStakingDataRequest_BucketsCount_{
    57  			BucketsCount: &iotexapi.ReadStakingDataRequest_BucketsCount{},
    58  		},
    59  	}
    60  	argumentsBytes, err := proto.Marshal(arguments)
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	return &BucketsCountStateContext{
    65  		&protocol.BaseStateContext{
    66  			Parameter: &protocol.Parameters{
    67  				MethodName: methodBytes,
    68  				Arguments:  [][]byte{argumentsBytes},
    69  			},
    70  		},
    71  	}, nil
    72  }
    73  
    74  // EncodeToEth encode proto to eth
    75  func (r *BucketsCountStateContext) EncodeToEth(resp *iotexapi.ReadStateResponse) (string, error) {
    76  	var result iotextypes.BucketsCount
    77  	if err := proto.Unmarshal(resp.Data, &result); err != nil {
    78  		return "", err
    79  	}
    80  
    81  	data, err := _bucketsCountMethod.Outputs.Pack(result.Total, result.Active)
    82  	if err != nil {
    83  		return "", err
    84  	}
    85  	return hex.EncodeToString(data), nil
    86  }