Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions contract/r/gnoswap/launchpad/v1/deposit_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,9 @@ func TestDeposit_Withdraw(t *testing.T) {
time.Now().Unix(),
time.Now().Add(time.Hour*24*30).Unix(),
)
currentHeight := int64(123)
amount := withdrawDeposit(deposit, currentHeight, tt.currentTime)
amount := withdrawDeposit(deposit, 123, tt.currentTime)
uassert.Equal(t, tt.expectedAmount, amount)
uassert.Equal(t, tt.currentTime, deposit.WithdrawnTime())
uassert.Equal(t, currentHeight, deposit.WithdrawnHeight())
uassert.Equal(t, tt.expectedRemaining, deposit.DepositAmount())
uassert.Equal(t, tt.expectedWithdrawn, deposit.IsWithdrawn())
})
Expand Down
5 changes: 2 additions & 3 deletions contract/r/gnoswap/launchpad/v1/launchpad_reward.gno
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ func (lp *launchpadV1) CollectRewardByDepositId(depositID string) int64 {
lp.assertIsDepositOwner(depositID, caller)

deposit := lp.mustGetDeposit(depositID)
currentHeight := runtime.ChainHeight()
currentTime := time.Now().Unix()

rewardTokenPath, rewardAmount, err := lp.collectDepositReward(deposit, currentHeight, currentTime)
rewardTokenPath, rewardAmount, err := lp.collectDepositReward(deposit, currentTime)
if err != nil {
panic(err)
}
Expand All @@ -54,7 +53,7 @@ func (lp *launchpadV1) CollectRewardByDepositId(depositID string) int64 {
}

// collectDepositReward calculates and collects the reward for a deposit.
func (lp *launchpadV1) collectDepositReward(deposit *launchpad.Deposit, currentHeight, currentTime int64) (string, int64, error) {
func (lp *launchpadV1) collectDepositReward(deposit *launchpad.Deposit, currentTime int64) (string, int64, error) {
if currentTime <= 0 {
return "", 0, makeErrorWithDetails(errInvalidTime, "currentTime must be positive")
}
Expand Down
3 changes: 1 addition & 2 deletions contract/r/gnoswap/launchpad/v1/launchpad_reward_test.gno
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package v1

import (
"chain/runtime"
"math"
"testing"
"time"
Expand Down Expand Up @@ -104,7 +103,7 @@ func TestLaunchpadReward_CollectDepositReward(t *testing.T) {
deposit := launchpad.NewDeposit("deposit_1", tt.projectID, tt.tier, testutils.TestAddress("depositor"), tt.depositAmount, 100, 100, 200)

// Execute
_, _, err := lp.collectDepositReward(deposit, runtime.ChainHeight(), tt.currentTime)
_, _, err := lp.collectDepositReward(deposit, tt.currentTime)

// Verify
if tt.expectedError != "" {
Expand Down
6 changes: 3 additions & 3 deletions contract/r/gnoswap/launchpad/v1/launchpad_withdraw.gno
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ func (lp *launchpadV1) CollectDepositGns(depositID string) (int64, error) {
currentTime := time.Now().Unix()

// Collect reward before withdrawal
rewardTokenPath, rewardAmount, err := lp.collectDepositReward(deposit, currentHeight, currentTime)
rewardTokenPath, rewardAmount, err := lp.collectDepositReward(deposit, currentTime)
if err != nil {
panic(err)
}

recipient, withdrawalAmount, err := lp.withdrawDeposit(deposit, runtime.ChainHeight(), currentTime)
recipient, withdrawalAmount, err := lp.withdrawDeposit(deposit, currentHeight, currentTime)
if err != nil {
panic(err.Error())
}
Expand Down Expand Up @@ -128,7 +128,7 @@ func (lp *launchpadV1) withdrawDeposit(deposit *launchpad.Deposit, currentHeight
project.SetTier(deposit.Tier(), projectTier)

// Finalize withdrawal
withdrawalAmount := withdrawDeposit(deposit, currentHeight, currentTime)
withdrawalAmount := withdrawDeposit(deposit, runtime.ChainHeight(), currentTime)
Comment thread
junghoon-vans marked this conversation as resolved.
Outdated

// Remove reward state from AVL tree after withdrawal
// This improves iteration performance for calculateClaimableRewardsForActiveDeposits
Expand Down
10 changes: 6 additions & 4 deletions contract/r/gnoswap/launchpad/v1/launchpad_withdraw_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ func TestLaunchpadWithdraw_WithdrawDeposit(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
initLaunchpadWithdrawTestWithTier(t, userAddr, tt.tierDuration)
lp := getTestImplementation()
currentHeight := runtime.ChainHeight()

if tt.skipHeights > 0 {
testing.SkipHeights(tt.skipHeights)
Expand All @@ -251,7 +252,7 @@ func TestLaunchpadWithdraw_WithdrawDeposit(t *testing.T) {

if tt.withdrawTwice {
// First withdrawal should succeed
_, _, err = lp.withdrawDeposit(deposit, runtime.ChainHeight(), currentTime)
_, _, err = lp.withdrawDeposit(deposit, currentHeight, currentTime)
uassert.NoError(t, err)

// Refresh deposit to get updated state
Expand All @@ -260,7 +261,7 @@ func TestLaunchpadWithdraw_WithdrawDeposit(t *testing.T) {
}

// Execute withdrawal (or second withdrawal for withdrawTwice case)
_, amount, err := lp.withdrawDeposit(deposit, runtime.ChainHeight(), currentTime)
_, amount, err := lp.withdrawDeposit(deposit, currentHeight, currentTime)

if tt.expectedErr {
uassert.Error(t, err)
Expand Down Expand Up @@ -508,8 +509,9 @@ func TestLaunchpadWithdraw_RewardStateRemoval(t *testing.T) {

// Perform withdrawal
testing.SkipHeights(tt.skipHeight)
currentHeight := runtime.ChainHeight()
currentTime := time.Now().Unix()
_, _, err = lp.withdrawDeposit(deposit, runtime.ChainHeight(), currentTime)
_, _, err = lp.withdrawDeposit(deposit, currentHeight, currentTime)
uassert.NoError(t, err)

// Verify reward state is removed after withdrawal
Expand Down Expand Up @@ -616,7 +618,7 @@ func TestLaunchpadWithdraw_RewardStateRemovalWithMultipleDeposits(t *testing.T)
withdrawDepositID := depositIDs[tt.withdrawDepositIndex]
deposit, _ := getDeposit(withdrawDepositID)
currentTime = time.Now().Unix()
_, _, err := lp.withdrawDeposit(deposit, runtime.ChainHeight(), currentTime)
_, _, err := lp.withdrawDeposit(deposit, currentHeight, currentTime)
uassert.NoError(t, err)

// Verify reward state count after withdrawal
Expand Down
12 changes: 6 additions & 6 deletions contract/r/gnoswap/launchpad/v1/project_tier_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestProjectTier_Status(t *testing.T) {
name string
startHeight int64
endHeight int64
currentHeight int64
currentTime int64
expectedIsActive bool
expectedIsEnded bool
expectedHasError bool
Expand All @@ -117,23 +117,23 @@ func TestProjectTier_Status(t *testing.T) {
name: "project tier status inactive and not ended when before start",
startHeight: 100,
endHeight: 200,
currentHeight: 50,
currentTime: 50,
expectedIsActive: false,
expectedIsEnded: false,
},
{
name: "project tier status active and not ended during active period",
startHeight: 100,
endHeight: 200,
currentHeight: 150,
currentTime: 150,
expectedIsActive: true,
expectedIsEnded: false,
},
{
name: "project tier status inactive and ended after end",
startHeight: 100,
endHeight: 200,
currentHeight: 250,
currentTime: 250,
expectedIsActive: false,
expectedIsEnded: true,
},
Expand All @@ -145,8 +145,8 @@ func TestProjectTier_Status(t *testing.T) {
tier := launchpad.NewProjectTier("test_project", 180, 1000, tt.startHeight, tt.endHeight)

// Execute and verify
isActive := tier.IsActivated(tt.currentHeight)
isEnded := tier.IsEnded(tt.currentHeight)
isActive := tier.IsActivated(tt.currentTime)
isEnded := tier.IsEnded(tt.currentTime)

uassert.Equal(t, isActive, tt.expectedIsActive)
uassert.Equal(t, isEnded, tt.expectedIsEnded)
Expand Down
11 changes: 2 additions & 9 deletions contract/r/gnoswap/launchpad/v1/reward_manager_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func TestRewardManager_UpdateRewardPerDepositX128(t *testing.T) {
totalDistributeAmount int64
distributeStartHeight int64
distributeEndHeight int64
currentHeight int64
rewardHeight int64
expectedAccumulatedRewardPerDeposit string
expectedHasError bool
Expand All @@ -72,7 +71,6 @@ func TestRewardManager_UpdateRewardPerDepositX128(t *testing.T) {
totalDistributeAmount: 1000,
distributeStartHeight: 100,
distributeEndHeight: 200,
currentHeight: 150,
rewardHeight: 100,
expectedAccumulatedRewardPerDeposit: "0",
expectedHasError: false,
Expand All @@ -82,7 +80,6 @@ func TestRewardManager_UpdateRewardPerDepositX128(t *testing.T) {
totalDistributeAmount: 1000,
distributeStartHeight: 100,
distributeEndHeight: 200,
currentHeight: 50,
rewardHeight: 0,
expectedAccumulatedRewardPerDeposit: "0",
expectedHasError: false,
Expand Down Expand Up @@ -232,7 +229,6 @@ func TestRewardManager_CollectReward(t *testing.T) {
totalDistributeAmount int64
distributeStartTime int64
distributeEndTime int64
currentHeight int64
depositId string
deposit *launchpad.Deposit
existingDeposits []*launchpad.Deposit
Expand All @@ -246,7 +242,6 @@ func TestRewardManager_CollectReward(t *testing.T) {
totalDistributeAmount: 1000,
distributeStartTime: 100,
distributeEndTime: 200,
currentHeight: 150,
deposit: launchpad.NewDeposit(
"1",
"test",
Expand Down Expand Up @@ -328,7 +323,6 @@ func TestnewRewardManager(t *testing.T) {
distributeStartTime int64
distributeEndTime int64
distributeAmountPerSecondX128 *u256.Uint
currentHeight int64
expectedHasError bool
expectedError string
}{
Expand All @@ -338,7 +332,6 @@ func TestnewRewardManager(t *testing.T) {
distributeStartTime: 100,
distributeEndTime: 200,
distributeAmountPerSecondX128: u256.NewUintFromInt64(100),
currentHeight: 100,
expectedHasError: false,
},
}
Expand Down Expand Up @@ -1832,8 +1825,8 @@ func TestRewardManager_addRewardPerDepositX128(t *testing.T) {
result := u256.Zero().Sub(maxUint128, u256.NewUint(500))
return result.ToString()
}(),
expectedAccumulatedTime: 1100,
expectNoUpdate: false,
expectedAccumulatedTime: 1100,
expectNoUpdate: false,
},
{
name: "very small reward addition",
Expand Down
16 changes: 8 additions & 8 deletions contract/r/gnoswap/launchpad/v1/reward_state_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,29 @@ func TestRewardState_IsClaimable(t *testing.T) {
tests := []struct {
name string
claimableTime int64
currentHeight int64
currentTime int64
expectedIsClaimable bool
expectedHasError bool
expectedError string
}{
{
name: "is claimable return false when current height is before claimable height",
name: "is claimable return false when current time is before claimable time",
claimableTime: 100,
currentHeight: 50,
currentTime: 50,
expectedIsClaimable: false,
expectedHasError: false,
},
{
name: "is claimable return true when current height equals claimable height",
name: "is claimable return true when current time equals claimable time",
claimableTime: 100,
currentHeight: 100,
currentTime: 100,
expectedIsClaimable: true,
expectedHasError: false,
},
{
name: "is claimable return true when current height is after claimable height",
name: "is claimable return true when current time is after claimable time",
claimableTime: 100,
currentHeight: 150,
currentTime: 150,
expectedIsClaimable: true,
expectedHasError: false,
},
Expand All @@ -47,7 +47,7 @@ func TestRewardState_IsClaimable(t *testing.T) {
state := launchpad.NewRewardState(u256.Zero(), 1000, 100, 200, tt.claimableTime)

// Execute
result := isRewardStateClaimable(state, tt.currentHeight)
result := isRewardStateClaimable(state, tt.currentTime)

// Verify
uassert.Equal(t, result, tt.expectedIsClaimable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ func (lp *launchpadV1) CollectRewardByDepositId(depositID string) int64 {
lp.assertIsDepositOwner(depositID, caller)

deposit := lp.mustGetDeposit(depositID)
currentHeight := runtime.ChainHeight()
currentTime := time.Now().Unix()
rewardAmount, err := lp.collectDepositReward(deposit, currentHeight, currentTime)
rewardAmount, err := lp.collectDepositReward(deposit, currentTime)
if err != nil {
panic(err)
}
Expand All @@ -47,7 +46,7 @@ func (lp *launchpadV1) CollectRewardByDepositId(depositID string) int64 {
}

// collectDepositReward calculates and collects the reward for a deposit.
func (lp *launchpadV1) collectDepositReward(deposit *launchpad.Deposit, currentHeight, currentTime int64) (int64, error) {
func (lp *launchpadV1) collectDepositReward(deposit *launchpad.Deposit, currentTime int64) (int64, error) {
if currentTime <= 0 {
return 0, makeErrorWithDetails(errInvalidTime, "currentTime must be positive")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ func (lp *launchpadV1) CollectDepositGns(depositID string) (int64, error) {

deposit := lp.mustGetDeposit(depositID)

currentHeight := runtime.ChainHeight()
currentTime := time.Now().Unix()
recipient, withdrawalAmount, err := lp.withdrawDeposit(deposit, runtime.ChainHeight(), currentTime)
recipient, withdrawalAmount, err := lp.withdrawDeposit(deposit, currentHeight, currentTime)
if err != nil {
panic(err.Error())
}
Expand Down Expand Up @@ -111,7 +112,7 @@ func (lp *launchpadV1) withdrawDeposit(deposit *launchpad.Deposit, currentHeight
}

// Finalize withdrawal
withdrawalAmount := withdrawDeposit(deposit, currentHeight, currentTime)
withdrawalAmount := withdrawDeposit(deposit, runtime.ChainHeight(), currentTime)

// Store updated deposit in state
deposits := lp.store.GetDeposits()
Expand Down
Loading