github.com/iotexproject/iotex-core@v1.14.1-rc1/action/grantreward.go (about)

     1  // Copyright (c) 2019 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package action
     7  
     8  import (
     9  	"math/big"
    10  	"strings"
    11  
    12  	"google.golang.org/protobuf/proto"
    13  
    14  	"github.com/ethereum/go-ethereum/accounts/abi"
    15  	"github.com/ethereum/go-ethereum/core/types"
    16  	"github.com/iotexproject/iotex-core/pkg/util/byteutil"
    17  	"github.com/iotexproject/iotex-proto/golang/iotextypes"
    18  )
    19  
    20  var (
    21  	_grantRewardMethod abi.Method
    22  	_                  EthCompatibleAction = (*GrantReward)(nil)
    23  )
    24  
    25  const (
    26  	// BlockReward indicates that the action is to grant block reward
    27  	BlockReward = iota
    28  	// EpochReward indicates that the action is to grant epoch reward
    29  	EpochReward
    30  
    31  	_grantrewardInterfaceABI = `[
    32  		{
    33  			"inputs": [
    34  				{
    35  					"internalType": "int8",
    36  					"name": "rewardType",
    37  					"type": "int8"
    38  				},
    39  				{
    40  					"internalType": "uint64",
    41  					"name": "height",
    42  					"type": "uint64"
    43  				}
    44  			],
    45  			"name": "grantReward",
    46  			"outputs": [],
    47  			"stateMutability": "nonpayable",
    48  			"type": "function"
    49  		}
    50  	]`
    51  )
    52  
    53  func init() {
    54  	grantRewardInterface, err := abi.JSON(strings.NewReader(_grantrewardInterfaceABI))
    55  	if err != nil {
    56  		panic(err)
    57  	}
    58  	var ok bool
    59  	_grantRewardMethod, ok = grantRewardInterface.Methods["grantReward"]
    60  	if !ok {
    61  		panic("fail to load the method")
    62  	}
    63  }
    64  
    65  // GrantReward is the action to grant either block or epoch reward
    66  type GrantReward struct {
    67  	AbstractAction
    68  
    69  	rewardType int
    70  	height     uint64
    71  }
    72  
    73  // RewardType returns the grant reward type
    74  func (g *GrantReward) RewardType() int { return g.rewardType }
    75  
    76  // Height returns the block height to grant reward
    77  func (g *GrantReward) Height() uint64 { return g.height }
    78  
    79  // Serialize returns a raw byte stream of a grant reward action
    80  func (g *GrantReward) Serialize() []byte {
    81  	return byteutil.Must(proto.Marshal(g.Proto()))
    82  }
    83  
    84  // Proto converts a grant reward action struct to a grant reward action protobuf
    85  func (g *GrantReward) Proto() *iotextypes.GrantReward {
    86  	gProto := iotextypes.GrantReward{
    87  		Height: g.height,
    88  	}
    89  	switch g.rewardType {
    90  	case BlockReward:
    91  		gProto.Type = iotextypes.RewardType_BlockReward
    92  	case EpochReward:
    93  		gProto.Type = iotextypes.RewardType_EpochReward
    94  	}
    95  	return &gProto
    96  }
    97  
    98  // LoadProto converts a grant reward action protobuf to a grant reward action struct
    99  func (g *GrantReward) LoadProto(gProto *iotextypes.GrantReward) error {
   100  	*g = GrantReward{
   101  		height: gProto.Height,
   102  	}
   103  	switch gProto.Type {
   104  	case iotextypes.RewardType_BlockReward:
   105  		g.rewardType = BlockReward
   106  	case iotextypes.RewardType_EpochReward:
   107  		g.rewardType = EpochReward
   108  	}
   109  	return nil
   110  }
   111  
   112  // IntrinsicGas returns the intrinsic gas of a grant reward action, which is 0
   113  func (*GrantReward) IntrinsicGas() (uint64, error) {
   114  	return 0, nil
   115  }
   116  
   117  // Cost returns the total cost of a grant reward action
   118  func (*GrantReward) Cost() (*big.Int, error) {
   119  	return big.NewInt(0), nil
   120  }
   121  
   122  // EncodeABIBinary encodes data in abi encoding
   123  func (g *GrantReward) EncodeABIBinary() ([]byte, error) {
   124  	return g.encodeABIBinary()
   125  }
   126  
   127  func (g *GrantReward) encodeABIBinary() ([]byte, error) {
   128  	data, err := _grantRewardMethod.Inputs.Pack(
   129  		int8(g.rewardType),
   130  		g.height,
   131  	)
   132  	if err != nil {
   133  		return nil, err
   134  	}
   135  	return append(_grantRewardMethod.ID, data...), nil
   136  }
   137  
   138  // ToEthTx converts a grant reward action to an ethereum transaction
   139  func (g *GrantReward) ToEthTx(_ uint32) (*types.Transaction, error) {
   140  	data, err := g.encodeABIBinary()
   141  	if err != nil {
   142  		return nil, err
   143  	}
   144  	return types.NewTx(&types.LegacyTx{
   145  		Nonce:    g.Nonce(),
   146  		GasPrice: g.GasPrice(),
   147  		Gas:      g.GasLimit(),
   148  		To:       &_rewardingProtocolEthAddr,
   149  		Data:     data,
   150  		Value:    big.NewInt(0),
   151  	}), nil
   152  }
   153  
   154  // GrantRewardBuilder is the struct to build GrantReward
   155  type GrantRewardBuilder struct {
   156  	Builder
   157  	grantReward GrantReward
   158  }
   159  
   160  // SetRewardType sets the grant reward type
   161  func (b *GrantRewardBuilder) SetRewardType(t int) *GrantRewardBuilder {
   162  	b.grantReward.rewardType = t
   163  	return b
   164  }
   165  
   166  // SetHeight sets the grant reward block height
   167  func (b *GrantRewardBuilder) SetHeight(height uint64) *GrantRewardBuilder {
   168  	b.grantReward.height = height
   169  	return b
   170  }
   171  
   172  // Build builds a new grant reward action
   173  func (b *GrantRewardBuilder) Build() GrantReward {
   174  	b.grantReward.AbstractAction = b.Builder.Build()
   175  	return b.grantReward
   176  }