/
keeper.go
91 lines (72 loc) · 2.72 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
package cosmwasmpool
import (
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/osmosis-labs/osmosis/v16/x/cosmwasmpool/types"
poolmanagertypes "github.com/osmosis-labs/osmosis/v16/x/poolmanager/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)
type Keeper struct {
cdc codec.BinaryCodec
storeKey sdk.StoreKey
paramSpace paramtypes.Subspace
// keepers
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
poolmanagerKeeper types.PoolManagerKeeper
contractKeeper types.ContractKeeper
wasmKeeper types.WasmKeeper
}
func NewKeeper(cdc codec.BinaryCodec, storeKey sdk.StoreKey, paramSpace paramtypes.Subspace, accountKeeper types.AccountKeeper, bankKeeper types.BankKeeper) *Keeper {
// set KeyTable if it has not already been set
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
}
return &Keeper{cdc: cdc, storeKey: storeKey, paramSpace: paramSpace, accountKeeper: accountKeeper, bankKeeper: bankKeeper}
}
// GetParams returns the total set of cosmwasmpool parameters.
func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) {
k.paramSpace.GetParamSet(ctx, ¶ms)
return params
}
// SetParams sets the total set of cosmwasmpool parameters.
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramSpace.SetParamSet(ctx, ¶ms)
}
// Set the poolmanager keeper.
func (k *Keeper) SetPoolManagerKeeper(poolmanagerKeeper types.PoolManagerKeeper) {
k.poolmanagerKeeper = poolmanagerKeeper
}
// Set the contract keeper.
func (k *Keeper) SetContractKeeper(contractKeeper types.ContractKeeper) {
k.contractKeeper = contractKeeper
}
// Set the wasm keeper.
func (k *Keeper) SetWasmKeeper(wasmKeeper types.WasmKeeper) {
k.wasmKeeper = wasmKeeper
}
// asCosmwasmPool converts a poolI to a CosmWasmExtension.
func (k *Keeper) asCosmwasmPool(poolI poolmanagertypes.PoolI) (types.CosmWasmExtension, error) {
cosmwasmPool, ok := poolI.(types.CosmWasmExtension)
if !ok {
return nil, types.InvalidPoolTypeError{
ActualPool: poolI,
}
}
cosmwasmPool.SetWasmKeeper(k.wasmKeeper)
return cosmwasmPool, nil
}
// GetCodeIdByPoolId returns the contract address and code id associated with the given pool.
func (k Keeper) GetCodeIdByPoolId(ctx sdk.Context, poolId uint64) (sdk.AccAddress, uint64, error) {
pool, err := k.GetPoolById(ctx, poolId)
if err != nil {
return nil, 0, err
}
contractAddress := sdk.MustAccAddressFromBech32(pool.GetContractAddress())
contractInfo := k.wasmKeeper.GetContractInfo(ctx, contractAddress)
if contractInfo == nil {
return nil, 0, fmt.Errorf("code id for pool id (%d) not found", poolId)
}
return contractAddress, contractInfo.CodeID, nil
}