Skip to content

Commit

Permalink
Merge branch 'mconcat/refactor-emission-part-1' of github.com:gnoswap…
Browse files Browse the repository at this point in the history
…-labs/gnoswap into mconcat/refactor-emission-part-1
  • Loading branch information
mconcat committed Dec 30, 2024
2 parents a8371d1 + 13bfd75 commit baa53f2
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,30 @@ func GetPoolStakedLiquidityUpdates(poolPath string, startHeight, endHeight uint6
return marshal(builder.Node())
}

func GetPoolRewardUpdates(poolPath string, startHeight, endHeight uint64) string {
pool, ok := pools.Get(poolPath)
if !ok {
return ""
}
// func GetPoolRewardUpdates(poolPath string, startHeight, endHeight uint64) string {
// pool, ok := pools.Get(poolPath)
// if !ok {
// return ""
// }

// TODO: remove comment
// if endHeight > pool.lastRewardCacheHeight {
// poolTier.cacheReward(endHeight)
// poolTier.cachePoolReward(pool.rewardCache, poolPath, endHeight)
// }
// // TODO: remove comment
// // if endHeight > pool.lastRewardCacheHeight {
// // poolTier.cacheReward(endHeight)
// // poolTier.cachePoolReward(pool.rewardCache, poolPath, endHeight)
// // }

builder := json.Builder().WriteArray("", func(ab *json.ArrayBuilder) {
pool.rewardCache.Iterate(startHeight, endHeight, func(key uint64, value interface{}) bool {
ab.WriteObject(func(ob *json.NodeBuilder) {
ob.WriteString("blockNumber", ufmt.Sprintf("%d", key))
ob.WriteString("reward", value.(*u256.Uint).ToString())
})
return false
})
})
// builder := json.Builder().WriteArray("", func(ab *json.ArrayBuilder) {
// pool.rewardCache.Iterate(startHeight, endHeight, func(key uint64, value interface{}) bool {
// ab.WriteObject(func(ob *json.NodeBuilder) {
// ob.WriteString("blockNumber", ufmt.Sprintf("%d", key))
// ob.WriteString("reward", value.(*u256.Uint).ToString())
// })
// return false
// })
// })

return marshal(builder.Node())
}
// return marshal(builder.Node())
// }

// poolsPositions
// func GetPoolsPositions() string {
Expand Down Expand Up @@ -94,50 +94,50 @@ func GetPoolRewardUpdates(poolPath string, startHeight, endHeight uint64) string
// }

// consts.Q96
func GetQ96() string {
en.MintAndDistributeGns()
// func GetQ96() string {
// en.MintAndDistributeGns()

return "79228162514264337593543950336"
}
// return "79228162514264337593543950336"
// }

// positionExternal
func GetPositionExternal() string {
en.MintAndDistributeGns()
// // positionExternal
// func GetPositionExternal() string {
// en.MintAndDistributeGns()

if len(positionExternal) == 0 {
return ""
}
// if len(positionExternal) == 0 {
// return ""
// }

builder := json.Builder().WriteArray("", func(ab *json.ArrayBuilder) {
for tokenId, externals := range positionExternal {
ab.WriteObject(func(ob *json.NodeBuilder) {
ob.WriteString("tokenId", ufmt.Sprintf("%d", tokenId))
ob.WriteArray("externals", func(extAb *json.ArrayBuilder) {
buildPositionExternals(externals, extAb)
})
})
}
})
// builder := json.Builder().WriteArray("", func(ab *json.ArrayBuilder) {
// for tokenId, externals := range positionExternal {
// ab.WriteObject(func(ob *json.NodeBuilder) {
// ob.WriteString("tokenId", ufmt.Sprintf("%d", tokenId))
// ob.WriteArray("externals", func(extAb *json.ArrayBuilder) {
// buildPositionExternals(externals, extAb)
// })
// })
// }
// })

return marshal(builder.Node())
}
// return marshal(builder.Node())
// }

func buildExternalReward(incentiveId string, reward *externalRewards, ob *json.NodeBuilder) {
ob.WriteString("incentiveId", incentiveId)
ob.WriteString("poolPath", reward.poolPath)
ob.WriteString("tokenPath", reward.tokenPath)
ob.WriteString("tokenAmountX96", reward.tokenAmountX96.ToString())
ob.WriteString("tokenAmountFull", ufmt.Sprintf("%d", reward.tokenAmountFull))
ob.WriteString("tokenAmountToGive", ufmt.Sprintf("%d", reward.tokenAmountToGive))
}
// func buildExternalReward(incentiveId string, reward *externalRewards, ob *json.NodeBuilder) {
// ob.WriteString("incentiveId", incentiveId)
// ob.WriteString("poolPath", reward.poolPath)
// ob.WriteString("tokenPath", reward.tokenPath)
// ob.WriteString("tokenAmountX96", reward.tokenAmountX96.ToString())
// ob.WriteString("tokenAmountFull", ufmt.Sprintf("%d", reward.tokenAmountFull))
// ob.WriteString("tokenAmountToGive", ufmt.Sprintf("%d", reward.tokenAmountToGive))
// }

func buildPositionExternals(externals map[string]externalRewards, ab *json.ArrayBuilder) {
for incentiveId, externalRewards := range externals {
ab.WriteObject(func(ob *json.NodeBuilder) {
buildExternalReward(incentiveId, &externalRewards, ob)
})
}
}
// func buildPositionExternals(externals map[string]externalRewards, ab *json.ArrayBuilder) {
// for incentiveId, externalRewards := range externals {
// ab.WriteObject(func(ob *json.NodeBuilder) {
// buildExternalReward(incentiveId, &externalRewards, ob)
// })
// }
// }

// positionsInternalWarmUpAmount
// func GetPositionsInternalWarmUpAmount() string {
Expand Down
74 changes: 74 additions & 0 deletions staker/api_calculation_base_data_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package staker

import (
"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)
}
})
}

0 comments on commit baa53f2

Please sign in to comment.