0g-chain/x/savings/keeper/withdraw_test.go

203 lines
7.8 KiB
Go
Raw Normal View History

package keeper_test
import (
"fmt"
"strings"
sdkmath "cosmossdk.io/math"
Update cosmos-sdk to v0.47.7 (#1811) * Update cometbft, cosmos, ethermint, and ibc-go * Replace github.com/tendermint/tendermint by github.com/cometbft/cometbft * Replace github.com/tendermint/tm-db by github.com/cometbft/cometbft-db * Replace gogo/protobuf with cosmos/gogoproto & simapp replacement * Replace cosmos-sdk/simapp/helpers with cosmos-sdk/testutil/sims * Remove no longer used simulations * Replace ibchost with ibcexported See https://github.com/cosmos/ibc-go/blob/v7.2.2/docs/migrations/v6-to-v7.md#ibc-module-constants * Add new consensus params keeper * Add consensus keeper to blockers * Fix keeper and module issues in app.go * Add IsSendEnabledCoins and update SetParams interface changes * Fix protobuf build for cosmos 47 (#1800) * fix cp errors by using -f; fix lint by only linting our proto dir; and use proofs.proto directly from ics23 for ibc-go v7 * run proto-all; commit updated third party deps and swagger changes * regenerate proto files * use correct gocosmos build plugin for buf * re-gen all protobuf files to update paths for new gocosmos plugin * update protoc and buf to latest versions * fix staking keeper issues in app.go * update tally handler for gov changes * chain id fix and flag fixes * update deps for cometbft 47.7 upgrade * remove all module legacy queriers * update stakingKeeper to pointer * Replace ModuleCdc from govv1beta1 to govcodec * remove simulations * abci.LastCommitInfo → abci.CommitInfo * Remove unused code in keys.go * simapp.MakeTestEncodingConfig -> moduletestutil.MakeTestEncodingConfi * Fix chain id issues in tests * Fix remaining unit test issues * Update changelog for upgrade * Fix e2e tests using updated kvtool * Update protonet to v47 compatible genesis * Bump cometbft-db to v0.9.1-kava.1 * Update kvtool * Remove extra changelog * Fix merged rocksdb issues * go mod cleanup * Bump cometbft-db to v9 and go to 1.21 * Bump rocksdb version to v8.10.0 * Update kvtool to latest version * Update gin to v1.9.0 * Use ibctm.ModuleName in app_test * Fallback to genesis chain id instead of client toml * Remove all simulations * Fix cdp migrations issue with v47 * Update dependencies to correct tags --------- Co-authored-by: Nick DeLuca <nickdeluca08@gmail.com>
2024-02-06 22:54:10 +00:00
"github.com/cometbft/cometbft/crypto"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
tmtime "github.com/cometbft/cometbft/types/time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/kava-labs/kava/app"
"github.com/kava-labs/kava/x/savings/types"
)
func (suite *KeeperTestSuite) TestWithdraw() {
_, addrs := app.GeneratePrivKeyAddressPairs(5)
valAccAddr, delegator := addrs[0], addrs[1]
valAddr := sdk.ValAddress(valAccAddr)
initialBalance := sdkmath.NewInt(1e9)
bkavaDenom := fmt.Sprintf("bkava-%s", valAddr.String())
type args struct {
allowedDenoms []string
depositor sdk.AccAddress
initialDepositorBalance sdk.Coins
depositAmount sdk.Coins
withdrawAmount sdk.Coins
expectedAccountBalance sdk.Coins
expectedModAccountBalance sdk.Coins
expectedDepositCoins sdk.Coins
}
type errArgs struct {
expectPass bool
expectDelete bool
contains string
}
type withdrawTest struct {
name string
args args
errArgs errArgs
}
testCases := []withdrawTest{
{
"valid: partial withdraw",
args{
allowedDenoms: []string{"bnb", "btcb", "ukava"},
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
initialDepositorBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdkmath.NewInt(1000)), sdk.NewCoin("btcb", sdkmath.NewInt(1000))),
depositAmount: sdk.NewCoins(sdk.NewCoin("bnb", sdkmath.NewInt(200))),
withdrawAmount: sdk.NewCoins(sdk.NewCoin("bnb", sdkmath.NewInt(100))),
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdkmath.NewInt(900)), sdk.NewCoin("btcb", sdkmath.NewInt(1000))),
expectedModAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdkmath.NewInt(100))),
expectedDepositCoins: sdk.NewCoins(sdk.NewCoin("bnb", sdkmath.NewInt(100))),
},
errArgs{
expectPass: true,
expectDelete: false,
contains: "",
},
},
{
"valid: partial bkava",
args{
allowedDenoms: []string{"bnb", "btcb", "ukava", "bkava"},
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
initialDepositorBalance: sdk.NewCoins(sdk.NewCoin(bkavaDenom, sdkmath.NewInt(1000)), sdk.NewCoin("btcb", sdkmath.NewInt(1000))),
depositAmount: sdk.NewCoins(sdk.NewCoin(bkavaDenom, sdkmath.NewInt(200))),
withdrawAmount: sdk.NewCoins(sdk.NewCoin(bkavaDenom, sdkmath.NewInt(100))),
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin(bkavaDenom, sdkmath.NewInt(900)), sdk.NewCoin("btcb", sdkmath.NewInt(1000))),
expectedModAccountBalance: sdk.NewCoins(sdk.NewCoin(bkavaDenom, sdkmath.NewInt(100))),
expectedDepositCoins: sdk.NewCoins(sdk.NewCoin(bkavaDenom, sdkmath.NewInt(100))),
},
errArgs{
expectPass: true,
expectDelete: false,
contains: "",
},
},
{
"valid: full withdraw",
args{
allowedDenoms: []string{"bnb", "btcb", "ukava"},
initialDepositorBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdkmath.NewInt(1000)), sdk.NewCoin("btcb", sdkmath.NewInt(1000))),
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
depositAmount: sdk.NewCoins(sdk.NewCoin("bnb", sdkmath.NewInt(200))),
withdrawAmount: sdk.NewCoins(sdk.NewCoin("bnb", sdkmath.NewInt(200))),
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdkmath.NewInt(1000)), sdk.NewCoin("btcb", sdkmath.NewInt(1000))),
expectedModAccountBalance: sdk.Coins{},
expectedDepositCoins: sdk.Coins{},
},
errArgs{
expectPass: true,
expectDelete: true,
contains: "",
},
},
{
"valid: withdraw exceeds deposit but is adjusted to match max deposit",
args{
allowedDenoms: []string{"bnb", "btcb", "ukava"},
initialDepositorBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdkmath.NewInt(1000)), sdk.NewCoin("btcb", sdkmath.NewInt(1000))),
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
depositAmount: sdk.NewCoins(sdk.NewCoin("bnb", sdkmath.NewInt(200))),
withdrawAmount: sdk.NewCoins(sdk.NewCoin("bnb", sdkmath.NewInt(300))),
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdkmath.NewInt(1000)), sdk.NewCoin("btcb", sdkmath.NewInt(1000))),
expectedModAccountBalance: sdk.Coins{},
expectedDepositCoins: sdk.Coins{},
},
errArgs{
expectPass: true,
expectDelete: true,
contains: "",
},
},
{
"invalid: withdraw non-supplied coin type",
args{
allowedDenoms: []string{"bnb", "btcb", "ukava"},
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
initialDepositorBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdkmath.NewInt(1000)), sdk.NewCoin("btcb", sdkmath.NewInt(1000))),
depositAmount: sdk.NewCoins(sdk.NewCoin("bnb", sdkmath.NewInt(200))),
withdrawAmount: sdk.NewCoins(sdk.NewCoin("btcb", sdkmath.NewInt(200))),
expectedAccountBalance: sdk.Coins{},
expectedModAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdkmath.NewInt(200))),
expectedDepositCoins: sdk.Coins{},
},
errArgs{
expectPass: false,
expectDelete: false,
contains: "invalid withdraw denom",
},
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
// Initialize test app and set context
tApp := app.NewTestApp()
ctx := tApp.NewContext(true, tmproto.Header{Height: 1, Time: tmtime.Now()})
authGS := app.NewFundedGenStateWithCoins(
tApp.AppCodec(),
[]sdk.Coins{tc.args.initialDepositorBalance},
[]sdk.AccAddress{tc.args.depositor},
)
savingsGS := types.NewGenesisState(
types.NewParams(tc.args.allowedDenoms),
types.Deposits{},
)
stakingParams := stakingtypes.DefaultParams()
stakingParams.BondDenom = "ukava"
tApp.InitializeFromGenesisStates(authGS,
app.GenesisState{types.ModuleName: tApp.AppCodec().MustMarshalJSON(&savingsGS)},
app.GenesisState{stakingtypes.ModuleName: tApp.AppCodec().MustMarshalJSON(stakingtypes.NewGenesisState(stakingParams, nil, nil))},
)
keeper := tApp.GetSavingsKeeper()
suite.app = tApp
suite.ctx = ctx
suite.keeper = keeper
bankKeeper := tApp.GetBankKeeper()
// Create validator and delegate for bkava
suite.CreateAccountWithAddress(valAccAddr, cs(c("ukava", 100e10)))
suite.CreateAccountWithAddress(delegator, cs(c("ukava", 100e10)))
suite.CreateNewUnbondedValidator(valAddr, initialBalance)
suite.CreateDelegation(valAddr, delegator, initialBalance)
staking.EndBlocker(suite.ctx, suite.app.GetStakingKeeper())
err := suite.keeper.Deposit(suite.ctx, tc.args.depositor, tc.args.depositAmount)
suite.Require().NoError(err)
err = suite.keeper.Withdraw(suite.ctx, tc.args.depositor, tc.args.withdrawAmount)
if tc.errArgs.expectPass {
suite.Require().NoError(err)
// Check depositor's account balance
acc := suite.getAccount(tc.args.depositor)
suite.Require().Equal(tc.args.expectedAccountBalance, bankKeeper.GetAllBalances(ctx, acc.GetAddress()))
// Check savings module account balance
mAcc := suite.getModuleAccount(types.ModuleAccountName)
suite.Require().True(tc.args.expectedModAccountBalance.IsEqual(bankKeeper.GetAllBalances(ctx, mAcc.GetAddress())))
// Check deposit
testDeposit, f := suite.keeper.GetDeposit(suite.ctx, tc.args.depositor)
if tc.errArgs.expectDelete {
suite.Require().False(f)
} else {
suite.Require().True(f)
suite.Require().Equal(tc.args.expectedDepositCoins, testDeposit.Amount)
}
} else {
suite.Require().Error(err)
suite.Require().True(strings.Contains(err.Error(), tc.errArgs.contains))
}
})
}
}