/
pool.go
167 lines (129 loc) · 3.8 KB
/
pool.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
gogotypes "github.com/gogo/protobuf/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/osmosis-labs/osmosis/x/gamm/types"
)
func (k Keeper) MarshalPool(pool types.PoolI) ([]byte, error) {
return k.cdc.MarshalInterface(pool)
}
func (k Keeper) UnmarshalPool(bz []byte) (types.PoolI, error) {
var acc types.PoolI
return acc, k.cdc.UnmarshalInterface(bz, &acc)
}
func (k Keeper) GetPool(ctx sdk.Context, poolId uint64) (types.PoolI, error) {
store := ctx.KVStore(k.storeKey)
poolKey := types.GetKeyPrefixPools(poolId)
if !store.Has(poolKey) {
return nil, fmt.Errorf("pool with ID %d does not exist", poolId)
}
bz := store.Get(poolKey)
pool, err := k.UnmarshalPool(bz)
if err != nil {
return nil, err
}
pool.PokeTokenWeights(ctx.BlockTime())
return pool, nil
}
func (k Keeper) iterator(ctx sdk.Context, prefix []byte) sdk.Iterator {
store := ctx.KVStore(k.storeKey)
return sdk.KVStorePrefixIterator(store, prefix)
}
func (k Keeper) GetPools(ctx sdk.Context) (res []types.PoolI, err error) {
iter := k.iterator(ctx, types.KeyPrefixPools)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
bz := iter.Value()
pool, err := k.UnmarshalPool(bz)
if err != nil {
return nil, err
}
pool.PokeTokenWeights(ctx.BlockTime())
res = append(res, pool)
}
return
}
func (k Keeper) SetPool(ctx sdk.Context, pool types.PoolI) error {
bz, err := k.MarshalPool(pool)
if err != nil {
return err
}
store := ctx.KVStore(k.storeKey)
poolKey := types.GetKeyPrefixPools(pool.GetId())
store.Set(poolKey, bz)
return nil
}
// newPool is an internal function that creates a new Pool object with the provided
// parameters, initial assets, and future governor.
func (k Keeper) newPool(ctx sdk.Context, poolParams types.PoolParams, assets []types.PoolAsset, futureGovernor string) (types.PoolI, error) {
poolId := k.GetNextPoolNumber(ctx)
pool, err := types.NewPool(poolId, poolParams, assets, futureGovernor, ctx.BlockTime())
if err != nil {
return nil, err
}
acc := k.accountKeeper.GetAccount(ctx, pool.GetAddress())
if acc != nil {
return nil, sdkerrors.Wrapf(types.ErrPoolAlreadyExist, "pool %d already exist", poolId)
}
err = k.SetPool(ctx, pool)
if err != nil {
return nil, err
}
// Create and save corresponding module account to the account keeper
acc = k.accountKeeper.NewAccount(ctx, authtypes.NewModuleAccount(
authtypes.NewBaseAccountWithAddress(
pool.GetAddress(),
),
pool.GetAddress().String(),
))
k.accountKeeper.SetAccount(ctx, acc)
return pool, nil
}
// SetNextPoolNumber sets next pool number
func (k Keeper) SetNextPoolNumber(ctx sdk.Context, poolNumber uint64) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinaryBare(&gogotypes.UInt64Value{Value: poolNumber})
store.Set(types.KeyNextGlobalPoolNumber, bz)
}
// GetNextPoolNumber returns the next pool number
func (k Keeper) GetNextPoolNumber(ctx sdk.Context) uint64 {
var poolNumber uint64
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.KeyNextGlobalPoolNumber)
if bz == nil {
// initialize the pool id numbers
poolNumber = 1
} else {
val := gogotypes.UInt64Value{}
err := k.cdc.UnmarshalBinaryBare(bz, &val)
if err != nil {
panic(err)
}
poolNumber = val.GetValue()
}
k.SetNextPoolNumber(ctx, poolNumber+1)
return poolNumber
}
func (k Keeper) getPoolAndInOutAssets(
ctx sdk.Context, poolId uint64,
tokenInDenom string,
tokenOutDenom string) (
pool types.PoolI,
inAsset types.PoolAsset,
outAsset types.PoolAsset,
err error,
) {
pool, err = k.GetPool(ctx, poolId)
if err != nil {
return
}
inAsset, err = pool.GetPoolAsset(tokenInDenom)
if err != nil {
return
}
outAsset, err = pool.GetPoolAsset(tokenOutDenom)
return
}