github.com/iotexproject/iotex-core@v1.14.1-rc1/action/depositreward_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  func TestDepositRewardSerialize(t *testing.T) {
    12  	r := require.New(t)
    13  
    14  	rp := &DepositToRewardingFund{}
    15  	data := rp.Serialize()
    16  	r.NotNil(data)
    17  
    18  	rp.amount = big.NewInt(100)
    19  	rp.data = []byte{1}
    20  	data = rp.Serialize()
    21  	r.NotNil(data)
    22  	r.EqualValues("0a03313030120101", hex.EncodeToString(data))
    23  }
    24  
    25  func TestDepositRewardIntrinsicGas(t *testing.T) {
    26  	r := require.New(t)
    27  
    28  	rp := &DepositToRewardingFund{}
    29  	gas, err := rp.IntrinsicGas()
    30  	r.NoError(err)
    31  	r.EqualValues(10000, gas)
    32  
    33  	rp.amount = big.NewInt(100000000)
    34  	gas, err = rp.IntrinsicGas()
    35  	r.NoError(err)
    36  	r.EqualValues(10000, gas)
    37  
    38  	rp.data = []byte{1}
    39  	gas, err = rp.IntrinsicGas()
    40  	r.NoError(err)
    41  	r.EqualValues(10100, gas)
    42  }
    43  
    44  func TestDepositRewardSanityCheck(t *testing.T) {
    45  	r := require.New(t)
    46  
    47  	rp := &DepositToRewardingFund{}
    48  
    49  	rp.amount = big.NewInt(1)
    50  	err := rp.SanityCheck()
    51  	r.NoError(err)
    52  
    53  	rp.amount = big.NewInt(-1)
    54  	err = rp.SanityCheck()
    55  	r.NotNil(err)
    56  	r.EqualValues(_errNegativeNumberMsg, err.Error())
    57  }
    58  
    59  func TestDepositRewardCost(t *testing.T) {
    60  	r := require.New(t)
    61  
    62  	rp := &DepositToRewardingFund{}
    63  	rp.gasPrice = _defaultGasPrice
    64  	rp.amount = big.NewInt(100)
    65  	cost, err := rp.Cost()
    66  	r.NoError(err)
    67  	r.EqualValues("10000000000000000", cost.String())
    68  
    69  	rp.data = []byte{1}
    70  	cost, err = rp.Cost()
    71  	r.NoError(err)
    72  	r.EqualValues("10100000000000000", cost.String())
    73  }
    74  
    75  func TestDepositRewardEncodeABIBinary(t *testing.T) {
    76  	r := require.New(t)
    77  
    78  	rp := &DepositToRewardingFund{}
    79  
    80  	rp.amount = big.NewInt(101)
    81  	data, err := rp.encodeABIBinary()
    82  	r.NoError(err)
    83  	r.EqualValues(
    84  		"27852a6b000000000000000000000000000000000000000000000000000000000000006500000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000",
    85  		hex.EncodeToString(data),
    86  	)
    87  
    88  	rp.data = []byte{1, 2, 3}
    89  	data, err = rp.encodeABIBinary()
    90  	r.NoError(err)
    91  	r.EqualValues(
    92  		"27852a6b000000000000000000000000000000000000000000000000000000000000006500000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
    93  		hex.EncodeToString(data),
    94  	)
    95  }
    96  
    97  func TestDepositRewardToEthTx(t *testing.T) {
    98  	r := require.New(t)
    99  
   100  	rp := &DepositToRewardingFund{}
   101  	rp.amount = big.NewInt(101)
   102  	tx, err := rp.ToEthTx(0)
   103  	r.NoError(err)
   104  	r.EqualValues(_rewardingProtocolEthAddr, *tx.To())
   105  	r.EqualValues(
   106  		"27852a6b000000000000000000000000000000000000000000000000000000000000006500000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000",
   107  		hex.EncodeToString(tx.Data()),
   108  	)
   109  	r.EqualValues("0", tx.Value().String())
   110  
   111  	rp.data = []byte{1, 2, 3}
   112  	tx, err = rp.ToEthTx(0)
   113  	r.NoError(err)
   114  	r.EqualValues(_rewardingProtocolEthAddr, *tx.To())
   115  	r.EqualValues(
   116  		"27852a6b000000000000000000000000000000000000000000000000000000000000006500000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
   117  		hex.EncodeToString(tx.Data()),
   118  	)
   119  	r.EqualValues("0", tx.Value().String())
   120  }
   121  
   122  func TestNewRewardingDepositFromABIBinary(t *testing.T) {
   123  	r := require.New(t)
   124  
   125  	data, _ := hex.DecodeString("27852a6b000000000000000000000000000000000000000000000000000000000000006500000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003")
   126  
   127  	rp, err := NewDepositToRewardingFundFromABIBinary(data)
   128  	r.NoError(err)
   129  	r.IsType(&DepositToRewardingFund{}, rp)
   130  	r.EqualValues("101", rp.Amount().String())
   131  	r.EqualValues([]byte{1, 2, 3}, rp.Data())
   132  }