Skip to content

Commit

Permalink
admin vesting
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkCherepovskyi committed Jul 18, 2024
1 parent 132d67d commit b8d555e
Show file tree
Hide file tree
Showing 26 changed files with 1,763 additions and 89 deletions.
28 changes: 28 additions & 0 deletions proto/cosmos/accumulator/admin.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
syntax = "proto3";
package cosmos.accumulator;

import "google/protobuf/any.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
import "google/api/annotations.proto";
import "gogoproto/gogo.proto";
import "cosmos/base/v1beta1/coin.proto";

option go_package = "github.com/cosmos/cosmos-sdk/x/accumulator/types";

message Admin {
string address = 1;

int64 vesting_period = 2;

cosmos.base.v1beta1.Coin reward_per_period = 3 [(gogoproto.nullable) = false];

int64 vesting_periods_count = 4;

int64 last_vesting_time = 5;

int64 vesting_counter = 6;

string denom = 7;
}


7 changes: 5 additions & 2 deletions proto/cosmos/accumulator/genesis.proto
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
syntax = "proto3";
package accumulator;
package cosmos.accumulator;

import "gogoproto/gogo.proto";
import "cosmos/accumulator/params.proto";
import "cosmos/accumulator/admin.proto";

// this line is used by starport scaffolding # genesis/proto/import

option go_package = "github.com/cosmos/cosmos-sdk/x/accumulator/types";

// GenesisState defines the bruhaccumulator module's genesis state.
// GenesisState defines the accumulator module's genesis state.
message GenesisState {
Params params = 1 [(gogoproto.nullable) = false];
repeated cosmos.accumulator.Admin admins = 2 [(gogoproto.nullable) = false];

// this line is used by starport scaffolding # genesis/proto/state
}
3 changes: 2 additions & 1 deletion proto/cosmos/accumulator/params.proto
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
syntax = "proto3";
package accumulator;
package cosmos.accumulator;

import "gogoproto/gogo.proto";
// this line is used by starport scaffolding # proto/tx/import
Expand All @@ -9,4 +9,5 @@ option go_package = "github.com/cosmos/cosmos-sdk/x/accumulator/types";

// Params defines the parameters for the module.
message Params {
string master_admin = 1;
}
2 changes: 1 addition & 1 deletion proto/cosmos/accumulator/query.proto
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
syntax = "proto3";
package accumulator;
package cosmos.accumulator;

import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
Expand Down
24 changes: 22 additions & 2 deletions proto/cosmos/accumulator/tx.proto
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
syntax = "proto3";
package accumulator;
package cosmos.accumulator;

// this line is used by starport scaffolding # proto/tx/import
import "gogoproto/gogo.proto";
Expand All @@ -10,6 +10,26 @@ import "cosmos/base/v1beta1/coin.proto";

option go_package = "github.com/cosmos/cosmos-sdk/x/accumulator/types";

service Msg {}
service Msg {
rpc AddAdmin(MsgAddAdmin) returns (MsgAddAdminResponse);
}

// this line is used by starport scaffolding # proto/tx/message

message MsgAddAdmin {
string creator = 1;

string address = 2;

cosmos.base.v1beta1.Coin reward_per_period = 3 [(gogoproto.nullable) = false];

int64 vesting_periods_count = 4;

string denom = 5;

int64 vesting_period = 6;

}

message MsgAddAdminResponse {}

1 change: 0 additions & 1 deletion proto/cosmos/nft/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ message MsgDelegate {
string address = 2;
string validator = 3;
cosmos.base.v1beta1.Coin amount = 5 [(gogoproto.nullable) = false];

}

message MsgDelegateResponse {}
Expand Down
36 changes: 36 additions & 0 deletions x/accumulator/abci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package accumulator

import (
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/accumulator/keeper"
"github.com/cosmos/cosmos-sdk/x/accumulator/types"
"time"
)

// update vesting state for each admin
func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)

for _, admin := range k.GetAllAdmins(ctx) {
if ctx.BlockTime().Unix()-admin.LastVestingTime < admin.VestingPeriod {
return
}

if admin.VestingCounter >= admin.VestingPeriodsCount {
return
}

address, _ := sdk.AccAddressFromBech32(admin.Address)
err := k.DistributeToAccount(ctx, types.AdminPoolName, sdk.NewCoins(sdk.NewCoin(admin.Denom, admin.RewardPerPeriod.Amount)), address)
if err != nil {
return
}

admin.VestingCounter++
admin.LastVestingTime = ctx.BlockTime().Unix()

k.SetAdmin(ctx, admin)
}

}
6 changes: 3 additions & 3 deletions x/accumulator/client/cli/query_derive_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ func CmdDerivePoolAddress() *cobra.Command {
Use: "derive-pool",
Short: "Derive pool addressess",
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Printf("Pool %s address: %s\n", types.AdminPoolName, keeper.GetPoolAddress(types.AdminPoolName).String())
fmt.Printf("Pool %s address: %s\n", types.ValidatorPoolName, keeper.GetPoolAddress(types.ValidatorPoolName).String())
fmt.Printf("Pool %s address: %s\n", types.NFTPoolName, keeper.GetPoolAddress(types.NFTPoolName).String())

return nil
},
}

