mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-27 00:35:18 +00:00
ef874f9913
* feat: community pool deposit/withdraw proposals * fix: check community pool balance in tests * add new msg type definitions * add msg methods and tests * add module and keeper skeleton * add deposit and withdraw methods (no delegation) * untested depsit/withdraw with delegation methods * add cli cmds * fix cli argument parsing * add tests for delegate/undelegate msgs * emit un/delegate events * add godoc comments * tally handler with liquid staking support * clean up * update for liquid keeper changes * Exclude non-bkava denoms from aggregate underlying ukava calculation * wip Add claim * Add distr keeper and claiming * Add claim test * Update claim test with failures * wip Add staking rewards * -S Fix savings to earn incentive methods * Use a single accural time for all earn incentives * Add additional required liquid methods * Update genesis to only include 1 accrual time for earn * Revert "Update genesis to only include 1 accrual time for earn" This reverts commit cc7e35347298681c0c8a4a0b9bf9b9b296c25531. * Revert "Use a single accural time for all earn incentives" This reverts commit aeb49c4622d4e3d99dc6421c8830932b1b546be9. * Update tests with incentive distribution * Add earn to incentive rewards query * add earn cli tx * Update claim example to use ukava large * add proposal to gov router * fix example tx formating * add proposal handlers to gov app module * fix: define gov router after earn keeper * fix: correct proposal type * remove outdated comment * refactor withdraw so that fee pool is allows adjusted by the actual withdraw amount * fix: lint proto file * use non blocked module account instead of dist acc * add fund mod account to app, enable receiving * update to new withdraw interface * add human readable apy test cases * remove duplicate changes from previous merge * remove deprecated io/ioutil package * standardize proposal type as a pointer (also matches sdk) * minior comments and formatting * use withdraw amount in router msgs Co-authored-by: rhuairahrighairigh <ruaridh.odonnell@gmail.com> Co-authored-by: Draco <draco@dracoli.com> Co-authored-by: drklee3 <derrick@dlee.dev> Co-authored-by: Ruaridh <rhuairahrighairidh@users.noreply.github.com>
323 lines
9.5 KiB
Go
323 lines
9.5 KiB
Go
package keeper_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/kava-labs/kava/x/earn/testutil"
|
|
"github.com/kava-labs/kava/x/earn/types"
|
|
"github.com/kava-labs/kava/x/earn/types/mocks"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/stretchr/testify/mock"
|
|
"github.com/stretchr/testify/suite"
|
|
)
|
|
|
|
type hookTestSuite struct {
|
|
testutil.Suite
|
|
}
|
|
|
|
func (suite *hookTestSuite) SetupTest() {
|
|
suite.Suite.SetupTest()
|
|
suite.Keeper.SetParams(suite.Ctx, types.DefaultParams())
|
|
}
|
|
|
|
func TestHookTestSuite(t *testing.T) {
|
|
suite.Run(t, new(hookTestSuite))
|
|
}
|
|
|
|
func (suite *hookTestSuite) TestHooks_DepositAndWithdraw() {
|
|
suite.Keeper.ClearHooks()
|
|
earnHooks := &mocks.EarnHooks{}
|
|
suite.Keeper.SetHooks(earnHooks)
|
|
|
|
vault1Denom := "usdx"
|
|
vault2Denom := "ukava"
|
|
deposit1Amount := sdk.NewInt64Coin(vault1Denom, 100)
|
|
deposit2Amount := sdk.NewInt64Coin(vault2Denom, 100)
|
|
|
|
suite.CreateVault(vault1Denom, types.StrategyTypes{types.STRATEGY_TYPE_HARD}, false, nil)
|
|
suite.CreateVault(vault2Denom, types.StrategyTypes{types.STRATEGY_TYPE_SAVINGS}, false, nil)
|
|
|
|
acc := suite.CreateAccount(sdk.NewCoins(
|
|
sdk.NewInt64Coin(vault1Denom, 1000),
|
|
sdk.NewInt64Coin(vault2Denom, 1000),
|
|
), 0)
|
|
|
|
// first deposit creates vault - calls AfterVaultDepositCreated with initial shares
|
|
// shares are 1:1
|
|
earnHooks.On(
|
|
"AfterVaultDepositCreated",
|
|
suite.Ctx,
|
|
deposit1Amount.Denom,
|
|
acc.GetAddress(),
|
|
deposit1Amount.Amount.ToDec(),
|
|
).Once()
|
|
err := suite.Keeper.Deposit(
|
|
suite.Ctx,
|
|
acc.GetAddress(),
|
|
deposit1Amount,
|
|
types.STRATEGY_TYPE_HARD,
|
|
)
|
|
suite.Require().NoError(err)
|
|
|
|
// second deposit adds to vault - calls BeforeVaultDepositModified
|
|
// shares given are the initial shares, not new the shares added to the vault
|
|
earnHooks.On(
|
|
"BeforeVaultDepositModified",
|
|
suite.Ctx,
|
|
deposit1Amount.Denom,
|
|
acc.GetAddress(),
|
|
deposit1Amount.Amount.ToDec(),
|
|
).Once()
|
|
err = suite.Keeper.Deposit(
|
|
suite.Ctx,
|
|
acc.GetAddress(),
|
|
deposit1Amount,
|
|
types.STRATEGY_TYPE_HARD,
|
|
)
|
|
suite.Require().NoError(err)
|
|
|
|
// get the shares from the store from the last deposit
|
|
shareRecord, found := suite.Keeper.GetVaultAccountShares(
|
|
suite.Ctx,
|
|
acc.GetAddress(),
|
|
)
|
|
suite.Require().True(found)
|
|
|
|
// third deposit adds to vault - calls BeforeVaultDepositModified
|
|
// shares given are the shares added in previous deposit, not the shares added to the vault now
|
|
earnHooks.On(
|
|
"BeforeVaultDepositModified",
|
|
suite.Ctx,
|
|
deposit1Amount.Denom,
|
|
acc.GetAddress(),
|
|
shareRecord.AmountOf(deposit1Amount.Denom),
|
|
).Once()
|
|
err = suite.Keeper.Deposit(
|
|
suite.Ctx,
|
|
acc.GetAddress(),
|
|
deposit1Amount,
|
|
types.STRATEGY_TYPE_HARD,
|
|
)
|
|
suite.Require().NoError(err)
|
|
|
|
// new deposit denom into vault creates the deposit and calls AfterVaultDepositCreated
|
|
earnHooks.On(
|
|
"AfterVaultDepositCreated",
|
|
suite.Ctx,
|
|
deposit2Amount.Denom,
|
|
acc.GetAddress(),
|
|
deposit2Amount.Amount.ToDec(),
|
|
).Once()
|
|
err = suite.Keeper.Deposit(
|
|
suite.Ctx,
|
|
acc.GetAddress(),
|
|
deposit2Amount,
|
|
types.STRATEGY_TYPE_SAVINGS,
|
|
)
|
|
suite.Require().NoError(err)
|
|
|
|
// second deposit into vault calls BeforeVaultDepositModified with initial shares given
|
|
earnHooks.On(
|
|
"BeforeVaultDepositModified",
|
|
suite.Ctx,
|
|
deposit2Amount.Denom,
|
|
acc.GetAddress(),
|
|
deposit2Amount.Amount.ToDec(),
|
|
).Once()
|
|
err = suite.Keeper.Deposit(
|
|
suite.Ctx,
|
|
acc.GetAddress(),
|
|
deposit2Amount,
|
|
types.STRATEGY_TYPE_SAVINGS,
|
|
)
|
|
suite.Require().NoError(err)
|
|
|
|
// get the shares from the store from the last deposit
|
|
shareRecord, found = suite.Keeper.GetVaultAccountShares(
|
|
suite.Ctx,
|
|
acc.GetAddress(),
|
|
)
|
|
suite.Require().True(found)
|
|
|
|
// third deposit into vault calls BeforeVaultDepositModified with shares from last deposit
|
|
earnHooks.On(
|
|
"BeforeVaultDepositModified",
|
|
suite.Ctx,
|
|
deposit2Amount.Denom,
|
|
acc.GetAddress(),
|
|
shareRecord.AmountOf(deposit2Amount.Denom),
|
|
).Once()
|
|
err = suite.Keeper.Deposit(
|
|
suite.Ctx,
|
|
acc.GetAddress(),
|
|
deposit2Amount,
|
|
types.STRATEGY_TYPE_SAVINGS,
|
|
)
|
|
suite.Require().NoError(err)
|
|
|
|
// ------------------------------------------------------------
|
|
// test hooks with a full withdraw of all shares deposit 1 denom
|
|
shareRecord, found = suite.Keeper.GetVaultAccountShares(
|
|
suite.Ctx,
|
|
acc.GetAddress(),
|
|
)
|
|
suite.Require().True(found)
|
|
|
|
// all shares given to BeforeVaultDepositModified
|
|
earnHooks.On(
|
|
"BeforeVaultDepositModified",
|
|
suite.Ctx,
|
|
deposit1Amount.Denom,
|
|
acc.GetAddress(),
|
|
shareRecord.AmountOf(deposit1Amount.Denom),
|
|
).Once()
|
|
_, err = suite.Keeper.Withdraw(
|
|
suite.Ctx,
|
|
acc.GetAddress(),
|
|
// 3 deposits, multiply original deposit amount by 3
|
|
sdk.NewCoin(deposit1Amount.Denom, deposit1Amount.Amount.MulRaw(3)),
|
|
types.STRATEGY_TYPE_HARD,
|
|
)
|
|
suite.Require().NoError(err)
|
|
|
|
// test hooks on partial withdraw
|
|
shareRecord, found = suite.Keeper.GetVaultAccountShares(
|
|
suite.Ctx,
|
|
acc.GetAddress(),
|
|
)
|
|
suite.Require().True(found)
|
|
|
|
// all shares given to before deposit modified even with partial withdraw
|
|
earnHooks.On(
|
|
"BeforeVaultDepositModified",
|
|
suite.Ctx,
|
|
deposit2Amount.Denom,
|
|
acc.GetAddress(),
|
|
shareRecord.AmountOf(deposit2Amount.Denom),
|
|
).Once()
|
|
_, err = suite.Keeper.Withdraw(
|
|
suite.Ctx,
|
|
acc.GetAddress(),
|
|
deposit2Amount,
|
|
types.STRATEGY_TYPE_SAVINGS,
|
|
)
|
|
suite.Require().NoError(err)
|
|
|
|
// test hooks on second partial withdraw
|
|
shareRecord, found = suite.Keeper.GetVaultAccountShares(
|
|
suite.Ctx,
|
|
acc.GetAddress(),
|
|
)
|
|
suite.Require().True(found)
|
|
|
|
// all shares given to before deposit modified even with partial withdraw
|
|
earnHooks.On(
|
|
"BeforeVaultDepositModified",
|
|
suite.Ctx,
|
|
deposit2Amount.Denom,
|
|
acc.GetAddress(),
|
|
shareRecord.AmountOf(deposit2Amount.Denom),
|
|
).Once()
|
|
_, err = suite.Keeper.Withdraw(
|
|
suite.Ctx,
|
|
acc.GetAddress(),
|
|
deposit2Amount,
|
|
types.STRATEGY_TYPE_SAVINGS,
|
|
)
|
|
suite.Require().NoError(err)
|
|
|
|
// test hooks withdraw all remaining shares
|
|
shareRecord, found = suite.Keeper.GetVaultAccountShares(
|
|
suite.Ctx,
|
|
acc.GetAddress(),
|
|
)
|
|
suite.Require().True(found)
|
|
|
|
// all shares given to before deposit modified even with partial withdraw
|
|
earnHooks.On(
|
|
"BeforeVaultDepositModified",
|
|
suite.Ctx,
|
|
deposit2Amount.Denom,
|
|
acc.GetAddress(),
|
|
shareRecord.AmountOf(deposit2Amount.Denom),
|
|
).Once()
|
|
_, err = suite.Keeper.Withdraw(
|
|
suite.Ctx,
|
|
acc.GetAddress(),
|
|
deposit2Amount,
|
|
types.STRATEGY_TYPE_SAVINGS,
|
|
)
|
|
suite.Require().NoError(err)
|
|
|
|
earnHooks.AssertExpectations(suite.T())
|
|
}
|
|
|
|
func (suite *hookTestSuite) TestHooks_NoPanicsOnNilHooks() {
|
|
suite.Keeper.ClearHooks()
|
|
|
|
vaultDenom := "usdx"
|
|
startBalance := sdk.NewInt64Coin(vaultDenom, 1000)
|
|
depositAmount := sdk.NewInt64Coin(vaultDenom, 100)
|
|
withdrawAmount := sdk.NewInt64Coin(vaultDenom, 100)
|
|
|
|
suite.CreateVault(vaultDenom, types.StrategyTypes{types.STRATEGY_TYPE_HARD}, false, nil)
|
|
|
|
acc := suite.CreateAccount(sdk.NewCoins(startBalance), 0)
|
|
|
|
// AfterVaultDepositModified should not panic if no hooks are registered
|
|
err := suite.Keeper.Deposit(suite.Ctx, acc.GetAddress(), depositAmount, types.STRATEGY_TYPE_HARD)
|
|
suite.Require().NoError(err)
|
|
|
|
// BeforeVaultDepositModified should not panic if no hooks are registered
|
|
err = suite.Keeper.Deposit(suite.Ctx, acc.GetAddress(), depositAmount, types.STRATEGY_TYPE_HARD)
|
|
suite.Require().NoError(err)
|
|
|
|
// BeforeVaultDepositModified should not panic if no hooks are registered
|
|
_, err = suite.Keeper.Withdraw(suite.Ctx, acc.GetAddress(), withdrawAmount, types.STRATEGY_TYPE_HARD)
|
|
suite.Require().NoError(err)
|
|
}
|
|
|
|
func (suite *hookTestSuite) TestHooks_HookOrdering() {
|
|
suite.Keeper.ClearHooks()
|
|
earnHooks := &mocks.EarnHooks{}
|
|
suite.Keeper.SetHooks(earnHooks)
|
|
|
|
vaultDenom := "usdx"
|
|
startBalance := sdk.NewInt64Coin(vaultDenom, 1000)
|
|
depositAmount := sdk.NewInt64Coin(vaultDenom, 100)
|
|
|
|
suite.CreateVault(vaultDenom, types.StrategyTypes{types.STRATEGY_TYPE_HARD}, false, nil)
|
|
|
|
acc := suite.CreateAccount(sdk.NewCoins(startBalance), 0)
|
|
|
|
earnHooks.On("AfterVaultDepositCreated", suite.Ctx, depositAmount.Denom, acc.GetAddress(), depositAmount.Amount.ToDec()).
|
|
Run(func(args mock.Arguments) {
|
|
shares, found := suite.Keeper.GetVaultAccountShares(suite.Ctx, acc.GetAddress())
|
|
suite.Require().True(found, "expected after hook to be called after shares are updated")
|
|
suite.Require().Equal(depositAmount.Amount.ToDec(), shares.AmountOf(depositAmount.Denom))
|
|
})
|
|
err := suite.Keeper.Deposit(suite.Ctx, acc.GetAddress(), depositAmount, types.STRATEGY_TYPE_HARD)
|
|
suite.Require().NoError(err)
|
|
|
|
earnHooks.On("BeforeVaultDepositModified", suite.Ctx, depositAmount.Denom, acc.GetAddress(), depositAmount.Amount.ToDec()).
|
|
Run(func(args mock.Arguments) {
|
|
shares, found := suite.Keeper.GetVaultAccountShares(suite.Ctx, acc.GetAddress())
|
|
suite.Require().True(found, "expected after hook to be called after shares are updated")
|
|
suite.Require().Equal(depositAmount.Amount.ToDec(), shares.AmountOf(depositAmount.Denom))
|
|
})
|
|
err = suite.Keeper.Deposit(suite.Ctx, acc.GetAddress(), depositAmount, types.STRATEGY_TYPE_HARD)
|
|
suite.Require().NoError(err)
|
|
|
|
existingShares, found := suite.Keeper.GetVaultAccountShares(suite.Ctx, acc.GetAddress())
|
|
suite.Require().True(found)
|
|
earnHooks.On("BeforeVaultDepositModified", suite.Ctx, depositAmount.Denom, acc.GetAddress(), existingShares.AmountOf(depositAmount.Denom)).
|
|
Run(func(args mock.Arguments) {
|
|
shares, found := suite.Keeper.GetVaultAccountShares(suite.Ctx, acc.GetAddress())
|
|
suite.Require().True(found, "expected after hook to be called after shares are updated")
|
|
suite.Require().Equal(depositAmount.Amount.MulRaw(2).ToDec(), shares.AmountOf(depositAmount.Denom))
|
|
})
|
|
_, err = suite.Keeper.Withdraw(suite.Ctx, acc.GetAddress(), depositAmount, types.STRATEGY_TYPE_HARD)
|
|
suite.Require().NoError(err)
|
|
}
|