cosmos-sdk/x/bank/keeper/keeper_test.go

930 lines
36 KiB
Go
Raw Normal View History

2019-08-19 06:29:17 -07:00
package keeper_test
2018-04-09 15:10:58 -07:00
import (
"testing"
"time"
2018-04-09 15:10:58 -07:00
2020-01-30 13:31:16 -08:00
"github.com/stretchr/testify/suite"
2019-01-02 13:19:48 -08:00
abci "github.com/tendermint/tendermint/abci/types"
tmkv "github.com/tendermint/tendermint/libs/kv"
tmtime "github.com/tendermint/tendermint/types/time"
2019-01-02 13:19:48 -08:00
2019-08-19 06:29:17 -07:00
"github.com/cosmos/cosmos-sdk/simapp"
2018-04-09 15:10:58 -07:00
sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
"github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/cosmos/cosmos-sdk/x/bank/types"
2018-04-09 15:10:58 -07:00
)
2020-01-30 13:31:16 -08:00
const (
fooDenom = "foo"
barDenom = "bar"
initialPower = int64(100)
holder = "holder"
multiPerm = "multiple permissions account"
randomPerm = "random permission"
)
var (
holderAcc = authtypes.NewEmptyModuleAccount(holder)
burnerAcc = authtypes.NewEmptyModuleAccount(authtypes.Burner, authtypes.Burner)
minterAcc = authtypes.NewEmptyModuleAccount(authtypes.Minter, authtypes.Minter)
multiPermAcc = authtypes.NewEmptyModuleAccount(multiPerm, authtypes.Burner, authtypes.Minter, authtypes.Staking)
randomPermAcc = authtypes.NewEmptyModuleAccount(randomPerm, "random")
initTokens = sdk.TokensFromConsensusPower(initialPower)
initCoins = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))
2020-01-30 13:31:16 -08:00
)
2018-04-09 15:10:58 -07:00
2020-01-30 13:31:16 -08:00
func newFooCoin(amt int64) sdk.Coin {
return sdk.NewInt64Coin(fooDenom, amt)
}
2018-04-09 15:10:58 -07:00
2020-01-30 13:31:16 -08:00
func newBarCoin(amt int64) sdk.Coin {
return sdk.NewInt64Coin(barDenom, amt)
}
2018-04-09 15:10:58 -07:00
// nolint: interfacer
func getCoinsByName(ctx sdk.Context, bk keeper.Keeper, ak types.AccountKeeper, moduleName string) sdk.Coins {
moduleAddress := ak.GetModuleAddress(moduleName)
macc := ak.GetAccount(ctx, moduleAddress)
if macc == nil {
return sdk.Coins(nil)
}
return bk.GetAllBalances(ctx, macc.GetAddress())
}
2020-01-30 13:31:16 -08:00
type IntegrationTestSuite struct {
suite.Suite
2018-04-09 15:10:58 -07:00
2020-01-30 13:31:16 -08:00
app *simapp.SimApp
ctx sdk.Context
}
2018-04-09 15:10:58 -07:00
2020-01-30 13:31:16 -08:00
func (suite *IntegrationTestSuite) SetupTest() {
app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, abci.Header{})
2018-04-09 15:10:58 -07:00
app.AccountKeeper.SetParams(ctx, authtypes.DefaultParams())
2020-01-30 13:31:16 -08:00
app.BankKeeper.SetSendEnabled(ctx, true)
2018-04-09 15:10:58 -07:00
2020-01-30 13:31:16 -08:00
suite.app = app
suite.ctx = ctx
}
2018-04-09 15:10:58 -07:00
func (suite *IntegrationTestSuite) TestSupply() {
app, ctx := suite.app, suite.ctx
initialPower := int64(100)
initTokens := sdk.TokensFromConsensusPower(initialPower)
totalSupply := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))
app.BankKeeper.SetSupply(ctx, types.NewSupply(totalSupply))
total := app.BankKeeper.GetSupply(ctx).GetTotal()
suite.Require().Equal(totalSupply, total)
}
func (suite *IntegrationTestSuite) TestSupply_SendCoins() {
app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, abci.Header{Height: 1})
appCodec := app.AppCodec()
// add module accounts to supply keeper
maccPerms := simapp.GetMaccPerms()
maccPerms[holder] = nil
maccPerms[authtypes.Burner] = []string{authtypes.Burner}
maccPerms[authtypes.Minter] = []string{authtypes.Minter}
maccPerms[multiPerm] = []string{authtypes.Burner, authtypes.Minter, authtypes.Staking}
maccPerms[randomPerm] = []string{"random"}
authKeeper := authkeeper.NewAccountKeeper(
appCodec, app.GetKey(types.StoreKey), app.GetSubspace(types.ModuleName),
authtypes.ProtoBaseAccount, maccPerms,
)
keeper := keeper.NewBaseKeeper(
appCodec, app.GetKey(types.StoreKey), authKeeper,
app.GetSubspace(types.ModuleName), make(map[string]bool),
)
baseAcc := authKeeper.NewAccountWithAddress(ctx, authtypes.NewModuleAddress("baseAcc"))
suite.Require().NoError(keeper.SetBalances(ctx, holderAcc.GetAddress(), initCoins))
keeper.SetSupply(ctx, types.NewSupply(initCoins))
authKeeper.SetModuleAccount(ctx, holderAcc)
authKeeper.SetModuleAccount(ctx, burnerAcc)
authKeeper.SetAccount(ctx, baseAcc)
suite.Require().Panics(func() {
keeper.SendCoinsFromModuleToModule(ctx, "", holderAcc.GetName(), initCoins) // nolint:errcheck
})
suite.Require().Panics(func() {
keeper.SendCoinsFromModuleToModule(ctx, authtypes.Burner, "", initCoins) // nolint:errcheck
})
suite.Require().Panics(func() {
keeper.SendCoinsFromModuleToAccount(ctx, "", baseAcc.GetAddress(), initCoins) // nolint:errcheck
})
suite.Require().Error(
keeper.SendCoinsFromModuleToAccount(ctx, holderAcc.GetName(), baseAcc.GetAddress(), initCoins.Add(initCoins...)),
)
suite.Require().NoError(
keeper.SendCoinsFromModuleToModule(ctx, holderAcc.GetName(), authtypes.Burner, initCoins),
)
suite.Require().Equal(sdk.Coins(nil), getCoinsByName(ctx, keeper, authKeeper, holderAcc.GetName()))
suite.Require().Equal(initCoins, getCoinsByName(ctx, keeper, authKeeper, authtypes.Burner))
suite.Require().NoError(
keeper.SendCoinsFromModuleToAccount(ctx, authtypes.Burner, baseAcc.GetAddress(), initCoins),
)
suite.Require().Equal(sdk.Coins(nil), getCoinsByName(ctx, keeper, authKeeper, authtypes.Burner))
suite.Require().Equal(initCoins, keeper.GetAllBalances(ctx, baseAcc.GetAddress()))
suite.Require().NoError(keeper.SendCoinsFromAccountToModule(ctx, baseAcc.GetAddress(), authtypes.Burner, initCoins))
suite.Require().Equal(sdk.Coins(nil), keeper.GetAllBalances(ctx, baseAcc.GetAddress()))
suite.Require().Equal(initCoins, getCoinsByName(ctx, keeper, authKeeper, authtypes.Burner))
}
func (suite *IntegrationTestSuite) TestSupply_MintCoins() {
app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, abci.Header{Height: 1})
appCodec := app.AppCodec()
// add module accounts to supply keeper
maccPerms := simapp.GetMaccPerms()
maccPerms[holder] = nil
maccPerms[authtypes.Burner] = []string{authtypes.Burner}
maccPerms[authtypes.Minter] = []string{authtypes.Minter}
maccPerms[multiPerm] = []string{authtypes.Burner, authtypes.Minter, authtypes.Staking}
maccPerms[randomPerm] = []string{"random"}
authKeeper := authkeeper.NewAccountKeeper(
appCodec, app.GetKey(types.StoreKey), app.GetSubspace(types.ModuleName),
authtypes.ProtoBaseAccount, maccPerms,
)
keeper := keeper.NewBaseKeeper(
appCodec, app.GetKey(types.StoreKey), authKeeper,
app.GetSubspace(types.ModuleName), make(map[string]bool),
)
authKeeper.SetModuleAccount(ctx, burnerAcc)
authKeeper.SetModuleAccount(ctx, minterAcc)
authKeeper.SetModuleAccount(ctx, multiPermAcc)
authKeeper.SetModuleAccount(ctx, randomPermAcc)
initialSupply := keeper.GetSupply(ctx)
suite.Require().Panics(func() { keeper.MintCoins(ctx, "", initCoins) }, "no module account") // nolint:errcheck
suite.Require().Panics(func() { keeper.MintCoins(ctx, authtypes.Burner, initCoins) }, "invalid permission") // nolint:errcheck
err := keeper.MintCoins(ctx, authtypes.Minter, sdk.Coins{sdk.Coin{Denom: "denom", Amount: sdk.NewInt(-10)}})
suite.Require().Error(err, "insufficient coins")
suite.Require().Panics(func() { keeper.MintCoins(ctx, randomPerm, initCoins) }) // nolint:errcheck
err = keeper.MintCoins(ctx, authtypes.Minter, initCoins)
suite.Require().NoError(err)
suite.Require().Equal(initCoins, getCoinsByName(ctx, keeper, authKeeper, authtypes.Minter))
suite.Require().Equal(initialSupply.GetTotal().Add(initCoins...), keeper.GetSupply(ctx).GetTotal())
// test same functionality on module account with multiple permissions
initialSupply = keeper.GetSupply(ctx)
err = keeper.MintCoins(ctx, multiPermAcc.GetName(), initCoins)
suite.Require().NoError(err)
suite.Require().Equal(initCoins, getCoinsByName(ctx, keeper, authKeeper, multiPermAcc.GetName()))
suite.Require().Equal(initialSupply.GetTotal().Add(initCoins...), keeper.GetSupply(ctx).GetTotal())
suite.Require().Panics(func() { keeper.MintCoins(ctx, authtypes.Burner, initCoins) }) // nolint:errcheck
}
func (suite *IntegrationTestSuite) TestSupply_BurnCoins() {
app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, abci.Header{Height: 1})
appCodec, _ := simapp.MakeCodecs()
// add module accounts to supply keeper
maccPerms := simapp.GetMaccPerms()
maccPerms[holder] = nil
maccPerms[authtypes.Burner] = []string{authtypes.Burner}
maccPerms[authtypes.Minter] = []string{authtypes.Minter}
maccPerms[multiPerm] = []string{authtypes.Burner, authtypes.Minter, authtypes.Staking}
maccPerms[randomPerm] = []string{"random"}
authKeeper := authkeeper.NewAccountKeeper(
appCodec, app.GetKey(types.StoreKey), app.GetSubspace(types.ModuleName),
authtypes.ProtoBaseAccount, maccPerms,
)
keeper := keeper.NewBaseKeeper(
appCodec, app.GetKey(types.StoreKey), authKeeper,
app.GetSubspace(types.ModuleName), make(map[string]bool),
)
suite.Require().NoError(keeper.SetBalances(ctx, burnerAcc.GetAddress(), initCoins))
keeper.SetSupply(ctx, types.NewSupply(initCoins))
authKeeper.SetModuleAccount(ctx, burnerAcc)
initialSupply := keeper.GetSupply(ctx)
initialSupply.Inflate(initCoins)
keeper.SetSupply(ctx, initialSupply)
suite.Require().Panics(func() { keeper.BurnCoins(ctx, "", initCoins) }, "no module account") // nolint:errcheck
suite.Require().Panics(func() { keeper.BurnCoins(ctx, authtypes.Minter, initCoins) }, "invalid permission") // nolint:errcheck
suite.Require().Panics(func() { keeper.BurnCoins(ctx, randomPerm, initialSupply.GetTotal()) }, "random permission") // nolint:errcheck
err := keeper.BurnCoins(ctx, authtypes.Burner, initialSupply.GetTotal())
suite.Require().Error(err, "insufficient coins")
err = keeper.BurnCoins(ctx, authtypes.Burner, initCoins)
suite.Require().NoError(err)
suite.Require().Equal(sdk.Coins(nil), getCoinsByName(ctx, keeper, authKeeper, authtypes.Burner))
suite.Require().Equal(initialSupply.GetTotal().Sub(initCoins), keeper.GetSupply(ctx).GetTotal())
// test same functionality on module account with multiple permissions
initialSupply = keeper.GetSupply(ctx)
initialSupply.Inflate(initCoins)
keeper.SetSupply(ctx, initialSupply)
suite.Require().NoError(keeper.SetBalances(ctx, multiPermAcc.GetAddress(), initCoins))
authKeeper.SetModuleAccount(ctx, multiPermAcc)
err = keeper.BurnCoins(ctx, multiPermAcc.GetName(), initCoins)
suite.Require().NoError(err)
suite.Require().Equal(sdk.Coins(nil), getCoinsByName(ctx, keeper, authKeeper, multiPermAcc.GetName()))
suite.Require().Equal(initialSupply.GetTotal().Sub(initCoins), keeper.GetSupply(ctx).GetTotal())
}
func (suite *IntegrationTestSuite) TestSendCoinsNewAccount() {
app, ctx := suite.app, suite.ctx
balances := sdk.NewCoins(newFooCoin(100), newBarCoin(50))
addr1 := sdk.AccAddress([]byte("addr1"))
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
app.AccountKeeper.SetAccount(ctx, acc1)
suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, balances))
acc1Balances := app.BankKeeper.GetAllBalances(ctx, addr1)
suite.Require().Equal(balances, acc1Balances)
addr2 := sdk.AccAddress([]byte("addr2"))
suite.Require().Nil(app.AccountKeeper.GetAccount(ctx, addr2))
suite.Require().Empty(app.BankKeeper.GetAllBalances(ctx, addr2))
sendAmt := sdk.NewCoins(newFooCoin(50), newBarCoin(25))
suite.Require().NoError(app.BankKeeper.SendCoins(ctx, addr1, addr2, sendAmt))
acc2Balances := app.BankKeeper.GetAllBalances(ctx, addr2)
suite.Require().Equal(sendAmt, acc2Balances)
suite.Require().NotNil(app.AccountKeeper.GetAccount(ctx, addr2))
}
func (suite *IntegrationTestSuite) TestInputOutputNewAccount() {
app, ctx := suite.app, suite.ctx
balances := sdk.NewCoins(newFooCoin(100), newBarCoin(50))
addr1 := sdk.AccAddress([]byte("addr1"))
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
app.AccountKeeper.SetAccount(ctx, acc1)
suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, balances))
acc1Balances := app.BankKeeper.GetAllBalances(ctx, addr1)
suite.Require().Equal(balances, acc1Balances)
addr2 := sdk.AccAddress([]byte("addr2"))
suite.Require().Nil(app.AccountKeeper.GetAccount(ctx, addr2))
suite.Require().Empty(app.BankKeeper.GetAllBalances(ctx, addr2))
inputs := []types.Input{
{Address: addr1, Coins: sdk.NewCoins(newFooCoin(30), newBarCoin(10))},
}
outputs := []types.Output{
{Address: addr2, Coins: sdk.NewCoins(newFooCoin(30), newBarCoin(10))},
}
suite.Require().NoError(app.BankKeeper.InputOutputCoins(ctx, inputs, outputs))
expected := sdk.NewCoins(newFooCoin(30), newBarCoin(10))
acc2Balances := app.BankKeeper.GetAllBalances(ctx, addr2)
suite.Require().Equal(expected, acc2Balances)
suite.Require().NotNil(app.AccountKeeper.GetAccount(ctx, addr2))
}
2020-01-30 13:31:16 -08:00
func (suite *IntegrationTestSuite) TestInputOutputCoins() {
app, ctx := suite.app, suite.ctx
balances := sdk.NewCoins(newFooCoin(90), newBarCoin(30))
2018-04-09 15:10:58 -07:00
2020-01-30 13:31:16 -08:00
addr1 := sdk.AccAddress([]byte("addr1"))
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
app.AccountKeeper.SetAccount(ctx, acc1)
2018-04-09 15:10:58 -07:00
2020-01-30 13:31:16 -08:00
addr2 := sdk.AccAddress([]byte("addr2"))
acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
app.AccountKeeper.SetAccount(ctx, acc2)
2018-04-09 15:10:58 -07:00
2020-01-30 13:31:16 -08:00
addr3 := sdk.AccAddress([]byte("addr3"))
acc3 := app.AccountKeeper.NewAccountWithAddress(ctx, addr3)
app.AccountKeeper.SetAccount(ctx, acc3)
2018-04-09 15:10:58 -07:00
inputs := []types.Input{
2020-01-30 13:31:16 -08:00
{Address: addr1, Coins: sdk.NewCoins(newFooCoin(30), newBarCoin(10))},
{Address: addr1, Coins: sdk.NewCoins(newFooCoin(30), newBarCoin(10))},
2018-04-09 15:10:58 -07:00
}
outputs := []types.Output{
2020-01-30 13:31:16 -08:00
{Address: addr2, Coins: sdk.NewCoins(newFooCoin(30), newBarCoin(10))},
{Address: addr3, Coins: sdk.NewCoins(newFooCoin(30), newBarCoin(10))},
2018-04-09 15:10:58 -07:00
}
2020-01-30 13:31:16 -08:00
suite.Require().Error(app.BankKeeper.InputOutputCoins(ctx, inputs, []types.Output{}))
suite.Require().Error(app.BankKeeper.InputOutputCoins(ctx, inputs, outputs))
suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, balances))
insufficientInputs := []types.Input{
{Address: addr1, Coins: sdk.NewCoins(newFooCoin(300), newBarCoin(100))},
{Address: addr1, Coins: sdk.NewCoins(newFooCoin(300), newBarCoin(100))},
2019-08-19 06:29:17 -07:00
}
2020-01-30 13:31:16 -08:00
insufficientOutputs := []types.Output{
{Address: addr2, Coins: sdk.NewCoins(newFooCoin(300), newBarCoin(100))},
{Address: addr3, Coins: sdk.NewCoins(newFooCoin(300), newBarCoin(100))},
}
suite.Require().Error(app.BankKeeper.InputOutputCoins(ctx, insufficientInputs, insufficientOutputs))
suite.Require().NoError(app.BankKeeper.InputOutputCoins(ctx, inputs, outputs))
acc1Balances := app.BankKeeper.GetAllBalances(ctx, addr1)
expected := sdk.NewCoins(newFooCoin(30), newBarCoin(10))
suite.Require().Equal(expected, acc1Balances)
acc2Balances := app.BankKeeper.GetAllBalances(ctx, addr2)
suite.Require().Equal(expected, acc2Balances)
acc3Balances := app.BankKeeper.GetAllBalances(ctx, addr3)
suite.Require().Equal(expected, acc3Balances)
2018-04-09 15:10:58 -07:00
}
2020-01-30 13:31:16 -08:00
func (suite *IntegrationTestSuite) TestSendCoins() {
app, ctx := suite.app, suite.ctx
balances := sdk.NewCoins(newFooCoin(100), newBarCoin(50))
2020-01-30 13:31:16 -08:00
addr1 := sdk.AccAddress([]byte("addr1"))
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
app.AccountKeeper.SetAccount(ctx, acc1)
2020-01-30 13:31:16 -08:00
addr2 := sdk.AccAddress([]byte("addr2"))
acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
app.AccountKeeper.SetAccount(ctx, acc2)
suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr2, balances))
2018-04-09 15:10:58 -07:00
2020-01-30 13:31:16 -08:00
sendAmt := sdk.NewCoins(newFooCoin(50), newBarCoin(25))
suite.Require().Error(app.BankKeeper.SendCoins(ctx, addr1, addr2, sendAmt))
suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, balances))
suite.Require().NoError(app.BankKeeper.SendCoins(ctx, addr1, addr2, sendAmt))
acc1Balances := app.BankKeeper.GetAllBalances(ctx, addr1)
expected := sdk.NewCoins(newFooCoin(50), newBarCoin(25))
suite.Require().Equal(expected, acc1Balances)
acc2Balances := app.BankKeeper.GetAllBalances(ctx, addr2)
expected = sdk.NewCoins(newFooCoin(150), newBarCoin(75))
suite.Require().Equal(expected, acc2Balances)
}
func (suite *IntegrationTestSuite) TestValidateBalance() {
app, ctx := suite.app, suite.ctx
now := tmtime.Now()
ctx = ctx.WithBlockHeader(abci.Header{Time: now})
endTime := now.Add(24 * time.Hour)
addr1 := sdk.AccAddress([]byte("addr1"))
2018-07-06 00:06:53 -07:00
addr2 := sdk.AccAddress([]byte("addr2"))
2018-04-09 15:10:58 -07:00
2020-01-30 13:31:16 -08:00
suite.Require().Error(app.BankKeeper.ValidateBalance(ctx, addr1))
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
2019-08-19 06:29:17 -07:00
app.AccountKeeper.SetAccount(ctx, acc)
2020-01-30 13:31:16 -08:00
balances := sdk.NewCoins(newFooCoin(100))
suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, balances))
suite.Require().NoError(app.BankKeeper.ValidateBalance(ctx, addr1))
bacc := authtypes.NewBaseAccountWithAddress(addr2)
vacc := vesting.NewContinuousVestingAccount(bacc, balances.Add(balances...), now.Unix(), endTime.Unix())
2020-01-30 13:31:16 -08:00
app.AccountKeeper.SetAccount(ctx, vacc)
suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr2, balances))
suite.Require().Error(app.BankKeeper.ValidateBalance(ctx, addr2))
2018-04-09 15:10:58 -07:00
}
2020-01-30 13:31:16 -08:00
func (suite *IntegrationTestSuite) TestBalance() {
app, ctx := suite.app, suite.ctx
addr := sdk.AccAddress([]byte("addr1"))
2020-01-30 13:31:16 -08:00
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr)
app.AccountKeeper.SetAccount(ctx, acc)
suite.Require().Equal(sdk.NewCoin(fooDenom, sdk.ZeroInt()), app.BankKeeper.GetBalance(ctx, addr, fooDenom))
balances := sdk.NewCoins(newFooCoin(100))
suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr, balances))
suite.Require().Equal(balances.AmountOf(fooDenom), app.BankKeeper.GetBalance(ctx, addr, fooDenom).Amount)
suite.Require().Equal(balances, app.BankKeeper.GetAllBalances(ctx, addr))
newFooBalance := newFooCoin(99)
suite.Require().NoError(app.BankKeeper.SetBalance(ctx, addr, newFooBalance))
suite.Require().Equal(newFooBalance, app.BankKeeper.GetBalance(ctx, addr, fooDenom))
balances = sdk.NewCoins(newBarCoin(500))
suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr, balances))
suite.Require().Equal(sdk.NewCoin(fooDenom, sdk.ZeroInt()), app.BankKeeper.GetBalance(ctx, addr, fooDenom))
suite.Require().Equal(balances.AmountOf(barDenom), app.BankKeeper.GetBalance(ctx, addr, barDenom).Amount)
suite.Require().Equal(balances, app.BankKeeper.GetAllBalances(ctx, addr))
2020-01-30 13:31:16 -08:00
invalidBalance := sdk.Coin{Denom: "fooDenom", Amount: sdk.NewInt(-50)}
suite.Require().Error(app.BankKeeper.SetBalance(ctx, addr, invalidBalance))
}
func (suite *IntegrationTestSuite) TestSendEnabled() {
app, ctx := suite.app, suite.ctx
enabled := false
app.BankKeeper.SetSendEnabled(ctx, enabled)
suite.Require().Equal(enabled, app.BankKeeper.GetSendEnabled(ctx))
}
func (suite *IntegrationTestSuite) TestHasBalance() {
app, ctx := suite.app, suite.ctx
addr := sdk.AccAddress([]byte("addr1"))
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr)
app.AccountKeeper.SetAccount(ctx, acc)
balances := sdk.NewCoins(newFooCoin(100))
suite.Require().False(app.BankKeeper.HasBalance(ctx, addr, newFooCoin(99)))
app.BankKeeper.SetBalances(ctx, addr, balances)
suite.Require().False(app.BankKeeper.HasBalance(ctx, addr, newFooCoin(101)))
suite.Require().True(app.BankKeeper.HasBalance(ctx, addr, newFooCoin(100)))
suite.Require().True(app.BankKeeper.HasBalance(ctx, addr, newFooCoin(1)))
}
func (suite *IntegrationTestSuite) TestMsgSendEvents() {
app, ctx := suite.app, suite.ctx
addr := sdk.AccAddress([]byte("addr1"))
addr2 := sdk.AccAddress([]byte("addr2"))
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr)
app.AccountKeeper.SetAccount(ctx, acc)
2020-01-30 13:31:16 -08:00
newCoins := sdk.NewCoins(sdk.NewInt64Coin(fooDenom, 50))
suite.Require().Error(app.BankKeeper.SendCoins(ctx, addr, addr2, newCoins))
2020-03-26 09:46:10 -07:00
events := ctx.EventManager().ABCIEvents()
2020-01-30 13:31:16 -08:00
suite.Require().Equal(2, len(events))
event1 := sdk.Event{
Type: types.EventTypeTransfer,
Attributes: []tmkv.Pair{},
}
event1.Attributes = append(
event1.Attributes,
2020-01-30 13:31:16 -08:00
tmkv.Pair{Key: []byte(types.AttributeKeyRecipient), Value: []byte(addr2.String())},
)
event1.Attributes = append(
event1.Attributes,
2020-01-30 13:31:16 -08:00
tmkv.Pair{Key: []byte(sdk.AttributeKeyAmount), Value: []byte(newCoins.String())},
)
event2 := sdk.Event{
Type: sdk.EventTypeMessage,
Attributes: []tmkv.Pair{},
}
event2.Attributes = append(
event2.Attributes,
2020-01-30 13:31:16 -08:00
tmkv.Pair{Key: []byte(types.AttributeKeySender), Value: []byte(addr.String())},
)
2020-03-26 10:50:34 -07:00
suite.Require().Equal(abci.Event(event1), events[0])
suite.Require().Equal(abci.Event(event2), events[1])
2020-01-30 13:31:16 -08:00
app.BankKeeper.SetBalances(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin(fooDenom, 50)))
newCoins = sdk.NewCoins(sdk.NewInt64Coin(fooDenom, 50))
suite.Require().NoError(app.BankKeeper.SendCoins(ctx, addr, addr2, newCoins))
2020-03-26 09:46:10 -07:00
events = ctx.EventManager().ABCIEvents()
2020-01-30 13:31:16 -08:00
suite.Require().Equal(4, len(events))
2020-03-26 10:50:34 -07:00
suite.Require().Equal(abci.Event(event1), events[2])
suite.Require().Equal(abci.Event(event2), events[3])
}
2020-01-30 13:31:16 -08:00
func (suite *IntegrationTestSuite) TestMsgMultiSendEvents() {
app, ctx := suite.app, suite.ctx
app.BankKeeper.SetSendEnabled(ctx, true)
addr := sdk.AccAddress([]byte("addr1"))
addr2 := sdk.AccAddress([]byte("addr2"))
addr3 := sdk.AccAddress([]byte("addr3"))
addr4 := sdk.AccAddress([]byte("addr4"))
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr)
acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
app.AccountKeeper.SetAccount(ctx, acc)
app.AccountKeeper.SetAccount(ctx, acc2)
2020-01-30 13:31:16 -08:00
newCoins := sdk.NewCoins(sdk.NewInt64Coin(fooDenom, 50))
newCoins2 := sdk.NewCoins(sdk.NewInt64Coin(barDenom, 100))
inputs := []types.Input{
{Address: addr, Coins: newCoins},
{Address: addr2, Coins: newCoins2},
}
outputs := []types.Output{
{Address: addr3, Coins: newCoins},
{Address: addr4, Coins: newCoins2},
}
2020-01-30 13:31:16 -08:00
suite.Require().Error(app.BankKeeper.InputOutputCoins(ctx, inputs, outputs))
2020-03-26 09:46:10 -07:00
events := ctx.EventManager().ABCIEvents()
2020-01-30 13:31:16 -08:00
suite.Require().Equal(0, len(events))
// Set addr's coins but not addr2's coins
2020-01-30 13:31:16 -08:00
app.BankKeeper.SetBalances(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin(fooDenom, 50)))
suite.Require().Error(app.BankKeeper.InputOutputCoins(ctx, inputs, outputs))
2020-03-26 09:46:10 -07:00
events = ctx.EventManager().ABCIEvents()
2020-01-30 13:31:16 -08:00
suite.Require().Equal(1, len(events))
event1 := sdk.Event{
Type: sdk.EventTypeMessage,
Attributes: []tmkv.Pair{},
}
event1.Attributes = append(
event1.Attributes,
2020-01-30 13:31:16 -08:00
tmkv.Pair{Key: []byte(types.AttributeKeySender), Value: []byte(addr.String())},
)
2020-03-26 10:50:34 -07:00
suite.Require().Equal(abci.Event(event1), events[0])
// Set addr's coins and addr2's coins
2020-01-30 13:31:16 -08:00
app.BankKeeper.SetBalances(ctx, addr, sdk.NewCoins(sdk.NewInt64Coin(fooDenom, 50)))
newCoins = sdk.NewCoins(sdk.NewInt64Coin(fooDenom, 50))
app.BankKeeper.SetBalances(ctx, addr2, sdk.NewCoins(sdk.NewInt64Coin(barDenom, 100)))
newCoins2 = sdk.NewCoins(sdk.NewInt64Coin(barDenom, 100))
suite.Require().NoError(app.BankKeeper.InputOutputCoins(ctx, inputs, outputs))
2020-03-26 09:46:10 -07:00
events = ctx.EventManager().ABCIEvents()
2020-01-30 13:31:16 -08:00
suite.Require().Equal(5, len(events))
event2 := sdk.Event{
Type: sdk.EventTypeMessage,
Attributes: []tmkv.Pair{},
}
event2.Attributes = append(
event2.Attributes,
2020-01-30 13:31:16 -08:00
tmkv.Pair{Key: []byte(types.AttributeKeySender), Value: []byte(addr2.String())},
)
event3 := sdk.Event{
Type: types.EventTypeTransfer,
Attributes: []tmkv.Pair{},
}
event3.Attributes = append(
event3.Attributes,
2020-01-30 13:31:16 -08:00
tmkv.Pair{Key: []byte(types.AttributeKeyRecipient), Value: []byte(addr3.String())},
)
event3.Attributes = append(
event3.Attributes,
tmkv.Pair{Key: []byte(sdk.AttributeKeyAmount), Value: []byte(newCoins.String())})
event4 := sdk.Event{
Type: types.EventTypeTransfer,
Attributes: []tmkv.Pair{},
}
event4.Attributes = append(
event4.Attributes,
2020-01-30 13:31:16 -08:00
tmkv.Pair{Key: []byte(types.AttributeKeyRecipient), Value: []byte(addr4.String())},
)
event4.Attributes = append(
event4.Attributes,
2020-01-30 13:31:16 -08:00
tmkv.Pair{Key: []byte(sdk.AttributeKeyAmount), Value: []byte(newCoins2.String())},
)
2020-03-26 10:50:34 -07:00
suite.Require().Equal(abci.Event(event1), events[1])
suite.Require().Equal(abci.Event(event2), events[2])
suite.Require().Equal(abci.Event(event3), events[3])
suite.Require().Equal(abci.Event(event4), events[4])
}
2020-01-30 13:31:16 -08:00
func (suite *IntegrationTestSuite) TestSpendableCoins() {
app, ctx := suite.app, suite.ctx
now := tmtime.Now()
ctx = ctx.WithBlockHeader(abci.Header{Time: now})
endTime := now.Add(24 * time.Hour)
2019-08-19 06:29:17 -07:00
2020-01-30 13:31:16 -08:00
origCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 100))
delCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 50))
2018-04-09 15:10:58 -07:00
2020-01-30 13:31:16 -08:00
addr1 := sdk.AccAddress([]byte("addr1"))
addr2 := sdk.AccAddress([]byte("addr2"))
addrModule := sdk.AccAddress([]byte("moduleAcc"))
macc := app.AccountKeeper.NewAccountWithAddress(ctx, addrModule)
bacc := authtypes.NewBaseAccountWithAddress(addr1)
vacc := vesting.NewContinuousVestingAccount(bacc, origCoins, ctx.BlockHeader().Time.Unix(), endTime.Unix())
2020-01-30 13:31:16 -08:00
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
2018-04-09 15:10:58 -07:00
2020-01-30 13:31:16 -08:00
app.AccountKeeper.SetAccount(ctx, macc)
app.AccountKeeper.SetAccount(ctx, vacc)
2019-08-19 06:29:17 -07:00
app.AccountKeeper.SetAccount(ctx, acc)
2020-01-30 13:31:16 -08:00
suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, origCoins))
suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr2, origCoins))
2018-04-09 15:10:58 -07:00
2020-01-30 13:31:16 -08:00
suite.Require().Equal(origCoins, app.BankKeeper.SpendableCoins(ctx, addr2))
2018-04-09 15:10:58 -07:00
2020-01-30 13:31:16 -08:00
ctx = ctx.WithBlockTime(now.Add(12 * time.Hour))
suite.Require().NoError(app.BankKeeper.DelegateCoins(ctx, addr2, addrModule, delCoins))
suite.Require().Equal(origCoins.Sub(delCoins), app.BankKeeper.SpendableCoins(ctx, addr1))
2018-04-09 15:10:58 -07:00
}
2020-01-30 13:31:16 -08:00
func (suite *IntegrationTestSuite) TestVestingAccountSend() {
app, ctx := suite.app, suite.ctx
now := tmtime.Now()
2019-08-19 06:29:17 -07:00
ctx = ctx.WithBlockHeader(abci.Header{Time: now})
endTime := now.Add(24 * time.Hour)
2019-03-14 07:32:46 -07:00
origCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 100))
sendCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 50))
addr1 := sdk.AccAddress([]byte("addr1"))
addr2 := sdk.AccAddress([]byte("addr2"))
2020-01-30 13:31:16 -08:00
bacc := authtypes.NewBaseAccountWithAddress(addr1)
vacc := vesting.NewContinuousVestingAccount(bacc, origCoins, now.Unix(), endTime.Unix())
2020-01-30 13:31:16 -08:00
2019-08-19 06:29:17 -07:00
app.AccountKeeper.SetAccount(ctx, vacc)
2020-01-30 13:31:16 -08:00
suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, origCoins))
// require that no coins be sendable at the beginning of the vesting schedule
2020-01-30 13:31:16 -08:00
suite.Require().Error(app.BankKeeper.SendCoins(ctx, addr1, addr2, sendCoins))
// receive some coins
2020-01-30 13:31:16 -08:00
suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, origCoins.Add(sendCoins...)))
// require that all vested coins are spendable plus any received
ctx = ctx.WithBlockTime(now.Add(12 * time.Hour))
2020-01-30 13:31:16 -08:00
suite.Require().NoError(app.BankKeeper.SendCoins(ctx, addr1, addr2, sendCoins))
suite.Require().Equal(origCoins, app.BankKeeper.GetAllBalances(ctx, addr1))
}
2020-01-30 13:31:16 -08:00
func (suite *IntegrationTestSuite) TestPeriodicVestingAccountSend() {
app, ctx := suite.app, suite.ctx
now := tmtime.Now()
ctx = ctx.WithBlockHeader(abci.Header{Time: now})
origCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 100))
sendCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 50))
addr1 := sdk.AccAddress([]byte("addr1"))
addr2 := sdk.AccAddress([]byte("addr2"))
periods := vesting.Periods{
vesting.Period{Length: int64(12 * 60 * 60), Amount: sdk.Coins{sdk.NewInt64Coin("stake", 50)}},
vesting.Period{Length: int64(6 * 60 * 60), Amount: sdk.Coins{sdk.NewInt64Coin("stake", 25)}},
vesting.Period{Length: int64(6 * 60 * 60), Amount: sdk.Coins{sdk.NewInt64Coin("stake", 25)}},
}
2020-01-30 13:31:16 -08:00
bacc := authtypes.NewBaseAccountWithAddress(addr1)
vacc := vesting.NewPeriodicVestingAccount(bacc, origCoins, ctx.BlockHeader().Time.Unix(), periods)
2020-01-30 13:31:16 -08:00
app.AccountKeeper.SetAccount(ctx, vacc)
2020-01-30 13:31:16 -08:00
suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, origCoins))
// require that no coins be sendable at the beginning of the vesting schedule
2020-01-30 13:31:16 -08:00
suite.Require().Error(app.BankKeeper.SendCoins(ctx, addr1, addr2, sendCoins))
// receive some coins
2020-01-30 13:31:16 -08:00
suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, origCoins.Add(sendCoins...)))
// require that all vested coins are spendable plus any received
ctx = ctx.WithBlockTime(now.Add(12 * time.Hour))
2020-01-30 13:31:16 -08:00
suite.Require().NoError(app.BankKeeper.SendCoins(ctx, addr1, addr2, sendCoins))
suite.Require().Equal(origCoins, app.BankKeeper.GetAllBalances(ctx, addr1))
}
2020-01-30 13:31:16 -08:00
func (suite *IntegrationTestSuite) TestVestingAccountReceive() {
app, ctx := suite.app, suite.ctx
now := tmtime.Now()
2019-08-19 06:29:17 -07:00
ctx = ctx.WithBlockHeader(abci.Header{Time: now})
endTime := now.Add(24 * time.Hour)
2019-03-14 07:32:46 -07:00
origCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 100))
sendCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 50))
addr1 := sdk.AccAddress([]byte("addr1"))
addr2 := sdk.AccAddress([]byte("addr2"))
bacc := authtypes.NewBaseAccountWithAddress(addr1)
vacc := vesting.NewContinuousVestingAccount(bacc, origCoins, ctx.BlockHeader().Time.Unix(), endTime.Unix())
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
2020-01-30 13:31:16 -08:00
app.AccountKeeper.SetAccount(ctx, vacc)
app.AccountKeeper.SetAccount(ctx, acc)
2020-01-30 13:31:16 -08:00
suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, origCoins))
suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr2, origCoins))
// send some coins to the vesting account
2020-01-30 13:31:16 -08:00
suite.Require().NoError(app.BankKeeper.SendCoins(ctx, addr2, addr1, sendCoins))
// require the coins are spendable
vacc = app.AccountKeeper.GetAccount(ctx, addr1).(*vesting.ContinuousVestingAccount)
2020-01-30 13:31:16 -08:00
balances := app.BankKeeper.GetAllBalances(ctx, addr1)
suite.Require().Equal(origCoins.Add(sendCoins...), balances)
suite.Require().Equal(balances.Sub(vacc.LockedCoins(now)), sendCoins)
// require coins are spendable plus any that have vested
2020-01-30 13:31:16 -08:00
suite.Require().Equal(balances.Sub(vacc.LockedCoins(now.Add(12*time.Hour))), origCoins)
}
2020-01-30 13:31:16 -08:00
func (suite *IntegrationTestSuite) TestPeriodicVestingAccountReceive() {
app, ctx := suite.app, suite.ctx
now := tmtime.Now()
ctx = ctx.WithBlockHeader(abci.Header{Time: now})
origCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 100))
sendCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 50))
addr1 := sdk.AccAddress([]byte("addr1"))
addr2 := sdk.AccAddress([]byte("addr2"))
bacc := authtypes.NewBaseAccountWithAddress(addr1)
periods := vesting.Periods{
vesting.Period{Length: int64(12 * 60 * 60), Amount: sdk.Coins{sdk.NewInt64Coin("stake", 50)}},
vesting.Period{Length: int64(6 * 60 * 60), Amount: sdk.Coins{sdk.NewInt64Coin("stake", 25)}},
vesting.Period{Length: int64(6 * 60 * 60), Amount: sdk.Coins{sdk.NewInt64Coin("stake", 25)}},
}
2020-01-30 13:31:16 -08:00
vacc := vesting.NewPeriodicVestingAccount(bacc, origCoins, ctx.BlockHeader().Time.Unix(), periods)
2019-08-19 06:29:17 -07:00
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
2020-01-30 13:31:16 -08:00
2019-08-19 06:29:17 -07:00
app.AccountKeeper.SetAccount(ctx, vacc)
app.AccountKeeper.SetAccount(ctx, acc)
2020-01-30 13:31:16 -08:00
suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, origCoins))
suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr2, origCoins))
// send some coins to the vesting account
2020-01-30 13:31:16 -08:00
suite.Require().NoError(app.BankKeeper.SendCoins(ctx, addr2, addr1, sendCoins))
// require the coins are spendable
vacc = app.AccountKeeper.GetAccount(ctx, addr1).(*vesting.PeriodicVestingAccount)
2020-01-30 13:31:16 -08:00
balances := app.BankKeeper.GetAllBalances(ctx, addr1)
suite.Require().Equal(origCoins.Add(sendCoins...), balances)
suite.Require().Equal(balances.Sub(vacc.LockedCoins(now)), sendCoins)
// require coins are spendable plus any that have vested
2020-01-30 13:31:16 -08:00
suite.Require().Equal(balances.Sub(vacc.LockedCoins(now.Add(12*time.Hour))), origCoins)
}
2020-01-30 13:31:16 -08:00
func (suite *IntegrationTestSuite) TestDelegateCoins() {
app, ctx := suite.app, suite.ctx
now := tmtime.Now()
2019-08-19 06:29:17 -07:00
ctx = ctx.WithBlockHeader(abci.Header{Time: now})
endTime := now.Add(24 * time.Hour)
2019-03-14 07:32:46 -07:00
origCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 100))
delCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 50))
addr1 := sdk.AccAddress([]byte("addr1"))
addr2 := sdk.AccAddress([]byte("addr2"))
2019-06-28 13:11:27 -07:00
addrModule := sdk.AccAddress([]byte("moduleAcc"))
2020-01-30 13:31:16 -08:00
macc := app.AccountKeeper.NewAccountWithAddress(ctx, addrModule) // we don't need to define an actual module account bc we just need the address for testing
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
bacc := authtypes.NewBaseAccountWithAddress(addr1)
vacc := vesting.NewContinuousVestingAccount(bacc, origCoins, ctx.BlockHeader().Time.Unix(), endTime.Unix())
2020-01-30 13:31:16 -08:00
app.AccountKeeper.SetAccount(ctx, vacc)
app.AccountKeeper.SetAccount(ctx, acc)
app.AccountKeeper.SetAccount(ctx, macc)
suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, origCoins))
suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr2, origCoins))
ctx = ctx.WithBlockTime(now.Add(12 * time.Hour))
// require the ability for a non-vesting account to delegate
2020-01-30 13:31:16 -08:00
suite.Require().NoError(app.BankKeeper.DelegateCoins(ctx, addr2, addrModule, delCoins))
suite.Require().Equal(origCoins.Sub(delCoins), app.BankKeeper.GetAllBalances(ctx, addr2))
suite.Require().Equal(delCoins, app.BankKeeper.GetAllBalances(ctx, addrModule))
// require the ability for a vesting account to delegate
2020-01-30 13:31:16 -08:00
suite.Require().NoError(app.BankKeeper.DelegateCoins(ctx, addr1, addrModule, delCoins))
suite.Require().Equal(delCoins, app.BankKeeper.GetAllBalances(ctx, addr1))
}
func (suite *IntegrationTestSuite) TestDelegateCoins_Invalid() {
app, ctx := suite.app, suite.ctx
origCoins := sdk.NewCoins(newFooCoin(100))
delCoins := sdk.NewCoins(newFooCoin(50))
addr1 := sdk.AccAddress([]byte("addr1"))
addrModule := sdk.AccAddress([]byte("moduleAcc"))
macc := app.AccountKeeper.NewAccountWithAddress(ctx, addrModule) // we don't need to define an actual module account bc we just need the address for testing
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
suite.Require().Error(app.BankKeeper.DelegateCoins(ctx, addr1, addrModule, delCoins))
invalidCoins := sdk.Coins{sdk.Coin{Denom: "fooDenom", Amount: sdk.NewInt(-50)}}
suite.Require().Error(app.BankKeeper.DelegateCoins(ctx, addr1, addrModule, invalidCoins))
app.AccountKeeper.SetAccount(ctx, macc)
suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, origCoins))
suite.Require().Error(app.BankKeeper.DelegateCoins(ctx, addr1, addrModule, delCoins))
app.AccountKeeper.SetAccount(ctx, acc)
suite.Require().Error(app.BankKeeper.DelegateCoins(ctx, addr1, addrModule, origCoins.Add(origCoins...)))
}
2020-01-30 13:31:16 -08:00
func (suite *IntegrationTestSuite) TestUndelegateCoins() {
app, ctx := suite.app, suite.ctx
now := tmtime.Now()
2019-08-19 06:29:17 -07:00
ctx = ctx.WithBlockHeader(abci.Header{Time: now})
endTime := now.Add(24 * time.Hour)
2019-03-14 07:32:46 -07:00
origCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 100))
delCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 50))
addr1 := sdk.AccAddress([]byte("addr1"))
addr2 := sdk.AccAddress([]byte("addr2"))
2019-06-28 13:11:27 -07:00
addrModule := sdk.AccAddress([]byte("moduleAcc"))
bacc := authtypes.NewBaseAccountWithAddress(addr1)
2020-01-30 13:31:16 -08:00
macc := app.AccountKeeper.NewAccountWithAddress(ctx, addrModule) // we don't need to define an actual module account bc we just need the address for testing
vacc := vesting.NewContinuousVestingAccount(bacc, origCoins, ctx.BlockHeader().Time.Unix(), endTime.Unix())
2020-01-30 13:31:16 -08:00
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
app.AccountKeeper.SetAccount(ctx, vacc)
app.AccountKeeper.SetAccount(ctx, acc)
app.AccountKeeper.SetAccount(ctx, macc)
suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, origCoins))
suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr2, origCoins))
ctx = ctx.WithBlockTime(now.Add(12 * time.Hour))
// require the ability for a non-vesting account to delegate
2019-08-19 06:29:17 -07:00
err := app.BankKeeper.DelegateCoins(ctx, addr2, addrModule, delCoins)
2020-01-30 13:31:16 -08:00
suite.Require().NoError(err)
2020-01-30 13:31:16 -08:00
suite.Require().Equal(origCoins.Sub(delCoins), app.BankKeeper.GetAllBalances(ctx, addr2))
suite.Require().Equal(delCoins, app.BankKeeper.GetAllBalances(ctx, addrModule))
2019-06-28 13:11:27 -07:00
// require the ability for a non-vesting account to undelegate
2020-01-30 13:31:16 -08:00
suite.Require().NoError(app.BankKeeper.UndelegateCoins(ctx, addrModule, addr2, delCoins))
2020-01-30 13:31:16 -08:00
suite.Require().Equal(origCoins, app.BankKeeper.GetAllBalances(ctx, addr2))
suite.Require().True(app.BankKeeper.GetAllBalances(ctx, addrModule).Empty())
// require the ability for a vesting account to delegate
2020-01-30 13:31:16 -08:00
suite.Require().NoError(app.BankKeeper.DelegateCoins(ctx, addr1, addrModule, delCoins))
2020-01-30 13:31:16 -08:00
suite.Require().Equal(origCoins.Sub(delCoins), app.BankKeeper.GetAllBalances(ctx, addr1))
suite.Require().Equal(delCoins, app.BankKeeper.GetAllBalances(ctx, addrModule))
2019-06-28 13:11:27 -07:00
// require the ability for a vesting account to undelegate
2020-01-30 13:31:16 -08:00
suite.Require().NoError(app.BankKeeper.UndelegateCoins(ctx, addrModule, addr1, delCoins))
suite.Require().Equal(origCoins, app.BankKeeper.GetAllBalances(ctx, addr1))
suite.Require().True(app.BankKeeper.GetAllBalances(ctx, addrModule).Empty())
}
func (suite *IntegrationTestSuite) TestUndelegateCoins_Invalid() {
app, ctx := suite.app, suite.ctx
origCoins := sdk.NewCoins(newFooCoin(100))
delCoins := sdk.NewCoins(newFooCoin(50))
addr1 := sdk.AccAddress([]byte("addr1"))
addrModule := sdk.AccAddress([]byte("moduleAcc"))
macc := app.AccountKeeper.NewAccountWithAddress(ctx, addrModule) // we don't need to define an actual module account bc we just need the address for testing
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
suite.Require().Error(app.BankKeeper.UndelegateCoins(ctx, addrModule, addr1, delCoins))
app.AccountKeeper.SetAccount(ctx, macc)
suite.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, origCoins))
suite.Require().Error(app.BankKeeper.UndelegateCoins(ctx, addrModule, addr1, delCoins))
app.AccountKeeper.SetAccount(ctx, acc)
suite.Require().Error(app.BankKeeper.UndelegateCoins(ctx, addrModule, addr1, delCoins))
}
2020-01-30 13:31:16 -08:00
func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(IntegrationTestSuite))
}