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

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