/
keeper.go
109 lines (95 loc) · 3.36 KB
/
keeper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package keeper
import (
"fmt"
"strconv"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/osmosis-labs/osmosis/v7/x/gamm/types"
)
func permContains(perms []string, perm string) bool {
for _, v := range perms {
if v == perm {
return true
}
}
return false
}
type Keeper struct {
storeKey sdk.StoreKey
cdc codec.BinaryCodec
paramSpace paramtypes.Subspace
hooks types.GammHooks
// keepers
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
distrKeeper types.DistrKeeper
}
func NewKeeper(cdc codec.BinaryCodec, storeKey sdk.StoreKey, paramSpace paramtypes.Subspace, accountKeeper types.AccountKeeper, bankKeeper types.BankKeeper, distrKeeper types.DistrKeeper) Keeper {
// Ensure that the module account are set.
moduleAddr, perms := accountKeeper.GetModuleAddressAndPermissions(types.ModuleName)
if moduleAddr == nil {
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
}
if !permContains(perms, authtypes.Minter) {
panic(fmt.Sprintf("%s module account should have the minter permission", types.ModuleName))
}
if !permContains(perms, authtypes.Burner) {
panic(fmt.Sprintf("%s module account should have the burner permission", types.ModuleName))
}
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
}
return Keeper{
storeKey: storeKey,
cdc: cdc,
paramSpace: paramSpace,
// keepers
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
distrKeeper: distrKeeper,
}
}
func (k *Keeper) createSwapEvent(ctx sdk.Context, sender sdk.AccAddress, poolId uint64, input sdk.Coins, output sdk.Coins) {
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.TypeEvtTokenSwapped,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, sender.String()),
sdk.NewAttribute(types.AttributeKeyPoolId, strconv.FormatUint(poolId, 10)),
sdk.NewAttribute(types.AttributeKeyTokensIn, input.String()),
sdk.NewAttribute(types.AttributeKeyTokensOut, output.String()),
),
})
}
func (k *Keeper) createAddLiquidityEvent(ctx sdk.Context, sender sdk.AccAddress, poolId uint64, liquidity sdk.Coins) {
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.TypeEvtPoolJoined,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, sender.String()),
sdk.NewAttribute(types.AttributeKeyPoolId, strconv.FormatUint(poolId, 10)),
sdk.NewAttribute(types.AttributeKeyTokensIn, liquidity.String()),
),
})
}
func (k *Keeper) createRemoveLiquidityEvent(ctx sdk.Context, sender sdk.AccAddress, poolId uint64, liquidity sdk.Coins) {
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.TypeEvtPoolExited,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, sender.String()),
sdk.NewAttribute(types.AttributeKeyPoolId, strconv.FormatUint(poolId, 10)),
sdk.NewAttribute(types.AttributeKeyTokensOut, liquidity.String()),
),
})
}
// 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
}