diff --git a/staker/_GET_qa_external_reward.gnoA b/staker/_GET_qa_external_reward.gnoA deleted file mode 100644 index 2d5c6f31..00000000 --- a/staker/_GET_qa_external_reward.gnoA +++ /dev/null @@ -1,216 +0,0 @@ -package staker - -import ( - "std" - "strconv" - "time" - - u256 "gno.land/p/gnoswap/uint256" - "gno.land/r/gnoswap/v1/consts" -) - -type currentExternalInfo struct { - height int64 - time int64 - externalIncentives []ExternalIncentive -} - -func printExternalInfo(poolPath string) { - println("***********************") - println("> height:", std.GetHeight()) - println("> time:", time.Now().Unix()) - println("[ START ] GET_EXTERNAL INCENTIVE") - pool, ok := pools.Get(poolPath) - if !ok { - println(" > pool not found") - return - } - if pool.IsExternallyIncentivizedPool(uint64(std.GetHeight())) { - pool.incentives.byHeight.Iterate("", "", func(key string, value interface{}) bool { - incentive := value.(*ExternalIncentive) - println(" > incentiveId:", incentive.incentiveId) - println(" > targetPoolPath:", incentive.targetPoolPath) - println(" > rewardToken:", incentive.rewardToken) - println(" > rewardAmount:", strconv.FormatUint(incentive.rewardAmount, 10)) - println(" > startTimestamp:", strconv.FormatInt(incentive.startTimestamp, 10)) - println(" > endTimestamp:", strconv.FormatInt(incentive.endTimestamp, 10)) - rewardPerBlockU256 := u256.MustFromDecimal(strconv.FormatUint(incentive.rewardPerBlock, 10)) - q96 := u256.MustFromDecimal(consts.Q96) - rewardPerBlockX96 := u256.Zero().Mul(rewardPerBlockU256, q96) - println(" > rewardPerBlockX96:", rewardPerBlockX96.ToString()) - println(" > rewardLeft:", strconv.FormatUint(incentive.rewardLeft, 10)) - println(" > refundee:", incentive.refundee.String()) - return false - }) - } else { - println(" > pool is not externally incentivized") - } - println("[ END ] GET_EXTERNAL INCENTIVE") -} - -type ApiExternalDebugInfo struct { - Height int64 `json:"height"` - Time int64 `json:"time"` - Position []ApiExternalDebugPosition `json:"pool"` -} - -type ApiExternalDebugPosition struct { - LpTokenId uint64 `json:"lpTokenId"` - StakedHeight int64 `json:"stakedHeight"` - StakedTimestamp int64 `json:"stakedTimestamp"` - Incentive []ApiExternalDebugIncentive `json:"incentive"` -} - -type ApiExternalDebugIncentive struct { - PoolPath string `json:"poolPath"` - IncentiveId string `json:"incentiveId"` - RewardToken string `json:"rewardToken"` - RewardAmount string `json:"rewardAmount"` - RewardLeft string `json:"rewardLeft"` - StartTimestamp int64 `json:"startTimestamp"` - EndTimestamp int64 `json:"endTimestamp"` - RewardPerBlockX96 string `json:"rewardPerBlockX96"` - RewardPerBlock string `json:"rewardPerBlock"` - Refundee std.Address `json:"refundee"` - // FROM positionExternal -> externalRewards - tokenAmountX96 *u256.Uint `json:"tokenAmountX96"` - tokenAmount uint64 `json:"tokenAmount"` - tokenAmountFull uint64 `json:"tokenAmountFull"` - tokenAmountToGive uint64 `json:"tokenAmountToGive"` - // FROM externalWarmUpAmount - full30 uint64 `json:"full30"` - give30 uint64 `json:"give30"` - full50 uint64 `json:"full50"` - give50 uint64 `json:"give50"` - full70 uint64 `json:"full70"` - give70 uint64 `json:"give70"` - full100 uint64 `json:"full100"` -} - -// -//func GetPrintExternalInfo() string { -// // TODO: LIMIT ONLY ABCI_QUERY CAN CALL THIS -// /* -// updateExternalIncentiveLeftAmount() -// -// externalDebug := ApiExternalDebugInfo{} -// externalDebug.Height = std.GetHeight() -// externalDebug.Time = time.Now().Unix() -// -// externalPositions := []ApiExternalDebugPosition{} -// for lpTokenId, externals := range positionExternal { -// externalPosition := ApiExternalDebugPosition{} -// externalPosition.LpTokenId = lpTokenId -// deposit, _ := deposits.get(lpTokenId) -// externalPosition.StakedHeight = deposit.stakeHeight -// externalPosition.StakedTimestamp = deposit.stakeTimestamp -// -// externalIncentives := []ApiExternalDebugIncentive{} -// for incentiveId, external := range externals { -// externalIncentive := ApiExternalDebugIncentive{} -// -// externalIncentive.PoolPath = external.poolPath -// externalIncentive.RewardToken = external.tokenPath -// -// incentive := incentives[incentiveId] -// externalIncentive.IncentiveId = incentiveId -// externalIncentive.RewardAmount = incentive.rewardAmount.ToString() -// externalIncentive.RewardLeft = incentive.rewardLeft.ToString() -// externalIncentive.StartTimestamp = incentive.startTimestamp -// externalIncentive.EndTimestamp = incentive.endTimestamp -// externalIncentive.RewardPerBlockX96 = incentive.rewardPerBlockX96.ToString() -// externalIncentive.RewardPerBlock = new(u256.Uint).Div(incentive.rewardPerBlockX96, u256.MustFromDecimal(consts.Q96)).ToString() -// externalIncentive.Refundee = incentive.refundee -// -// externalIncentive.tokenAmountX96 = external.tokenAmountX96 -// -// externalWarmUpAmount, exist := positionsExternalWarmUpAmount[lpTokenId][incentiveId] -// if !exist { -// continue -// } -// fullAmount := externalWarmUpAmount.full30 + externalWarmUpAmount.full50 + externalWarmUpAmount.full70 + externalWarmUpAmount.full100 -// toGive := externalWarmUpAmount.give30 + externalWarmUpAmount.give50 + externalWarmUpAmount.give70 + externalWarmUpAmount.full100 -// -// externalIncentive.full30 = externalWarmUpAmount.full30 -// externalIncentive.give30 = externalWarmUpAmount.give30 -// externalIncentive.full50 = externalWarmUpAmount.full50 -// externalIncentive.give50 = externalWarmUpAmount.give50 -// externalIncentive.full70 = externalWarmUpAmount.full70 -// externalIncentive.give70 = externalWarmUpAmount.give70 -// externalIncentive.full100 = externalWarmUpAmount.full100 -// -// externalIncentive.tokenAmountFull = fullAmount -// externalIncentive.tokenAmountToGive = toGive -// -// externalIncentives = append(externalIncentives, externalIncentive) -// } -// externalPosition.Incentive = externalIncentives -// -// externalPositions = append(externalPositions, externalPosition) -// } -// -// externalDebug.Position = externalPositions -// -// // MARSHAL -// node := json.ObjectNode("", map[string]*json.Node{ -// "height": json.NumberNode("", float64(externalDebug.Height)), -// "time": json.NumberNode("", float64(externalDebug.Time)), -// "position": json.ArrayNode("", makeExternalPositionsNode(externalDebug.Position)), -// }) -// -// b, err := json.Marshal(node) -// if err != nil { -// return "JSON MARSHAL ERROR" -// } -// -// return string(b) -// */ -// -// panic("SHOULD BE REIMPLEMENTED BEFORE MERGING") -//} -// -//func makeExternalPositionsNode(positions []ApiExternalDebugPosition) []*json.Node { -// externalPositions := make([]*json.Node, 0) -// -// for _, externalPosition := range positions { -// incentives := make([]*json.Node, 0) -// for _, incentive := range externalPosition.Incentive { -// deposit, _ := deposits.get(externalPosition.LpTokenId) -// _max := common.I64Max(incentive.StartTimestamp, deposit.stakeTimestamp) -// stakedOrExternalDuration := (time.Now().Unix() - _max) / consts.BLOCK_GENERATION_INTERVAL -// -// incentives = append(incentives, json.ObjectNode("", map[string]*json.Node{ -// "poolPath": json.StringNode("poolPath", incentive.PoolPath), -// "rewardToken": json.StringNode("rewardToken", incentive.RewardToken), -// "rewardAmount": json.StringNode("rewardAmount", incentive.RewardAmount), -// "startTimestamp": json.NumberNode("startTimestamp", float64(incentive.StartTimestamp)), -// "endTimestamp": json.NumberNode("endTimestamp", float64(incentive.EndTimestamp)), -// "rewardPerBlockX96": json.StringNode("rewardPerBlockX96", incentive.RewardPerBlockX96), -// "stakedOrExternalDuration": json.NumberNode("stakedOrExternalDuration", float64(stakedOrExternalDuration)), -// "rewardPerBlock": json.StringNode("rewardPerBlock", incentive.RewardPerBlock), -// "refundee": json.StringNode("refundee", incentive.Refundee.String()), -// // "tokenAmountX96": json.StringNode("tokenAmountX96", incentive.tokenAmountX96.ToString()), -// // "tokenAmount": json.NumberNode("tokenAmount", float64(new(u256.Uint).Div(incentive.tokenAmountX96, _q96).Uint64())), -// "tokenAmountFull": json.NumberNode("tokenAmountFull", float64(incentive.tokenAmountFull)), -// "tokenAmountToGive": json.NumberNode("tokenAmountToGive", float64(incentive.tokenAmountToGive)), -// // -// "full30": json.NumberNode("full30", float64(incentive.full30)), -// "give30": json.NumberNode("give30", float64(incentive.give30)), -// "full50": json.NumberNode("full50", float64(incentive.full50)), -// "give50": json.NumberNode("give50", float64(incentive.give50)), -// "full70": json.NumberNode("full70", float64(incentive.full70)), -// "give70": json.NumberNode("give70", float64(incentive.give70)), -// "full100": json.NumberNode("full100", float64(incentive.full100)), -// })) -// } -// -// externalPositions = append(externalPositions, json.ObjectNode("", map[string]*json.Node{ -// "lpTokenId": json.NumberNode("lpTokenId", float64(externalPosition.LpTokenId)), -// "stakedHeight": json.NumberNode("stakedHeight", float64(externalPosition.StakedHeight)), -// "stakedTimestamp": json.NumberNode("stakedTimestamp", float64(externalPosition.StakedTimestamp)), -// "incentive": json.ArrayNode("", incentives), -// })) -// } -// -// return externalPositions -//} diff --git a/staker/_GET_qa_internal_emission.gnoFIXME b/staker/_GET_qa_internal_emission.gnoFIXME deleted file mode 100644 index f7a8f3e8..00000000 --- a/staker/_GET_qa_internal_emission.gnoFIXME +++ /dev/null @@ -1,241 +0,0 @@ -package staker - -import ( - "std" - "time" - - "gno.land/p/demo/json" - - "gno.land/r/gnoswap/v1/consts" - "gno.land/r/gnoswap/v1/gns" - - // en "gno.land/r/gnoswap/v1/emission" -) - -// DEBUG INTERNAL (GNS EMISSION) -type currentInfo struct { - height int64 - time int64 - gnsStaker uint64 - gnsDevOps uint64 - gnsCommunityPool uint64 - gnsGovStaker uint64 - gnsProtocolFee uint64 - gnsADMIN uint64 -} - -func getCurrentInfo() currentInfo { - return currentInfo{ - height: std.GetHeight(), - time: time.Now().Unix(), - gnsStaker: gns.BalanceOf(a2u(consts.STAKER_ADDR)), - gnsDevOps: gns.BalanceOf(a2u(consts.DEV_OPS)), - gnsCommunityPool: gns.BalanceOf(a2u(consts.COMMUNITY_POOL_ADDR)), - gnsGovStaker: gns.BalanceOf(a2u(consts.GOV_STAKER_ADDR)), - gnsProtocolFee: gns.BalanceOf(a2u(consts.PROTOCOL_FEE_ADDR)), - gnsADMIN: gns.BalanceOf(a2u(consts.ADMIN)), - } -} - -func printInfo(prev currentInfo) currentInfo { - curr := getCurrentInfo() - /* - println("***********************") - println("> height:", curr.height) - println("> height inc by:", curr.height-prev.height) - println("> time:", curr.time) - println("> time inc by:", curr.time-prev.time) - println("GNS BALANCE CHANGE") - println("> staker_bal\t\t", curr.gnsStaker) - println("> staker_chg\t\t", int64(curr.gnsStaker-prev.gnsStaker)) - println("> dev ops\t\t", curr.gnsDevOps) - println("> dev ops_chg\t\t", int(curr.gnsDevOps-prev.gnsDevOps)) - println("> community pool_bal\t", curr.gnsCommunityPool) - println("> community pool_chg\t", int(curr.gnsCommunityPool-prev.gnsCommunityPool)) - println("> gov_staker_bal\t\t", curr.gnsGovStaker) - println("> gov_staker_chg\t\t", int(curr.gnsGovStaker-prev.gnsGovStaker)) - println("> protocol fee_bal\t", curr.gnsProtocolFee) - println("> protocol fee_chg\t", int(curr.gnsProtocolFee-prev.gnsProtocolFee)) - println("> ADMIN_bal\t\t", curr.gnsADMIN) - println("> ADMIN_chg\t\t", int(curr.gnsADMIN-prev.gnsADMIN)) - println("GNS POOL") - for k, v := range poolGns { - println("> poolPath:", k, "amount:", v) - } - - - println("GNS POSITION") - for k, v := range positionGns { - posWarmCalc := positionsInternalWarmUpAmount[k] - println("> tokenId:", k, "amount:", v, "warmUp:", getRewardRatio(curr.height-deposits[k].stakeHeight)) - println("> 100%", "full", posWarmCalc.full100, "give", posWarmCalc.full100) - println("> 70%", "full", posWarmCalc.full70, "give", posWarmCalc.give70) - println("> 50%", "full", posWarmCalc.full50, "give", posWarmCalc.give50) - println("> 30%", "full", posWarmCalc.full30, "give", posWarmCalc.give30) - } - */ - panic("SHOULD BE REIMPLEMENTED BEFORE MERGING") - - return curr -} - -type ApiEmissionDebugInfo struct { - Height int64 `json:"height"` - Time int64 `json:"time"` - GnsStaker uint64 `json:"gnsStaker"` - GnsDevOps uint64 `json:"gnsDevOps"` - GnsCommunityPool uint64 `json:"gnsCommunityPool"` - GnsGovStaker uint64 `json:"gnsGovStaker"` - GnsProtocolFee uint64 `json:"gnsProtocolFee"` - GnsADMIN uint64 `json:"gnsADMIN"` - Pool []ApiEmissionDebugPool `json:"pool"` -} - -type ApiEmissionDebugPool struct { - PoolPath string `json:"poolPath"` - Tier uint64 `json:"tier"` - NumPoolInSameTier uint64 `json:"numPoolInSameTier"` - PoolReward uint64 `json:"poolReward"` - Position []ApiEmissionDebugPosition `json:"position"` -} - -type ApiEmissionDebugPosition struct { - LpTokenId uint64 `json:"lpTokenId"` - StakedHeight int64 `json:"stakedHeight"` - StakedTimestamp int64 `json:"stakedTimestamp"` - StakedDuration int64 `json:"stakedDuration"` - FullAmount uint64 `json:"fullAmount"` - Ratio uint64 `json:"ratio"` - RatioAmount uint64 `json:"ratioAmount"` -} - -func GetPrintInfo() string { - emissionDebug := ApiEmissionDebugInfo{} - emissionDebug.Height = std.GetHeight() - emissionDebug.Time = time.Now().Unix() - emissionDebug.GnsStaker = gns.BalanceOf(a2u(consts.STAKER_ADDR)) - emissionDebug.GnsDevOps = gns.BalanceOf(a2u(consts.DEV_OPS)) - emissionDebug.GnsCommunityPool = gns.BalanceOf(a2u(consts.COMMUNITY_POOL_ADDR)) - emissionDebug.GnsGovStaker = gns.BalanceOf(a2u(consts.GOV_STAKER_ADDR)) - emissionDebug.GnsProtocolFee = gns.BalanceOf(a2u(consts.PROTOCOL_FEE_ADDR)) - emissionDebug.GnsADMIN = gns.BalanceOf(a2u(consts.ADMIN)) - - poolTiers.Iter(func(poolPath string, internalTier InternalTier) { - tier := internalTier.tier - pool := ApiEmissionDebugPool{} - pool.PoolPath = poolPath - pool.Tier = tier - - numTier1, numTier2, numTier3 := getNumPoolTiers(t) - if tier == 1 { - pool.NumPoolInSameTier = numTier1 - } else if tier == 2 { - pool.NumPoolInSameTier = numTier2 - } else if tier == 3 { - pool.NumPoolInSameTier = numTier3 - } - - for lpTokenId, deposit := range deposits { - if deposit.targetPoolPath == poolPath { - position := ApiEmissionDebugPosition{} - position.LpTokenId = lpTokenId - position.StakedHeight = deposit.stakeHeight - position.StakedTimestamp = deposit.stakeTimestamp - position.StakedDuration = emissionDebug.Height - deposit.stakeHeight - - position.Ratio = getRewardRatio(t, position.StakedDuration) - pool.Position = append(pool.Position, position) - } - } - - emissionDebug.Pool = append(emissionDebug.Pool, pool) - }) - - node := json.ObjectNode("", map[string]*json.Node{ - "height": json.NumberNode("", float64(emissionDebug.Height)), - "time": json.NumberNode("", float64(emissionDebug.Time)), - "gns": json.ObjectNode("", map[string]*json.Node{ - "staker": json.NumberNode("", float64(emissionDebug.GnsStaker)), - "devOps": json.NumberNode("", float64(emissionDebug.GnsDevOps)), - "communityPool": json.NumberNode("", float64(emissionDebug.GnsCommunityPool)), - "govStaker": json.NumberNode("", float64(emissionDebug.GnsGovStaker)), - "protocolFee": json.NumberNode("", float64(emissionDebug.GnsProtocolFee)), - "GnoswapAdmin": json.NumberNode("", float64(emissionDebug.GnsADMIN)), - }), - "pool": json.ArrayNode("", makePoolsNode(emissionDebug.Pool)), - }) - - b, err := json.Marshal(node) - if err != nil { - return "JSON MARSHAL ERROR" - } - - return string(b) -} - -func makePoolsNode(emissionPool []ApiEmissionDebugPool) []*json.Node { - pools := make([]*json.Node, 0) - - poolTiers.Iter(func(poolPath string, internalTier InternalTier) { - numTier1, numTier2, numTier3 := getNumPoolTiers(t) - numPoolSameTier := uint64(0) - tier := internalTier.tier - if tier == 1 { - numPoolSameTier = numTier1 - } else if tier == 2 { - numPoolSameTier = numTier2 - } else if tier == 3 { - numPoolSameTier = numTier3 - } - - pools = append(pools, json.ObjectNode("", map[string]*json.Node{ - "poolPath": json.StringNode("poolPath", poolPath), - "startTimestamp": json.NumberNode("startTimestamp", float64(internalTier.startTimestamp)), - "tier": json.NumberNode("tier", float64(tier)), - "numPoolSameTier": json.NumberNode("numPoolSameTier", float64(numPoolSameTier)), - "position": json.ArrayNode("", makePositionsNode(poolPath)), - })) - }) - - return pools -} - -func makePositionsNode(poolPath string) []*json.Node { - /* - positions := make([]*json.Node, 0) - - for lpTokenId, deposit := range deposits { - if deposit.targetPoolPath == poolPath { - stakedDuration := std.GetHeight() - deposit.stakeHeight - ratio := getRewardRatio(stakedDuration) - - internalWarmUpAmount, exist := positionsInternalWarmUpAmount[lpTokenId] - if !exist { - continue - } - fullAmount := internalWarmUpAmount.full30 + internalWarmUpAmount.full50 + internalWarmUpAmount.full70 + internalWarmUpAmount.full100 - warmUpAmount := internalWarmUpAmount.give30 + internalWarmUpAmount.give50 + internalWarmUpAmount.give70 + internalWarmUpAmount.full100 - - positions = append(positions, json.ObjectNode("", map[string]*json.Node{ - "lpTokenId": json.NumberNode("lpTokenId", float64(lpTokenId)), - "stakedHeight": json.NumberNode("stakedHeight", float64(deposit.stakeHeight)), - "stakedTimestamp": json.NumberNode("stakedTimestamp", float64(deposit.stakeTimestamp)), - "stakedDuration": json.NumberNode("stakedDuration", float64(stakedDuration)), - "fullAmount": json.NumberNode("fullAmount", float64(fullAmount)), - "ratio": json.NumberNode("ratio", float64(ratio)), - "warmUpAmount": json.NumberNode("warmUpAmount", float64(warmUpAmount)), - "full30": json.NumberNode("full30", float64(internalWarmUpAmount.full30)), - "give30": json.NumberNode("give30", float64(internalWarmUpAmount.give30)), - "full50": json.NumberNode("full50", float64(internalWarmUpAmount.full50)), - "give50": json.NumberNode("give50", float64(internalWarmUpAmount.give50)), - "full70": json.NumberNode("full70", float64(internalWarmUpAmount.full70)), - "give70": json.NumberNode("give70", float64(internalWarmUpAmount.give70)), - "full100": json.NumberNode("full100", float64(internalWarmUpAmount.full100)), - })) - } - } - - return positions - */ - panic("SHOULD BE REIMPLEMENTED BEFORE MERGING") -} diff --git a/staker/_RPC_api_incentive.gnoA b/staker/_RPC_api_incentive.gnoA deleted file mode 100644 index 69e873e6..00000000 --- a/staker/_RPC_api_incentive.gnoA +++ /dev/null @@ -1,712 +0,0 @@ -package staker - -import ( - "std" - "time" - - "gno.land/p/demo/json" - "gno.land/p/demo/ufmt" - - u256 "gno.land/p/gnoswap/uint256" - - en "gno.land/r/gnoswap/v1/emission" - pl "gno.land/r/gnoswap/v1/pool" - - "gno.land/r/gnoswap/v1/consts" - - "gno.land/r/gnoswap/v1/gns" -) - -type RewardToken struct { - PoolPath string `json:"poolPath"` - RewardsTokenList []string `json:"rewardsTokenList"` -} - -type ApiExternalIncentive struct { - IncentiveId string `json:"incentiveId"` - PoolPath string `json:"poolPath"` - RewardToken string `json:"rewardToken"` - RewardAmount string `json:"rewardAmount"` - RewardLeft string `json:"rewardLeft"` - StartTimestamp int64 `json:"startTimestamp"` - EndTimestamp int64 `json:"endTimestamp"` - Active bool `json:"active"` - Refundee string `json:"refundee"` - CreatedHeight int64 `json:"createdHeight"` - DepositGnsAmount uint64 `json:"depositGnsAmount"` -} - -type ApiInternalIncentive struct { - PoolPath string `json:"poolPath"` - Tier uint64 `json:"tier"` - StartTimestamp int64 `json:"startTimestamp"` - RewardPerBlock string `json:"rewardPerBlock"` -} - -func ApiGetRewardTokens() string { - en.MintAndDistributeGns() - if consts.EMISSION_REFACTORED { - CalcPoolPositionRefactor() - } else { - CalcPoolPosition() - } - - rewardTokens := []RewardToken{} - - poolList := pl.PoolGetPoolList() - for _, poolPath := range poolList { - thisPoolRewardTokens := []string{} - - // HANDLE INTERNAL - if isExistPoolTiers(poolPath) { - thisPoolRewardTokens = append(thisPoolRewardTokens, consts.GNS_PATH) - } - - // HANDLE EXTERNAL - ictvList, exists := poolIncentives.Get(poolPath) - if !exists { - continue - } - - for _, incentiveId := range ictvList { - ictv, exist := incentives.Get(incentiveId) - if !exist { - continue - } - if ictv.RewardToken() == "" { - continue - } - thisPoolRewardTokens = append(thisPoolRewardTokens, ictv.RewardToken()) - } - - if len(thisPoolRewardTokens) == 0 { - continue - } - - rewardTokens = append(rewardTokens, RewardToken{ - PoolPath: poolPath, - RewardsTokenList: thisPoolRewardTokens, - }) - } - - // STAT NODE - _stat := json.ObjectNode("", map[string]*json.Node{ - "height": json.NumberNode("height", float64(std.GetHeight())), - "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), - }) - - // RESPONSE (ARRAY) NODE - responses := json.ArrayNode("", []*json.Node{}) - for _, rewardToken := range rewardTokens { - _rewardTokenNode := json.ObjectNode("", map[string]*json.Node{ - "poolPath": json.StringNode("poolPath", rewardToken.PoolPath), - "tokens": json.ArrayNode("tokens", makeRewardTokensArray(rewardToken.RewardsTokenList)), - }) - responses.AppendArray(_rewardTokenNode) - } - - node := json.ObjectNode("", map[string]*json.Node{ - "stat": _stat, - "response": responses, - }) - - b, err := json.Marshal(node) - if err != nil { - panic(err.Error()) - } - - return string(b) -} - -func ApiGetRewardTokensByPoolPath(targetPoolPath string) string { - en.MintAndDistributeGns() - if consts.EMISSION_REFACTORED { - CalcPoolPositionRefactor() - } else { - CalcPoolPosition() - } - - rewardTokens := []RewardToken{} - - poolList := pl.PoolGetPoolList() - for _, poolPath := range poolList { - if poolPath != targetPoolPath { - continue - } - - thisPoolRewardTokens := []string{} - - // HANDLE INTERNAL - if isExistPoolTiers(poolPath) { - thisPoolRewardTokens = append(thisPoolRewardTokens, consts.GNS_PATH) - } - - // HANDLE EXTERNAL - ictvList, exists := poolIncentives.Get(poolPath) - if !exists { - continue - } - - for _, incentiveId := range ictvList { - ictv, exist := incentives.Get(incentiveId) - if !exist { - continue - } - thisPoolRewardTokens = append(thisPoolRewardTokens, ictv.RewardToken()) - } - - rewardTokens = append(rewardTokens, RewardToken{ - PoolPath: poolPath, - RewardsTokenList: thisPoolRewardTokens, - }) - } - - // STAT NODE - _stat := json.ObjectNode("", map[string]*json.Node{ - "height": json.NumberNode("height", float64(std.GetHeight())), - "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), - }) - - // RESPONSE (ARRAY) NODE - responses := json.ArrayNode("", []*json.Node{}) - for _, rewardToken := range rewardTokens { - _rewardTokenNode := json.ObjectNode("", map[string]*json.Node{ - "poolPath": json.StringNode("poolPath", rewardToken.PoolPath), - "tokens": json.ArrayNode("tokens", makeRewardTokensArray(rewardToken.RewardsTokenList)), - }) - responses.AppendArray(_rewardTokenNode) - } - - node := json.ObjectNode("", map[string]*json.Node{ - "stat": _stat, - "response": responses, - }) - - b, err := json.Marshal(node) - if err != nil { - panic(err.Error()) - } - - return string(b) -} - -func ApiGetExternalIncentives() string { - en.MintAndDistributeGns() - if consts.EMISSION_REFACTORED { - CalcPoolPositionRefactor() - } else { - CalcPoolPosition() - } - - updateExternalIncentiveLeftAmount() - - apiExternalIncentives := []ApiExternalIncentive{} - - for incentiveId, incentive := range incentives { - apiExternalIncentives = append(apiExternalIncentives, ApiExternalIncentive{ - IncentiveId: incentiveId, - PoolPath: incentive.targetPoolPath, - RewardToken: incentive.rewardToken, - RewardAmount: incentive.rewardAmount.ToString(), - RewardLeft: incentive.rewardLeft.ToString(), - StartTimestamp: incentive.startTimestamp, - EndTimestamp: incentive.endTimestamp, - Refundee: incentive.refundee.String(), - CreatedHeight: incentive.createdHeight, - DepositGnsAmount: incentive.depositGnsAmount, - }) - } - - // STAT NODE - _stat := json.ObjectNode("", map[string]*json.Node{ - "height": json.NumberNode("height", float64(std.GetHeight())), - "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), - }) - - // RESPONSE (ARRAY) NODE - responses := json.ArrayNode("", []*json.Node{}) - for _, incentive := range apiExternalIncentives { - active := false - if time.Now().Unix() >= incentive.StartTimestamp && time.Now().Unix() <= incentive.EndTimestamp { - active = true - } - - _incentiveNode := json.ObjectNode("", map[string]*json.Node{ - "incentiveId": json.StringNode("incentiveId", incentive.IncentiveId), - "poolPath": json.StringNode("poolPath", incentive.PoolPath), - "rewardToken": json.StringNode("rewardToken", incentive.RewardToken), - "rewardAmount": json.StringNode("rewardAmount", incentive.RewardAmount), - "rewardLeft": json.StringNode("rewardLeft", incentive.RewardLeft), - "startTimestamp": json.NumberNode("startTimestamp", float64(incentive.StartTimestamp)), - "endTimestamp": json.NumberNode("endTimestamp", float64(incentive.EndTimestamp)), - "active": json.BoolNode("active", active), - "refundee": json.StringNode("refundee", incentive.Refundee), - "createdHeight": json.NumberNode("createdHeight", float64(incentive.CreatedHeight)), - "depositGnsAmount": json.NumberNode("depositGnsAmount", float64(incentive.DepositGnsAmount)), - }) - responses.AppendArray(_incentiveNode) - } - - // RETURN - node := json.ObjectNode("", map[string]*json.Node{ - "stat": _stat, - "response": responses, - }) - - b, err := json.Marshal(node) - if err != nil { - panic(err.Error()) - } - - return string(b) -} - -func ApiGetExternalIncentiveById(incentiveId string) string { - en.MintAndDistributeGns() - if consts.EMISSION_REFACTORED { - CalcPoolPositionRefactor() - } else { - CalcPoolPosition() - } - - updateExternalIncentiveLeftAmount() - - apiExternalIncentives := []ApiExternalIncentive{} - - incentive, exist := incentives.Get(incentiveId) - if !exist { - panic(addDetailToError( - errDataNotFound, - ufmt.Sprintf("incentive(%s) not found", incentiveId), - )) - } - - apiExternalIncentives = append(apiExternalIncentives, ApiExternalIncentive{ - IncentiveId: incentiveId, - PoolPath: incentive.targetPoolPath, - RewardToken: incentive.rewardToken, - RewardAmount: incentive.rewardAmount.ToString(), - RewardLeft: incentive.rewardLeft.ToString(), - StartTimestamp: incentive.startTimestamp, - EndTimestamp: incentive.endTimestamp, - Refundee: incentive.refundee.String(), - CreatedHeight: incentive.createdHeight, - DepositGnsAmount: incentive.depositGnsAmount, - }) - - // STAT NODE - _stat := json.ObjectNode("", map[string]*json.Node{ - "height": json.NumberNode("height", float64(std.GetHeight())), - "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), - }) - - // RESPONSE (ARRAY) NODE - responses := json.ArrayNode("", []*json.Node{}) - for _, incentive := range apiExternalIncentives { - active := false - if time.Now().Unix() >= incentive.StartTimestamp && time.Now().Unix() <= incentive.EndTimestamp { - active = true - } - - _incentiveNode := json.ObjectNode("", map[string]*json.Node{ - "incentiveId": json.StringNode("incentiveId", incentive.IncentiveId), - "poolPath": json.StringNode("poolPath", incentive.PoolPath), - "rewardToken": json.StringNode("rewardToken", incentive.RewardToken), - "rewardAmount": json.StringNode("rewardAmount", incentive.RewardAmount), - "rewardLeft": json.StringNode("rewardLeft", incentive.RewardLeft), - "startTimestamp": json.NumberNode("startTimestamp", float64(incentive.StartTimestamp)), - "endTimestamp": json.NumberNode("endTimestamp", float64(incentive.EndTimestamp)), - "active": json.BoolNode("active", active), - "refundee": json.StringNode("refundee", incentive.Refundee), - "createdHeight": json.NumberNode("createdHeight", float64(incentive.CreatedHeight)), - "depositGnsAmount": json.NumberNode("depositGnsAmount", float64(incentive.DepositGnsAmount)), - }) - responses.AppendArray(_incentiveNode) - } - - // RETURN - node := json.ObjectNode("", map[string]*json.Node{ - "stat": _stat, - "response": responses, - }) - - b, err := json.Marshal(node) - if err != nil { - panic(err.Error()) - } - - return string(b) -} - -func ApiGetExternalIncentivesByPoolPath(targetPoolPath string) string { - en.MintAndDistributeGns() - if consts.EMISSION_REFACTORED { - CalcPoolPositionRefactor() - } else { - CalcPoolPosition() - } - - updateExternalIncentiveLeftAmount() - - apiExternalIncentives := []ApiExternalIncentive{} - - for incentiveId, incentive := range incentives { - if incentive.targetPoolPath != targetPoolPath { - continue - } - - apiExternalIncentives = append(apiExternalIncentives, ApiExternalIncentive{ - IncentiveId: incentiveId, - PoolPath: incentive.targetPoolPath, - RewardToken: incentive.rewardToken, - RewardAmount: incentive.rewardAmount.ToString(), - RewardLeft: incentive.rewardLeft.ToString(), - StartTimestamp: incentive.startTimestamp, - EndTimestamp: incentive.endTimestamp, - Refundee: incentive.refundee.String(), - CreatedHeight: incentive.createdHeight, - DepositGnsAmount: incentive.depositGnsAmount, - }) - } - - // STAT NODE - _stat := json.ObjectNode("", map[string]*json.Node{ - "height": json.NumberNode("height", float64(std.GetHeight())), - "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), - }) - - // RESPONSE (ARRAY) NODE - responses := json.ArrayNode("", []*json.Node{}) - for _, incentive := range apiExternalIncentives { - active := false - if time.Now().Unix() >= incentive.StartTimestamp && time.Now().Unix() <= incentive.EndTimestamp { - active = true - } - - _incentiveNode := json.ObjectNode("", map[string]*json.Node{ - "incentiveId": json.StringNode("incentiveId", incentive.IncentiveId), - "poolPath": json.StringNode("poolPath", incentive.PoolPath), - "rewardToken": json.StringNode("rewardToken", incentive.RewardToken), - "rewardAmount": json.StringNode("rewardAmount", incentive.RewardAmount), - "rewardLeft": json.StringNode("rewardLeft", incentive.RewardLeft), - "startTimestamp": json.NumberNode("startTimestamp", float64(incentive.StartTimestamp)), - "endTimestamp": json.NumberNode("endTimestamp", float64(incentive.EndTimestamp)), - "active": json.BoolNode("active", active), - "refundee": json.StringNode("refundee", incentive.Refundee), - "createdHeight": json.NumberNode("createdHeight", float64(incentive.CreatedHeight)), - "depositGnsAmount": json.NumberNode("depositGnsAmount", float64(incentive.DepositGnsAmount)), - }) - responses.AppendArray(_incentiveNode) - } - - // RETURN - node := json.ObjectNode("", map[string]*json.Node{ - "stat": _stat, - "response": responses, - }) - - b, err := json.Marshal(node) - if err != nil { - panic(err.Error()) - } - - return string(b) -} - -func ApiGetExternalIncentivesByRewardTokenPath(rewardTokenPath string) string { - en.MintAndDistributeGns() - if consts.EMISSION_REFACTORED { - CalcPoolPositionRefactor() - } else { - CalcPoolPosition() - } - - updateExternalIncentiveLeftAmount() - - apiExternalIncentives := []ApiExternalIncentive{} - - for incentiveId, incentive := range incentives { - if incentive.rewardToken != rewardTokenPath { - continue - } - - apiExternalIncentives = append(apiExternalIncentives, ApiExternalIncentive{ - IncentiveId: incentiveId, - PoolPath: incentive.targetPoolPath, - RewardToken: incentive.rewardToken, - RewardAmount: incentive.rewardAmount.ToString(), - RewardLeft: incentive.rewardLeft.ToString(), - StartTimestamp: incentive.startTimestamp, - EndTimestamp: incentive.endTimestamp, - Refundee: incentive.refundee.String(), - CreatedHeight: incentive.createdHeight, - DepositGnsAmount: incentive.depositGnsAmount, - }) - } - - // STAT NODE - _stat := json.ObjectNode("", map[string]*json.Node{ - "height": json.NumberNode("height", float64(std.GetHeight())), - "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), - }) - - // RESPONSE (ARRAY) NODE - responses := json.ArrayNode("", []*json.Node{}) - for _, incentive := range apiExternalIncentives { - active := false - if time.Now().Unix() >= incentive.StartTimestamp && time.Now().Unix() <= incentive.EndTimestamp { - active = true - } - - _incentiveNode := json.ObjectNode("", map[string]*json.Node{ - "incentiveId": json.StringNode("incentiveId", incentive.IncentiveId), - "poolPath": json.StringNode("poolPath", incentive.PoolPath), - "rewardToken": json.StringNode("rewardToken", incentive.RewardToken), - "rewardAmount": json.StringNode("rewardAmount", incentive.RewardAmount), - "rewardLeft": json.StringNode("rewardLeft", incentive.RewardLeft), - "startTimestamp": json.NumberNode("startTimestamp", float64(incentive.StartTimestamp)), - "endTimestamp": json.NumberNode("endTimestamp", float64(incentive.EndTimestamp)), - "active": json.BoolNode("active", active), - "refundee": json.StringNode("refundee", incentive.Refundee), - "createdHeight": json.NumberNode("createdHeight", float64(incentive.CreatedHeight)), - "depositGnsAmount": json.NumberNode("depositGnsAmount", float64(incentive.DepositGnsAmount)), - }) - responses.AppendArray(_incentiveNode) - } - - // RETURN - node := json.ObjectNode("", map[string]*json.Node{ - "stat": _stat, - "response": responses, - }) - - b, err := json.Marshal(node) - if err != nil { - panic(err.Error()) - } - - return string(b) -} - -func ApiGetInternalIncentives() string { - en.MintAndDistributeGns() - if consts.EMISSION_REFACTORED { - CalcPoolPositionRefactor() - } else { - CalcPoolPosition() - } - - apiInternalIncentives := []ApiInternalIncentive{} - - poolTiers.Iter(func(poolPath string, internalTier InternalTier) { - apiInternalIncentives = append(apiInternalIncentives, ApiInternalIncentive{ - PoolPath: poolPath, - Tier: internalTier.tier, - StartTimestamp: internalTier.startTimestamp, - RewardPerBlock: calculateInternalRewardPerBlockByPoolPath(poolPath), - }) - }) - - // STAT NODE - _stat := json.ObjectNode("", map[string]*json.Node{ - "height": json.NumberNode("height", float64(std.GetHeight())), - "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), - }) - - // RESPONSE (ARRAY) NODE - responses := json.ArrayNode("", []*json.Node{}) - for _, incentive := range apiInternalIncentives { - _incentiveNode := json.ObjectNode("", map[string]*json.Node{ - "poolPath": json.StringNode("poolPath", incentive.PoolPath), - "rewardToken": json.StringNode("rewardToken", consts.GNS_PATH), - "tier": json.NumberNode("tier", float64(incentive.Tier)), - "startTimestamp": json.NumberNode("startTimestamp", float64(incentive.StartTimestamp)), - "rewardPerBlock": json.StringNode("rewardPerBlock", incentive.RewardPerBlock), - "accuGns": json.NumberNode("accuGns", float64(poolAccuGns[incentive.PoolPath])), - }) - responses.AppendArray(_incentiveNode) - } - - // RETURN - node := json.ObjectNode("", map[string]*json.Node{ - "stat": _stat, - "response": responses, - }) - - b, err := json.Marshal(node) - if err != nil { - panic(err.Error()) - } - - return string(b) -} - -func ApiGetInternalIncentivesByPoolPath(targetPoolPath string) string { - en.MintAndDistributeGns() - if consts.EMISSION_REFACTORED { - CalcPoolPositionRefactor() - } else { - CalcPoolPosition() - } - - apiInternalIncentives := []ApiInternalIncentive{} - - poolTiers.Iter(func(poolPath string, internalTier InternalTier) { - if poolPath != targetPoolPath { - return - } - - apiInternalIncentives = append(apiInternalIncentives, ApiInternalIncentive{ - PoolPath: poolPath, - Tier: internalTier.tier, - StartTimestamp: internalTier.startTimestamp, - RewardPerBlock: calculateInternalRewardPerBlockByPoolPath(poolPath), - }) - }) - - // STAT NODE - _stat := json.ObjectNode("", map[string]*json.Node{ - "height": json.NumberNode("height", float64(std.GetHeight())), - "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), - }) - - // RESPONSE (ARRAY) NODE - responses := json.ArrayNode("", []*json.Node{}) - for _, incentive := range apiInternalIncentives { - _incentiveNode := json.ObjectNode("", map[string]*json.Node{ - "poolPath": json.StringNode("poolPath", incentive.PoolPath), - "rewardToken": json.StringNode("rewardToken", consts.GNS_PATH), - "tier": json.NumberNode("tier", float64(incentive.Tier)), - "startTimestamp": json.NumberNode("startTimestamp", float64(incentive.StartTimestamp)), - "rewardPerBlock": json.StringNode("rewardPerBlock", incentive.RewardPerBlock), - "accuGns": json.NumberNode("accuGns", float64(poolAccuGns[targetPoolPath])), - }) - responses.AppendArray(_incentiveNode) - } - - // RETURN - node := json.ObjectNode("", map[string]*json.Node{ - "stat": _stat, - "response": responses, - }) - - b, err := json.Marshal(node) - if err != nil { - panic(err.Error()) - } - - return string(b) -} - -func ApiGetInternalIncentivesByTiers(targetTier uint64) string { - en.MintAndDistributeGns() - if consts.EMISSION_REFACTORED { - CalcPoolPositionRefactor() - } else { - CalcPoolPosition() - } - - apiInternalIncentives := []ApiInternalIncentive{} - - poolTiers.Iter(func(poolPath string, internalTier InternalTier) { - if internalTier.tier != targetTier { - return - } - - apiInternalIncentives = append(apiInternalIncentives, ApiInternalIncentive{ - PoolPath: poolPath, - Tier: internalTier.tier, - StartTimestamp: internalTier.startTimestamp, - RewardPerBlock: calculateInternalRewardPerBlockByPoolPath(poolPath), - }) - }) - - // STAT NODE - _stat := json.ObjectNode("", map[string]*json.Node{ - "height": json.NumberNode("height", float64(std.GetHeight())), - "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), - }) - - // RESPONSE (ARRAY) NODE - responses := json.ArrayNode("", []*json.Node{}) - for _, incentive := range apiInternalIncentives { - _incentiveNode := json.ObjectNode("", map[string]*json.Node{ - "poolPath": json.StringNode("poolPath", incentive.PoolPath), - "rewardToken": json.StringNode("rewardToken", consts.GNS_PATH), - "tier": json.NumberNode("tier", float64(incentive.Tier)), - "startTimestamp": json.NumberNode("startTimestamp", float64(incentive.StartTimestamp)), - "rewardPerBlock": json.StringNode("rewardPerBlock", incentive.RewardPerBlock), - "accuGns": json.NumberNode("accuGns", float64(poolAccuGns[incentive.PoolPath])), - }) - responses.AppendArray(_incentiveNode) - } - - // RETURN - node := json.ObjectNode("", map[string]*json.Node{ - "stat": _stat, - "response": responses, - }) - - b, err := json.Marshal(node) - if err != nil { - panic(err.Error()) - } - - return string(b) -} - -func makeRewardTokensArray(rewardsTokenList []string) []*json.Node { - rewardsTokenArray := make([]*json.Node, len(rewardsTokenList)) - for i, rewardToken := range rewardsTokenList { - rewardsTokenArray[i] = json.StringNode("", rewardToken) - } - return rewardsTokenArray -} - -func calculateInternalRewardPerBlockByPoolPath(poolPath string) string { - nowHeight := std.GetHeight() - fullGnsForThisHeight := gns.GetAmountByHeight(nowHeight) - - // staker distribution pct - bpsPct := en.GetDistributionPct(1) - - // calculate reward per block - stakerGns := fullGnsForThisHeight * bpsPct / 10000 - - tier1Amount, tier2Amount, tier3Amount := getTiersAmount(stakerGns) - tier1Num, tier2Num, tier3Num := getNumPoolTiers() - - internalTier, exist := poolTiers.Get(poolPath) - if !exist { - return "0" - } - - tier := internalTier.Tier() - - if tier == 1 { - return ufmt.Sprintf("%d", tier1Amount/tier1Num) - } else if tier == 2 { - return ufmt.Sprintf("%d", tier2Amount/tier2Num) - } else if tier == 3 { - return ufmt.Sprintf("%d", tier3Amount/tier3Num) - } - - return "0" -} - -func updateExternalIncentiveLeftAmount() { - // external incentive reward left update - for _, positionWarmUpAmount := range positionsExternalWarmUpAmount { - for incentiveId, warmUpAmount := range positionWarmUpAmount { - - full := warmUpAmount.full100 + warmUpAmount.full70 + warmUpAmount.full50 + warmUpAmount.full30 - - ictv, exist := incentives.Get(incentiveId) - if !exist { - continue - } - ictv.rewardLeft = new(u256.Uint).Sub(ictv.rewardLeft, u256.NewUint(full)) - incentives.Set(incentiveId, ictv) - } - } -} diff --git a/staker/_RPC_api_stake.gnoA b/staker/_RPC_api_stake.gnoA deleted file mode 100644 index f45b3d18..00000000 --- a/staker/_RPC_api_stake.gnoA +++ /dev/null @@ -1,651 +0,0 @@ -package staker - -import ( - "std" - "time" - - "gno.land/p/demo/json" - - "gno.land/r/gnoswap/v1/consts" - "gno.land/r/gnoswap/v1/common" - - en "gno.land/r/gnoswap/v1/emission" -) - -// LpTokenReward represents the rewards associated with a specific LP token -type LpTokenReward struct { - LpTokenId uint64 `json:"lpTokenId"` // The ID of the LP token - Address string `json:"address"` // The address associated with the LP token - Rewards []Reward `json:"rewards"` -} - -// Reward represents a single reward for a staked LP token -type Reward struct { - IncentiveType string `json:"incentiveType"` // The type of incentive (INTERNAL or EXTERNAL) - IncentiveId string `json:"incentiveId"` // The unique identifier of the incentive - TargetPoolPath string `json:"targetPoolPath"` // The path of the target pool for the reward - RewardTokenPath string `json:"rewardTokenPath"` // The pathe of the reward token - RewardTokenAmount uint64 `json:"rewardTokenAmount"` // The amount of the reward token - StakeTimestamp int64 `json:"stakeTimestamp"` // The timestamp when the LP token was staked - StakeHeight int64 `json:"stakeHeight"` // The block height when the LP token was staked - IncentiveStart int64 `json:"incentiveStart"` // The timestamp when the incentive started -} - -// Stake represents a single stake -type Stake struct { - TokenId uint64 `json:"tokenId"` // The ID of the staked LP token - Owner std.Address `json:"owner"` // The address of the owner of the staked LP token - NumberOfStakes uint64 `json:"numberOfStakes"` // The number of times this LP token has been staked - StakeTimestamp int64 `json:"stakeTimestamp"` // The timestamp when the LP token was staked - StakeHeight int64 `json:"stakeHeight"` // The block height when the LP token was staked - TargetPoolPath string `json:"targetPoolPath"` // The path of the target pool for the stake -} - -// ResponseQueryBase contains basic information about a query response. -type ResponseQueryBase struct { - Height int64 `json:"height"` // The block height at the time of the query - Timestamp int64 `json:"timestamp"` // The timestamp at the time of the query -} - -// ResponseApiGetRewards represents the API response for getting rewards. -type ResponseApiGetRewards struct { - Stat ResponseQueryBase `json:"stat"` // Basic query information - Response []LpTokenReward `json:"response"` // A slice of LpTokenReward structs -} - -// ResponseApiGetRewardByLpTokenId represents the API response for getting rewards for a specific LP token. -type ResponseApiGetRewardByLpTokenId struct { - Stat ResponseQueryBase `json:"stat"` // Basic query information - Response LpTokenReward `json:"response"` // The LpTokenReward for the specified LP token -} - -// ResponseApiGetStakes represents the API response for getting stakes. -type ResponseApiGetStakes struct { - Stat ResponseQueryBase `json:"stat"` // Basic query information - Response []Stake `json:"response"` // A slice of Stake structs -} - -func ApiGetRewards() string { - en.MintAndDistributeGns() - if consts.EMISSION_REFACTORED { - CalcPoolPositionRefactor() - } else { - CalcPoolPosition() - } - - lpTokenRewards := []LpTokenReward{} - - // TODO: extract as function - deposits.Iter(func(tokenId uint64, deposit Deposit) { - rewards := []Reward{} - - // get internal gns reward - internalWarmUpAmount, exist := positionsInternalWarmUpAmount[tokenId] - if !exist { - return - } - internalGNS := internalWarmUpAmount.give30 + internalWarmUpAmount.give50 + internalWarmUpAmount.give70 + internalWarmUpAmount.full100 - - if internalGNS > 0 { - rewards = append(rewards, Reward{ - IncentiveType: "INTERNAL", - IncentiveId: "", - TargetPoolPath: deposit.targetPoolPath, - RewardTokenPath: consts.GNS_PATH, - RewardTokenAmount: internalGNS, - StakeTimestamp: deposit.stakeTimestamp, - StakeHeight: deposit.stakeHeight, - IncentiveStart: deposit.stakeTimestamp, - }) - } - - // find all external reward list for poolPath which lpTokenId is staked - ictvList, exists := poolIncentives.Get(deposit.targetPoolPath) - if !exists { - return - } - - for _, ictvId := range ictvList { - ictv, exists := incentives.Get(ictvId) - if !exists { - return - } - - stakedOrCreatedAt := common.I64Max(deposit.stakeTimestamp, ictv.startTimestamp) - now := time.Now().Unix() - if now < stakedOrCreatedAt { - return - } - - externalWarmUpAmount, exist := positionsExternalWarmUpAmount[tokenId][ictvId] - if !exist { - return - } - externalReward := externalWarmUpAmount.give30 + externalWarmUpAmount.give50 + externalWarmUpAmount.give70 + externalWarmUpAmount.full100 - if externalReward >= 0 { - rewards = append(rewards, Reward{ - IncentiveType: "EXTERNAL", - IncentiveId: ictvId, - TargetPoolPath: deposit.targetPoolPath, - RewardTokenPath: ictv.rewardToken, - RewardTokenAmount: externalReward, - StakeTimestamp: deposit.stakeTimestamp, - StakeHeight: deposit.stakeHeight, - IncentiveStart: ictv.startTimestamp, - }) - } - } - - if len(rewards) > 0 { - lpTokenReward := LpTokenReward{ - LpTokenId: tokenId, - Address: deposit.owner.String(), - Rewards: rewards, - } - lpTokenRewards = append(lpTokenRewards, lpTokenReward) - } - }) - - qb := ResponseQueryBase{ - Height: std.GetHeight(), - Timestamp: time.Now().Unix(), - } - - r := ResponseApiGetRewards{ - Stat: qb, - Response: lpTokenRewards, - } - - // STAT NODE - _stat := json.ObjectNode("", map[string]*json.Node{ - "height": json.NumberNode("height", float64(std.GetHeight())), - "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), - }) - - // RESPONSE (ARRAY) NODE - responses := json.ArrayNode("", []*json.Node{}) - for _, reward := range r.Response { - _rewardNode := json.ObjectNode("", map[string]*json.Node{ - "lpTokenId": json.NumberNode("lpTokenId", float64(reward.LpTokenId)), - "address": json.StringNode("address", reward.Address), - "rewards": json.ArrayNode("rewards", makeRewardsArray(reward.Rewards)), - }) - responses.AppendArray(_rewardNode) - } - - node := json.ObjectNode("", map[string]*json.Node{ - "stat": _stat, - "response": responses, - }) - - b, err := json.Marshal(node) - if err != nil { - panic(err.Error()) - } - - return string(b) -} - -func ApiGetRewardsByLpTokenId(targetLpTokenId uint64) string { - en.MintAndDistributeGns() - if consts.EMISSION_REFACTORED { - CalcPoolPositionRefactor() - } else { - CalcPoolPosition() - } - - lpTokenRewards := []LpTokenReward{} - - deposits.Iter(func(tokenId uint64, deposit Deposit) { - if tokenId != targetLpTokenId { - return - } - - rewards := []Reward{} - - // get internal gns reward - internalWarmUpAmount, exist := positionsInternalWarmUpAmount[tokenId] - if !exist { - return - } - internalGNS := internalWarmUpAmount.give30 + internalWarmUpAmount.give50 + internalWarmUpAmount.give70 + internalWarmUpAmount.full100 - - if internalGNS > 0 { - rewards = append(rewards, Reward{ - IncentiveType: "INTERNAL", - IncentiveId: "", - TargetPoolPath: deposit.targetPoolPath, - RewardTokenPath: consts.GNS_PATH, - RewardTokenAmount: internalGNS, - StakeTimestamp: deposit.stakeTimestamp, - StakeHeight: deposit.stakeHeight, - IncentiveStart: deposit.stakeTimestamp, - }) - } - - // find all external reward list for poolPath which lpTokenId is staked - ictvList, exists := poolIncentives.Get(deposit.targetPoolPath) - if !exists { - return - } - - for _, ictvId := range ictvList { - ictv, exists := incentives.Get(ictvId) - if !exists { - return - } - - stakedOrCreatedAt := common.I64Max(deposit.stakeTimestamp, ictv.startTimestamp) - now := time.Now().Unix() - if now < stakedOrCreatedAt { - return - } - - externalWarmUpAmount, exist := positionsExternalWarmUpAmount[tokenId][ictvId] - if !exist { - return - } - externalReward := externalWarmUpAmount.give30 + externalWarmUpAmount.give50 + externalWarmUpAmount.give70 + externalWarmUpAmount.full100 - if externalReward > 0 { - rewards = append(rewards, Reward{ - IncentiveType: "EXTERNAL", - IncentiveId: ictvId, - TargetPoolPath: deposit.targetPoolPath, - RewardTokenPath: ictv.rewardToken, - RewardTokenAmount: externalReward, - StakeTimestamp: deposit.stakeTimestamp, - StakeHeight: deposit.stakeHeight, - IncentiveStart: ictv.startTimestamp, - }) - } - } - - lpTokenReward := LpTokenReward{ - LpTokenId: tokenId, - Address: deposit.owner.String(), - Rewards: rewards, - } - lpTokenRewards = append(lpTokenRewards, lpTokenReward) - }) - - qb := ResponseQueryBase{ - Height: std.GetHeight(), - Timestamp: time.Now().Unix(), - } - - r := ResponseApiGetRewards{ - Stat: qb, - Response: lpTokenRewards, - } - - // STAT NODE - _stat := json.ObjectNode("", map[string]*json.Node{ - "height": json.NumberNode("height", float64(std.GetHeight())), - "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), - }) - - // RESPONSE (ARRAY) NODE - responses := json.ArrayNode("", []*json.Node{}) - for _, reward := range r.Response { - _rewardNode := json.ObjectNode("", map[string]*json.Node{ - "lpTokenId": json.NumberNode("lpTokenId", float64(reward.LpTokenId)), - "address": json.StringNode("address", reward.Address), - "rewards": json.ArrayNode("rewards", makeRewardsArray(reward.Rewards)), - }) - responses.AppendArray(_rewardNode) - } - - node := json.ObjectNode("", map[string]*json.Node{ - "stat": _stat, - "response": responses, - }) - - b, err := json.Marshal(node) - if err != nil { - panic(err.Error()) - } - - return string(b) -} - -func ApiGetRewardsByAddress(targetAddress string) string { - en.MintAndDistributeGns() - if consts.EMISSION_REFACTORED { - CalcPoolPositionRefactor() - } else { - CalcPoolPosition() - } - - lpTokenRewards := []LpTokenReward{} - - deposits.Iter(func(tokenId uint64, deposit Deposit) { - if deposit.owner.String() != targetAddress { - return - } - - rewards := []Reward{} - - // get internal gns reward - internalWarmUpAmount, exist := positionsInternalWarmUpAmount[tokenId] - if !exist { - return - } - internalGNS := internalWarmUpAmount.give30 + internalWarmUpAmount.give50 + internalWarmUpAmount.give70 + internalWarmUpAmount.full100 - - if internalGNS > 0 { - rewards = append(rewards, Reward{ - IncentiveType: "INTERNAL", - IncentiveId: "", - TargetPoolPath: deposit.targetPoolPath, - RewardTokenPath: consts.GNS_PATH, - RewardTokenAmount: internalGNS, - StakeTimestamp: deposit.stakeTimestamp, - StakeHeight: deposit.stakeHeight, - IncentiveStart: deposit.stakeTimestamp, - }) - } - - // find all external reward list for poolPath which lpTokenId is staked - ictvList, exists := poolIncentives.Get(deposit.targetPoolPath) - if !exists { - return - } - - for _, ictvId := range ictvList { - ictv, exists := incentives.Get(ictvId) - if !exists { - return - } - - stakedOrCreatedAt := common.I64Max(deposit.stakeTimestamp, ictv.startTimestamp) - now := time.Now().Unix() - if now < stakedOrCreatedAt { - return - } - - externalWarmUpAmount, exist := positionsExternalWarmUpAmount[tokenId][ictvId] - if !exist { - return - } - externalReward := externalWarmUpAmount.give30 + externalWarmUpAmount.give50 + externalWarmUpAmount.give70 + externalWarmUpAmount.full100 - rewards = append(rewards, Reward{ - IncentiveType: "EXTERNAL", - IncentiveId: ictvId, - TargetPoolPath: deposit.targetPoolPath, - RewardTokenPath: ictv.rewardToken, - RewardTokenAmount: externalReward, - StakeTimestamp: deposit.stakeTimestamp, - StakeHeight: deposit.stakeHeight, - IncentiveStart: ictv.startTimestamp, - }) - } - lpTokenReward := LpTokenReward{ - LpTokenId: tokenId, - Address: deposit.owner.String(), - Rewards: rewards, - } - lpTokenRewards = append(lpTokenRewards, lpTokenReward) - }) - - qb := ResponseQueryBase{ - Height: std.GetHeight(), - Timestamp: time.Now().Unix(), - } - - r := ResponseApiGetRewards{ - Stat: qb, - Response: lpTokenRewards, - } - - // STAT NODE - _stat := json.ObjectNode("", map[string]*json.Node{ - "height": json.NumberNode("height", float64(std.GetHeight())), - "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), - }) - - // RESPONSE (ARRAY) NODE - responses := json.ArrayNode("", []*json.Node{}) - for _, reward := range r.Response { - _rewardNode := json.ObjectNode("", map[string]*json.Node{ - "lpTokenId": json.NumberNode("lpTokenId", float64(reward.LpTokenId)), - "address": json.StringNode("address", reward.Address), - "rewards": json.ArrayNode("rewards", makeRewardsArray(reward.Rewards)), - }) - responses.AppendArray(_rewardNode) - } - - node := json.ObjectNode("", map[string]*json.Node{ - "stat": _stat, - "response": responses, - }) - - b, err := json.Marshal(node) - if err != nil { - panic(err.Error()) - } - - return string(b) -} - -func ApiGetStakes() string { - en.MintAndDistributeGns() - if consts.EMISSION_REFACTORED { - CalcPoolPositionRefactor() - } else { - CalcPoolPosition() - } - - stakes := []Stake{} - deposits.Iter(func(tokenId uint64, deposit Deposit) { - stakes = append(stakes, Stake{ - TokenId: tokenId, - Owner: deposit.owner, - NumberOfStakes: deposit.numberOfStakes, - StakeTimestamp: deposit.stakeTimestamp, - StakeHeight: deposit.stakeHeight, - TargetPoolPath: deposit.targetPoolPath, - }) - }) - - qb := ResponseQueryBase{ - Height: std.GetHeight(), - Timestamp: time.Now().Unix(), - } - - r := ResponseApiGetStakes{ - Stat: qb, - Response: stakes, - } - - // STAT NODE - _stat := json.ObjectNode("", map[string]*json.Node{ - "height": json.NumberNode("height", float64(std.GetHeight())), - "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), - }) - - // RESPONSE (ARRAY) NODE - responses := json.ArrayNode("", []*json.Node{}) - for _, stake := range r.Response { - _stakeNode := json.ObjectNode("", map[string]*json.Node{ - "tokenId": json.NumberNode("tokenId", float64(stake.TokenId)), - "owner": json.StringNode("owner", stake.Owner.String()), - "numberOfStakes": json.NumberNode("numberOfStakes", float64(stake.NumberOfStakes)), - "stakeTimestamp": json.NumberNode("stakeTimestamp", float64(stake.StakeTimestamp)), - "stakeHeight": json.NumberNode("stakeHeight", float64(stake.StakeHeight)), - "targetPoolPath": json.StringNode("targetPoolPath", stake.TargetPoolPath), - }) - responses.AppendArray(_stakeNode) - } - - node := json.ObjectNode("", map[string]*json.Node{ - "stat": _stat, - "response": responses, - }) - - b, err := json.Marshal(node) - if err != nil { - panic(err.Error()) - } - - return string(b) -} - -func ApiGetStakesByLpTokenId(targetLpTokenId uint64) string { - en.MintAndDistributeGns() - if consts.EMISSION_REFACTORED { - CalcPoolPositionRefactor() - } else { - CalcPoolPosition() - } - - stakes := []Stake{} - - deposits.Iter(func(tokenId uint64, deposit Deposit) { - if tokenId != targetLpTokenId { - return - } - - stakes = append(stakes, Stake{ - TokenId: tokenId, - Owner: deposit.owner, - NumberOfStakes: deposit.numberOfStakes, - StakeTimestamp: deposit.stakeTimestamp, - StakeHeight: deposit.stakeHeight, - TargetPoolPath: deposit.targetPoolPath, - }) - }) - - qb := ResponseQueryBase{ - Height: std.GetHeight(), - Timestamp: time.Now().Unix(), - } - - r := ResponseApiGetStakes{ - Stat: qb, - Response: stakes, - } - - // STAT NODE - _stat := json.ObjectNode("", map[string]*json.Node{ - "height": json.NumberNode("height", float64(std.GetHeight())), - "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), - }) - - // RESPONSE (ARRAY) NODE - responses := json.ArrayNode("", []*json.Node{}) - for _, stake := range r.Response { - _stakeNode := json.ObjectNode("", map[string]*json.Node{ - "tokenId": json.NumberNode("tokenId", float64(stake.TokenId)), - "owner": json.StringNode("owner", stake.Owner.String()), - "numberOfStakes": json.NumberNode("numberOfStakes", float64(stake.NumberOfStakes)), - "stakeTimestamp": json.NumberNode("stakeTimestamp", float64(stake.StakeTimestamp)), - "stakeHeight": json.NumberNode("stakeHeight", float64(stake.StakeHeight)), - "targetPoolPath": json.StringNode("targetPoolPath", stake.TargetPoolPath), - }) - responses.AppendArray(_stakeNode) - } - - node := json.ObjectNode("", map[string]*json.Node{ - "stat": _stat, - "response": responses, - }) - - b, err := json.Marshal(node) - if err != nil { - panic(err.Error()) - } - - return string(b) -} - -func ApiGetStakesByAddress(targetAddress string) string { - en.MintAndDistributeGns() - if consts.EMISSION_REFACTORED { - CalcPoolPositionRefactor() - } else { - CalcPoolPosition() - } - - stakes := []Stake{} - - deposits.Iter(func(tokenId uint64, deposit Deposit) { - if deposit.owner.String() != targetAddress { - return - } - - stakes = append(stakes, Stake{ - TokenId: tokenId, - Owner: deposit.owner, - NumberOfStakes: deposit.numberOfStakes, - StakeTimestamp: deposit.stakeTimestamp, - StakeHeight: deposit.stakeHeight, - TargetPoolPath: deposit.targetPoolPath, - }) - }) - - qb := ResponseQueryBase{ - Height: std.GetHeight(), - Timestamp: time.Now().Unix(), - } - - r := ResponseApiGetStakes{ - Stat: qb, - Response: stakes, - } - - // STAT NODE - _stat := json.ObjectNode("", map[string]*json.Node{ - "height": json.NumberNode("height", float64(std.GetHeight())), - "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), - }) - - // RESPONSE (ARRAY) NODE - responses := json.ArrayNode("", []*json.Node{}) - for _, stake := range r.Response { - _stakeNode := json.ObjectNode("", map[string]*json.Node{ - "tokenId": json.NumberNode("tokenId", float64(stake.TokenId)), - "owner": json.StringNode("owner", stake.Owner.String()), - "numberOfStakes": json.NumberNode("numberOfStakes", float64(stake.NumberOfStakes)), - "stakeTimestamp": json.NumberNode("stakeTimestamp", float64(stake.StakeTimestamp)), - "stakeHeight": json.NumberNode("stakeHeight", float64(stake.StakeHeight)), - "targetPoolPath": json.StringNode("targetPoolPath", stake.TargetPoolPath), - }) - responses.AppendArray(_stakeNode) - } - - node := json.ObjectNode("", map[string]*json.Node{ - "stat": _stat, - "response": responses, - }) - - b, err := json.Marshal(node) - if err != nil { - panic(err.Error()) - } - - return string(b) -} - -// for off chain to check if lpTokenId is staked via RPC -func IsStaked(tokenId uint64) bool { - _, exist := deposits[tokenId] - return exist -} - -func makeRewardsArray(rewards []Reward) []*json.Node { - rewardsArray := make([]*json.Node, len(rewards)) - - for i, reward := range rewards { - rewardsArray[i] = json.ObjectNode("", map[string]*json.Node{ - "incentiveType": json.StringNode("incentiveType", reward.IncentiveType), - "incentiveId": json.StringNode("incentiveId", reward.IncentiveId), - "targetPoolPath": json.StringNode("targetPoolPath", reward.TargetPoolPath), - "rewardTokenPath": json.StringNode("rewardTokenPath", reward.RewardTokenPath), - "rewardTokenAmount": json.NumberNode("rewardTokenAmount", float64(reward.RewardTokenAmount)), - "stakeTimestamp": json.NumberNode("stakeTimestamp", float64(reward.StakeTimestamp)), - "stakeHeight": json.NumberNode("stakeHeight", float64(reward.StakeHeight)), - "incentiveStart": json.NumberNode("incentiveStart", float64(reward.IncentiveStart)), - }) - } - return rewardsArray -} diff --git a/staker/api.gno b/staker/api.gno new file mode 100644 index 00000000..349ba0d3 --- /dev/null +++ b/staker/api.gno @@ -0,0 +1,1330 @@ +package staker + +type RewardToken struct { + PoolPath string `json:"poolPath"` + RewardsTokenList []string `json:"rewardsTokenList"` +} + +type ApiExternalIncentive struct { + IncentiveId string `json:"incentiveId"` + PoolPath string `json:"poolPath"` + RewardToken string `json:"rewardToken"` + RewardAmount string `json:"rewardAmount"` + RewardLeft string `json:"rewardLeft"` + StartTimestamp int64 `json:"startTimestamp"` + EndTimestamp int64 `json:"endTimestamp"` + Active bool `json:"active"` + Refundee string `json:"refundee"` + CreatedHeight int64 `json:"createdHeight"` + DepositGnsAmount uint64 `json:"depositGnsAmount"` +} + +type ApiInternalIncentive struct { + PoolPath string `json:"poolPath"` + Tier uint64 `json:"tier"` + StartTimestamp int64 `json:"startTimestamp"` + RewardPerBlock string `json:"rewardPerBlock"` +} + +// +//func ApiGetRewardTokens() string { +// en.MintAndDistributeGns() +// +// rewardTokens := []RewardToken{} +// +// poolList := pl.PoolGetPoolList() +// for _, poolPath := range poolList { +// thisPoolRewardTokens := []string{} +// +// // HANDLE INTERNAL +// if isExistPoolTiers(poolPath) { +// thisPoolRewardTokens = append(thisPoolRewardTokens, consts.GNS_PATH) +// } +// +// // HANDLE EXTERNAL +// ictvList, exists := poolIncentives.Get(poolPath) +// if !exists { +// continue +// } +// +// for _, incentiveId := range ictvList { +// ictv, exist := incentives.Get(incentiveId) +// if !exist { +// continue +// } +// if ictv.RewardToken() == "" { +// continue +// } +// thisPoolRewardTokens = append(thisPoolRewardTokens, ictv.RewardToken()) +// } +// +// if len(thisPoolRewardTokens) == 0 { +// continue +// } +// +// rewardTokens = append(rewardTokens, RewardToken{ +// PoolPath: poolPath, +// RewardsTokenList: thisPoolRewardTokens, +// }) +// } +// +// // STAT NODE +// _stat := json.ObjectNode("", map[string]*json.Node{ +// "height": json.NumberNode("height", float64(std.GetHeight())), +// "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), +// }) +// +// // RESPONSE (ARRAY) NODE +// responses := json.ArrayNode("", []*json.Node{}) +// for _, rewardToken := range rewardTokens { +// _rewardTokenNode := json.ObjectNode("", map[string]*json.Node{ +// "poolPath": json.StringNode("poolPath", rewardToken.PoolPath), +// "tokens": json.ArrayNode("tokens", makeRewardTokensArray(rewardToken.RewardsTokenList)), +// }) +// responses.AppendArray(_rewardTokenNode) +// } +// +// node := json.ObjectNode("", map[string]*json.Node{ +// "stat": _stat, +// "response": responses, +// }) +// +// b, err := json.Marshal(node) +// if err != nil { +// panic(err.Error()) +// } +// +// return string(b) +//} +// +//func ApiGetRewardTokensByPoolPath(targetPoolPath string) string { +// en.MintAndDistributeGns() +// if consts.EMISSION_REFACTORED { +// CalcPoolPositionRefactor() +// } else { +// CalcPoolPosition() +// } +// +// rewardTokens := []RewardToken{} +// +// poolList := pl.PoolGetPoolList() +// for _, poolPath := range poolList { +// if poolPath != targetPoolPath { +// continue +// } +// +// thisPoolRewardTokens := []string{} +// +// // HANDLE INTERNAL +// if isExistPoolTiers(poolPath) { +// thisPoolRewardTokens = append(thisPoolRewardTokens, consts.GNS_PATH) +// } +// +// // HANDLE EXTERNAL +// ictvList, exists := poolIncentives.Get(poolPath) +// if !exists { +// continue +// } +// +// for _, incentiveId := range ictvList { +// ictv, exist := incentives.Get(incentiveId) +// if !exist { +// continue +// } +// thisPoolRewardTokens = append(thisPoolRewardTokens, ictv.RewardToken()) +// } +// +// rewardTokens = append(rewardTokens, RewardToken{ +// PoolPath: poolPath, +// RewardsTokenList: thisPoolRewardTokens, +// }) +// } +// +// // STAT NODE +// _stat := json.ObjectNode("", map[string]*json.Node{ +// "height": json.NumberNode("height", float64(std.GetHeight())), +// "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), +// }) +// +// // RESPONSE (ARRAY) NODE +// responses := json.ArrayNode("", []*json.Node{}) +// for _, rewardToken := range rewardTokens { +// _rewardTokenNode := json.ObjectNode("", map[string]*json.Node{ +// "poolPath": json.StringNode("poolPath", rewardToken.PoolPath), +// "tokens": json.ArrayNode("tokens", makeRewardTokensArray(rewardToken.RewardsTokenList)), +// }) +// responses.AppendArray(_rewardTokenNode) +// } +// +// node := json.ObjectNode("", map[string]*json.Node{ +// "stat": _stat, +// "response": responses, +// }) +// +// b, err := json.Marshal(node) +// if err != nil { +// panic(err.Error()) +// } +// +// return string(b) +//} +// +//func ApiGetExternalIncentives() string { +// en.MintAndDistributeGns() +// if consts.EMISSION_REFACTORED { +// CalcPoolPositionRefactor() +// } else { +// CalcPoolPosition() +// } +// +// updateExternalIncentiveLeftAmount() +// +// apiExternalIncentives := []ApiExternalIncentive{} +// +// for incentiveId, incentive := range incentives { +// apiExternalIncentives = append(apiExternalIncentives, ApiExternalIncentive{ +// IncentiveId: incentiveId, +// PoolPath: incentive.targetPoolPath, +// RewardToken: incentive.rewardToken, +// RewardAmount: incentive.rewardAmount.ToString(), +// RewardLeft: incentive.rewardLeft.ToString(), +// StartTimestamp: incentive.startTimestamp, +// EndTimestamp: incentive.endTimestamp, +// Refundee: incentive.refundee.String(), +// CreatedHeight: incentive.createdHeight, +// DepositGnsAmount: incentive.depositGnsAmount, +// }) +// } +// +// // STAT NODE +// _stat := json.ObjectNode("", map[string]*json.Node{ +// "height": json.NumberNode("height", float64(std.GetHeight())), +// "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), +// }) +// +// // RESPONSE (ARRAY) NODE +// responses := json.ArrayNode("", []*json.Node{}) +// for _, incentive := range apiExternalIncentives { +// active := false +// if time.Now().Unix() >= incentive.StartTimestamp && time.Now().Unix() <= incentive.EndTimestamp { +// active = true +// } +// +// _incentiveNode := json.ObjectNode("", map[string]*json.Node{ +// "incentiveId": json.StringNode("incentiveId", incentive.IncentiveId), +// "poolPath": json.StringNode("poolPath", incentive.PoolPath), +// "rewardToken": json.StringNode("rewardToken", incentive.RewardToken), +// "rewardAmount": json.StringNode("rewardAmount", incentive.RewardAmount), +// "rewardLeft": json.StringNode("rewardLeft", incentive.RewardLeft), +// "startTimestamp": json.NumberNode("startTimestamp", float64(incentive.StartTimestamp)), +// "endTimestamp": json.NumberNode("endTimestamp", float64(incentive.EndTimestamp)), +// "active": json.BoolNode("active", active), +// "refundee": json.StringNode("refundee", incentive.Refundee), +// "createdHeight": json.NumberNode("createdHeight", float64(incentive.CreatedHeight)), +// "depositGnsAmount": json.NumberNode("depositGnsAmount", float64(incentive.DepositGnsAmount)), +// }) +// responses.AppendArray(_incentiveNode) +// } +// +// // RETURN +// node := json.ObjectNode("", map[string]*json.Node{ +// "stat": _stat, +// "response": responses, +// }) +// +// b, err := json.Marshal(node) +// if err != nil { +// panic(err.Error()) +// } +// +// return string(b) +//} +// +//func ApiGetExternalIncentiveById(incentiveId string) string { +// en.MintAndDistributeGns() +// if consts.EMISSION_REFACTORED { +// CalcPoolPositionRefactor() +// } else { +// CalcPoolPosition() +// } +// +// updateExternalIncentiveLeftAmount() +// +// apiExternalIncentives := []ApiExternalIncentive{} +// +// incentive, exist := incentives.Get(incentiveId) +// if !exist { +// panic(addDetailToError( +// errDataNotFound, +// ufmt.Sprintf("incentive(%s) not found", incentiveId), +// )) +// } +// +// apiExternalIncentives = append(apiExternalIncentives, ApiExternalIncentive{ +// IncentiveId: incentiveId, +// PoolPath: incentive.targetPoolPath, +// RewardToken: incentive.rewardToken, +// RewardAmount: incentive.rewardAmount.ToString(), +// RewardLeft: incentive.rewardLeft.ToString(), +// StartTimestamp: incentive.startTimestamp, +// EndTimestamp: incentive.endTimestamp, +// Refundee: incentive.refundee.String(), +// CreatedHeight: incentive.createdHeight, +// DepositGnsAmount: incentive.depositGnsAmount, +// }) +// +// // STAT NODE +// _stat := json.ObjectNode("", map[string]*json.Node{ +// "height": json.NumberNode("height", float64(std.GetHeight())), +// "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), +// }) +// +// // RESPONSE (ARRAY) NODE +// responses := json.ArrayNode("", []*json.Node{}) +// for _, incentive := range apiExternalIncentives { +// active := false +// if time.Now().Unix() >= incentive.StartTimestamp && time.Now().Unix() <= incentive.EndTimestamp { +// active = true +// } +// +// _incentiveNode := json.ObjectNode("", map[string]*json.Node{ +// "incentiveId": json.StringNode("incentiveId", incentive.IncentiveId), +// "poolPath": json.StringNode("poolPath", incentive.PoolPath), +// "rewardToken": json.StringNode("rewardToken", incentive.RewardToken), +// "rewardAmount": json.StringNode("rewardAmount", incentive.RewardAmount), +// "rewardLeft": json.StringNode("rewardLeft", incentive.RewardLeft), +// "startTimestamp": json.NumberNode("startTimestamp", float64(incentive.StartTimestamp)), +// "endTimestamp": json.NumberNode("endTimestamp", float64(incentive.EndTimestamp)), +// "active": json.BoolNode("active", active), +// "refundee": json.StringNode("refundee", incentive.Refundee), +// "createdHeight": json.NumberNode("createdHeight", float64(incentive.CreatedHeight)), +// "depositGnsAmount": json.NumberNode("depositGnsAmount", float64(incentive.DepositGnsAmount)), +// }) +// responses.AppendArray(_incentiveNode) +// } +// +// // RETURN +// node := json.ObjectNode("", map[string]*json.Node{ +// "stat": _stat, +// "response": responses, +// }) +// +// b, err := json.Marshal(node) +// if err != nil { +// panic(err.Error()) +// } +// +// return string(b) +//} +// +//func ApiGetExternalIncentivesByPoolPath(targetPoolPath string) string { +// en.MintAndDistributeGns() +// if consts.EMISSION_REFACTORED { +// CalcPoolPositionRefactor() +// } else { +// CalcPoolPosition() +// } +// +// updateExternalIncentiveLeftAmount() +// +// apiExternalIncentives := []ApiExternalIncentive{} +// +// for incentiveId, incentive := range incentives { +// if incentive.targetPoolPath != targetPoolPath { +// continue +// } +// +// apiExternalIncentives = append(apiExternalIncentives, ApiExternalIncentive{ +// IncentiveId: incentiveId, +// PoolPath: incentive.targetPoolPath, +// RewardToken: incentive.rewardToken, +// RewardAmount: incentive.rewardAmount.ToString(), +// RewardLeft: incentive.rewardLeft.ToString(), +// StartTimestamp: incentive.startTimestamp, +// EndTimestamp: incentive.endTimestamp, +// Refundee: incentive.refundee.String(), +// CreatedHeight: incentive.createdHeight, +// DepositGnsAmount: incentive.depositGnsAmount, +// }) +// } +// +// // STAT NODE +// _stat := json.ObjectNode("", map[string]*json.Node{ +// "height": json.NumberNode("height", float64(std.GetHeight())), +// "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), +// }) +// +// // RESPONSE (ARRAY) NODE +// responses := json.ArrayNode("", []*json.Node{}) +// for _, incentive := range apiExternalIncentives { +// active := false +// if time.Now().Unix() >= incentive.StartTimestamp && time.Now().Unix() <= incentive.EndTimestamp { +// active = true +// } +// +// _incentiveNode := json.ObjectNode("", map[string]*json.Node{ +// "incentiveId": json.StringNode("incentiveId", incentive.IncentiveId), +// "poolPath": json.StringNode("poolPath", incentive.PoolPath), +// "rewardToken": json.StringNode("rewardToken", incentive.RewardToken), +// "rewardAmount": json.StringNode("rewardAmount", incentive.RewardAmount), +// "rewardLeft": json.StringNode("rewardLeft", incentive.RewardLeft), +// "startTimestamp": json.NumberNode("startTimestamp", float64(incentive.StartTimestamp)), +// "endTimestamp": json.NumberNode("endTimestamp", float64(incentive.EndTimestamp)), +// "active": json.BoolNode("active", active), +// "refundee": json.StringNode("refundee", incentive.Refundee), +// "createdHeight": json.NumberNode("createdHeight", float64(incentive.CreatedHeight)), +// "depositGnsAmount": json.NumberNode("depositGnsAmount", float64(incentive.DepositGnsAmount)), +// }) +// responses.AppendArray(_incentiveNode) +// } +// +// // RETURN +// node := json.ObjectNode("", map[string]*json.Node{ +// "stat": _stat, +// "response": responses, +// }) +// +// b, err := json.Marshal(node) +// if err != nil { +// panic(err.Error()) +// } +// +// return string(b) +//} +// +//func ApiGetExternalIncentivesByRewardTokenPath(rewardTokenPath string) string { +// en.MintAndDistributeGns() +// if consts.EMISSION_REFACTORED { +// CalcPoolPositionRefactor() +// } else { +// CalcPoolPosition() +// } +// +// updateExternalIncentiveLeftAmount() +// +// apiExternalIncentives := []ApiExternalIncentive{} +// +// for incentiveId, incentive := range incentives { +// if incentive.rewardToken != rewardTokenPath { +// continue +// } +// +// apiExternalIncentives = append(apiExternalIncentives, ApiExternalIncentive{ +// IncentiveId: incentiveId, +// PoolPath: incentive.targetPoolPath, +// RewardToken: incentive.rewardToken, +// RewardAmount: incentive.rewardAmount.ToString(), +// RewardLeft: incentive.rewardLeft.ToString(), +// StartTimestamp: incentive.startTimestamp, +// EndTimestamp: incentive.endTimestamp, +// Refundee: incentive.refundee.String(), +// CreatedHeight: incentive.createdHeight, +// DepositGnsAmount: incentive.depositGnsAmount, +// }) +// } +// +// // STAT NODE +// _stat := json.ObjectNode("", map[string]*json.Node{ +// "height": json.NumberNode("height", float64(std.GetHeight())), +// "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), +// }) +// +// // RESPONSE (ARRAY) NODE +// responses := json.ArrayNode("", []*json.Node{}) +// for _, incentive := range apiExternalIncentives { +// active := false +// if time.Now().Unix() >= incentive.StartTimestamp && time.Now().Unix() <= incentive.EndTimestamp { +// active = true +// } +// +// _incentiveNode := json.ObjectNode("", map[string]*json.Node{ +// "incentiveId": json.StringNode("incentiveId", incentive.IncentiveId), +// "poolPath": json.StringNode("poolPath", incentive.PoolPath), +// "rewardToken": json.StringNode("rewardToken", incentive.RewardToken), +// "rewardAmount": json.StringNode("rewardAmount", incentive.RewardAmount), +// "rewardLeft": json.StringNode("rewardLeft", incentive.RewardLeft), +// "startTimestamp": json.NumberNode("startTimestamp", float64(incentive.StartTimestamp)), +// "endTimestamp": json.NumberNode("endTimestamp", float64(incentive.EndTimestamp)), +// "active": json.BoolNode("active", active), +// "refundee": json.StringNode("refundee", incentive.Refundee), +// "createdHeight": json.NumberNode("createdHeight", float64(incentive.CreatedHeight)), +// "depositGnsAmount": json.NumberNode("depositGnsAmount", float64(incentive.DepositGnsAmount)), +// }) +// responses.AppendArray(_incentiveNode) +// } +// +// // RETURN +// node := json.ObjectNode("", map[string]*json.Node{ +// "stat": _stat, +// "response": responses, +// }) +// +// b, err := json.Marshal(node) +// if err != nil { +// panic(err.Error()) +// } +// +// return string(b) +//} +// +//func ApiGetInternalIncentives() string { +// en.MintAndDistributeGns() +// if consts.EMISSION_REFACTORED { +// CalcPoolPositionRefactor() +// } else { +// CalcPoolPosition() +// } +// +// apiInternalIncentives := []ApiInternalIncentive{} +// +// poolTiers.Iter(func(poolPath string, internalTier InternalTier) { +// apiInternalIncentives = append(apiInternalIncentives, ApiInternalIncentive{ +// PoolPath: poolPath, +// Tier: internalTier.tier, +// StartTimestamp: internalTier.startTimestamp, +// RewardPerBlock: calculateInternalRewardPerBlockByPoolPath(poolPath), +// }) +// }) +// +// // STAT NODE +// _stat := json.ObjectNode("", map[string]*json.Node{ +// "height": json.NumberNode("height", float64(std.GetHeight())), +// "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), +// }) +// +// // RESPONSE (ARRAY) NODE +// responses := json.ArrayNode("", []*json.Node{}) +// for _, incentive := range apiInternalIncentives { +// _incentiveNode := json.ObjectNode("", map[string]*json.Node{ +// "poolPath": json.StringNode("poolPath", incentive.PoolPath), +// "rewardToken": json.StringNode("rewardToken", consts.GNS_PATH), +// "tier": json.NumberNode("tier", float64(incentive.Tier)), +// "startTimestamp": json.NumberNode("startTimestamp", float64(incentive.StartTimestamp)), +// "rewardPerBlock": json.StringNode("rewardPerBlock", incentive.RewardPerBlock), +// "accuGns": json.NumberNode("accuGns", float64(poolAccuGns[incentive.PoolPath])), +// }) +// responses.AppendArray(_incentiveNode) +// } +// +// // RETURN +// node := json.ObjectNode("", map[string]*json.Node{ +// "stat": _stat, +// "response": responses, +// }) +// +// b, err := json.Marshal(node) +// if err != nil { +// panic(err.Error()) +// } +// +// return string(b) +//} +// +//func ApiGetInternalIncentivesByPoolPath(targetPoolPath string) string { +// en.MintAndDistributeGns() +// if consts.EMISSION_REFACTORED { +// CalcPoolPositionRefactor() +// } else { +// CalcPoolPosition() +// } +// +// apiInternalIncentives := []ApiInternalIncentive{} +// +// poolTiers.Iter(func(poolPath string, internalTier InternalTier) { +// if poolPath != targetPoolPath { +// return +// } +// +// apiInternalIncentives = append(apiInternalIncentives, ApiInternalIncentive{ +// PoolPath: poolPath, +// Tier: internalTier.tier, +// StartTimestamp: internalTier.startTimestamp, +// RewardPerBlock: calculateInternalRewardPerBlockByPoolPath(poolPath), +// }) +// }) +// +// // STAT NODE +// _stat := json.ObjectNode("", map[string]*json.Node{ +// "height": json.NumberNode("height", float64(std.GetHeight())), +// "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), +// }) +// +// // RESPONSE (ARRAY) NODE +// responses := json.ArrayNode("", []*json.Node{}) +// for _, incentive := range apiInternalIncentives { +// _incentiveNode := json.ObjectNode("", map[string]*json.Node{ +// "poolPath": json.StringNode("poolPath", incentive.PoolPath), +// "rewardToken": json.StringNode("rewardToken", consts.GNS_PATH), +// "tier": json.NumberNode("tier", float64(incentive.Tier)), +// "startTimestamp": json.NumberNode("startTimestamp", float64(incentive.StartTimestamp)), +// "rewardPerBlock": json.StringNode("rewardPerBlock", incentive.RewardPerBlock), +// "accuGns": json.NumberNode("accuGns", float64(poolAccuGns[targetPoolPath])), +// }) +// responses.AppendArray(_incentiveNode) +// } +// +// // RETURN +// node := json.ObjectNode("", map[string]*json.Node{ +// "stat": _stat, +// "response": responses, +// }) +// +// b, err := json.Marshal(node) +// if err != nil { +// panic(err.Error()) +// } +// +// return string(b) +//} +// +//func ApiGetInternalIncentivesByTiers(targetTier uint64) string { +// en.MintAndDistributeGns() +// if consts.EMISSION_REFACTORED { +// CalcPoolPositionRefactor() +// } else { +// CalcPoolPosition() +// } +// +// apiInternalIncentives := []ApiInternalIncentive{} +// +// poolTiers.Iter(func(poolPath string, internalTier InternalTier) { +// if internalTier.tier != targetTier { +// return +// } +// +// apiInternalIncentives = append(apiInternalIncentives, ApiInternalIncentive{ +// PoolPath: poolPath, +// Tier: internalTier.tier, +// StartTimestamp: internalTier.startTimestamp, +// RewardPerBlock: calculateInternalRewardPerBlockByPoolPath(poolPath), +// }) +// }) +// +// // STAT NODE +// _stat := json.ObjectNode("", map[string]*json.Node{ +// "height": json.NumberNode("height", float64(std.GetHeight())), +// "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), +// }) +// +// // RESPONSE (ARRAY) NODE +// responses := json.ArrayNode("", []*json.Node{}) +// for _, incentive := range apiInternalIncentives { +// _incentiveNode := json.ObjectNode("", map[string]*json.Node{ +// "poolPath": json.StringNode("poolPath", incentive.PoolPath), +// "rewardToken": json.StringNode("rewardToken", consts.GNS_PATH), +// "tier": json.NumberNode("tier", float64(incentive.Tier)), +// "startTimestamp": json.NumberNode("startTimestamp", float64(incentive.StartTimestamp)), +// "rewardPerBlock": json.StringNode("rewardPerBlock", incentive.RewardPerBlock), +// "accuGns": json.NumberNode("accuGns", float64(poolAccuGns[incentive.PoolPath])), +// }) +// responses.AppendArray(_incentiveNode) +// } +// +// // RETURN +// node := json.ObjectNode("", map[string]*json.Node{ +// "stat": _stat, +// "response": responses, +// }) +// +// b, err := json.Marshal(node) +// if err != nil { +// panic(err.Error()) +// } +// +// return string(b) +//} +// +//func makeRewardTokensArray(rewardsTokenList []string) []*json.Node { +// rewardsTokenArray := make([]*json.Node, len(rewardsTokenList)) +// for i, rewardToken := range rewardsTokenList { +// rewardsTokenArray[i] = json.StringNode("", rewardToken) +// } +// return rewardsTokenArray +//} +// +//func calculateInternalRewardPerBlockByPoolPath(poolPath string) string { +// nowHeight := std.GetHeight() +// fullGnsForThisHeight := gns.GetAmountByHeight(nowHeight) +// +// // staker distribution pct +// bpsPct := en.GetDistributionPct(1) +// +// // calculate reward per block +// stakerGns := fullGnsForThisHeight * bpsPct / 10000 +// +// tier1Amount, tier2Amount, tier3Amount := getTiersAmount(stakerGns) +// tier1Num, tier2Num, tier3Num := getNumPoolTiers() +// +// internalTier, exist := poolTiers.Get(poolPath) +// if !exist { +// return "0" +// } +// +// tier := internalTier.Tier() +// +// if tier == 1 { +// return ufmt.Sprintf("%d", tier1Amount/tier1Num) +// } else if tier == 2 { +// return ufmt.Sprintf("%d", tier2Amount/tier2Num) +// } else if tier == 3 { +// return ufmt.Sprintf("%d", tier3Amount/tier3Num) +// } +// +// return "0" +//} +// +//func updateExternalIncentiveLeftAmount() { +// // external incentive reward left update +// for _, positionWarmUpAmount := range positionsExternalWarmUpAmount { +// for incentiveId, warmUpAmount := range positionWarmUpAmount { +// +// full := warmUpAmount.full100 + warmUpAmount.full70 + warmUpAmount.full50 + warmUpAmount.full30 +// +// ictv, exist := incentives.Get(incentiveId) +// if !exist { +// continue +// } +// ictv.rewardLeft = new(u256.Uint).Sub(ictv.rewardLeft, u256.NewUint(full)) +// incentives.Set(incentiveId, ictv) +// } +// } +//} + +// +//// LpTokenReward represents the rewards associated with a specific LP token +//type LpTokenReward struct { +// LpTokenId uint64 `json:"lpTokenId"` // The ID of the LP token +// Address string `json:"address"` // The address associated with the LP token +// Rewards []Reward `json:"rewards"` +//} +// +//// Reward represents a single reward for a staked LP token +//type Reward struct { +// IncentiveType string `json:"incentiveType"` // The type of incentive (INTERNAL or EXTERNAL) +// IncentiveId string `json:"incentiveId"` // The unique identifier of the incentive +// TargetPoolPath string `json:"targetPoolPath"` // The path of the target pool for the reward +// RewardTokenPath string `json:"rewardTokenPath"` // The pathe of the reward token +// RewardTokenAmount uint64 `json:"rewardTokenAmount"` // The amount of the reward token +// StakeTimestamp int64 `json:"stakeTimestamp"` // The timestamp when the LP token was staked +// StakeHeight int64 `json:"stakeHeight"` // The block height when the LP token was staked +// IncentiveStart int64 `json:"incentiveStart"` // The timestamp when the incentive started +//} +// +//// Stake represents a single stake +//type Stake struct { +// TokenId uint64 `json:"tokenId"` // The ID of the staked LP token +// Owner std.Address `json:"owner"` // The address of the owner of the staked LP token +// NumberOfStakes uint64 `json:"numberOfStakes"` // The number of times this LP token has been staked +// StakeTimestamp int64 `json:"stakeTimestamp"` // The timestamp when the LP token was staked +// StakeHeight int64 `json:"stakeHeight"` // The block height when the LP token was staked +// TargetPoolPath string `json:"targetPoolPath"` // The path of the target pool for the stake +//} +// +//// ResponseQueryBase contains basic information about a query response. +//type ResponseQueryBase struct { +// Height int64 `json:"height"` // The block height at the time of the query +// Timestamp int64 `json:"timestamp"` // The timestamp at the time of the query +//} +// +//// ResponseApiGetRewards represents the API response for getting rewards. +//type ResponseApiGetRewards struct { +// Stat ResponseQueryBase `json:"stat"` // Basic query information +// Response []LpTokenReward `json:"response"` // A slice of LpTokenReward structs +//} +// +//// ResponseApiGetRewardByLpTokenId represents the API response for getting rewards for a specific LP token. +//type ResponseApiGetRewardByLpTokenId struct { +// Stat ResponseQueryBase `json:"stat"` // Basic query information +// Response LpTokenReward `json:"response"` // The LpTokenReward for the specified LP token +//} +// +//// ResponseApiGetStakes represents the API response for getting stakes. +//type ResponseApiGetStakes struct { +// Stat ResponseQueryBase `json:"stat"` // Basic query information +// Response []Stake `json:"response"` // A slice of Stake structs +//} +// +//func ApiGetRewards() string { +// en.MintAndDistributeGns() +// if consts.EMISSION_REFACTORED { +// CalcPoolPositionRefactor() +// } else { +// CalcPoolPosition() +// } +// +// lpTokenRewards := []LpTokenReward{} +// +// // TODO: extract as function +// deposits.Iter(func(tokenId uint64, deposit Deposit) { +// rewards := []Reward{} +// +// // get internal gns reward +// internalWarmUpAmount, exist := positionsInternalWarmUpAmount[tokenId] +// if !exist { +// return +// } +// internalGNS := internalWarmUpAmount.give30 + internalWarmUpAmount.give50 + internalWarmUpAmount.give70 + internalWarmUpAmount.full100 +// +// if internalGNS > 0 { +// rewards = append(rewards, Reward{ +// IncentiveType: "INTERNAL", +// IncentiveId: "", +// TargetPoolPath: deposit.targetPoolPath, +// RewardTokenPath: consts.GNS_PATH, +// RewardTokenAmount: internalGNS, +// StakeTimestamp: deposit.stakeTimestamp, +// StakeHeight: deposit.stakeHeight, +// IncentiveStart: deposit.stakeTimestamp, +// }) +// } +// +// // find all external reward list for poolPath which lpTokenId is staked +// ictvList, exists := poolIncentives.Get(deposit.targetPoolPath) +// if !exists { +// return +// } +// +// for _, ictvId := range ictvList { +// ictv, exists := incentives.Get(ictvId) +// if !exists { +// return +// } +// +// stakedOrCreatedAt := common.I64Max(deposit.stakeTimestamp, ictv.startTimestamp) +// now := time.Now().Unix() +// if now < stakedOrCreatedAt { +// return +// } +// +// externalWarmUpAmount, exist := positionsExternalWarmUpAmount[tokenId][ictvId] +// if !exist { +// return +// } +// externalReward := externalWarmUpAmount.give30 + externalWarmUpAmount.give50 + externalWarmUpAmount.give70 + externalWarmUpAmount.full100 +// if externalReward >= 0 { +// rewards = append(rewards, Reward{ +// IncentiveType: "EXTERNAL", +// IncentiveId: ictvId, +// TargetPoolPath: deposit.targetPoolPath, +// RewardTokenPath: ictv.rewardToken, +// RewardTokenAmount: externalReward, +// StakeTimestamp: deposit.stakeTimestamp, +// StakeHeight: deposit.stakeHeight, +// IncentiveStart: ictv.startTimestamp, +// }) +// } +// } +// +// if len(rewards) > 0 { +// lpTokenReward := LpTokenReward{ +// LpTokenId: tokenId, +// Address: deposit.owner.String(), +// Rewards: rewards, +// } +// lpTokenRewards = append(lpTokenRewards, lpTokenReward) +// } +// }) +// +// qb := ResponseQueryBase{ +// Height: std.GetHeight(), +// Timestamp: time.Now().Unix(), +// } +// +// r := ResponseApiGetRewards{ +// Stat: qb, +// Response: lpTokenRewards, +// } +// +// // STAT NODE +// _stat := json.ObjectNode("", map[string]*json.Node{ +// "height": json.NumberNode("height", float64(std.GetHeight())), +// "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), +// }) +// +// // RESPONSE (ARRAY) NODE +// responses := json.ArrayNode("", []*json.Node{}) +// for _, reward := range r.Response { +// _rewardNode := json.ObjectNode("", map[string]*json.Node{ +// "lpTokenId": json.NumberNode("lpTokenId", float64(reward.LpTokenId)), +// "address": json.StringNode("address", reward.Address), +// "rewards": json.ArrayNode("rewards", makeRewardsArray(reward.Rewards)), +// }) +// responses.AppendArray(_rewardNode) +// } +// +// node := json.ObjectNode("", map[string]*json.Node{ +// "stat": _stat, +// "response": responses, +// }) +// +// b, err := json.Marshal(node) +// if err != nil { +// panic(err.Error()) +// } +// +// return string(b) +//} +// +//func ApiGetRewardsByLpTokenId(targetLpTokenId uint64) string { +// en.MintAndDistributeGns() +// if consts.EMISSION_REFACTORED { +// CalcPoolPositionRefactor() +// } else { +// CalcPoolPosition() +// } +// +// lpTokenRewards := []LpTokenReward{} +// +// deposits.Iter(func(tokenId uint64, deposit Deposit) { +// if tokenId != targetLpTokenId { +// return +// } +// +// rewards := []Reward{} +// +// // get internal gns reward +// internalWarmUpAmount, exist := positionsInternalWarmUpAmount[tokenId] +// if !exist { +// return +// } +// internalGNS := internalWarmUpAmount.give30 + internalWarmUpAmount.give50 + internalWarmUpAmount.give70 + internalWarmUpAmount.full100 +// +// if internalGNS > 0 { +// rewards = append(rewards, Reward{ +// IncentiveType: "INTERNAL", +// IncentiveId: "", +// TargetPoolPath: deposit.targetPoolPath, +// RewardTokenPath: consts.GNS_PATH, +// RewardTokenAmount: internalGNS, +// StakeTimestamp: deposit.stakeTimestamp, +// StakeHeight: deposit.stakeHeight, +// IncentiveStart: deposit.stakeTimestamp, +// }) +// } +// +// // find all external reward list for poolPath which lpTokenId is staked +// ictvList, exists := poolIncentives.Get(deposit.targetPoolPath) +// if !exists { +// return +// } +// +// for _, ictvId := range ictvList { +// ictv, exists := incentives.Get(ictvId) +// if !exists { +// return +// } +// +// stakedOrCreatedAt := common.I64Max(deposit.stakeTimestamp, ictv.startTimestamp) +// now := time.Now().Unix() +// if now < stakedOrCreatedAt { +// return +// } +// +// externalWarmUpAmount, exist := positionsExternalWarmUpAmount[tokenId][ictvId] +// if !exist { +// return +// } +// externalReward := externalWarmUpAmount.give30 + externalWarmUpAmount.give50 + externalWarmUpAmount.give70 + externalWarmUpAmount.full100 +// if externalReward > 0 { +// rewards = append(rewards, Reward{ +// IncentiveType: "EXTERNAL", +// IncentiveId: ictvId, +// TargetPoolPath: deposit.targetPoolPath, +// RewardTokenPath: ictv.rewardToken, +// RewardTokenAmount: externalReward, +// StakeTimestamp: deposit.stakeTimestamp, +// StakeHeight: deposit.stakeHeight, +// IncentiveStart: ictv.startTimestamp, +// }) +// } +// } +// +// lpTokenReward := LpTokenReward{ +// LpTokenId: tokenId, +// Address: deposit.owner.String(), +// Rewards: rewards, +// } +// lpTokenRewards = append(lpTokenRewards, lpTokenReward) +// }) +// +// qb := ResponseQueryBase{ +// Height: std.GetHeight(), +// Timestamp: time.Now().Unix(), +// } +// +// r := ResponseApiGetRewards{ +// Stat: qb, +// Response: lpTokenRewards, +// } +// +// // STAT NODE +// _stat := json.ObjectNode("", map[string]*json.Node{ +// "height": json.NumberNode("height", float64(std.GetHeight())), +// "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), +// }) +// +// // RESPONSE (ARRAY) NODE +// responses := json.ArrayNode("", []*json.Node{}) +// for _, reward := range r.Response { +// _rewardNode := json.ObjectNode("", map[string]*json.Node{ +// "lpTokenId": json.NumberNode("lpTokenId", float64(reward.LpTokenId)), +// "address": json.StringNode("address", reward.Address), +// "rewards": json.ArrayNode("rewards", makeRewardsArray(reward.Rewards)), +// }) +// responses.AppendArray(_rewardNode) +// } +// +// node := json.ObjectNode("", map[string]*json.Node{ +// "stat": _stat, +// "response": responses, +// }) +// +// b, err := json.Marshal(node) +// if err != nil { +// panic(err.Error()) +// } +// +// return string(b) +//} +// +//func ApiGetRewardsByAddress(targetAddress string) string { +// en.MintAndDistributeGns() +// if consts.EMISSION_REFACTORED { +// CalcPoolPositionRefactor() +// } else { +// CalcPoolPosition() +// } +// +// lpTokenRewards := []LpTokenReward{} +// +// deposits.Iter(func(tokenId uint64, deposit Deposit) { +// if deposit.owner.String() != targetAddress { +// return +// } +// +// rewards := []Reward{} +// +// // get internal gns reward +// internalWarmUpAmount, exist := positionsInternalWarmUpAmount[tokenId] +// if !exist { +// return +// } +// internalGNS := internalWarmUpAmount.give30 + internalWarmUpAmount.give50 + internalWarmUpAmount.give70 + internalWarmUpAmount.full100 +// +// if internalGNS > 0 { +// rewards = append(rewards, Reward{ +// IncentiveType: "INTERNAL", +// IncentiveId: "", +// TargetPoolPath: deposit.targetPoolPath, +// RewardTokenPath: consts.GNS_PATH, +// RewardTokenAmount: internalGNS, +// StakeTimestamp: deposit.stakeTimestamp, +// StakeHeight: deposit.stakeHeight, +// IncentiveStart: deposit.stakeTimestamp, +// }) +// } +// +// // find all external reward list for poolPath which lpTokenId is staked +// ictvList, exists := poolIncentives.Get(deposit.targetPoolPath) +// if !exists { +// return +// } +// +// for _, ictvId := range ictvList { +// ictv, exists := incentives.Get(ictvId) +// if !exists { +// return +// } +// +// stakedOrCreatedAt := common.I64Max(deposit.stakeTimestamp, ictv.startTimestamp) +// now := time.Now().Unix() +// if now < stakedOrCreatedAt { +// return +// } +// +// externalWarmUpAmount, exist := positionsExternalWarmUpAmount[tokenId][ictvId] +// if !exist { +// return +// } +// externalReward := externalWarmUpAmount.give30 + externalWarmUpAmount.give50 + externalWarmUpAmount.give70 + externalWarmUpAmount.full100 +// rewards = append(rewards, Reward{ +// IncentiveType: "EXTERNAL", +// IncentiveId: ictvId, +// TargetPoolPath: deposit.targetPoolPath, +// RewardTokenPath: ictv.rewardToken, +// RewardTokenAmount: externalReward, +// StakeTimestamp: deposit.stakeTimestamp, +// StakeHeight: deposit.stakeHeight, +// IncentiveStart: ictv.startTimestamp, +// }) +// } +// lpTokenReward := LpTokenReward{ +// LpTokenId: tokenId, +// Address: deposit.owner.String(), +// Rewards: rewards, +// } +// lpTokenRewards = append(lpTokenRewards, lpTokenReward) +// }) +// +// qb := ResponseQueryBase{ +// Height: std.GetHeight(), +// Timestamp: time.Now().Unix(), +// } +// +// r := ResponseApiGetRewards{ +// Stat: qb, +// Response: lpTokenRewards, +// } +// +// // STAT NODE +// _stat := json.ObjectNode("", map[string]*json.Node{ +// "height": json.NumberNode("height", float64(std.GetHeight())), +// "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), +// }) +// +// // RESPONSE (ARRAY) NODE +// responses := json.ArrayNode("", []*json.Node{}) +// for _, reward := range r.Response { +// _rewardNode := json.ObjectNode("", map[string]*json.Node{ +// "lpTokenId": json.NumberNode("lpTokenId", float64(reward.LpTokenId)), +// "address": json.StringNode("address", reward.Address), +// "rewards": json.ArrayNode("rewards", makeRewardsArray(reward.Rewards)), +// }) +// responses.AppendArray(_rewardNode) +// } +// +// node := json.ObjectNode("", map[string]*json.Node{ +// "stat": _stat, +// "response": responses, +// }) +// +// b, err := json.Marshal(node) +// if err != nil { +// panic(err.Error()) +// } +// +// return string(b) +//} +// +//func ApiGetStakes() string { +// en.MintAndDistributeGns() +// if consts.EMISSION_REFACTORED { +// CalcPoolPositionRefactor() +// } else { +// CalcPoolPosition() +// } +// +// stakes := []Stake{} +// deposits.Iter(func(tokenId uint64, deposit Deposit) { +// stakes = append(stakes, Stake{ +// TokenId: tokenId, +// Owner: deposit.owner, +// NumberOfStakes: deposit.numberOfStakes, +// StakeTimestamp: deposit.stakeTimestamp, +// StakeHeight: deposit.stakeHeight, +// TargetPoolPath: deposit.targetPoolPath, +// }) +// }) +// +// qb := ResponseQueryBase{ +// Height: std.GetHeight(), +// Timestamp: time.Now().Unix(), +// } +// +// r := ResponseApiGetStakes{ +// Stat: qb, +// Response: stakes, +// } +// +// // STAT NODE +// _stat := json.ObjectNode("", map[string]*json.Node{ +// "height": json.NumberNode("height", float64(std.GetHeight())), +// "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), +// }) +// +// // RESPONSE (ARRAY) NODE +// responses := json.ArrayNode("", []*json.Node{}) +// for _, stake := range r.Response { +// _stakeNode := json.ObjectNode("", map[string]*json.Node{ +// "tokenId": json.NumberNode("tokenId", float64(stake.TokenId)), +// "owner": json.StringNode("owner", stake.Owner.String()), +// "numberOfStakes": json.NumberNode("numberOfStakes", float64(stake.NumberOfStakes)), +// "stakeTimestamp": json.NumberNode("stakeTimestamp", float64(stake.StakeTimestamp)), +// "stakeHeight": json.NumberNode("stakeHeight", float64(stake.StakeHeight)), +// "targetPoolPath": json.StringNode("targetPoolPath", stake.TargetPoolPath), +// }) +// responses.AppendArray(_stakeNode) +// } +// +// node := json.ObjectNode("", map[string]*json.Node{ +// "stat": _stat, +// "response": responses, +// }) +// +// b, err := json.Marshal(node) +// if err != nil { +// panic(err.Error()) +// } +// +// return string(b) +//} +// +//func ApiGetStakesByLpTokenId(targetLpTokenId uint64) string { +// en.MintAndDistributeGns() +// if consts.EMISSION_REFACTORED { +// CalcPoolPositionRefactor() +// } else { +// CalcPoolPosition() +// } +// +// stakes := []Stake{} +// +// deposits.Iter(func(tokenId uint64, deposit Deposit) { +// if tokenId != targetLpTokenId { +// return +// } +// +// stakes = append(stakes, Stake{ +// TokenId: tokenId, +// Owner: deposit.owner, +// NumberOfStakes: deposit.numberOfStakes, +// StakeTimestamp: deposit.stakeTimestamp, +// StakeHeight: deposit.stakeHeight, +// TargetPoolPath: deposit.targetPoolPath, +// }) +// }) +// +// qb := ResponseQueryBase{ +// Height: std.GetHeight(), +// Timestamp: time.Now().Unix(), +// } +// +// r := ResponseApiGetStakes{ +// Stat: qb, +// Response: stakes, +// } +// +// // STAT NODE +// _stat := json.ObjectNode("", map[string]*json.Node{ +// "height": json.NumberNode("height", float64(std.GetHeight())), +// "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), +// }) +// +// // RESPONSE (ARRAY) NODE +// responses := json.ArrayNode("", []*json.Node{}) +// for _, stake := range r.Response { +// _stakeNode := json.ObjectNode("", map[string]*json.Node{ +// "tokenId": json.NumberNode("tokenId", float64(stake.TokenId)), +// "owner": json.StringNode("owner", stake.Owner.String()), +// "numberOfStakes": json.NumberNode("numberOfStakes", float64(stake.NumberOfStakes)), +// "stakeTimestamp": json.NumberNode("stakeTimestamp", float64(stake.StakeTimestamp)), +// "stakeHeight": json.NumberNode("stakeHeight", float64(stake.StakeHeight)), +// "targetPoolPath": json.StringNode("targetPoolPath", stake.TargetPoolPath), +// }) +// responses.AppendArray(_stakeNode) +// } +// +// node := json.ObjectNode("", map[string]*json.Node{ +// "stat": _stat, +// "response": responses, +// }) +// +// b, err := json.Marshal(node) +// if err != nil { +// panic(err.Error()) +// } +// +// return string(b) +//} +// +//func ApiGetStakesByAddress(targetAddress string) string { +// en.MintAndDistributeGns() +// if consts.EMISSION_REFACTORED { +// CalcPoolPositionRefactor() +// } else { +// CalcPoolPosition() +// } +// +// stakes := []Stake{} +// +// deposits.Iter(func(tokenId uint64, deposit Deposit) { +// if deposit.owner.String() != targetAddress { +// return +// } +// +// stakes = append(stakes, Stake{ +// TokenId: tokenId, +// Owner: deposit.owner, +// NumberOfStakes: deposit.numberOfStakes, +// StakeTimestamp: deposit.stakeTimestamp, +// StakeHeight: deposit.stakeHeight, +// TargetPoolPath: deposit.targetPoolPath, +// }) +// }) +// +// qb := ResponseQueryBase{ +// Height: std.GetHeight(), +// Timestamp: time.Now().Unix(), +// } +// +// r := ResponseApiGetStakes{ +// Stat: qb, +// Response: stakes, +// } +// +// // STAT NODE +// _stat := json.ObjectNode("", map[string]*json.Node{ +// "height": json.NumberNode("height", float64(std.GetHeight())), +// "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), +// }) +// +// // RESPONSE (ARRAY) NODE +// responses := json.ArrayNode("", []*json.Node{}) +// for _, stake := range r.Response { +// _stakeNode := json.ObjectNode("", map[string]*json.Node{ +// "tokenId": json.NumberNode("tokenId", float64(stake.TokenId)), +// "owner": json.StringNode("owner", stake.Owner.String()), +// "numberOfStakes": json.NumberNode("numberOfStakes", float64(stake.NumberOfStakes)), +// "stakeTimestamp": json.NumberNode("stakeTimestamp", float64(stake.StakeTimestamp)), +// "stakeHeight": json.NumberNode("stakeHeight", float64(stake.StakeHeight)), +// "targetPoolPath": json.StringNode("targetPoolPath", stake.TargetPoolPath), +// }) +// responses.AppendArray(_stakeNode) +// } +// +// node := json.ObjectNode("", map[string]*json.Node{ +// "stat": _stat, +// "response": responses, +// }) +// +// b, err := json.Marshal(node) +// if err != nil { +// panic(err.Error()) +// } +// +// return string(b) +//} +// +//// for off chain to check if lpTokenId is staked via RPC +//func IsStaked(tokenId uint64) bool { +// _, exist := deposits[tokenId] +// return exist +//} +// +//func makeRewardsArray(rewards []Reward) []*json.Node { +// rewardsArray := make([]*json.Node, len(rewards)) +// +// for i, reward := range rewards { +// rewardsArray[i] = json.ObjectNode("", map[string]*json.Node{ +// "incentiveType": json.StringNode("incentiveType", reward.IncentiveType), +// "incentiveId": json.StringNode("incentiveId", reward.IncentiveId), +// "targetPoolPath": json.StringNode("targetPoolPath", reward.TargetPoolPath), +// "rewardTokenPath": json.StringNode("rewardTokenPath", reward.RewardTokenPath), +// "rewardTokenAmount": json.NumberNode("rewardTokenAmount", float64(reward.RewardTokenAmount)), +// "stakeTimestamp": json.NumberNode("stakeTimestamp", float64(reward.StakeTimestamp)), +// "stakeHeight": json.NumberNode("stakeHeight", float64(reward.StakeHeight)), +// "incentiveStart": json.NumberNode("incentiveStart", float64(reward.IncentiveStart)), +// }) +// } +// return rewardsArray +//} diff --git a/staker/api_calculation_base_data_test.gnoA b/staker/api_calculation_base_data_test.gnoA deleted file mode 100644 index a0d40434..00000000 --- a/staker/api_calculation_base_data_test.gnoA +++ /dev/null @@ -1,199 +0,0 @@ -package staker - -import ( - "std" - "testing" - - "gno.land/p/demo/json" - "gno.land/p/demo/uassert" - - u256 "gno.land/p/gnoswap/uint256" -) - -func TestGetPoolStakedLiquidityUpdates(t *testing.T) { - pools = NewPools() - - t.Run("not exist pool", func(t *testing.T) { - result := GetPoolStakedLiquidityUpdates("invalid_pool", 0, 100) - if result != "" { - t.Errorf("not exist pool should return empty string. got: %s", result) - } - }) - - t.Run("valid pool", func(t *testing.T) { - poolPath := "test_pool" - pool := &Pool{ - stakedLiquidity: NewUintTree(), - } - pools.Set(poolPath, pool) - - testData := []struct { - height uint64 - value string - }{ - {10, "1000"}, - {20, "2000"}, - {30, "3000"}, - } - - for _, td := range testData { - pool.stakedLiquidity.Set(td.height, u256.MustFromDecimal(td.value)) - } - - result := GetPoolStakedLiquidityUpdates(poolPath, 0, 100) - - node, err := json.Unmarshal([]byte(result)) - if err != nil { - uassert.NoError(t, err) - } - - array, err := node.GetKey("") - uassert.NoError(t, err) - uassert.Equal(t, array.Size(), 3) - - expectedData := []struct { - blockNumber string - liquidity string - }{ - {"10", "1000"}, - {"20", "2000"}, - {"30", "3000"}, - } - - for i, expect := range expectedData { - item := array.MustIndex(i) - - blockNum, err := item.MustKey("blockNumber").GetString() - uassert.NoError(t, err) - uassert.Equal(t, blockNum, expect.blockNumber) - - liquidity, err := item.MustKey("liquidity").GetString() - uassert.NoError(t, err) - uassert.Equal(t, liquidity, expect.liquidity) - } - }) -} - -func TestGetPoolRewardUpdates(t *testing.T) { - pools = NewPools() - - t.Run("not exist pool", func(t *testing.T) { - result := GetPoolRewardUpdates("invalid_pool", 0, 100) - if result != "" { - t.Errorf("not exist pool should return empty string. got: %s", result) - } - }) - - t.Run("valid pool", func(t *testing.T) { - poolPath := "test_pool" - pool := &Pool{ - rewardCache: NewRewardCacheTree(), - } - pools.Set(poolPath, pool) - - testData := []struct { - height uint64 - value string - }{ - {15, "500"}, - {25, "1500"}, - {35, "2500"}, - } - - for _, td := range testData { - pool.rewardCache.Set(td.height, u256.MustFromDecimal(td.value)) - } - - result := GetPoolRewardUpdates(poolPath, 0, 100) - - node, err := json.Unmarshal([]byte(result)) - if err != nil { - uassert.NoError(t, err) - } - - array, err := node.GetKey("") - uassert.NoError(t, err) - uassert.Equal(t, array.Size(), 3) - - expectedData := []struct { - blockNumber string - reward string - }{ - {"15", "500"}, - {"25", "1500"}, - {"35", "2500"}, - } - - for i, expect := range expectedData { - item := array.MustIndex(i) - - blockNum, err := item.MustKey("blockNumber").GetString() - uassert.NoError(t, err) - uassert.Equal(t, blockNum, expect.blockNumber) - - reward, err := item.MustKey("reward").GetString() - uassert.NoError(t, err) - uassert.Equal(t, reward, expect.reward) - } - }) -} - -func TestGetWarmUpPeriods(t *testing.T) { - tests := []struct { - name string - currentHeight int64 - expectedOutput string - }{ - { - name: "normal warm-up period", - currentHeight: 100, - expectedOutput: "30*STAKER*50*STAKER*70*STAKER*100", - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - result := GetWarmUpPeriods() - uassert.Equal(t, result, tt.expectedOutput) - }) - } -} - -func TestGetWarmUpPeriodsWithModifiedTemplate(t *testing.T) { - originalTemplate := make([]Warmup, len(warmupTemplate)) - copy(originalTemplate, warmupTemplate) - - defer func() { - warmupTemplate = originalTemplate - }() - - modifyWarmup(0, 1000) - - result := GetWarmUpPeriods() - expected := "30*STAKER*50*STAKER*70*STAKER*100" - - uassert.Equal(t, result, expected) -} - -func TestGetWarmUpPeriodsEdgeCases(t *testing.T) { - tests := []struct { - name string - expectedOutput string - }{ - { - name: "test 1", - expectedOutput: "30*STAKER*50*STAKER*70*STAKER*100", - }, - { - name: "test 2", - expectedOutput: "30*STAKER*50*STAKER*70*STAKER*100", - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - result := GetWarmUpPeriods() - uassert.Equal(t, result, tt.expectedOutput) - }) - } -} diff --git a/staker/api_calculation_base_data_test.gnoB b/staker/api_calculation_base_data_test.gnoB deleted file mode 100644 index a0d40434..00000000 --- a/staker/api_calculation_base_data_test.gnoB +++ /dev/null @@ -1,199 +0,0 @@ -package staker - -import ( - "std" - "testing" - - "gno.land/p/demo/json" - "gno.land/p/demo/uassert" - - u256 "gno.land/p/gnoswap/uint256" -) - -func TestGetPoolStakedLiquidityUpdates(t *testing.T) { - pools = NewPools() - - t.Run("not exist pool", func(t *testing.T) { - result := GetPoolStakedLiquidityUpdates("invalid_pool", 0, 100) - if result != "" { - t.Errorf("not exist pool should return empty string. got: %s", result) - } - }) - - t.Run("valid pool", func(t *testing.T) { - poolPath := "test_pool" - pool := &Pool{ - stakedLiquidity: NewUintTree(), - } - pools.Set(poolPath, pool) - - testData := []struct { - height uint64 - value string - }{ - {10, "1000"}, - {20, "2000"}, - {30, "3000"}, - } - - for _, td := range testData { - pool.stakedLiquidity.Set(td.height, u256.MustFromDecimal(td.value)) - } - - result := GetPoolStakedLiquidityUpdates(poolPath, 0, 100) - - node, err := json.Unmarshal([]byte(result)) - if err != nil { - uassert.NoError(t, err) - } - - array, err := node.GetKey("") - uassert.NoError(t, err) - uassert.Equal(t, array.Size(), 3) - - expectedData := []struct { - blockNumber string - liquidity string - }{ - {"10", "1000"}, - {"20", "2000"}, - {"30", "3000"}, - } - - for i, expect := range expectedData { - item := array.MustIndex(i) - - blockNum, err := item.MustKey("blockNumber").GetString() - uassert.NoError(t, err) - uassert.Equal(t, blockNum, expect.blockNumber) - - liquidity, err := item.MustKey("liquidity").GetString() - uassert.NoError(t, err) - uassert.Equal(t, liquidity, expect.liquidity) - } - }) -} - -func TestGetPoolRewardUpdates(t *testing.T) { - pools = NewPools() - - t.Run("not exist pool", func(t *testing.T) { - result := GetPoolRewardUpdates("invalid_pool", 0, 100) - if result != "" { - t.Errorf("not exist pool should return empty string. got: %s", result) - } - }) - - t.Run("valid pool", func(t *testing.T) { - poolPath := "test_pool" - pool := &Pool{ - rewardCache: NewRewardCacheTree(), - } - pools.Set(poolPath, pool) - - testData := []struct { - height uint64 - value string - }{ - {15, "500"}, - {25, "1500"}, - {35, "2500"}, - } - - for _, td := range testData { - pool.rewardCache.Set(td.height, u256.MustFromDecimal(td.value)) - } - - result := GetPoolRewardUpdates(poolPath, 0, 100) - - node, err := json.Unmarshal([]byte(result)) - if err != nil { - uassert.NoError(t, err) - } - - array, err := node.GetKey("") - uassert.NoError(t, err) - uassert.Equal(t, array.Size(), 3) - - expectedData := []struct { - blockNumber string - reward string - }{ - {"15", "500"}, - {"25", "1500"}, - {"35", "2500"}, - } - - for i, expect := range expectedData { - item := array.MustIndex(i) - - blockNum, err := item.MustKey("blockNumber").GetString() - uassert.NoError(t, err) - uassert.Equal(t, blockNum, expect.blockNumber) - - reward, err := item.MustKey("reward").GetString() - uassert.NoError(t, err) - uassert.Equal(t, reward, expect.reward) - } - }) -} - -func TestGetWarmUpPeriods(t *testing.T) { - tests := []struct { - name string - currentHeight int64 - expectedOutput string - }{ - { - name: "normal warm-up period", - currentHeight: 100, - expectedOutput: "30*STAKER*50*STAKER*70*STAKER*100", - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - result := GetWarmUpPeriods() - uassert.Equal(t, result, tt.expectedOutput) - }) - } -} - -func TestGetWarmUpPeriodsWithModifiedTemplate(t *testing.T) { - originalTemplate := make([]Warmup, len(warmupTemplate)) - copy(originalTemplate, warmupTemplate) - - defer func() { - warmupTemplate = originalTemplate - }() - - modifyWarmup(0, 1000) - - result := GetWarmUpPeriods() - expected := "30*STAKER*50*STAKER*70*STAKER*100" - - uassert.Equal(t, result, expected) -} - -func TestGetWarmUpPeriodsEdgeCases(t *testing.T) { - tests := []struct { - name string - expectedOutput string - }{ - { - name: "test 1", - expectedOutput: "30*STAKER*50*STAKER*70*STAKER*100", - }, - { - name: "test 2", - expectedOutput: "30*STAKER*50*STAKER*70*STAKER*100", - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - result := GetWarmUpPeriods() - uassert.Equal(t, result, tt.expectedOutput) - }) - } -} diff --git a/staker/getter.gno b/staker/getter.gno index 0278b8b2..3a5daf86 100644 --- a/staker/getter.gno +++ b/staker/getter.gno @@ -2,10 +2,14 @@ package staker import ( "std" + "strconv" + "time" - ufmt "gno.land/p/demo/ufmt" - + "gno.land/p/demo/ufmt" u256 "gno.land/p/gnoswap/uint256" + + "gno.land/r/gnoswap/v1/consts" + "gno.land/r/gnoswap/v1/gns" ) //! TODO: Change function names to follow a naming convention @@ -222,37 +226,6 @@ func StakerDepositOwner(lpTokenId uint64) std.Address { return deposit.owner } -// StakerDepositNumberOfStakes returns the number of stakes for a given LP token ID -// -// Parameters: -// - lpTokenId (uint64): The ID of the LP token -// -// Returns: -// - uint64: The number of stakes for the deposit -// -// Panics: -// - If the deposit does not exist for the given lpTokenId -/* -func StakerDepositNumberOfStakes(lpTokenId uint64) uint64 { - en.MintAndDistributeGns() - if consts.EMISSION_REFACTORED { - CalcPoolPositionRefactor() - } else { - CalcPoolPosition() - } - - deposit, exist := deposits.Get(lpTokenId) - if !exist { - panic(addDetailToError( - errDataNotFound, - ufmt.Sprintf("tokenId(%d) deposit does not exist", lpTokenId), - )) - } - - return deposit.numberOfStakes -} -*/ - // StakerDepositStakeTimestamp returns the stake timestamp for a given LP token ID // // Parameters: @@ -304,3 +277,407 @@ func StakerDepositTargetPoolPath(lpTokenId uint64) string { func StakerPoolTier(poolPath string) uint64 { return poolTier.CurrentTier(poolPath, uint64(std.GetHeight())) } + +type currentExternalInfo struct { + height int64 + time int64 + externalIncentives []ExternalIncentive +} + +func printExternalInfo(poolPath string) { + println("***********************") + println("> height:", std.GetHeight()) + println("> time:", time.Now().Unix()) + println("[ START ] GET_EXTERNAL INCENTIVE") + pool, ok := pools.Get(poolPath) + if !ok { + println(" > pool not found") + return + } + if pool.IsExternallyIncentivizedPool(uint64(std.GetHeight())) { + pool.incentives.byHeight.Iterate("", "", func(key string, value interface{}) bool { + incentive := value.(*ExternalIncentive) + println(" > incentiveId:", incentive.incentiveId) + println(" > targetPoolPath:", incentive.targetPoolPath) + println(" > rewardToken:", incentive.rewardToken) + println(" > rewardAmount:", strconv.FormatUint(incentive.rewardAmount, 10)) + println(" > startTimestamp:", strconv.FormatInt(incentive.startTimestamp, 10)) + println(" > endTimestamp:", strconv.FormatInt(incentive.endTimestamp, 10)) + rewardPerBlockU256 := u256.MustFromDecimal(strconv.FormatUint(incentive.rewardPerBlock, 10)) + q96 := u256.MustFromDecimal(consts.Q96) + rewardPerBlockX96 := u256.Zero().Mul(rewardPerBlockU256, q96) + println(" > rewardPerBlockX96:", rewardPerBlockX96.ToString()) + println(" > rewardLeft:", strconv.FormatUint(incentive.rewardLeft, 10)) + println(" > refundee:", incentive.refundee.String()) + return false + }) + } else { + println(" > pool is not externally incentivized") + } + println("[ END ] GET_EXTERNAL INCENTIVE") +} + +type ApiExternalDebugInfo struct { + Height int64 `json:"height"` + Time int64 `json:"time"` + Position []ApiExternalDebugPosition `json:"pool"` +} + +type ApiExternalDebugPosition struct { + LpTokenId uint64 `json:"lpTokenId"` + StakedHeight int64 `json:"stakedHeight"` + StakedTimestamp int64 `json:"stakedTimestamp"` + Incentive []ApiExternalDebugIncentive `json:"incentive"` +} + +type ApiExternalDebugIncentive struct { + PoolPath string `json:"poolPath"` + IncentiveId string `json:"incentiveId"` + RewardToken string `json:"rewardToken"` + RewardAmount string `json:"rewardAmount"` + RewardLeft string `json:"rewardLeft"` + StartTimestamp int64 `json:"startTimestamp"` + EndTimestamp int64 `json:"endTimestamp"` + RewardPerBlockX96 string `json:"rewardPerBlockX96"` + RewardPerBlock string `json:"rewardPerBlock"` + Refundee std.Address `json:"refundee"` + // FROM positionExternal -> externalRewards + tokenAmountX96 *u256.Uint `json:"tokenAmountX96"` + tokenAmount uint64 `json:"tokenAmount"` + tokenAmountFull uint64 `json:"tokenAmountFull"` + tokenAmountToGive uint64 `json:"tokenAmountToGive"` + // FROM externalWarmUpAmount + full30 uint64 `json:"full30"` + give30 uint64 `json:"give30"` + full50 uint64 `json:"full50"` + give50 uint64 `json:"give50"` + full70 uint64 `json:"full70"` + give70 uint64 `json:"give70"` + full100 uint64 `json:"full100"` +} + +// +//func GetPrintExternalInfo() string { +// // TODO: LIMIT ONLY ABCI_QUERY CAN CALL THIS +// +// updateExternalIncentiveLeftAmount() +// +// externalDebug := ApiExternalDebugInfo{} +// externalDebug.Height = std.GetHeight() +// externalDebug.Time = time.Now().Unix() +// +// externalPositions := []ApiExternalDebugPosition{} +// for lpTokenId, externals := range positionExternal { +// externalPosition := ApiExternalDebugPosition{} +// externalPosition.LpTokenId = lpTokenId +// deposit, _ := deposits.get(lpTokenId) +// externalPosition.StakedHeight = deposit.stakeHeight +// externalPosition.StakedTimestamp = deposit.stakeTimestamp +// +// externalIncentives := []ApiExternalDebugIncentive{} +// for incentiveId, external := range externals { +// externalIncentive := ApiExternalDebugIncentive{} +// +// externalIncentive.PoolPath = external.poolPath +// externalIncentive.RewardToken = external.tokenPath +// +// incentive := incentives[incentiveId] +// externalIncentive.IncentiveId = incentiveId +// externalIncentive.RewardAmount = incentive.rewardAmount.ToString() +// externalIncentive.RewardLeft = incentive.rewardLeft.ToString() +// externalIncentive.StartTimestamp = incentive.startTimestamp +// externalIncentive.EndTimestamp = incentive.endTimestamp +// externalIncentive.RewardPerBlockX96 = incentive.rewardPerBlockX96.ToString() +// externalIncentive.RewardPerBlock = new(u256.Uint).Div(incentive.rewardPerBlockX96, u256.MustFromDecimal(consts.Q96)).ToString() +// externalIncentive.Refundee = incentive.refundee +// +// externalIncentive.tokenAmountX96 = external.tokenAmountX96 +// +// externalWarmUpAmount, exist := positionsExternalWarmUpAmount[lpTokenId][incentiveId] +// if !exist { +// continue +// } +// fullAmount := externalWarmUpAmount.full30 + externalWarmUpAmount.full50 + externalWarmUpAmount.full70 + externalWarmUpAmount.full100 +// toGive := externalWarmUpAmount.give30 + externalWarmUpAmount.give50 + externalWarmUpAmount.give70 + externalWarmUpAmount.full100 +// +// externalIncentive.full30 = externalWarmUpAmount.full30 +// externalIncentive.give30 = externalWarmUpAmount.give30 +// externalIncentive.full50 = externalWarmUpAmount.full50 +// externalIncentive.give50 = externalWarmUpAmount.give50 +// externalIncentive.full70 = externalWarmUpAmount.full70 +// externalIncentive.give70 = externalWarmUpAmount.give70 +// externalIncentive.full100 = externalWarmUpAmount.full100 +// +// externalIncentive.tokenAmountFull = fullAmount +// externalIncentive.tokenAmountToGive = toGive +// +// externalIncentives = append(externalIncentives, externalIncentive) +// } +// externalPosition.Incentive = externalIncentives +// +// externalPositions = append(externalPositions, externalPosition) +// } +// +// externalDebug.Position = externalPositions +// +// // MARSHAL +// node := json.ObjectNode("", map[string]*json.Node{ +// "height": json.NumberNode("", float64(externalDebug.Height)), +// "time": json.NumberNode("", float64(externalDebug.Time)), +// "position": json.ArrayNode("", makeExternalPositionsNode(externalDebug.Position)), +// }) +// +// b, err := json.Marshal(node) +// if err != nil { +// return "JSON MARSHAL ERROR" +// } +// +// return string(b) +// +// panic("SHOULD BE REIMPLEMENTED BEFORE MERGING") +//} +// +//func makeExternalPositionsNode(positions []ApiExternalDebugPosition) []*json.Node { +// externalPositions := make([]*json.Node, 0) +// +// for _, externalPosition := range positions { +// incentives := make([]*json.Node, 0) +// for _, incentive := range externalPosition.Incentive { +// deposit, _ := deposits.get(externalPosition.LpTokenId) +// _max := common.I64Max(incentive.StartTimestamp, deposit.stakeTimestamp) +// stakedOrExternalDuration := (time.Now().Unix() - _max) / consts.BLOCK_GENERATION_INTERVAL +// +// incentives = append(incentives, json.ObjectNode("", map[string]*json.Node{ +// "poolPath": json.StringNode("poolPath", incentive.PoolPath), +// "rewardToken": json.StringNode("rewardToken", incentive.RewardToken), +// "rewardAmount": json.StringNode("rewardAmount", incentive.RewardAmount), +// "startTimestamp": json.NumberNode("startTimestamp", float64(incentive.StartTimestamp)), +// "endTimestamp": json.NumberNode("endTimestamp", float64(incentive.EndTimestamp)), +// "rewardPerBlockX96": json.StringNode("rewardPerBlockX96", incentive.RewardPerBlockX96), +// "stakedOrExternalDuration": json.NumberNode("stakedOrExternalDuration", float64(stakedOrExternalDuration)), +// "rewardPerBlock": json.StringNode("rewardPerBlock", incentive.RewardPerBlock), +// "refundee": json.StringNode("refundee", incentive.Refundee.String()), +// // "tokenAmountX96": json.StringNode("tokenAmountX96", incentive.tokenAmountX96.ToString()), +// // "tokenAmount": json.NumberNode("tokenAmount", float64(new(u256.Uint).Div(incentive.tokenAmountX96, _q96).Uint64())), +// "tokenAmountFull": json.NumberNode("tokenAmountFull", float64(incentive.tokenAmountFull)), +// "tokenAmountToGive": json.NumberNode("tokenAmountToGive", float64(incentive.tokenAmountToGive)), +// // +// "full30": json.NumberNode("full30", float64(incentive.full30)), +// "give30": json.NumberNode("give30", float64(incentive.give30)), +// "full50": json.NumberNode("full50", float64(incentive.full50)), +// "give50": json.NumberNode("give50", float64(incentive.give50)), +// "full70": json.NumberNode("full70", float64(incentive.full70)), +// "give70": json.NumberNode("give70", float64(incentive.give70)), +// "full100": json.NumberNode("full100", float64(incentive.full100)), +// })) +// } +// +// externalPositions = append(externalPositions, json.ObjectNode("", map[string]*json.Node{ +// "lpTokenId": json.NumberNode("lpTokenId", float64(externalPosition.LpTokenId)), +// "stakedHeight": json.NumberNode("stakedHeight", float64(externalPosition.StakedHeight)), +// "stakedTimestamp": json.NumberNode("stakedTimestamp", float64(externalPosition.StakedTimestamp)), +// "incentive": json.ArrayNode("", incentives), +// })) +// } +// +// return externalPositions +//} + +// DEBUG INTERNAL (GNS EMISSION) +type currentInfo struct { + height int64 + time int64 + gnsStaker uint64 + gnsDevOps uint64 + gnsCommunityPool uint64 + gnsGovStaker uint64 + gnsProtocolFee uint64 + gnsADMIN uint64 +} + +func getCurrentInfo() currentInfo { + return currentInfo{ + height: std.GetHeight(), + time: time.Now().Unix(), + gnsStaker: gns.BalanceOf(a2u(consts.STAKER_ADDR)), + gnsDevOps: gns.BalanceOf(a2u(consts.DEV_OPS)), + gnsCommunityPool: gns.BalanceOf(a2u(consts.COMMUNITY_POOL_ADDR)), + gnsGovStaker: gns.BalanceOf(a2u(consts.GOV_STAKER_ADDR)), + gnsProtocolFee: gns.BalanceOf(a2u(consts.PROTOCOL_FEE_ADDR)), + gnsADMIN: gns.BalanceOf(a2u(consts.ADMIN)), + } +} + +func printInfo(prev currentInfo) currentInfo { + curr := getCurrentInfo() + /* + println("***********************") + println("> height:", curr.height) + println("> height inc by:", curr.height-prev.height) + println("> time:", curr.time) + println("> time inc by:", curr.time-prev.time) + println("GNS BALANCE CHANGE") + println("> staker_bal\t\t", curr.gnsStaker) + println("> staker_chg\t\t", int64(curr.gnsStaker-prev.gnsStaker)) + println("> dev ops\t\t", curr.gnsDevOps) + println("> dev ops_chg\t\t", int(curr.gnsDevOps-prev.gnsDevOps)) + println("> community pool_bal\t", curr.gnsCommunityPool) + println("> community pool_chg\t", int(curr.gnsCommunityPool-prev.gnsCommunityPool)) + println("> gov_staker_bal\t\t", curr.gnsGovStaker) + println("> gov_staker_chg\t\t", int(curr.gnsGovStaker-prev.gnsGovStaker)) + println("> protocol fee_bal\t", curr.gnsProtocolFee) + println("> protocol fee_chg\t", int(curr.gnsProtocolFee-prev.gnsProtocolFee)) + println("> ADMIN_bal\t\t", curr.gnsADMIN) + println("> ADMIN_chg\t\t", int(curr.gnsADMIN-prev.gnsADMIN)) + println("GNS POOL") + for k, v := range poolGns { + println("> poolPath:", k, "amount:", v) + } + + + println("GNS POSITION") + for k, v := range positionGns { + posWarmCalc := positionsInternalWarmUpAmount[k] + println("> tokenId:", k, "amount:", v, "warmUp:", getRewardRatio(curr.height-deposits[k].stakeHeight)) + println("> 100%", "full", posWarmCalc.full100, "give", posWarmCalc.full100) + println("> 70%", "full", posWarmCalc.full70, "give", posWarmCalc.give70) + println("> 50%", "full", posWarmCalc.full50, "give", posWarmCalc.give50) + println("> 30%", "full", posWarmCalc.full30, "give", posWarmCalc.give30) + } + */ + //panic("SHOULD BE REIMPLEMENTED BEFORE MERGING") + + return curr +} + +// +//func GetPrintInfo() string { +// emissionDebug := ApiEmissionDebugInfo{} +// emissionDebug.Height = std.GetHeight() +// emissionDebug.Time = time.Now().Unix() +// emissionDebug.GnsStaker = gns.BalanceOf(a2u(consts.STAKER_ADDR)) +// emissionDebug.GnsDevOps = gns.BalanceOf(a2u(consts.DEV_OPS)) +// emissionDebug.GnsCommunityPool = gns.BalanceOf(a2u(consts.COMMUNITY_POOL_ADDR)) +// emissionDebug.GnsGovStaker = gns.BalanceOf(a2u(consts.GOV_STAKER_ADDR)) +// emissionDebug.GnsProtocolFee = gns.BalanceOf(a2u(consts.PROTOCOL_FEE_ADDR)) +// emissionDebug.GnsADMIN = gns.BalanceOf(a2u(consts.ADMIN)) +// +// poolTiers.Iter(func(poolPath string, internalTier InternalTier) { +// tier := internalTier.tier +// pool := ApiEmissionDebugPool{} +// pool.PoolPath = poolPath +// pool.Tier = tier +// +// numTier1, numTier2, numTier3 := getNumPoolTiers(t) +// if tier == 1 { +// pool.NumPoolInSameTier = numTier1 +// } else if tier == 2 { +// pool.NumPoolInSameTier = numTier2 +// } else if tier == 3 { +// pool.NumPoolInSameTier = numTier3 +// } +// +// for lpTokenId, deposit := range deposits { +// if deposit.targetPoolPath == poolPath { +// position := ApiEmissionDebugPosition{} +// position.LpTokenId = lpTokenId +// position.StakedHeight = deposit.stakeHeight +// position.StakedTimestamp = deposit.stakeTimestamp +// position.StakedDuration = emissionDebug.Height - deposit.stakeHeight +// +// position.Ratio = getRewardRatio(t, position.StakedDuration) +// pool.Position = append(pool.Position, position) +// } +// } +// +// emissionDebug.Pool = append(emissionDebug.Pool, pool) +// }) +// +// node := json.ObjectNode("", map[string]*json.Node{ +// "height": json.NumberNode("", float64(emissionDebug.Height)), +// "time": json.NumberNode("", float64(emissionDebug.Time)), +// "gns": json.ObjectNode("", map[string]*json.Node{ +// "staker": json.NumberNode("", float64(emissionDebug.GnsStaker)), +// "devOps": json.NumberNode("", float64(emissionDebug.GnsDevOps)), +// "communityPool": json.NumberNode("", float64(emissionDebug.GnsCommunityPool)), +// "govStaker": json.NumberNode("", float64(emissionDebug.GnsGovStaker)), +// "protocolFee": json.NumberNode("", float64(emissionDebug.GnsProtocolFee)), +// "GnoswapAdmin": json.NumberNode("", float64(emissionDebug.GnsADMIN)), +// }), +// "pool": json.ArrayNode("", makePoolsNode(emissionDebug.Pool)), +// }) +// +// b, err := json.Marshal(node) +// if err != nil { +// return "JSON MARSHAL ERROR" +// } +// +// return string(b) +//} +// +//func makePoolsNode(emissionPool []ApiEmissionDebugPool) []*json.Node { +// pools := make([]*json.Node, 0) +// +// poolTiers.Iter(func(poolPath string, internalTier InternalTier) { +// numTier1, numTier2, numTier3 := getNumPoolTiers(t) +// numPoolSameTier := uint64(0) +// tier := internalTier.tier +// if tier == 1 { +// numPoolSameTier = numTier1 +// } else if tier == 2 { +// numPoolSameTier = numTier2 +// } else if tier == 3 { +// numPoolSameTier = numTier3 +// } +// +// pools = append(pools, json.ObjectNode("", map[string]*json.Node{ +// "poolPath": json.StringNode("poolPath", poolPath), +// "startTimestamp": json.NumberNode("startTimestamp", float64(internalTier.startTimestamp)), +// "tier": json.NumberNode("tier", float64(tier)), +// "numPoolSameTier": json.NumberNode("numPoolSameTier", float64(numPoolSameTier)), +// "position": json.ArrayNode("", makePositionsNode(poolPath)), +// })) +// }) +// +// return pools +//} +// +//func makePositionsNode(poolPath string) []*json.Node { +// /* +// positions := make([]*json.Node, 0) +// +// for lpTokenId, deposit := range deposits { +// if deposit.targetPoolPath == poolPath { +// stakedDuration := std.GetHeight() - deposit.stakeHeight +// ratio := getRewardRatio(stakedDuration) +// +// internalWarmUpAmount, exist := positionsInternalWarmUpAmount[lpTokenId] +// if !exist { +// continue +// } +// fullAmount := internalWarmUpAmount.full30 + internalWarmUpAmount.full50 + internalWarmUpAmount.full70 + internalWarmUpAmount.full100 +// warmUpAmount := internalWarmUpAmount.give30 + internalWarmUpAmount.give50 + internalWarmUpAmount.give70 + internalWarmUpAmount.full100 +// +// positions = append(positions, json.ObjectNode("", map[string]*json.Node{ +// "lpTokenId": json.NumberNode("lpTokenId", float64(lpTokenId)), +// "stakedHeight": json.NumberNode("stakedHeight", float64(deposit.stakeHeight)), +// "stakedTimestamp": json.NumberNode("stakedTimestamp", float64(deposit.stakeTimestamp)), +// "stakedDuration": json.NumberNode("stakedDuration", float64(stakedDuration)), +// "fullAmount": json.NumberNode("fullAmount", float64(fullAmount)), +// "ratio": json.NumberNode("ratio", float64(ratio)), +// "warmUpAmount": json.NumberNode("warmUpAmount", float64(warmUpAmount)), +// "full30": json.NumberNode("full30", float64(internalWarmUpAmount.full30)), +// "give30": json.NumberNode("give30", float64(internalWarmUpAmount.give30)), +// "full50": json.NumberNode("full50", float64(internalWarmUpAmount.full50)), +// "give50": json.NumberNode("give50", float64(internalWarmUpAmount.give50)), +// "full70": json.NumberNode("full70", float64(internalWarmUpAmount.full70)), +// "give70": json.NumberNode("give70", float64(internalWarmUpAmount.give70)), +// "full100": json.NumberNode("full100", float64(internalWarmUpAmount.full100)), +// })) +// } +// } +// +// return positions +// */ +// panic("SHOULD BE REIMPLEMENTED BEFORE MERGING") +//} diff --git a/staker/reward_calculation_canonical_test.gno b/staker/reward_calculation_canonical_test.gnoA similarity index 100% rename from staker/reward_calculation_canonical_test.gno rename to staker/reward_calculation_canonical_test.gnoA diff --git a/staker/tests/__TEST_short_wramup_internal_gnot_gns_3000_test.gnoA b/staker/tests/__TEST_short_wramup_internal_gnot_gns_3000_test.gnoA index c0e5ccd8..8f9ba3eb 100644 --- a/staker/tests/__TEST_short_wramup_internal_gnot_gns_3000_test.gnoA +++ b/staker/tests/__TEST_short_wramup_internal_gnot_gns_3000_test.gnoA @@ -1,4 +1,4 @@ -package staker +package tests import ( "math"