Skip to content

Commit

Permalink
added hooks for gamm and lockup
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnya97 committed Apr 8, 2021
1 parent 65e39f1 commit 17dd544
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 15 deletions.
16 changes: 14 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,20 @@ func NewOsmosisApp(
// If evidence needs to be handled for the app, set routes in router here and seal
app.EvidenceKeeper = *evidenceKeeper

app.GAMMKeeper = gammkeeper.NewKeeper(appCodec, keys[gammtypes.StoreKey], app.AccountKeeper, app.BankKeeper)
app.LockupKeeper = *lockupkeeper.NewKeeper(appCodec, keys[lockuptypes.StoreKey], app.AccountKeeper, app.BankKeeper)
gammKeeper := gammkeeper.NewKeeper(appCodec, keys[gammtypes.StoreKey], app.AccountKeeper, app.BankKeeper)
lockupKeeper := lockupkeeper.NewKeeper(appCodec, keys[lockuptypes.StoreKey], app.AccountKeeper, app.BankKeeper)

app.GAMMKeeper = *gammKeeper.SetHooks(
gammtypes.NewMultiGammHooks(
// insert gamm hooks receivers here
),
)

app.LockupKeeper = *lockupKeeper.SetHooks(
lockuptypes.NewMultiLockupHooks(
// insert lockup hooks receivers here
),
)

/**** Module Options ****/

Expand Down
13 changes: 13 additions & 0 deletions x/gamm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type Keeper struct {
storeKey sdk.StoreKey
cdc codec.BinaryMarshaler

hooks types.GammHooks

// keepers
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
Expand All @@ -50,3 +52,14 @@ func NewKeeper(cdc codec.BinaryMarshaler, storeKey sdk.StoreKey, accountKeeper t
bankKeeper: bankKeeper,
}
}

// Set the gamm hooks
func (k *Keeper) SetHooks(gh types.GammHooks) *Keeper {
if k.hooks != nil {
panic("cannot set gamm hooks twice")
}

k.hooks = gh

return k
}
14 changes: 8 additions & 6 deletions x/gamm/keeper/pool_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ func (k Keeper) CreatePool(
return 0, err
}

k.hooks.AfterPoolCreated(ctx, poolAcc.GetId())

return poolAcc.GetId(), nil
}

Expand Down Expand Up @@ -246,14 +248,14 @@ func (k Keeper) JoinSwapShareAmountOut(
return sdk.Int{}, types.ErrPoolLocked
}

PoolAsset, err := poolAcc.GetPoolAsset(tokenInDenom)
poolAsset, err := poolAcc.GetPoolAsset(tokenInDenom)
if err != nil {
return sdk.Int{}, err
}

tokenInAmount = calcSingleInGivenPoolOut(
PoolAsset.Token.Amount.ToDec(),
PoolAsset.Weight.ToDec(),
poolAsset.Token.Amount.ToDec(),
poolAsset.Weight.ToDec(),
poolAcc.GetTotalShare().Amount.ToDec(),
poolAcc.GetTotalWeight().ToDec(),
shareOutAmount.ToDec(),
Expand All @@ -265,11 +267,11 @@ func (k Keeper) JoinSwapShareAmountOut(
}

if tokenInAmount.GT(tokenInMaxAmount) {
return sdk.Int{}, sdkerrors.Wrapf(types.ErrLimitMaxAmount, "%s token is larger than max amount", PoolAsset.Token.Denom)
return sdk.Int{}, sdkerrors.Wrapf(types.ErrLimitMaxAmount, "%s token is larger than max amount", poolAsset.Token.Denom)
}

PoolAsset.Token.Amount = PoolAsset.Token.Amount.Add(tokenInAmount)
err = poolAcc.SetPoolAssets([]types.PoolAsset{PoolAsset})
poolAsset.Token.Amount = poolAsset.Token.Amount.Add(tokenInAmount)
err = poolAcc.SetPoolAssets([]types.PoolAsset{poolAsset})
if err != nil {
return sdk.Int{}, err
}
Expand Down
20 changes: 20 additions & 0 deletions x/gamm/types/hooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package types

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

type GammHooks interface {
AfterPoolCreated(ctx sdk.Context, poolId uint64)
}

// combine multiple gamm hooks, all hook functions are run in array sequence
type MultiGammHooks []GammHooks

func NewMultiGammHooks(hooks ...GammHooks) MultiGammHooks {
return hooks
}

func (h MultiGammHooks) AfterPoolCreated(ctx sdk.Context, poolId uint64) {
for i := range h {
h[i].AfterPoolCreated(ctx, poolId)
}
}
23 changes: 19 additions & 4 deletions x/lockup/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,21 @@ import (
type Keeper struct {
cdc codec.Marshaler
storeKey sdk.StoreKey
ak authkeeper.AccountKeeper
bk types.BankKeeper

hooks types.LockupHooks

ak authkeeper.AccountKeeper
bk types.BankKeeper
}

// NewKeeper returns an instance of Keeper
func NewKeeper(cdc codec.Marshaler, storeKey sdk.StoreKey, ak authkeeper.AccountKeeper, bk types.BankKeeper) *Keeper {
return &Keeper{
cdc: cdc,
storeKey: storeKey,
ak: ak,
bk: bk,

ak: ak,
bk: bk,
}
}

Expand All @@ -34,6 +38,17 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}

// Set the lockup hooks
func (k *Keeper) SetHooks(lh types.LockupHooks) *Keeper {
if k.hooks != nil {
panic("cannot set lockup hooks twice")
}

k.hooks = lh

return k
}

// AdminKeeper defines a god priviledge keeper functions to remove tokens from locks and create new locks
// For the governance system of token pools, we want a "ragequit" feature
// So governance changes will take 1 week to go into effect
Expand Down
3 changes: 3 additions & 0 deletions x/lockup/keeper/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ func (k Keeper) Lock(ctx sdk.Context, lock types.PeriodLock) error {
return err
}
}

