Skip to content

Commit

Permalink
add test for important functions
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon committed Dec 15, 2024
1 parent 78cd7b0 commit c37e0c4
Show file tree
Hide file tree
Showing 7 changed files with 1,179 additions and 364 deletions.
158 changes: 158 additions & 0 deletions staker/reward_internal_emission_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package staker

import (
"testing"

u256 "gno.land/p/gnoswap/uint256"
)

func TestRemoveInRangePosition(t *testing.T) {
tests := []struct {
name string
poolPath string
tokenId uint64
setup func() *InternalEmissionReward
verify func(*InternalEmissionReward)
}{
{
name: "Normal position removal",
poolPath: "test/pool/path",
tokenId: 1,
setup: func() *InternalEmissionReward {
r := NewInternalEmissionReward()
recipientsMap := NewRewardRecipientMap()
poolLiquidity := NewPoolLiquidity()

// Initial liquidity setup
inRangeLiquidity := NewInRangeLiquidity()
inRangeLiquidity.SetLiquidity(u256.NewUint(1000))
poolLiquidity.AddInRangePosition(1, inRangeLiquidity)

recipientsMap.SetPoolLiquidity("test/pool/path", poolLiquidity)
r.SetRewardRecipientsMap(recipientsMap)
return r
},
verify: func(r *InternalEmissionReward) {
recipientsMap := r.GetRewardRecipientsMap()
poolLiquidity := recipientsMap.GetPoolLiquidity("test/pool/path")

// Check if position is removed
position := poolLiquidity.GetInRangeLiquidity(1)
if !position.GetLiquidity().IsZero() {
t.Errorf("Liquidity should be 0, but it is %s", position.GetLiquidity().ToString())
}
if !position.GetLiquidityRatio().IsZero() {
t.Errorf("Liquidity ratio should be 0, but it is %s", position.GetLiquidityRatio().ToString())
}
if position.GetStakedHeight() != 0 {
t.Errorf("Staking height should be 0, but it is %d", position.GetStakedHeight())
}
},
},
{
name: "Non-existent pool path",
poolPath: "non/existent/path",
tokenId: 1,
setup: func() *InternalEmissionReward {
return NewInternalEmissionReward()
},
verify: func(r *InternalEmissionReward) {
// No Error
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := tt.setup()
r.RemoveInRangePosition(tt.poolPath, tt.tokenId)
tt.verify(r)
})
}
}

func TestSelectRewardPools(t *testing.T) {
tests := []struct {
name string
pools map[string]InternalTier
setup func() *InternalEmissionReward
verify func(*InternalEmissionReward)
}{
{
name: "Normal pool selection",
pools: map[string]InternalTier{
"pool/1": {tier: TIER1_INDEX},
"pool/2": {tier: TIER2_INDEX},
"pool/3": {tier: TIER3_INDEX},
"pool/4": {tier: 4}, // Invalid tier
},
setup: func() *InternalEmissionReward {
return NewInternalEmissionReward()
},
verify: func(r *InternalEmissionReward) {
rewardPool := r.GetRewardPoolsMap()

// TIER1 pool verification
pool1 := rewardPool.GetRewardPoolByPoolPath("pool/1")
if pool1.GetTier() != TIER1_INDEX {
t.Errorf("pool/1 should be TIER1, but got %d", pool1.GetTier())
}

// TIER2 pool verification
pool2 := rewardPool.GetRewardPoolByPoolPath("pool/2")
if pool2.GetTier() != TIER2_INDEX {
t.Errorf("pool/2 should be TIER2, but got %d", pool2.GetTier())
}

// TIER3 pool verification
pool3 := rewardPool.GetRewardPoolByPoolPath("pool/3")
if pool3.GetTier() != TIER3_INDEX {
t.Errorf("pool/3 should be TIER3, but got %d", pool3.GetTier())
}

// Invalid tier pool verification
pool4 := rewardPool.GetRewardPoolByPoolPath("pool/4")
if pool4.GetTier() != 0 {
t.Errorf("pool/4 should not be selected, but got tier %d", pool4.GetTier())
}
},
},
{
name: "Empty pool map",
pools: map[string]InternalTier{},
setup: func() *InternalEmissionReward {
return NewInternalEmissionReward()
},
verify: func(r *InternalEmissionReward) {
rewardPool := r.GetRewardPoolsMap()
if len(rewardPool.GetRewardPools()) != 0 {
t.Errorf("reward pools should be empty, but got %d pools", len(rewardPool.GetRewardPools()))
}
},
},
{
name: "Invalid tier only",
pools: map[string]InternalTier{
"pool/1": {tier: 4},
"pool/2": {tier: 5},
},
setup: func() *InternalEmissionReward {
return NewInternalEmissionReward()
},
verify: func(r *InternalEmissionReward) {
rewardPool := r.GetRewardPoolsMap()
if len(rewardPool.GetRewardPools()) != 0 {
t.Errorf("no pools should be selected, but got %d pools", len(rewardPool.GetRewardPools()))
}
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := tt.setup()
r.SelectRewardPools(tt.pools)
tt.verify(r)
})
}
}
191 changes: 191 additions & 0 deletions staker/reward_manager_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
package staker

import (
"testing"

u256 "gno.land/p/gnoswap/uint256"
)

