From 5b69911591ab08b279ce8e5001bfd5ddda666ad0 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Tue, 18 Jan 2022 16:10:56 -0500 Subject: [PATCH] refactor: remove x/auth vesting dependency in x/bank (#10967) --- x/bank/keeper/keeper.go | 5 ++--- x/bank/keeper/keeper_test.go | 5 ++--- x/bank/keeper/view.go | 5 ++--- x/bank/types/vesting.go | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 x/bank/types/vesting.go diff --git a/x/bank/keeper/keeper.go b/x/bank/keeper/keeper.go index 31ed533c7..85a0176e7 100644 --- a/x/bank/keeper/keeper.go +++ b/x/bank/keeper/keeper.go @@ -11,7 +11,6 @@ import ( sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/types/query" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - vestexported "github.com/cosmos/cosmos-sdk/x/auth/vesting/exported" "github.com/cosmos/cosmos-sdk/x/bank/types" paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" ) @@ -471,7 +470,7 @@ func (k BaseKeeper) trackDelegation(ctx sdk.Context, addr sdk.AccAddress, balanc return sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "account %s does not exist", addr) } - vacc, ok := acc.(vestexported.VestingAccount) + vacc, ok := acc.(types.VestingAccount) if ok { // TODO: return error on account.TrackDelegation vacc.TrackDelegation(ctx.BlockHeader().Time, balance, amt) @@ -488,7 +487,7 @@ func (k BaseKeeper) trackUndelegation(ctx sdk.Context, addr sdk.AccAddress, amt return sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "account %s does not exist", addr) } - vacc, ok := acc.(vestexported.VestingAccount) + vacc, ok := acc.(types.VestingAccount) if ok { // TODO: return error on account.TrackUndelegation vacc.TrackUndelegation(amt) diff --git a/x/bank/keeper/keeper_test.go b/x/bank/keeper/keeper_test.go index 3a8a8f64e..87338b4fb 100644 --- a/x/bank/keeper/keeper_test.go +++ b/x/bank/keeper/keeper_test.go @@ -15,7 +15,6 @@ import ( "github.com/cosmos/cosmos-sdk/types/query" 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/exported" vesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" "github.com/cosmos/cosmos-sdk/x/bank/keeper" "github.com/cosmos/cosmos-sdk/x/bank/testutil" @@ -888,7 +887,7 @@ func (suite *IntegrationTestSuite) TestDelegateCoins() { // require that delegated vesting amount is equal to what was delegated with DelegateCoins acc = app.AccountKeeper.GetAccount(ctx, addr1) - vestingAcc, ok := acc.(exported.VestingAccount) + vestingAcc, ok := acc.(types.VestingAccount) suite.Require().True(ok) suite.Require().Equal(delCoins, vestingAcc.GetDelegatedVesting()) } @@ -968,7 +967,7 @@ func (suite *IntegrationTestSuite) TestUndelegateCoins() { // require that delegated vesting amount is completely empty, since they were completely undelegated acc = app.AccountKeeper.GetAccount(ctx, addr1) - vestingAcc, ok := acc.(exported.VestingAccount) + vestingAcc, ok := acc.(types.VestingAccount) suite.Require().True(ok) suite.Require().Empty(vestingAcc.GetDelegatedVesting()) } diff --git a/x/bank/keeper/view.go b/x/bank/keeper/view.go index 6f178bb7f..c6feb7278 100644 --- a/x/bank/keeper/view.go +++ b/x/bank/keeper/view.go @@ -10,7 +10,6 @@ import ( storetypes "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - vestexported "github.com/cosmos/cosmos-sdk/x/auth/vesting/exported" "github.com/cosmos/cosmos-sdk/x/bank/types" ) @@ -170,7 +169,7 @@ func (k BaseViewKeeper) IterateAllBalances(ctx sdk.Context, cb func(sdk.AccAddre func (k BaseViewKeeper) LockedCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins { acc := k.ak.GetAccount(ctx, addr) if acc != nil { - vacc, ok := acc.(vestexported.VestingAccount) + vacc, ok := acc.(types.VestingAccount) if ok { return vacc.LockedCoins(ctx.BlockTime()) } @@ -220,7 +219,7 @@ func (k BaseViewKeeper) ValidateBalance(ctx sdk.Context, addr sdk.AccAddress) er return fmt.Errorf("account balance of %s is invalid", balances) } - vacc, ok := acc.(vestexported.VestingAccount) + vacc, ok := acc.(types.VestingAccount) if ok { ogv := vacc.GetOriginalVesting() if ogv.IsAnyGT(balances) { diff --git a/x/bank/types/vesting.go b/x/bank/types/vesting.go new file mode 100644 index 000000000..a288d4ec8 --- /dev/null +++ b/x/bank/types/vesting.go @@ -0,0 +1,32 @@ +package types + +import ( + "time" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// VestingAccount defines an interface used for account vesting. +type VestingAccount interface { + // LockedCoins returns the set of coins that are not spendable (i.e. locked), + // defined as the vesting coins that are not delegated. + // + // To get spendable coins of a vesting account, first the total balance must + // be retrieved and the locked tokens can be subtracted from the total balance. + // Note, the spendable balance can be negative. + LockedCoins(blockTime time.Time) sdk.Coins + + // TrackDelegation performs internal vesting accounting necessary when + // delegating from a vesting account. It accepts the current block time, the + // delegation amount and balance of all coins whose denomination exists in + // the account's original vesting balance. + TrackDelegation(blockTime time.Time, balance, amount sdk.Coins) + + // TrackUndelegation performs internal vesting accounting necessary when a + // vesting account performs an undelegation. + TrackUndelegation(amount sdk.Coins) + + GetOriginalVesting() sdk.Coins + GetDelegatedFree() sdk.Coins + GetDelegatedVesting() sdk.Coins +}