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

     1  package ethabi
     2  
     3  import (
     4  	"encoding/hex"
     5  
     6  	"github.com/ethereum/go-ethereum/accounts/abi"
     7  	"google.golang.org/protobuf/proto"
     8  
     9  	"github.com/iotexproject/iotex-proto/golang/iotexapi"
    10  	"github.com/iotexproject/iotex-proto/golang/iotextypes"
    11  
    12  	"github.com/iotexproject/iotex-core/action/protocol"
    13  	"github.com/iotexproject/iotex-core/action/protocol/abiutil"
    14  )
    15  
    16  const _compositeBucketsCountInterfaceABI = `[
    17  	{
    18  		"inputs": [],
    19  		"name": "compositeBucketsCount",
    20  		"outputs": [
    21  			{
    22  				"internalType": "uint64",
    23  				"name": "total",
    24  				"type": "uint64"
    25  			},
    26  			{
    27  				"internalType": "uint64",
    28  				"name": "active",
    29  				"type": "uint64"
    30  			}
    31  		],
    32  		"stateMutability": "view",
    33  		"type": "function"
    34  	}
    35  ]`
    36  
    37  var _compositeBucketsCountMethod abi.Method
    38  
    39  func init() {
    40  	_compositeBucketsCountMethod = abiutil.MustLoadMethod(_compositeBucketsCountInterfaceABI, "compositeBucketsCount")
    41  }
    42  
    43  // CompositeBucketsCountStateContext context for BucketsCount
    44  type CompositeBucketsCountStateContext struct {
    45  	*protocol.BaseStateContext
    46  }
    47  
    48  func newCompositeBucketsCountStateContext() (*CompositeBucketsCountStateContext, error) {
    49  	method := &iotexapi.ReadStakingDataMethod{
    50  		Method: iotexapi.ReadStakingDataMethod_COMPOSITE_BUCKETS_COUNT,
    51  	}
    52  	methodBytes, err := proto.Marshal(method)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	arguments := &iotexapi.ReadStakingDataRequest{
    57  		Request: &iotexapi.ReadStakingDataRequest_BucketsCount_{
    58  			BucketsCount: &iotexapi.ReadStakingDataRequest_BucketsCount{},
    59  		},
    60  	}
    61  	argumentsBytes, err := proto.Marshal(arguments)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	return &CompositeBucketsCountStateContext{
    66  		&protocol.BaseStateContext{
    67  			Parameter: &protocol.Parameters{
    68  				MethodName: methodBytes,
    69  				Arguments:  [][]byte{argumentsBytes},
    70  			},
    71  		},
    72  	}, nil
    73  }
    74  
    75  // EncodeToEth encode proto to eth
    76  func (r *CompositeBucketsCountStateContext) EncodeToEth(resp *iotexapi.ReadStateResponse) (string, error) {
    77  	var result iotextypes.BucketsCount
    78  	if err := proto.Unmarshal(resp.Data, &result); err != nil {
    79  		return "", err
    80  	}
    81  
    82  	data, err := _compositeBucketsCountMethod.Outputs.Pack(result.Total, result.Active)
    83  	if err != nil {
    84  		return "", err
    85  	}
    86  	return hex.EncodeToString(data), nil
    87  }