Skip to content

Commit

Permalink
feat(vesting): update module
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Jan 17, 2024
1 parent ab068fc commit 96f970c
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 24 deletions.
17 changes: 13 additions & 4 deletions x/vesting/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package vesting
import (
"encoding/json"

"google.golang.org/grpc"

"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"

abci "github.com/cometbft/cometbft/abci/types"

"cosmossdk.io/core/appmodule"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand All @@ -20,7 +23,8 @@ import (
)

var (
_ module.AppModule = AppModule{}
_ appmodule.AppModule = AppModule{}
_ appmodule.HasServices = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
)

Expand All @@ -34,7 +38,7 @@ func (AppModuleBasic) Name() string {
return types.ModuleName
}

// RegisterCodec registers the module's types with the given codec.
// RegisterLegacyAminoCodec registers the module's types with the given codec.
func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
types.RegisterLegacyAminoCodec(cdc)
}
Expand Down Expand Up @@ -78,6 +82,10 @@ type AppModule struct {
bankKeeper types.BankKeeper
}

func (am AppModule) IsOnePerModuleType() {}

func (am AppModule) IsAppModule() {}

func NewAppModule(ak keeper.AccountKeeper, bk types.BankKeeper) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{},
Expand All @@ -90,8 +98,9 @@ func NewAppModule(ak keeper.AccountKeeper, bk types.BankKeeper) AppModule {
func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}

// RegisterServices registers module services.
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), NewMsgServerImpl(am.accountKeeper, am.bankKeeper))
func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error {
types.RegisterMsgServer(registrar, NewMsgServerImpl(am.accountKeeper, am.bankKeeper))
return nil
}

