github.com/iotexproject/iotex-core@v1.14.1-rc1/action/protocol/rewarding/fund_test.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 rewarding
     7  
     8  import (
     9  	"context"
    10  	"math/big"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  
    16  	"github.com/iotexproject/iotex-address/address"
    17  	"github.com/iotexproject/iotex-proto/golang/iotextypes"
    18  
    19  	"github.com/iotexproject/iotex-core/action/protocol"
    20  	accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util"
    21  )
    22  
    23  func TestProtocol_Fund(t *testing.T) {
    24  	testProtocol(t, func(t *testing.T, ctx context.Context, sm protocol.StateManager, p *Protocol) {
    25  		actionCtx, ok := protocol.GetActionCtx(ctx)
    26  		require.True(t, ok)
    27  
    28  		// Deposit 5 token
    29  		rlog, err := p.Deposit(ctx, sm, big.NewInt(5), iotextypes.TransactionLogType_DEPOSIT_TO_REWARDING_FUND)
    30  		require.NoError(t, err)
    31  		require.NotNil(t, rlog)
    32  		require.Equal(t, big.NewInt(5).String(), rlog.Amount.String())
    33  		require.Equal(t, actionCtx.Caller.String(), rlog.Sender)
    34  		require.Equal(t, address.RewardingPoolAddr, rlog.Recipient)
    35  
    36  		totalBalance, _, err := p.TotalBalance(ctx, sm)
    37  		require.NoError(t, err)
    38  		assert.Equal(t, big.NewInt(5), totalBalance)
    39  		availableBalance, _, err := p.AvailableBalance(ctx, sm)
    40  		require.NoError(t, err)
    41  		assert.Equal(t, big.NewInt(5), availableBalance)
    42  		acc, err := accountutil.LoadAccount(sm, actionCtx.Caller)
    43  		require.NoError(t, err)
    44  		assert.Equal(t, big.NewInt(995), acc.Balance)
    45  
    46  		// Deposit another 6 token will fail because
    47  		_, err = p.Deposit(ctx, sm, big.NewInt(996), iotextypes.TransactionLogType_DEPOSIT_TO_REWARDING_FUND)
    48  		require.Error(t, err)
    49  	}, false)
    50  
    51  }
    52  
    53  func TestDepositNegativeGasFee(t *testing.T) {
    54  	testProtocol(t, func(t *testing.T, ctx context.Context, sm protocol.StateManager, p *Protocol) {
    55  		_, err := DepositGas(ctx, sm, big.NewInt(-1))
    56  		require.Error(t, err)
    57  	}, false)
    58  }