func TestRewardManager_Internal(t *testing.T) {
tests := []struct {
name string
setup func() *RewardManager
testFunc func(*RewardManager)
verify func(*RewardManager) bool
wantErr bool
}{
{
name: "create new RewardManager instance",
setup: func() *RewardManager {
return NewRewardManager()
},
verify: func(rm *RewardManager) bool {
return rm.internalReward != nil && rm.externalReward != nil
},
},
{
name: "set and get InternalEmissionReward",
setup: func() *RewardManager {
return NewRewardManager()
},
testFunc: func(rm *RewardManager) {
newInternalReward := NewInternalEmissionReward()
recipientsMap := NewRewardRecipientMap()
poolLiquidity := NewPoolLiquidity()
inRangeLiquidity := NewInRangeLiquidity()
inRangeLiquidity.SetLiquidity(u256.NewUint(1000))
poolLiquidity.AddInRangePosition(1, inRangeLiquidity)
recipientsMap.SetPoolLiquidity("test/pool", poolLiquidity)
newInternalReward.SetRewardRecipientsMap(recipientsMap)

rm.SetInternalEmissionReward(newInternalReward)
},
verify: func(rm *RewardManager) bool {
internalReward := rm.GetInternalEmissionReward()
if internalReward == nil {
return false
}

recipientsMap := internalReward.GetRewardRecipientsMap()
if recipientsMap == nil {
return false
}

poolLiquidity := recipientsMap.GetPoolLiquidity("test/pool")
if poolLiquidity == nil {
return false
}

position := poolLiquidity.GetInRangeLiquidity(1)
if position == nil {
return false
}

return position.GetLiquidity().Eq(u256.NewUint(1000))
},
},
{
name: "set nil InternalEmissionReward",
setup: func() *RewardManager {
return NewRewardManager()
},
testFunc: func(rm *RewardManager) {
rm.SetInternalEmissionReward(nil)
},
verify: func(rm *RewardManager) bool {
return rm.GetInternalEmissionReward() == nil
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rm := tt.setup()

if tt.testFunc != nil {
tt.testFunc(rm)
}

if result := tt.verify(rm); !result {
t.Errorf("%s: verification failed", tt.name)
}
})
}
}

func TestRewardManager_External(t *testing.T) {
tests := []struct {
name string
setup func() *RewardManager
testFunc func(*RewardManager)
verify func(*RewardManager) bool
wantErr bool
}{
{
name: "create new RewardManager instance for external reward",
setup: func() *RewardManager {
return NewRewardManager()
},
verify: func(rm *RewardManager) bool {
return rm.externalReward != nil
},
},
{
name: "set and get ExternalIncentiveReward",
setup: func() *RewardManager {
return NewRewardManager()
},
testFunc: func(rm *RewardManager) {
newExternalReward := NewExternalIncentiveReward()
externalCalculator := NewExternalCalculator(100)
newExternalReward.SetExternalCalculator(externalCalculator)

rm.SetExternalIncentiveReward(newExternalReward)
},
verify: func(rm *RewardManager) bool {
externalReward := rm.GetExternalIncentiveReward()
if externalReward == nil {
return false
}

calculator := externalReward.GetExternalCalculator()
if calculator == nil {
return false
}

return true
},
},
{
name: "set nil ExternalIncentiveReward",
setup: func() *RewardManager {
return NewRewardManager()
},
testFunc: func(rm *RewardManager) {
rm.SetExternalIncentiveReward(nil)
},
verify: func(rm *RewardManager) bool {
return rm.GetExternalIncentiveReward() == nil
},
},
{
name: "get or create ExternalCalculator",
setup: func() *RewardManager {
rm := NewRewardManager()
newExternalReward := NewExternalIncentiveReward()
rm.SetExternalIncentiveReward(newExternalReward)
return rm
},
testFunc: func(rm *RewardManager) {
externalReward := rm.GetExternalIncentiveReward()
calculator := externalReward.GetOrCreateExternalCalculator(200) // 높이 200으로 설정
if calculator == nil {
t.Error("calculator should not be nil")
}
},
verify: func(rm *RewardManager) bool {
externalReward := rm.GetExternalIncentiveReward()
if externalReward == nil {
return false
}

calculator := externalReward.GetExternalCalculator()
return calculator != nil
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rm := tt.setup()

if tt.testFunc != nil {
tt.testFunc(rm)
}

if result := tt.verify(rm); !result {
t.Errorf("%s: verification failed", tt.name)
}
})
}
}
29 changes: 28 additions & 1 deletion staker/reward_recipient_store.gno
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (r *RewardRecipientsMap) CalculateLiquidityRatioAndGetTokenIdMap() map[stri
// 2. calculate the liquidity ratio for each position
for poolPath, poolLiquidity := range poolLiquidityMap {
inRangePosition := poolLiquidity.GetInRangeLiquidityMap()
for tokenId, _ := range inRangePosition {
for tokenId := range inRangePosition {
r.UpdateInRangeLiquidityRatio(poolPath, tokenId)
tokenIdMap[poolPath] = append(tokenIdMap[poolPath], tokenId)
}
Expand All @@ -195,6 +195,33 @@ func NewPoolLiquidity() *PoolLiquidity {
}
}

// AddInRangePosition adds a new in-range position to the pool
func (p *PoolLiquidity) AddInRangePosition(tokenID uint64, position *InRangeLiquidity) {
// nil check for map
if p.inRangeLiquidityMap == nil {
p.inRangeLiquidityMap = make(map[uint64]*InRangeLiquidity)
}

// nil check for position
if position == nil {
position = NewInRangeLiquidity()
}

// nil check for totalLiquidity
if p.totalLiquidity == nil {
p.totalLiquidity = u256.Zero()
}

// Add position's liquidity to total liquidity
positionLiquidity := position.GetLiquidity()
if positionLiquidity != nil {
p.totalLiquidity.Add(p.totalLiquidity, positionLiquidity)
}

// Store the position
p.inRangeLiquidityMap[tokenID] = position
}

func (p *PoolLiquidity) SetTotalLiquidity(totalLiquidity *u256.Uint) {
p.totalLiquidity = totalLiquidity
}
Expand Down
Loading

0 comments on commit c37e0c4

Please sign in to comment.