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