Skip to content

Commit

Permalink
test: fix TestValidateRewardCollection
Browse files Browse the repository at this point in the history
- no such error
  • Loading branch information
r3v4s authored and onlyhyde committed Jan 8, 2025
1 parent 94681f5 commit 5504b2e
Showing 1 changed file with 114 additions and 127 deletions.
241 changes: 114 additions & 127 deletions launchpad/reward_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package launchpad
import (
"testing"

"gno.land/p/demo/ufmt"
"gno.land/p/demo/uassert"
"gno.land/p/demo/ufmt"
u256 "gno.land/p/gnoswap/uint256"
)

Expand All @@ -21,28 +21,16 @@ func TestValidateRewardCollection(t *testing.T) {
{
name: "Valid reward ready to collect",
deposit: Deposit{
//rewardAmount: 1000,
claimableHeight: 50, // Less than current height
claimableHeight: 50, // Less than current height
rewardCollectTime: 0,
},
height: currentHeight,
shouldError: false,
},
{
name: "No reward available",
deposit: Deposit{
//rewardAmount: 0,
claimableHeight: 50,
},
height: currentHeight,
shouldError: true,
errorMsg: "no reward available",
},
{
name: "Reward not yet claimable",
deposit: Deposit{
//rewardAmount: 1000,
claimableHeight: 150, // Greater than current height
claimableHeight: 150, // Greater than current height
rewardCollectTime: 0,
},
height: currentHeight,
Expand All @@ -52,7 +40,6 @@ func TestValidateRewardCollection(t *testing.T) {
{
name: "Already collected reward can be collected before claimable height",
deposit: Deposit{
//rewardAmount: 1000,
claimableHeight: 150,
rewardCollectTime: 1, // Already collected before
},
Expand Down Expand Up @@ -118,125 +105,125 @@ func TestCalculateDepositRatioX96(t *testing.T) {
}

func TestProcessDepositReward(t *testing.T) {
q96 = u256.MustFromDecimal("79228162514264337593543950336")

tests := []struct {
name string
deposit Deposit
rewardX96 *u256.Uint
tierAmount uint64
expectedReward uint64
shouldError bool
}{
{
name: "Normal reward calculation",
deposit: Deposit{
amount: 1000,
//rewardAmount: 0,
},
rewardX96: u256.NewUint(1000).Mul(u256.NewUint(1000), q96),
tierAmount: 2000,
expectedReward: 500, // (1000/2000) * 1000 = 500
shouldError: false,
},
q96 = u256.MustFromDecimal("79228162514264337593543950336")

tests := []struct {
name string
deposit Deposit
rewardX96 *u256.Uint
tierAmount uint64
expectedReward uint64
shouldError bool
}{
{
name: "Normal reward calculation",
deposit: Deposit{
amount: 1000,
//rewardAmount: 0,
},
rewardX96: u256.NewUint(1000).Mul(u256.NewUint(1000), q96),
tierAmount: 2000,
expectedReward: 500, // (1000/2000) * 1000 = 500
shouldError: false,
},
/*
{
name: "Tier amount is 0",
deposit: Deposit{
amount: 1000,
//rewardAmount: 0,
},
rewardX96: u256.NewUint(1000),
tierAmount: 0,
expectedReward: 0,
shouldError: true,
},*/
{
name: "Reward is accumulated",
deposit: Deposit{
amount: 1000,
//rewardAmount: 100,
},
rewardX96: u256.NewUint(1000).Mul(u256.NewUint(1000), q96),
tierAmount: 2000,
expectedReward: 500, // 600, // Existing 100 + New reward 500
shouldError: false,
},
}

for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
{
name: "Tier amount is 0",
deposit: Deposit{
amount: 1000,
//rewardAmount: 0,
},
rewardX96: u256.NewUint(1000),
tierAmount: 0,
expectedReward: 0,
shouldError: true,
},*/
{
name: "Reward is accumulated",
deposit: Deposit{
amount: 1000,
//rewardAmount: 100,
},
rewardX96: u256.NewUint(1000).Mul(u256.NewUint(1000), q96),
tierAmount: 2000,
expectedReward: 500, // 600, // Existing 100 + New reward 500
shouldError: false,
},
}

for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
state := NewRewardState(tt.rewardX96, 1000, 2000)
depositId := ufmt.Sprintf("depositId-%d", i)
state.AddStake(1000, depositId, tt.deposit.amount)
state.TotalStake = tt.tierAmount // force overriding total stake
state.finalize(1001)

reward := state.CalculateReward(depositId)
uassert.Equal(t, tt.expectedReward, reward)
})
}
reward := state.CalculateReward(depositId)
uassert.Equal(t, tt.expectedReward, reward)
})
}
}