k.hooks.OnTokenLocked(ctx, lock.Owner, lock.ID, lock.Coins, lock.Duration, lock.EndTime)
return nil
}

Expand Down Expand Up @@ -306,5 +308,6 @@ func (k Keeper) Unlock(ctx sdk.Context, lock types.PeriodLock) error {
return err
}
}
k.hooks.OnTokenUnlocked(ctx, lock.Owner, lock.ID, lock.Coins, lock.Duration, lock.EndTime)
return nil
}
6 changes: 3 additions & 3 deletions x/lockup/spec/06_hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ In this section we describe the "hooks" that `lockup` module provide for other m
Upon successful coin lock/unlock, other modules might need to do few actions automatically instead of endblocker basis synchronization.

```go
onTokenLocked(address sdk.AccAddress, lockID uint64, amount sdk.Coins, lockDuration time.Duration, unlockTime time.Time)
onTokenUnlocked(address sdk.AccAddress, lockID uint64, amount sdk.Coins, lockDuration time.Duration, unlockTime time.Time)
```
OnTokenLocked(ctx sdk.Context, address sdk.AccAddress, lockID uint64, amount sdk.Coins, lockDuration time.Duration, unlockTime time.Time)
OnTokenUnlocked(ctx sdk.Context, address sdk.AccAddress, lockID uint64, amount sdk.Coins, lockDuration time.Duration, unlockTime time.Time)
```
33 changes: 33 additions & 0 deletions x/lockup/types/hooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package types

import (
"time"

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

type LockupHooks interface {
OnTokenLocked(ctx sdk.Context, address sdk.AccAddress, lockID uint64, amount sdk.Coins, lockDuration time.Duration, unlockTime time.Time)
OnTokenUnlocked(ctx sdk.Context, address sdk.AccAddress, lockID uint64, amount sdk.Coins, lockDuration time.Duration, unlockTime time.Time)
}

var _ LockupHooks = MultiLockupHooks{}

// combine multiple gamm hooks, all hook functions are run in array sequence
type MultiLockupHooks []LockupHooks

func NewMultiLockupHooks(hooks ...LockupHooks) MultiLockupHooks {
return hooks
}

func (h MultiLockupHooks) OnTokenLocked(ctx sdk.Context, address sdk.AccAddress, lockID uint64, amount sdk.Coins, lockDuration time.Duration, unlockTime time.Time) {
for i := range h {
h[i].OnTokenLocked(ctx, address, lockID, amount, lockDuration, unlockTime)
}
}

func (h MultiLockupHooks) OnTokenUnlocked(ctx sdk.Context, address sdk.AccAddress, lockID uint64, amount sdk.Coins, lockDuration time.Duration, unlockTime time.Time) {
for i := range h {
h[i].OnTokenUnlocked(ctx, address, lockID, amount, lockDuration, unlockTime)
}
}

0 comments on commit 17dd544

Please sign in to comment.