code.vegaprotocol.io/vega@v0.79.0/core/banking/gov_transfers_test.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package banking_test
    17  
    18  import (
    19  	"context"
    20  	"testing"
    21  
    22  	"code.vegaprotocol.io/vega/core/assets"
    23  	"code.vegaprotocol.io/vega/core/banking"
    24  	"code.vegaprotocol.io/vega/libs/num"
    25  	"code.vegaprotocol.io/vega/protos/vega"
    26  
    27  	"github.com/golang/mock/gomock"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func TestCalculateGovernanceTransferAmount(t *testing.T) {
    32  	e := getTestEngine(t)
    33  	e.assets.EXPECT().Get(gomock.Any()).AnyTimes().Return(assets.NewAsset(&mockAsset{quantum: num.DecimalFromFloat(10)}), nil)
    34  
    35  	e.OnMaxAmountChanged(context.Background(), num.DecimalFromInt64(100000))
    36  	e.OnMaxFractionChanged(context.Background(), num.MustDecimalFromString("0.5"))
    37  
    38  	e.col.EXPECT().GetSystemAccountBalance(gomock.Any(), gomock.Any(), gomock.Any()).Return(num.NewUint(1000000), nil).AnyTimes()
    39  	balance, err := e.CalculateGovernanceTransferAmount("asset", "", vega.AccountType_ACCOUNT_TYPE_GLOBAL_REWARD, num.DecimalFromFloat(0.2), num.NewUint(10000), vega.GovernanceTransferType_GOVERNANCE_TRANSFER_TYPE_ALL_OR_NOTHING)
    40  	require.NoError(t, err)
    41  
    42  	// max amount allowed by max fraction = 500k
    43  	// max amount = 1000k
    44  	// max amount by transfer = 10k
    45  	// amount by transfer fraction = 200k
    46  	// => amount to be transferred = min(500k, 1000k, 10k, 200k) = 10k which is fine for all or nothing
    47  	require.Equal(t, num.NewUint(10000), balance)
    48  	balance, err = e.CalculateGovernanceTransferAmount("asset", "", vega.AccountType_ACCOUNT_TYPE_GLOBAL_REWARD, num.DecimalFromFloat(0.2), num.NewUint(400000), vega.GovernanceTransferType_GOVERNANCE_TRANSFER_TYPE_ALL_OR_NOTHING)
    49  	require.NoError(t, err)
    50  
    51  	// max amount allowed by max fraction = 500k
    52  	// max amount = 1000k
    53  	// max amount by transfer = 400k
    54  	// amount by transfer fraction = 200k
    55  	// => amount to be transferred = min(500k, 1000k, 400k, 200k) = 200k which is fine for all or nothing
    56  	require.Equal(t, num.NewUint(200000), balance)
    57  
    58  	e.OnMaxAmountChanged(context.Background(), num.DecimalFromInt64(10000))
    59  	balance, err = e.CalculateGovernanceTransferAmount("asset", "", vega.AccountType_ACCOUNT_TYPE_GLOBAL_REWARD, num.DecimalFromFloat(0.2), num.NewUint(400000), vega.GovernanceTransferType_GOVERNANCE_TRANSFER_TYPE_ALL_OR_NOTHING)
    60  
    61  	// max amount allowed by max fraction = 500k
    62  	// max amount = 100k
    63  	// max amount by transfer = 400k
    64  	// amount by transfer fraction = 200k
    65  	// => amount to be transferred = min(500k, 100k, 400k, 200k) = 100k which is not fine for all or nothing
    66  	require.Nil(t, balance)
    67  	require.Equal(t, "invalid transfer amount for transfer type all or nothing", err.Error())
    68  
    69  	// same settings with best effort would give 50k
    70  	balance, err = e.CalculateGovernanceTransferAmount("asset", "", vega.AccountType_ACCOUNT_TYPE_GLOBAL_REWARD, num.DecimalFromFloat(0.2), num.NewUint(400000), vega.GovernanceTransferType_GOVERNANCE_TRANSFER_TYPE_BEST_EFFORT)
    71  	require.NoError(t, err)
    72  	require.Equal(t, num.NewUint(100000), balance)
    73  
    74  	e.OnMaxAmountChanged(context.Background(), num.DecimalFromInt64(100000))
    75  	e.OnMaxFractionChanged(context.Background(), num.MustDecimalFromString("0.05"))
    76  
    77  	balance, err = e.CalculateGovernanceTransferAmount("asset", "", vega.AccountType_ACCOUNT_TYPE_GLOBAL_REWARD, num.DecimalFromFloat(0.2), num.NewUint(400000), vega.GovernanceTransferType_GOVERNANCE_TRANSFER_TYPE_ALL_OR_NOTHING)
    78  
    79  	// max amount allowed by max fraction = 50k
    80  	// max amount = 100k
    81  	// max amount by transfer = 400k
    82  	// amount by transfer fraction = 200k
    83  	// => amount to be transferred = min(50k, 100k, 400k, 200k) = 50k which is not fine for all or nothing
    84  	require.Nil(t, balance)
    85  	require.Equal(t, "invalid transfer amount for transfer type all or nothing", err.Error())
    86  
    87  	// same settings with best effort would give 50k
    88  	balance, err = e.CalculateGovernanceTransferAmount("asset", "", vega.AccountType_ACCOUNT_TYPE_GLOBAL_REWARD, num.DecimalFromFloat(0.2), num.NewUint(400000), vega.GovernanceTransferType_GOVERNANCE_TRANSFER_TYPE_BEST_EFFORT)
    89  	require.NoError(t, err)
    90  	require.Equal(t, num.NewUint(50000), balance)
    91  }
    92  
    93  func TestCalculateDecayedAmount(t *testing.T) {
    94  	// no decay
    95  	require.Equal(t, num.NewUint(1000), banking.CalculateDecayedAmount(num.NewUint(1000), 1, 10, ""))
    96  	// 0.5 decay, after one epoch
    97  	require.Equal(t, num.NewUint(500), banking.CalculateDecayedAmount(num.NewUint(1000), 1, 2, "0.5"))
    98  	// 0.5 decay, after two epochs
    99  	require.Equal(t, num.NewUint(250), banking.CalculateDecayedAmount(num.NewUint(1000), 1, 3, "0.5"))
   100  }