func TestCalculateTierRewards(t *testing.T) {
q96 = u256.MustFromDecimal("79228162514264337593543950336")

tests := []struct {
name string
tier Tier
currentHeight uint64
lastCalcHeight uint64
expectedRewardX96 string
expectedReward uint64
shouldBeZero bool
}{
{
name: "tierAmountPerBlockX96 is 0",
tier: Tier{
tierAmountPerBlockX96: u256.Zero(),
ended: TimeInfo{height: 1000},
},
currentHeight: 500,
lastCalcHeight: 400,
shouldBeZero: true,
},
{
name: "sinceLast is 0",
tier: Tier{
tierAmountPerBlockX96: u256.NewUint(1000),
ended: TimeInfo{height: 1000},
},
currentHeight: 500,
lastCalcHeight: 500,
shouldBeZero: true,
},
{
name: "Current height exceeds end height",
tier: Tier{
tierAmountPerBlockX96: u256.NewUint(1000),
ended: TimeInfo{height: 450},
},
currentHeight: 500,
lastCalcHeight: 400,
expectedRewardX96: "50000", // 1000 * (450-400)
expectedReward: 0, // (50000 / Q96)
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rewardX96, reward, err := calculateTierRewards(tt.tier, tt.currentHeight, tt.lastCalcHeight)
uassert.NoError(t, err)

if tt.shouldBeZero {
uassert.Equal(t, true, rewardX96.IsZero())
uassert.Equal(t, uint64(0), reward)
} else {
expected := u256.MustFromDecimal(tt.expectedRewardX96)
uassert.Equal(t, 0, rewardX96.Cmp(expected))
uassert.Equal(t, tt.expectedReward, reward)
}
})
}
q96 = u256.MustFromDecimal("79228162514264337593543950336")

tests := []struct {
name string
tier Tier
currentHeight uint64
lastCalcHeight uint64
expectedRewardX96 string
expectedReward uint64
shouldBeZero bool
}{
{
name: "tierAmountPerBlockX96 is 0",
tier: Tier{
tierAmountPerBlockX96: u256.Zero(),
ended: TimeInfo{height: 1000},
},
currentHeight: 500,
lastCalcHeight: 400,
shouldBeZero: true,
},
{
name: "sinceLast is 0",
tier: Tier{
tierAmountPerBlockX96: u256.NewUint(1000),
ended: TimeInfo{height: 1000},
},
currentHeight: 500,
lastCalcHeight: 500,
shouldBeZero: true,
},
{
name: "Current height exceeds end height",
tier: Tier{
tierAmountPerBlockX96: u256.NewUint(1000),
ended: TimeInfo{height: 450},
},
currentHeight: 500,
lastCalcHeight: 400,
expectedRewardX96: "50000", // 1000 * (450-400)
expectedReward: 0, // (50000 / Q96)
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rewardX96, reward, err := calculateTierRewards(tt.tier, tt.currentHeight, tt.lastCalcHeight)

uassert.NoError(t, err)

if tt.shouldBeZero {
uassert.Equal(t, true, rewardX96.IsZero())
uassert.Equal(t, uint64(0), reward)
} else {
expected := u256.MustFromDecimal(tt.expectedRewardX96)
uassert.Equal(t, 0, rewardX96.Cmp(expected))
uassert.Equal(t, tt.expectedReward, reward)
}
})
}
}

0 comments on commit 5504b2e

Please sign in to comment.