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

     1  package action
     2  
     3  import (
     4  	"encoding/hex"
     5  	"math/big"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  var (
    12  	_defaultGasPrice      = big.NewInt(1000000000000)
    13  	_errNegativeNumberMsg = "negative value"
    14  )
    15  
    16  func TestClaimRewardSerialize(t *testing.T) {
    17  	r := require.New(t)
    18  
    19  	rc := &ClaimFromRewardingFund{}
    20  	data := rc.Serialize()
    21  	r.NotNil(data)
    22  
    23  	rc.amount = big.NewInt(100)
    24  	rc.data = []byte{1}
    25  	data = rc.Serialize()
    26  	r.NotNil(data)
    27  	r.EqualValues("0a03313030120101", hex.EncodeToString(data))
    28  }
    29  
    30  func TestClaimRewardIntrinsicGas(t *testing.T) {
    31  	r := require.New(t)
    32  
    33  	rc := &ClaimFromRewardingFund{}
    34  	gas, err := rc.IntrinsicGas()
    35  	r.NoError(err)
    36  	r.EqualValues(10000, gas)
    37  
    38  	rc.amount = big.NewInt(100000000)
    39  	gas, err = rc.IntrinsicGas()
    40  	r.NoError(err)
    41  	r.EqualValues(10000, gas)
    42  
    43  	rc.data = []byte{1}
    44  	gas, err = rc.IntrinsicGas()
    45  	r.NoError(err)
    46  	r.EqualValues(10100, gas)
    47  }
    48  
    49  func TestClaimRewardSanityCheck(t *testing.T) {
    50  	r := require.New(t)
    51  
    52  	rc := &ClaimFromRewardingFund{}
    53  
    54  	rc.amount = big.NewInt(1)
    55  	err := rc.SanityCheck()
    56  	r.NoError(err)
    57  
    58  	rc.amount = big.NewInt(-1)
    59  	err = rc.SanityCheck()
    60  	r.NotNil(err)
    61  	r.EqualValues(_errNegativeNumberMsg, err.Error())
    62  }
    63  
    64  func TestClaimRewardCost(t *testing.T) {
    65  	r := require.New(t)
    66  
    67  	rc := &ClaimFromRewardingFund{}
    68  	rc.gasPrice = _defaultGasPrice
    69  	cost, err := rc.Cost()
    70  	r.Nil(err)
    71  	r.EqualValues("10000000000000000", cost.String())
    72  
    73  	rc.amount = big.NewInt(100)
    74  	cost, err = rc.Cost()
    75  	r.Nil(err)
    76  	r.EqualValues("10000000000000000", cost.String())
    77  
    78  	rc.data = []byte{1}
    79  	cost, err = rc.Cost()
    80  	r.Nil(err)
    81  	r.EqualValues("10100000000000000", cost.String())
    82  }
    83  
    84  func TestClaimRewardEncodeABIBinary(t *testing.T) {
    85  	r := require.New(t)
    86  
    87  	rc := &ClaimFromRewardingFund{}
    88  	rc.amount = big.NewInt(101)
    89  	data, err := rc.encodeABIBinary()
    90  	r.Nil(err)
    91  	r.EqualValues(
    92  		"2df163ef000000000000000000000000000000000000000000000000000000000000006500000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000",
    93  		hex.EncodeToString(data),
    94  	)
    95  
    96  	rc.data = []byte{1, 2, 3}
    97  	data, err = rc.encodeABIBinary()
    98  	r.Nil(err)
    99  	r.EqualValues(
   100  		"2df163ef000000000000000000000000000000000000000000000000000000000000006500000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
   101  		hex.EncodeToString(data),
   102  	)
   103  }
   104  
   105  func TestClaimRewardToEthTx(t *testing.T) {
   106  	r := require.New(t)
   107  
   108  	rc := &ClaimFromRewardingFund{}
   109  	rc.amount = big.NewInt(101)
   110  	tx, err := rc.ToEthTx(0)
   111  	r.Nil(err)
   112  	r.EqualValues(_rewardingProtocolEthAddr, *tx.To())
   113  	r.EqualValues(
   114  		"2df163ef000000000000000000000000000000000000000000000000000000000000006500000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000",
   115  		hex.EncodeToString(tx.Data()),
   116  	)
   117  	r.EqualValues("0", tx.Value().String())
   118  
   119  	rc.data = []byte{1, 2, 3}
   120  	tx, err = rc.ToEthTx(0)
   121  	r.Nil(err)
   122  	r.EqualValues(_rewardingProtocolEthAddr, *tx.To())
   123  	r.EqualValues(
   124  		"2df163ef000000000000000000000000000000000000000000000000000000000000006500000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
   125  		hex.EncodeToString(tx.Data()),
   126  	)
   127  	r.EqualValues("0", tx.Value().String())
   128  }
   129  
   130  func TestNewRewardingClaimFromABIBinary(t *testing.T) {
   131  	r := require.New(t)
   132  
   133  	data, _ := hex.DecodeString("2df163ef000000000000000000000000000000000000000000000000000000000000006500000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003")
   134  
   135  	rc, err := NewClaimFromRewardingFundFromABIBinary(data)
   136  	r.Nil(err)
   137  	r.IsType(&ClaimFromRewardingFund{}, rc)
   138  	r.EqualValues("101", rc.Amount().String())
   139  	r.EqualValues([]byte{1, 2, 3}, rc.Data())
   140  }