-
Notifications
You must be signed in to change notification settings - Fork 2
/
farm_pool.go
205 lines (175 loc) · 7.14 KB
/
farm_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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package keeper
import (
sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
swaptypes "github.com/fibonacci-chain/fbc/x/ammswap/types"
"github.com/fibonacci-chain/fbc/x/farm/types"
)
func (k Keeper) SetFarmPool(ctx sdk.Context, pool types.FarmPool) {
store := ctx.KVStore(k.storeKey)
store.Set(types.GetFarmPoolKey(pool.Name), k.cdc.MustMarshalBinaryLengthPrefixed(pool))
}
func (k Keeper) GetFarmPool(ctx sdk.Context, poolName string) (pool types.FarmPool, found bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetFarmPoolKey(poolName))
if bz == nil {
return pool, false
}
k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &pool)
return pool, true
}
func (k Keeper) HasFarmPool(ctx sdk.Context, poolName string) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.GetFarmPoolKey(poolName))
}
func (k Keeper) DeleteFarmPool(ctx sdk.Context, poolName string) {
store := ctx.KVStore(k.storeKey)
// delete pool from whitelist
store.Delete(types.GetWhitelistMemberKey(poolName))
// delete pool key
store.Delete(types.GetFarmPoolKey(poolName))
}
// GetFarmPoolNamesForAccount gets all pool names that an account has locked coins in from the store
func (k Keeper) GetFarmPoolNamesForAccount(ctx sdk.Context, accAddr sdk.AccAddress) (poolNames types.PoolNameList) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, append(types.Address2PoolPrefix, accAddr...))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
poolNames = append(poolNames, types.SplitPoolNameFromLockInfoKey(iterator.Key()))
}
return
}
// getAccountsLockedTo gets all addresses of accounts that have locked coins in a pool
func (k Keeper) getAccountsLockedTo(ctx sdk.Context, poolName string) (lockerAddrList types.AccAddrList) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, append(types.Pool2AddressPrefix, []byte(poolName)...))
defer iterator.Close()
splitIndex := 1 + len(poolName)
for ; iterator.Valid(); iterator.Next() {
lockerAddrList = append(lockerAddrList, iterator.Key()[splitIndex:])
}
return
}
// getPoolNum gets the number of pools that already exist
func (k Keeper) getPoolNum(ctx sdk.Context) types.PoolNum {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.FarmPoolPrefix)
defer iterator.Close()
var num uint
for ; iterator.Valid(); iterator.Next() {
num++
}
return types.NewPoolNum(num)
}
// GetFarmPools gets all pools that exist currently in the store
func (k Keeper) GetFarmPools(ctx sdk.Context) (pools types.FarmPools) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.FarmPoolPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var pool types.FarmPool
k.cdc.MustUnmarshalBinaryLengthPrefixed(iterator.Value(), &pool)
pools = append(pools, pool)
}
return
}
func (k Keeper) SetAddressInFarmPool(ctx sdk.Context, poolName string, addr sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
store.Set(types.GetAddressInFarmPoolKey(poolName, addr), []byte(""))
}
// HasAddressInFarmPool check existence of the pool associated with a address
func (k Keeper) HasAddressInFarmPool(ctx sdk.Context, poolName string, addr sdk.AccAddress) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.GetAddressInFarmPoolKey(poolName, addr))
}
func (k Keeper) DeleteAddressInFarmPool(ctx sdk.Context, poolName string, addr sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetAddressInFarmPoolKey(poolName, addr))
}
func (k Keeper) SetLockInfo(ctx sdk.Context, lockInfo types.LockInfo) {
store := ctx.KVStore(k.storeKey)
store.Set(types.GetLockInfoKey(lockInfo.Owner, lockInfo.PoolName), k.cdc.MustMarshalBinaryLengthPrefixed(lockInfo))
}
func (k Keeper) GetLockInfo(ctx sdk.Context, addr sdk.AccAddress, poolName string) (info types.LockInfo, found bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetLockInfoKey(addr, poolName))
if bz == nil {
return info, false
}
k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &info)
return info, true
}
// HasLockInfo check existence of the address associated with a pool
func (k Keeper) HasLockInfo(ctx sdk.Context, addr sdk.AccAddress, poolName string) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.GetLockInfoKey(addr, poolName))
}
func (k Keeper) DeleteLockInfo(ctx sdk.Context, addr sdk.AccAddress, poolName string) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetLockInfoKey(addr, poolName))
}
// GetPoolLockedValue gets the value of locked tokens in pool priced in quote symbol
func (k Keeper) GetPoolLockedValue(ctx sdk.Context, pool types.FarmPool) sdk.Dec {
if pool.TotalValueLocked.Amount.LTE(sdk.ZeroDec()) {
return sdk.ZeroDec()
}
poolValue := sdk.ZeroDec()
params := k.GetParams(ctx)
quoteSymbol := params.QuoteSymbol
swapParams := k.swapKeeper.GetParams(ctx)
// calculate locked lpt value
if swaptypes.IsPoolToken(pool.MinLockAmount.Denom) {
poolValue = k.calculateLockedLPTValue(ctx, pool, quoteSymbol, swapParams)
} else {
poolValue = k.calculateBaseValueInQuote(ctx, pool.TotalValueLocked, quoteSymbol, swapParams)
}
return poolValue
}
func (k Keeper) calculateLockedLPTValue(
ctx sdk.Context, pool types.FarmPool, quoteSymbol string, swapParams swaptypes.Params,
) (poolValue sdk.Dec) {
token0Symbol, token1Symbol := swaptypes.SplitPoolToken(pool.MinLockAmount.Denom)
// calculate how much assets the TotalValueLocked can redeem
token0Amount, token1Amount, err := k.swapKeeper.GetRedeemableAssets(ctx, token0Symbol, token1Symbol,
pool.TotalValueLocked.Amount)
if err != nil {
return sdk.ZeroDec()
}
// calculate how much quote token the base token can swap
quote0TokenAmt := k.calculateBaseValueInQuote(ctx, token0Amount, quoteSymbol, swapParams)
quote1TokenAmt := k.calculateBaseValueInQuote(ctx, token1Amount, quoteSymbol, swapParams)
return quote0TokenAmt.Add(quote1TokenAmt)
}
// calculate base token value denominated in quote token
func (k Keeper) calculateBaseValueInQuote(
ctx sdk.Context, base sdk.SysCoin, quoteSymbol string, params swaptypes.Params,
) sdk.Dec {
// base token is quote symbol
if base.Denom == quoteSymbol {
return base.Amount
}
// calculate how much quote token the base token can swap
tokenPair, err := k.swapKeeper.GetSwapTokenPair(ctx, swaptypes.GetSwapTokenPairName(base.Denom, quoteSymbol))
if err != nil || tokenPair.BasePooledCoin.Amount.IsZero() || tokenPair.QuotePooledCoin.Amount.IsZero() {
return sdk.ZeroDec()
}
if tokenPair.QuotePooledCoin.Denom == quoteSymbol {
return base.Amount.MulTruncate(tokenPair.QuotePooledCoin.Amount).QuoTruncate(tokenPair.BasePooledCoin.Amount)
} else {
return base.Amount.MulTruncate(tokenPair.BasePooledCoin.Amount).QuoTruncate(tokenPair.QuotePooledCoin.Amount)
}
}
// Iterate over all lock infos
func (k Keeper) IterateAllLockInfos(
ctx sdk.Context, handler func(lockInfo types.LockInfo) (stop bool),
) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.Address2PoolPrefix)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var lockInfo types.LockInfo
k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &lockInfo)
if handler(lockInfo) {
break
}
}
}