From 4a06840c2bea5786f5cef4fb47efba9ad7765800 Mon Sep 17 00:00:00 2001 From: Evan Han Date: Thu, 21 Jan 2021 23:50:30 +0800 Subject: [PATCH] Merge PR: farm rest api (#578) * update farm dashboard api * update farm first pool api * update farm api * update farm api --- x/backend/keeper/farm_querier.go | 56 +++++++++++++++++++------------- x/backend/keeper/swap_querier.go | 6 ---- x/backend/orm/farm.go | 8 +++++ x/backend/types/farm.go | 1 + 4 files changed, 43 insertions(+), 28 deletions(-) diff --git a/x/backend/keeper/farm_querier.go b/x/backend/keeper/farm_querier.go index c0c24c6c2b..82f40cbd15 100644 --- a/x/backend/keeper/farm_querier.go +++ b/x/backend/keeper/farm_querier.go @@ -2,7 +2,6 @@ package keeper import ( "encoding/json" - "fmt" "sort" "time" @@ -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) @@ -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) } } @@ -219,7 +219,7 @@ 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) @@ -227,7 +227,8 @@ func queryFarmDashboard(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ( 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, @@ -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 @@ -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 { @@ -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 diff --git a/x/backend/keeper/swap_querier.go b/x/backend/keeper/swap_querier.go index a47e6d8555..938674cc85 100644 --- a/x/backend/keeper/swap_querier.go +++ b/x/backend/keeper/swap_querier.go @@ -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) diff --git a/x/backend/orm/farm.go b/x/backend/orm/farm.go index 120a422f8f..2ec49e7c6c 100644 --- a/x/backend/orm/farm.go +++ b/x/backend/orm/farm.go @@ -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 +} diff --git a/x/backend/types/farm.go b/x/backend/types/farm.go index 5cd3c57db8..4fac92ec7b 100644 --- a/x/backend/types/farm.go +++ b/x/backend/types/farm.go @@ -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"`