Skip to content

Commit

Permalink
Merge PR: farm rest api (#578)
Browse files Browse the repository at this point in the history
* update farm dashboard api

* update farm first pool api

* update farm api

* update farm api
  • Loading branch information
ilovers authored Jan 21, 2021
1 parent bdbde8b commit 4a06840
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 28 deletions.
56 changes: 34 additions & 22 deletions x/backend/keeper/farm_querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keeper

import (
"encoding/json"
"fmt"
"sort"
"time"

Expand Down Expand Up @@ -178,7 +177,7 @@ func queryFarmDashboard(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) (
}
}
// response
var responseList types.FarmResponseList
responseList := types.FarmResponseList{}
hasWhiteList := false
for _, poolName := range stakedPools {
farmPool, found := keeper.farmKeeper.GetFarmPool(ctx, poolName)
Expand All @@ -191,12 +190,13 @@ func queryFarmDashboard(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) (
// calculate staked in dollars and pool ratio
poolRatio := sdk.ZeroDec()
userStaked := sdk.ZeroDec()
userStakedDollars := sdk.ZeroDec()
totalStakedDollars := keeper.farmKeeper.GetPoolLockedValue(ctx, farmPool)
if lockInfo, found := keeper.farmKeeper.GetLockInfo(ctx, address, poolName); found {
if !farmPool.TotalValueLocked.Amount.IsZero() {
poolRatio = lockInfo.Amount.Amount.Quo(farmPool.TotalValueLocked.Amount)
//userStaked = poolRatio.Mul(totalStakedDollars)
userStaked = lockInfo.Amount.Amount
userStakedDollars = poolRatio.Mul(totalStakedDollars)
}
}

Expand All @@ -219,15 +219,16 @@ func queryFarmDashboard(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) (
unclaimed = earning.AmountYielded
unclaimedInDollars = calculateSysCoinsInDollars(ctx, keeper, unclaimed)
}
farmDetails := generateFarmDetails(claimed, earning.AmountYielded)
farmDetails := generateFarmDetails(claimed, unclaimed)
totalFarmed := calculateTotalFarmed(claimedInDollars, unclaimedInDollars)

status := getFarmPoolStatus(startAt, finishAt, farmPool)
responseList = append(responseList, types.FarmPoolResponse{
PoolName: farmPool.Name,
LockSymbol: farmPool.MinLockAmount.Denom,
YieldSymbol: farmPool.YieldedTokenInfos[0].RemainingAmount.Denom,
TotalStaked: userStaked,
TotalStaked: userStakedDollars,
UserStaked: userStaked,
PoolRatio: poolRatio,
StartAt: startAt,
FinishAt: finishAt,
Expand Down Expand Up @@ -408,17 +409,20 @@ func queryFarmStakedInfo(ctx sdk.Context, req abci.RequestQuery, keeper Keeper)
}

func generateFarmDetails(claimed sdk.SysCoins, unClaimed sdk.SysCoins) []types.FarmInfo {
var farmDetails []types.FarmInfo
claimedMap := make(map[string]sdk.Dec, len(claimed))
demonMap := make(map[string]struct{})
for _, coin := range claimed {
claimedMap[coin.Denom] = coin.Amount
demonMap[coin.Denom] = struct{}{}
}
for _, coin := range unClaimed {
claimedAmount := claimedMap[coin.Denom]
demonMap[coin.Denom] = struct{}{}
}

var farmDetails []types.FarmInfo
for demon := range demonMap {
farmDetails = append(farmDetails, types.FarmInfo{
Symbol: coin.Denom,
UnClaimed: coin.Amount,
Claimed: claimedAmount,
Symbol: demon,
UnClaimed: unClaimed.AmountOf(demon),
Claimed: claimed.AmountOf(demon),
})
}
return farmDetails
Expand Down Expand Up @@ -554,17 +558,7 @@ func queryFarmFirstPool(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) (
return nil, common.ErrUnMarshalJSONFailed(err.Error())
}

// invalid params
if queryParams.ClaimHeight < ctx.BlockHeight() {
return nil, common.ErrInvalidParam(fmt.Sprintf("claim_height %d is less than current height %d",
queryParams.ClaimHeight, ctx.BlockHeight()))
}
timeNow := ctx.BlockTime().Unix()
if timeNow < queryParams.StakeAt {
return nil, common.ErrInvalidParam(fmt.Sprintf("time now %d is less than state_at %d",
timeNow, queryParams.StakeAt))
}

// query farm pool
farmPool, found := keeper.farmKeeper.GetFarmPool(ctx, queryParams.PoolName)
if !found {
Expand Down Expand Up @@ -608,6 +602,24 @@ func queryFarmFirstPool(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) (
if !farmPool.TotalValueLocked.IsZero() {
firstPool.EstimatedFarm = farmAmount.Mul(firstPool.AccountStaked.Quo(farmPool.TotalValueLocked.Amount))
}

if firstPool.EstimatedFarm.IsZero() {
claimInfos := keeper.Orm.GetAccountClaimedByPool(queryParams.Address, queryParams.PoolName)
totalClaimed := sdk.ZeroDec()
for _, claimInfo := range claimInfos {
claimed, err := sdk.ParseDecCoins(claimInfo.Claimed)
if err != nil {
continue
}
totalClaimed = totalClaimed.Add(claimed.AmountOf(common.NativeToken))
}
earning, err := keeper.farmKeeper.GetEarnings(ctx, farmPool.Name, address)
unclaimed := sdk.ZeroDec()
if err == nil {
unclaimed = earning.AmountYielded.AmountOf(common.NativeToken)
}
firstPool.EstimatedFarm = totalClaimed.Add(unclaimed)
}
}

// response
Expand Down
6 changes: 0 additions & 6 deletions x/backend/keeper/swap_querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,9 @@ func getSwapCreateLiquidityTokens(ctx sdk.Context, keeper Keeper) []string {
func getSwapAddLiquidityTokens(ctx sdk.Context, keeper Keeper, baseTokenName string) []string {
var tokens []string

// whitelist map
whitelistMap := getSwapWhitelistMap(keeper)
// all swap token pairs
swapTokenPairs := keeper.swapKeeper.GetSwapTokenPairs(ctx)
for _, swapTokenPair := range swapTokenPairs {
// check if in whitelist
if _, found := whitelistMap[swapTokenPair.TokenPairName()]; !found {
continue
}
if baseTokenName == "" {
tokens = append(tokens, swapTokenPair.BasePooledCoin.Denom)
tokens = append(tokens, swapTokenPair.QuotePooledCoin.Denom)
Expand Down
8 changes: 8 additions & 0 deletions x/backend/orm/farm.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,11 @@ func (orm *ORM) GetAccountClaimInfos(address string) []types.ClaimInfo {
query.Order("timestamp asc").Find(&claimInfos)
return claimInfos
}

func (orm *ORM) GetAccountClaimedByPool(address string, poolName string) []types.ClaimInfo {
var claimInfos []types.ClaimInfo
query := orm.db.Model(types.ClaimInfo{}).Where("address = ? and pool_name = ?", address, poolName)

query.Order("timestamp asc").Find(&claimInfos)
return claimInfos
}
1 change: 1 addition & 0 deletions x/backend/types/farm.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type FarmPoolResponse struct {
LockSymbol string `json:"lock_symbol"`
YieldSymbol string `json:"yield_symbol"`
TotalStaked sdk.Dec `json:"total_staked"`
UserStaked sdk.Dec `json:"user_staked"`
StartAt int64 `json:"start_at"`
FinishAt int64 `json:"finish_at"`
PoolRate sdk.SysCoins `json:"pool_rate"`
Expand Down

0 comments on commit 4a06840

Please sign in to comment.