mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-10 18:15:19 +00:00
153 lines
5.3 KiB
Go
153 lines
5.3 KiB
Go
|
package keeper_test
|
||
|
|
||
|
import (
|
||
|
"strings"
|
||
|
|
||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||
|
"github.com/tendermint/tendermint/crypto"
|
||
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
||
|
tmtime "github.com/tendermint/tendermint/types/time"
|
||
|
|
||
|
"github.com/kava-labs/kava/app"
|
||
|
"github.com/kava-labs/kava/x/savings/types"
|
||
|
)
|
||
|
|
||
|
func (suite *KeeperTestSuite) TestDeposit() {
|
||
|
type args struct {
|
||
|
allowedDenoms []string
|
||
|
depositor sdk.AccAddress
|
||
|
initialDepositorBalance sdk.Coins
|
||
|
depositAmount sdk.Coins
|
||
|
numberDeposits int
|
||
|
expectedAccountBalance sdk.Coins
|
||
|
expectedModAccountBalance sdk.Coins
|
||
|
expectedDepositCoins sdk.Coins
|
||
|
}
|
||
|
type errArgs struct {
|
||
|
expectPass bool
|
||
|
contains string
|
||
|
}
|
||
|
type depositTest struct {
|
||
|
name string
|
||
|
args args
|
||
|
errArgs errArgs
|
||
|
}
|
||
|
testCases := []depositTest{
|
||
|
{
|
||
|
"valid",
|
||
|
args{
|
||
|
allowedDenoms: []string{"bnb", "btcb", "ukava"},
|
||
|
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||
|
initialDepositorBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(1000)), sdk.NewCoin("btcb", sdk.NewInt(1000))),
|
||
|
depositAmount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))),
|
||
|
numberDeposits: 1,
|
||
|
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(900)), sdk.NewCoin("btcb", sdk.NewInt(1000))),
|
||
|
expectedModAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))),
|
||
|
expectedDepositCoins: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))),
|
||
|
},
|
||
|
errArgs{
|
||
|
expectPass: true,
|
||
|
contains: "",
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
"valid multi deposit",
|
||
|
args{
|
||
|
allowedDenoms: []string{"bnb", "btcb", "ukava"},
|
||
|
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||
|
initialDepositorBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(1000)), sdk.NewCoin("btcb", sdk.NewInt(1000))),
|
||
|
depositAmount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))),
|
||
|
numberDeposits: 2,
|
||
|
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(800)), sdk.NewCoin("btcb", sdk.NewInt(1000))),
|
||
|
expectedModAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(200))),
|
||
|
expectedDepositCoins: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(200))),
|
||
|
},
|
||
|
errArgs{
|
||
|
expectPass: true,
|
||
|
contains: "",
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
"invalid deposit denom",
|
||
|
args{
|
||
|
allowedDenoms: []string{"bnb", "btcb", "ukava"},
|
||
|
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||
|
initialDepositorBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(1000)), sdk.NewCoin("btcb", sdk.NewInt(1000))),
|
||
|
depositAmount: sdk.NewCoins(sdk.NewCoin("fake", sdk.NewInt(100))),
|
||
|
numberDeposits: 1,
|
||
|
expectedAccountBalance: sdk.Coins{},
|
||
|
expectedModAccountBalance: sdk.Coins{},
|
||
|
expectedDepositCoins: sdk.Coins{},
|
||
|
},
|
||
|
errArgs{
|
||
|
expectPass: false,
|
||
|
contains: "invalid deposit denom",
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
"insufficient funds",
|
||
|
args{
|
||
|
allowedDenoms: []string{"bnb", "btcb", "ukava"},
|
||
|
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||
|
initialDepositorBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(1000)), sdk.NewCoin("btcb", sdk.NewInt(1000))),
|
||
|
depositAmount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(10000))),
|
||
|
numberDeposits: 1,
|
||
|
expectedAccountBalance: sdk.Coins{},
|
||
|
expectedModAccountBalance: sdk.Coins{},
|
||
|
expectedDepositCoins: sdk.Coins{},
|
||
|
},
|
||
|
errArgs{
|
||
|
expectPass: false,
|
||
|
contains: "insufficient funds",
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
for _, tc := range testCases {
|
||
|
suite.Run(tc.name, func() {
|
||
|
// create new app with one funded account
|
||
|
|
||
|
// 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))
|
||
|
|
||
|
tApp.InitializeFromGenesisStates(authGS,
|
||
|
app.GenesisState{types.ModuleName: tApp.AppCodec().MustMarshalJSON(&savingsGS)},
|
||
|
)
|
||
|
keeper := tApp.GetSavingsKeeper()
|
||
|
suite.app = tApp
|
||
|
suite.ctx = ctx
|
||
|
suite.keeper = keeper
|
||
|
|
||
|
// run the test
|
||
|
var err error
|
||
|
for i := 0; i < tc.args.numberDeposits; i++ {
|
||
|
err = suite.keeper.Deposit(suite.ctx, tc.args.depositor, tc.args.depositAmount)
|
||
|
}
|
||
|
|
||
|
// verify results
|
||
|
if tc.errArgs.expectPass {
|
||
|
suite.Require().NoError(err)
|
||
|
acc := suite.getAccount(tc.args.depositor)
|
||
|
suite.Require().Equal(tc.args.expectedAccountBalance, suite.getAccountCoins(acc))
|
||
|
mAcc := suite.getModuleAccount(types.ModuleAccountName)
|
||
|
suite.Require().Equal(tc.args.expectedModAccountBalance, suite.getAccountCoins(mAcc))
|
||
|
dep, f := suite.keeper.GetDeposit(suite.ctx, tc.args.depositor)
|
||
|
suite.Require().True(f)
|
||
|
suite.Require().Equal(tc.args.expectedDepositCoins, dep.Amount)
|
||
|
} else {
|
||
|
suite.Require().Error(err)
|
||
|
suite.Require().True(strings.Contains(err.Error(), tc.errArgs.contains))
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func c(denom string, amount int64) sdk.Coin { return sdk.NewInt64Coin(denom, amount) }
|
||
|
func cs(coins ...sdk.Coin) sdk.Coins { return sdk.NewCoins(coins...) }
|