// InitGenesis performs a no-op.
Expand Down
2 changes: 1 addition & 1 deletion x/vesting/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package vesting
import (
"context"

"github.com/armon/go-metrics"
"github.com/hashicorp/go-metrics"

errorsmod "cosmossdk.io/errors"

Expand Down
13 changes: 10 additions & 3 deletions x/vesting/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import (
"testing"
"time"

"github.com/cosmos/cosmos-sdk/runtime"

authcodec "github.com/cosmos/cosmos-sdk/x/auth/codec"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"

tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
tmtime "github.com/cometbft/cometbft/types/time"

storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
Expand Down Expand Up @@ -40,8 +45,9 @@ type VestingTestSuite struct {
}

func (s *VestingTestSuite) SetupTest() {
key := sdk.NewKVStoreKey(authtypes.StoreKey)
testCtx := testutil.DefaultContextWithDB(s.T(), key, sdk.NewTransientStoreKey("transient_test"))
key := storetypes.NewKVStoreKey(authtypes.StoreKey)
storeService := runtime.NewKVStoreService(key)
testCtx := testutil.DefaultContextWithDB(s.T(), key, storetypes.NewTransientStoreKey("transient_test"))
s.ctx = testCtx.Ctx.WithBlockHeader(tmproto.Header{Time: tmtime.Now()})
encCfg := moduletestutil.MakeTestEncodingConfig()

Expand All @@ -51,9 +57,10 @@ func (s *VestingTestSuite) SetupTest() {
s.bankKeeper = vestingtestutil.NewMockBankKeeper(ctrl)
s.accountKeeper = authkeeper.NewAccountKeeper(
encCfg.Codec,
key,
storeService,
authtypes.ProtoBaseAccount,
maccPerms,
authcodec.NewBech32Codec("cosmos"),
"cosmos",
authtypes.NewModuleAddress("gov").String(),
)
Expand Down
10 changes: 5 additions & 5 deletions x/vesting/testutil/expected_keepers_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions x/vesting/types/expected_keepers.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package types

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
)

// BankKeeper defines the expected interface contract the vesting module requires
// for creating vesting accounts with funds.
type BankKeeper interface {
IsSendEnabledCoins(ctx sdk.Context, coins ...sdk.Coin) error
SendCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error
IsSendEnabledCoins(ctx context.Context, coins ...sdk.Coin) error
SendCoins(ctx context.Context, fromAddr, toAddr sdk.AccAddress, amt sdk.Coins) error
BlockedAddr(addr sdk.AccAddress) bool
}
17 changes: 9 additions & 8 deletions x/vesting/types/vesting_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"sigs.k8s.io/yaml"

"cosmossdk.io/math"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down Expand Up @@ -71,7 +72,7 @@ func (bva *BaseVestingAccount) TrackDelegation(balance, vestingCoins, amount sdk
// compute x and y per the specification, where:
// X := min(max(V - DV, 0), D)
// Y := D - X
x := sdk.MinInt(sdk.MaxInt(vestingAmt.Sub(delVestingAmt), sdk.ZeroInt()), coin.Amount)
x := math.MinInt(math.MaxInt(vestingAmt.Sub(delVestingAmt), math.ZeroInt()), coin.Amount)
y := coin.Amount.Sub(x)

if !x.IsZero() {
Expand Down Expand Up @@ -108,8 +109,8 @@ func (bva *BaseVestingAccount) TrackUndelegation(amount sdk.Coins) {
// compute x and y per the specification, where:
// X := min(DF, D)
// Y := min(DV, D - X)
x := sdk.MinInt(delegatedFree, coin.Amount)
y := sdk.MinInt(delegatedVesting, coin.Amount.Sub(x))
x := math.MinInt(delegatedFree, coin.Amount)
y := math.MinInt(delegatedVesting, coin.Amount.Sub(x))

if !x.IsZero() {
xCoin := sdk.NewCoin(coin.Denom, x)
Expand Down Expand Up @@ -243,10 +244,10 @@ func (cva ContinuousVestingAccount) GetVestedCoins(blockTime time.Time) sdk.Coin
// calculate the vesting scalar
x := blockTime.Unix() - cva.StartTime
y := cva.EndTime - cva.StartTime
s := sdk.NewDec(x).Quo(sdk.NewDec(y))
s := math.LegacyNewDec(x).Quo(math.LegacyNewDec(y))

for _, ovc := range cva.OriginalVesting {
vestedAmt := sdk.NewDecFromInt(ovc.Amount).Mul(s).RoundInt()
vestedAmt := math.LegacyNewDecFromInt(ovc.Amount).Mul(s).RoundInt()
vestedCoins = append(vestedCoins, sdk.NewCoin(ovc.Denom, vestedAmt))
}

Expand Down Expand Up @@ -429,7 +430,7 @@ func (pva PeriodicVestingAccount) Validate() error {
if endTime != pva.EndTime {
return errors.New("vesting end time does not match length of all vesting periods")
}
if !originalVesting.IsEqual(pva.OriginalVesting) {
if !originalVesting.Equal(pva.OriginalVesting) {
return errors.New("original vesting coins does not match the sum of all coins in vesting periods")
}

Expand Down Expand Up @@ -652,10 +653,10 @@ func (cva CliffVestingAccount) GetVestedCoins(blockTime time.Time) sdk.Coins {
// calculate the vesting scalar
x := blockTime.Unix() - cva.StartTime
y := cva.EndTime - cva.StartTime
s := sdk.NewDec(x).Quo(sdk.NewDec(y))
s := math.LegacyNewDec(x).Quo(math.LegacyNewDec(y))

for _, ovc := range cva.OriginalVesting {
vestedAmt := sdk.NewDecFromInt(ovc.Amount).Mul(s).RoundInt()
vestedAmt := math.LegacyNewDecFromInt(ovc.Amount).Mul(s).RoundInt()
vestedCoins = append(vestedCoins, sdk.NewCoin(ovc.Denom, vestedAmt))
}

Expand Down
2 changes: 1 addition & 1 deletion x/vesting/types/vesting_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
tmtime "github.com/cometbft/cometbft/types/time"

storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down

0 comments on commit 96f970c

Please sign in to comment.