-
Notifications
You must be signed in to change notification settings - Fork 51
/
denom_liquidity.go
105 lines (94 loc) · 3.75 KB
/
denom_liquidity.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
package keeper
import (
"fmt"
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/amm/types"
)
// SetDenomLiquidity sets a specific denomLiquidity in the store from its index
func (k Keeper) SetDenomLiquidity(ctx sdk.Context, denomLiquidity types.DenomLiquidity) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.DenomLiquidityKeyPrefix))
b := k.cdc.MustMarshal(&denomLiquidity)
store.Set(types.DenomLiquidityKey(denomLiquidity.Denom), b)
}
// GetDenomLiquidity returns a denomLiquidity from its index
func (k Keeper) GetDenomLiquidity(ctx sdk.Context, denom string) (val types.DenomLiquidity, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.DenomLiquidityKeyPrefix))
b := store.Get(types.DenomLiquidityKey(denom))
if b == nil {
return val, false
}
k.cdc.MustUnmarshal(b, &val)
return val, true
}
// RemoveDenomLiquidity removes a denomLiquidity from the store
func (k Keeper) RemoveDenomLiquidity(ctx sdk.Context, denom string) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.DenomLiquidityKeyPrefix))
store.Delete(types.DenomLiquidityKey(denom))
}
// GetAllDenomLiquidity returns all denomLiquidity
func (k Keeper) GetAllDenomLiquidity(ctx sdk.Context) (list []types.DenomLiquidity) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.DenomLiquidityKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var val types.DenomLiquidity
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}
// IncreaseDenomLiquidity increases the liquidity of a denom by a certain amount
func (k Keeper) IncreaseDenomLiquidity(ctx sdk.Context, denom string, amount math.Int) error {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.DenomLiquidityKeyPrefix))
b := store.Get(types.DenomLiquidityKey(denom))
if b == nil {
return fmt.Errorf("denom not found")
}
var denomLiquidity types.DenomLiquidity
k.cdc.MustUnmarshal(b, &denomLiquidity)
denomLiquidity.Liquidity = denomLiquidity.Liquidity.Add(amount)
newB := k.cdc.MustMarshal(&denomLiquidity)
store.Set(types.DenomLiquidityKey(denom), newB)
return nil
}
// DecreaseDenomLiquidity decreases the liquidity of a denom by a certain amount
func (k Keeper) DecreaseDenomLiquidity(ctx sdk.Context, denom string, amount math.Int) error {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.DenomLiquidityKeyPrefix))
b := store.Get(types.DenomLiquidityKey(denom))
if b == nil {
return fmt.Errorf("denom not found")
}
var denomLiquidity types.DenomLiquidity
k.cdc.MustUnmarshal(b, &denomLiquidity)
if denomLiquidity.Liquidity.LT(amount) {
return fmt.Errorf("not enough liquidity")
}
denomLiquidity.Liquidity = denomLiquidity.Liquidity.Sub(amount)
newB := k.cdc.MustMarshal(&denomLiquidity)
store.Set(types.DenomLiquidityKey(denom), newB)
return nil
}
func (k Keeper) RecordTotalLiquidityIncrease(ctx sdk.Context, coins sdk.Coins) {
for _, coin := range coins {
_, found := k.GetDenomLiquidity(ctx, coin.Denom)
if !found {
k.SetDenomLiquidity(ctx, types.DenomLiquidity{Denom: coin.Denom, Liquidity: sdk.ZeroInt()})
}
k.IncreaseDenomLiquidity(ctx, coin.Denom, coin.Amount)
}
}
func (k Keeper) RecordTotalLiquidityDecrease(ctx sdk.Context, coins sdk.Coins) {
for _, coin := range coins {
_, found := k.GetDenomLiquidity(ctx, coin.Denom)
if !found {
k.SetDenomLiquidity(ctx, types.DenomLiquidity{Denom: coin.Denom, Liquidity: sdk.ZeroInt()})
}
err := k.DecreaseDenomLiquidity(ctx, coin.Denom, coin.Amount)
if err != nil {
k.Logger(ctx).Error(err.Error())
panic(err)
}
}
}