fmt.Printf("Pool %s address: %s\n", types.AdminPoolName, keeper.GetPoolAddress(types.AdminPoolName).String())
fmt.Printf("Pool %s address: %s\n", types.ValidatorPoolName, keeper.GetPoolAddress(types.ValidatorPoolName).String())
fmt.Printf("Pool %s address: %s\n", types.NFTPoolName, keeper.GetPoolAddress(types.NFTPoolName).String())
flags.AddQueryFlagsToCmd(cmd)

return cmd
Expand Down
5 changes: 5 additions & 0 deletions x/accumulator/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ import (
// InitGenesis initializes the module's state from a provided genesis state.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) {
// This line is used by starport scaffolding # genesis/module/init
for _, admin := range genState.Admins {
k.SetAdmin(ctx, admin)
}

k.SetParams(ctx, genState.Params)
}

// ExportGenesis returns the module's exported genesis
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
genesis := types.DefaultGenesis()
genesis.Params = k.GetParams(ctx)
genesis.Admins = k.GetAllAdmins(ctx)

// this line is used by starport scaffolding # genesis/module/export
return genesis
Expand Down
13 changes: 11 additions & 2 deletions x/accumulator/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@ package accumulator
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/accumulator/keeper"
"github.com/cosmos/cosmos-sdk/x/accumulator/types"
)

// NewHandler ...
func NewHandler(k keeper.Keeper) sdk.Handler {
// this line is used by starport scaffolding # handler/msgServer
msgServer := keeper.NewMsgServerImpl(k)

return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
ctx = ctx.WithEventManager(sdk.NewEventManager())

switch msg := msg.(type) {
case *types.MsgAddAdmin:
res, err := msgServer.AddAdmin(ctx, msg)
return sdk.WrapServiceResult(ctx, res, err)

}
return nil, nil
}

// this line is used by starport scaffolding # 1
// this line is used by starport scaffolding # 1
}
}
59 changes: 59 additions & 0 deletions x/accumulator/keeper/accumulator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package keeper

import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/accumulator/types"
)

// SetAdmin set a specific deposit in the store from its index
func (k BaseKeeper) SetAdmin(ctx sdk.Context, v types.Admin) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.AdminKeyPrefix))
b := k.cdc.MustMarshal(&v)
store.Set(types.AdminKey(v.Address), b)
}

// GetAdmin returns a Admin from its index
func (k BaseKeeper) GetAdmin(
ctx sdk.Context,
address string,
) (val types.Admin, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.AdminKeyPrefix))
b := store.Get(types.AdminKey(
address,
))

if b == nil {
return val, false
}

k.cdc.MustUnmarshal(b, &val)
return val, true
}

// RemoveAdmin removes a Admin from the store
func (k BaseKeeper) RemoveAdmin(
ctx sdk.Context,
address string,
) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.AdminKeyPrefix))
store.Delete(types.AdminKey(
address,
))
}

// GetAllAdmin returns all Admin
func (k BaseKeeper) GetAllAdmins(ctx sdk.Context) (list []types.Admin) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.AdminKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte{})

defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
var val types.Admin
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}

return
}
4 changes: 4 additions & 0 deletions x/accumulator/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ type (
Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error)
DistributeToModule(ctx sdk.Context, pool string, amount sdk.Coins, receiverModule string) error
DistributeToAccount(ctx sdk.Context, pool string, amount sdk.Coins, receiver sdk.AccAddress) error
SetAdmin(ctx sdk.Context, admin types.Admin)
GetAdmin(ctx sdk.Context, address string) (val types.Admin, found bool)
RemoveAdmin(ctx sdk.Context, address string)
GetAllAdmins(ctx sdk.Context) []types.Admin
}

BaseKeeper struct {
Expand Down
37 changes: 37 additions & 0 deletions x/accumulator/keeper/msg_server.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
package keeper

import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/accumulator/types"
)

type msgServer struct {
Keeper
}

func NewMsgServerImpl(keeper Keeper) types.MsgServer {
return &msgServer{Keeper: keeper}
}

var _ types.MsgServer = msgServer{}

func (m msgServer) AddAdmin(goctx context.Context, req *types.MsgAddAdmin) (*types.MsgAddAdminResponse, error) {
ctx := sdk.UnwrapSDKContext(goctx)

if m.GetParams(ctx).MasterAdmin != req.Creator {
return nil, types.ErrForbidden
}

if _, ok := m.GetAdmin(ctx, req.Address); ok {
return nil, types.ErrAdminExists
}
newAdmin := types.Admin{
Address: req.Address,
VestingPeriod: req.VestingPeriod,
RewardPerPeriod: req.RewardPerPeriod,
VestingPeriodsCount: req.VestingPeriodsCount,
VestingCounter: 0,
LastVestingTime: 0,
Denom: req.Denom,
}

m.SetAdmin(ctx, newAdmin)

return new(types.MsgAddAdminResponse), nil
}
3 changes: 2 additions & 1 deletion x/accumulator/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ func (AppModule) ConsensusVersion() uint64 { return 1 }
func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}

// EndBlock contains the logic that is automatically triggered at the end of each block
func (am AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
EndBlocker(ctx, am.keeper)
return []abci.ValidatorUpdate{}
}
Loading

0 comments on commit b8d555e

Please sign in to comment.