github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/farm/keeper/farm_pool.go (about) 1 package keeper 2 3 import ( 4 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 5 swaptypes "github.com/fibonacci-chain/fbc/x/ammswap/types" 6 "github.com/fibonacci-chain/fbc/x/farm/types" 7 ) 8 9 func (k Keeper) SetFarmPool(ctx sdk.Context, pool types.FarmPool) { 10 store := ctx.KVStore(k.storeKey) 11 store.Set(types.GetFarmPoolKey(pool.Name), k.cdc.MustMarshalBinaryLengthPrefixed(pool)) 12 } 13 14 func (k Keeper) GetFarmPool(ctx sdk.Context, poolName string) (pool types.FarmPool, found bool) { 15 store := ctx.KVStore(k.storeKey) 16 bz := store.Get(types.GetFarmPoolKey(poolName)) 17 if bz == nil { 18 return pool, false 19 } 20 k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &pool) 21 return pool, true 22 } 23 24 func (k Keeper) HasFarmPool(ctx sdk.Context, poolName string) bool { 25 store := ctx.KVStore(k.storeKey) 26 return store.Has(types.GetFarmPoolKey(poolName)) 27 } 28 29 func (k Keeper) DeleteFarmPool(ctx sdk.Context, poolName string) { 30 store := ctx.KVStore(k.storeKey) 31 // delete pool from whitelist 32 store.Delete(types.GetWhitelistMemberKey(poolName)) 33 // delete pool key 34 store.Delete(types.GetFarmPoolKey(poolName)) 35 } 36 37 // GetFarmPoolNamesForAccount gets all pool names that an account has locked coins in from the store 38 func (k Keeper) GetFarmPoolNamesForAccount(ctx sdk.Context, accAddr sdk.AccAddress) (poolNames types.PoolNameList) { 39 store := ctx.KVStore(k.storeKey) 40 iterator := sdk.KVStorePrefixIterator(store, append(types.Address2PoolPrefix, accAddr...)) 41 defer iterator.Close() 42 43 for ; iterator.Valid(); iterator.Next() { 44 poolNames = append(poolNames, types.SplitPoolNameFromLockInfoKey(iterator.Key())) 45 } 46 47 return 48 } 49 50 // getAccountsLockedTo gets all addresses of accounts that have locked coins in a pool 51 func (k Keeper) getAccountsLockedTo(ctx sdk.Context, poolName string) (lockerAddrList types.AccAddrList) { 52 store := ctx.KVStore(k.storeKey) 53 iterator := sdk.KVStorePrefixIterator(store, append(types.Pool2AddressPrefix, []byte(poolName)...)) 54 defer iterator.Close() 55 56 splitIndex := 1 + len(poolName) 57 for ; iterator.Valid(); iterator.Next() { 58 lockerAddrList = append(lockerAddrList, iterator.Key()[splitIndex:]) 59 } 60 61 return 62 } 63 64 // getPoolNum gets the number of pools that already exist 65 func (k Keeper) getPoolNum(ctx sdk.Context) types.PoolNum { 66 store := ctx.KVStore(k.storeKey) 67 iterator := sdk.KVStorePrefixIterator(store, types.FarmPoolPrefix) 68 defer iterator.Close() 69 var num uint 70 for ; iterator.Valid(); iterator.Next() { 71 num++ 72 } 73 74 return types.NewPoolNum(num) 75 } 76 77 // GetFarmPools gets all pools that exist currently in the store 78 func (k Keeper) GetFarmPools(ctx sdk.Context) (pools types.FarmPools) { 79 store := ctx.KVStore(k.storeKey) 80 iterator := sdk.KVStorePrefixIterator(store, types.FarmPoolPrefix) 81 defer iterator.Close() 82 83 for ; iterator.Valid(); iterator.Next() { 84 var pool types.FarmPool 85 k.cdc.MustUnmarshalBinaryLengthPrefixed(iterator.Value(), &pool) 86 pools = append(pools, pool) 87 } 88 89 return 90 } 91 92 func (k Keeper) SetAddressInFarmPool(ctx sdk.Context, poolName string, addr sdk.AccAddress) { 93 store := ctx.KVStore(k.storeKey) 94 store.Set(types.GetAddressInFarmPoolKey(poolName, addr), []byte("")) 95 } 96 97 // HasAddressInFarmPool check existence of the pool associated with a address 98 func (k Keeper) HasAddressInFarmPool(ctx sdk.Context, poolName string, addr sdk.AccAddress) bool { 99 store := ctx.KVStore(k.storeKey) 100 return store.Has(types.GetAddressInFarmPoolKey(poolName, addr)) 101 } 102 103 func (k Keeper) DeleteAddressInFarmPool(ctx sdk.Context, poolName string, addr sdk.AccAddress) { 104 store := ctx.KVStore(k.storeKey) 105 store.Delete(types.GetAddressInFarmPoolKey(poolName, addr)) 106 } 107 108 func (k Keeper) SetLockInfo(ctx sdk.Context, lockInfo types.LockInfo) { 109 store := ctx.KVStore(k.storeKey) 110 store.Set(types.GetLockInfoKey(lockInfo.Owner, lockInfo.PoolName), k.cdc.MustMarshalBinaryLengthPrefixed(lockInfo)) 111 } 112 113 func (k Keeper) GetLockInfo(ctx sdk.Context, addr sdk.AccAddress, poolName string) (info types.LockInfo, found bool) { 114 store := ctx.KVStore(k.storeKey) 115 bz := store.Get(types.GetLockInfoKey(addr, poolName)) 116 if bz == nil { 117 return info, false 118 } 119 k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &info) 120 return info, true 121 } 122 123 // HasLockInfo check existence of the address associated with a pool 124 func (k Keeper) HasLockInfo(ctx sdk.Context, addr sdk.AccAddress, poolName string) bool { 125 store := ctx.KVStore(k.storeKey) 126 return store.Has(types.GetLockInfoKey(addr, poolName)) 127 } 128 129 func (k Keeper) DeleteLockInfo(ctx sdk.Context, addr sdk.AccAddress, poolName string) { 130 store := ctx.KVStore(k.storeKey) 131 store.Delete(types.GetLockInfoKey(addr, poolName)) 132 } 133 134 // GetPoolLockedValue gets the value of locked tokens in pool priced in quote symbol 135 func (k Keeper) GetPoolLockedValue(ctx sdk.Context, pool types.FarmPool) sdk.Dec { 136 if pool.TotalValueLocked.Amount.LTE(sdk.ZeroDec()) { 137 return sdk.ZeroDec() 138 } 139 140 poolValue := sdk.ZeroDec() 141 params := k.GetParams(ctx) 142 quoteSymbol := params.QuoteSymbol 143 swapParams := k.swapKeeper.GetParams(ctx) 144 // calculate locked lpt value 145 if swaptypes.IsPoolToken(pool.MinLockAmount.Denom) { 146 poolValue = k.calculateLockedLPTValue(ctx, pool, quoteSymbol, swapParams) 147 } else { 148 poolValue = k.calculateBaseValueInQuote(ctx, pool.TotalValueLocked, quoteSymbol, swapParams) 149 } 150 return poolValue 151 } 152 153 func (k Keeper) calculateLockedLPTValue( 154 ctx sdk.Context, pool types.FarmPool, quoteSymbol string, swapParams swaptypes.Params, 155 ) (poolValue sdk.Dec) { 156 token0Symbol, token1Symbol := swaptypes.SplitPoolToken(pool.MinLockAmount.Denom) 157 158 // calculate how much assets the TotalValueLocked can redeem 159 token0Amount, token1Amount, err := k.swapKeeper.GetRedeemableAssets(ctx, token0Symbol, token1Symbol, 160 pool.TotalValueLocked.Amount) 161 if err != nil { 162 return sdk.ZeroDec() 163 } 164 165 // calculate how much quote token the base token can swap 166 quote0TokenAmt := k.calculateBaseValueInQuote(ctx, token0Amount, quoteSymbol, swapParams) 167 quote1TokenAmt := k.calculateBaseValueInQuote(ctx, token1Amount, quoteSymbol, swapParams) 168 return quote0TokenAmt.Add(quote1TokenAmt) 169 } 170 171 // calculate base token value denominated in quote token 172 func (k Keeper) calculateBaseValueInQuote( 173 ctx sdk.Context, base sdk.SysCoin, quoteSymbol string, params swaptypes.Params, 174 ) sdk.Dec { 175 // base token is quote symbol 176 if base.Denom == quoteSymbol { 177 return base.Amount 178 } 179 // calculate how much quote token the base token can swap 180 tokenPair, err := k.swapKeeper.GetSwapTokenPair(ctx, swaptypes.GetSwapTokenPairName(base.Denom, quoteSymbol)) 181 if err != nil || tokenPair.BasePooledCoin.Amount.IsZero() || tokenPair.QuotePooledCoin.Amount.IsZero() { 182 return sdk.ZeroDec() 183 } 184 if tokenPair.QuotePooledCoin.Denom == quoteSymbol { 185 return base.Amount.MulTruncate(tokenPair.QuotePooledCoin.Amount).QuoTruncate(tokenPair.BasePooledCoin.Amount) 186 } else { 187 return base.Amount.MulTruncate(tokenPair.BasePooledCoin.Amount).QuoTruncate(tokenPair.QuotePooledCoin.Amount) 188 } 189 } 190 191 // Iterate over all lock infos 192 func (k Keeper) IterateAllLockInfos( 193 ctx sdk.Context, handler func(lockInfo types.LockInfo) (stop bool), 194 ) { 195 store := ctx.KVStore(k.storeKey) 196 iter := sdk.KVStorePrefixIterator(store, types.Address2PoolPrefix) 197 defer iter.Close() 198 for ; iter.Valid(); iter.Next() { 199 var lockInfo types.LockInfo 200 k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &lockInfo) 201 if handler(lockInfo) { 202 break 203 } 204 } 205 }