Skip to content

Commit

Permalink
refactor: event message
Browse files Browse the repository at this point in the history
  • Loading branch information
onlyhyde committed Jan 8, 2025
1 parent 645126a commit d703148
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 37 deletions.
34 changes: 18 additions & 16 deletions launchpad/deposit.gno
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package launchpad
import (
"errors"
"std"
"strconv"
"time"

"gno.land/p/demo/ufmt"
Expand Down Expand Up @@ -158,7 +159,8 @@ func updateDepositIndices(deposit Deposit, state *DepositState) {

// processFirstDeposit handles the first deposit for a project tier
func processFirstDeposit(info ProjectTierInfo) (Tier, error) {
if info.Tier.totalParticipant != 0 {
info.Tier.totalParticipant++
if info.Tier.totalParticipant > 1 {
return info.Tier, nil
}

Expand Down Expand Up @@ -235,10 +237,10 @@ func DepositGns(targetProjectTierId string, amount uint64) string {
"prevAddr", prevAddr,
"prevRealm", prevPkgPath,
"targetProjectTierId", targetProjectTierId,
"amount", ufmt.Sprintf("%d", amount),
"amount", strconv.FormatUint(amount, 10),
"depositId", deposit.id,
"claimableHeight", ufmt.Sprintf("%d", deposit.claimableHeight),
"claimableTime", ufmt.Sprintf("%d", deposit.claimableTime),
"claimableHeight", strconv.FormatUint(deposit.claimableHeight, 10),
"claimableTime", strconv.FormatUint(deposit.claimableTime, 10),
)

// Process first deposit if applicable
Expand All @@ -249,14 +251,14 @@ func DepositGns(targetProjectTierId string, amount uint64) string {

if updatedTier.started.height == info.Height {
std.Emit(
"FirstDepoistForProjectTier",
"FirstDepositForProjectTier",
"prevAddr", prevAddr,
"prevRealm", prevPkgPath,
"targetProjectTierId", targetProjectTierId,
"amount", ufmt.Sprintf("%d", amount),
"amount", strconv.FormatUint(amount, 10),
"depositId", deposit.id,
"claimableHeight", ufmt.Sprintf("%d", deposit.claimableHeight),
"claimableTime", ufmt.Sprintf("%d", deposit.claimableTime),
"claimableHeight", strconv.FormatUint(deposit.claimableHeight, 10),
"claimableTime", strconv.FormatUint(deposit.claimableTime, 10),
"tierAmountPerBlockX96", updatedTier.tierAmountPerBlockX96.ToString(),
)
}
Expand All @@ -279,7 +281,7 @@ func processDepositCollection(
) (uint64, error) {
totalAmount := uint64(0) // gnsToUser
height := uint64(std.GetHeight())
prevAddr, prevRealm := getPrev()
prevAddr, prevPkgPath := getPrev()

for _, dpstId := range dpsts {
dpst := deposits[dpstId]
Expand Down Expand Up @@ -313,18 +315,18 @@ func processDepositCollection(
std.Emit(
"CollectDepositGnsByProjectId",
"prevAddr", prevAddr,
"prevRealm", prevRealm,
"prevRealm", prevPkgPath,
"projectId", projectId,
"depositId", dpstId,
"amount", ufmt.Sprintf("%d", dpst.amount),
"amount", strconv.FormatUint(dpst.amount, 10),
)
} else {
std.Emit(
"CollectDepositGns",
"prevAddr", prevAddr,
"prevRealm", prevRealm,
"prevRealm", prevPkgPath,
"depositId", dpstId,
"amount", ufmt.Sprintf("%d", dpst.amount),
"amount", strconv.FormatUint(dpst.amount, 10),
)
}
}
Expand Down Expand Up @@ -443,13 +445,13 @@ func CollectDepositGnsByDepositId(depositId string) uint64 {
gs.SetAmountByProjectWallet(project.recipient, amount, false)

// Emit collection event
prevAddr, prevRealm := getPrev()
prevAddr, prevPkgPath := getPrev()
std.Emit(
"CollectDepositGnsByDepositId",
"prevAddr", prevAddr,
"prevRealm", prevRealm,
"prevRealm", prevPkgPath,
"depositId", depositId,
"amount", ufmt.Sprintf("%d", amount),
"amount", strconv.FormatUint(amount, 10),
)

// Process token transfers if amount > 0
Expand Down
14 changes: 7 additions & 7 deletions launchpad/launchpad.gno
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,13 @@ func TransferLeftFromProjectByAdmin(projectId string, recipient std.Address) uin
"projectId", projectId,
"recipient", recipient.String(),
"tokenPath", project.tokenPath,
"leftReward", ufmt.Sprintf("%d", leftReward),
"tier30Full", ufmt.Sprintf("%d", project.tiers[30].tierAmount),
"tier30Left", ufmt.Sprintf("%d", project.tiers[30].tierAmount-project.tiers[30].calculatedAmount),
"tier90Full", ufmt.Sprintf("%d", project.tiers[90].tierAmount),
"tier90Left", ufmt.Sprintf("%d", project.tiers[90].tierAmount-project.tiers[90].calculatedAmount),
"tier180Full", ufmt.Sprintf("%d", project.tiers[180].tierAmount),
"tier180Left", ufmt.Sprintf("%d", project.tiers[180].tierAmount-project.tiers[180].calculatedAmount),
"leftReward", strconv.FormatUint(leftReward, 10),
"tier30Full", strconv.FormatUint(project.tiers[30].tierAmount, 10),
"tier30Left", strconv.FormatUint(project.tiers[30].tierAmount-project.tiers[30].calculatedAmount, 10),
"tier90Full", strconv.FormatUint(project.tiers[90].tierAmount, 10),
"tier90Left", strconv.FormatUint(project.tiers[90].tierAmount-project.tiers[90].calculatedAmount, 10),
"tier180Full", strconv.FormatUint(project.tiers[180].tierAmount, 10),
"tier180Left", strconv.FormatUint(project.tiers[180].tierAmount-project.tiers[180].calculatedAmount, 10),
)

project.refund = RefundInfo{
Expand Down
30 changes: 16 additions & 14 deletions launchpad/reward.gno
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package launchpad
import (
"errors"
"std"
"strconv"
"time"

"gno.land/p/demo/ufmt"
Expand All @@ -13,7 +14,7 @@ import (
)

var (
lastCalculatedHeight uint64
lastCalculatedHeight uint64
lastCalculateHeightForProjectTier = make(map[string]uint64) // using height
)

Expand Down Expand Up @@ -56,7 +57,7 @@ func calculateTierRewards(tier Tier, currentHeight uint64, lastCalcHeight uint64

endHeight := minU64(currentHeight, tier.ended.height)
sinceLast := endHeight - minU64(endHeight, lastCalcHeight)

if sinceLast == 0 {
return u256.Zero(), 0, nil
}
Expand All @@ -79,7 +80,7 @@ func processDepositReward(deposit Deposit, rewardX96 *u256.Uint, tierAmount uint
depositRewardX96 := u256.Zero().Div(depositRewardX96X96, q96)
depositRewardX := u256.Zero().Div(depositRewardX96, q96)
depositReward := depositRewardX.Uint64()

deposit.rewardAmount += depositReward
return deposit, nil
}
Expand All @@ -106,7 +107,7 @@ func CollectRewardByProjectId(projectId string) uint64 {

height := uint64(std.GetHeight())
totalReward := uint64(0) // toUser
prevAddr, prevRealm := getPrev()
prevAddr, prevPkgPath := getPrev()

for _, depositId := range depositIds {
deposit := deposits[depositId]
Expand All @@ -129,15 +130,15 @@ func CollectRewardByProjectId(projectId string) uint64 {
std.Emit(
"CollectRewardByProjectId",
"prevAddr", prevAddr,
"prevRealm", prevRealm,
"prevRealm", prevPkgPath,
"projectId", projectId,
"depositId", depositId,
"amount", ufmt.Sprintf("%d", reward),
"amount", strconv.FormatUint(reward, 10),
)

// Update project and tier stats
project.stats.totalCollected += reward

var tier Tier
switch deposit.tier {
case "30":
Expand Down Expand Up @@ -200,14 +201,14 @@ func CollectRewardByDepositId(depositId string) uint64 {
}

reward := rewardStates.Get(deposit.projectId, deposit.tier).Claim(deposit.id, height)
prevAddr, prevRealm := getPrev()

prevAddr, prevPkgPath := getPrev()
std.Emit(
"CollectRewardByDepositId",
"prevAddr", prevAddr,
"prevRealm", prevRealm,
"prevRealm", prevPkgPath,
"depositId", depositId,
"internal_amount", ufmt.Sprintf("%d", reward),
"amount", strconv.FormatUint(reward, 10),
)

// Update project
Expand Down Expand Up @@ -239,6 +240,7 @@ func CollectRewardByDepositId(depositId string) uint64 {

return reward
}

/*
// calculateProjectRewards calculates rewards for a project's deposits
func calculateProjectRewards(project Project, height uint64) (Project, error) {
Expand Down Expand Up @@ -271,11 +273,11 @@ func calculateProjectRewards(project Project, height uint64) (Project, error) {
}

// Calculate rewards for each tier
tier30RewardX96, reward30, _ := calculateTierRewards(tier30, height,
tier30RewardX96, reward30, _ := calculateTierRewards(tier30, height,
lastCalculateHeightForProjectTier[tier30.id])
tier90RewardX96, reward90, _ := calculateTierRewards(tier90, height,
tier90RewardX96, reward90, _ := calculateTierRewards(tier90, height,
lastCalculateHeightForProjectTier[tier90.id])
tier180RewardX96, reward180, _ := calculateTierRewards(tier180, height,
tier180RewardX96, reward180, _ := calculateTierRewards(tier180, height,
lastCalculateHeightForProjectTier[tier180.id])

// Update project tiers
Expand Down

0 comments on commit d703148

Please sign in to comment.