Skip to content

Commit

Permalink
add launchpad new logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mconcat committed Jan 6, 2025
1 parent d032cdf commit 0686d2a
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 66 deletions.
6 changes: 0 additions & 6 deletions launchpad/api_deposit.gno
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
)

func ApiGetClaimableDepositByAddress(address std.Address) uint64 {
calculateDepositReward()

if !address.IsValid() {
return 0
}
Expand Down Expand Up @@ -40,8 +38,6 @@ func ApiGetClaimableDepositByAddress(address std.Address) uint64 {
}

func ApiGetDepositByDepositId(depositId string) string {
calculateDepositReward()

deposit, exist := deposits[depositId]
if !exist {
return ""
Expand All @@ -55,8 +51,6 @@ func ApiGetDepositByDepositId(depositId string) string {
}

func ApiGetDepositFullByDepositId(depositId string) string {
calculateDepositReward()

deposit, exist := deposits[depositId]
if !exist {
return ""
Expand Down
6 changes: 0 additions & 6 deletions launchpad/api_project.gno
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
)

func ApiGetProjectAndTierStatisticsByProjectId(projectId string) string {
calculateDepositReward()

project, exist := projects[projectId]
if !exist {
return ""
Expand All @@ -24,8 +22,6 @@ func ApiGetProjectAndTierStatisticsByProjectId(projectId string) string {
}

func ApiGetProjectStatisticsByProjectId(projectId string) string {
calculateDepositReward()

project, exist := projects[projectId]
if !exist {
return ""
Expand All @@ -39,8 +35,6 @@ func ApiGetProjectStatisticsByProjectId(projectId string) string {
}

func ApiGetProjectStatisticsByProjectTierId(tierId string) string {
calculateDepositReward()

projectId, tierStr := getProjectIdAndTierFromTierId(tierId)
project, exist := projects[projectId]
if !exist {
Expand Down
12 changes: 2 additions & 10 deletions launchpad/api_reward.gno
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (

// protocol_fee reward for project's recipient
func ApiGetProjectRecipientRewardByProjectId(projectId string) string {
calculateDepositReward()

project, exist := projects[projectId]
if !exist {
return "0"
Expand All @@ -19,8 +17,6 @@ func ApiGetProjectRecipientRewardByProjectId(projectId string) string {
}

func ApiGetProjectRecipientRewardByAddress(address std.Address) string {
calculateDepositReward()

if !address.IsValid() {
return "0"
}
Expand All @@ -30,19 +26,15 @@ func ApiGetProjectRecipientRewardByAddress(address std.Address) string {

// project reward for deposit
func ApiGetDepositRewardByDepositId(depositId string) uint64 {
calculateDepositReward()

deposit, exist := deposits[depositId]
if !exist {
return 0
}

return deposit.rewardAmount
return rewardStates.Get(deposit.projectId, deposit.tier).CalculateReward(depositId)
}

func ApiGetDepositRewardByAddress(address std.Address) uint64 {
calculateDepositReward()

if !address.IsValid() {
return 0
}
Expand All @@ -58,7 +50,7 @@ func ApiGetDepositRewardByAddress(address std.Address) uint64 {
if !exist {
continue
}
totalReward += deposit.rewardAmount
totalReward += rewardStates.Get(deposit.projectId, deposit.tier).CalculateReward(depositId)
}

return totalReward
Expand Down
3 changes: 0 additions & 3 deletions launchpad/deposit.gno
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ func DepositGns(targetProjectTierId string, amount uint64) string {

en.MintAndDistributeGns()

calculateDepositReward()
project = projects[projectId] // get updates project
tier = getTier(project, tierStr) // get updates tier

Expand Down Expand Up @@ -357,7 +356,6 @@ func CollectDepositGnsByProjectId(projectId string) uint64 {
func processCollectedDeposits(dpsts []string, pid string) uint64 {
common.IsHalted()
en.MintAndDistributeGns()
calculateDepositReward()

amount, err := processDepositCollection(dpsts, pid)
if err != nil {
Expand Down Expand Up @@ -432,7 +430,6 @@ func CollectDepositGnsByDepositId(depositId string) uint64 {

en.MintAndDistributeGns()

calculateDepositReward()
project = projects[deposit.projectId] // get updated project

// Process deposit collection
Expand Down
1 change: 0 additions & 1 deletion launchpad/json_builder.gno
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ func DepositBuilder(b *json.NodeBuilder, deposit Deposit) *json.NodeBuilder {
WriteString("depositCollectTime", ufmt.Sprintf("%d", deposit.depositCollectTime)).
WriteString("claimableHeight", ufmt.Sprintf("%d", deposit.claimableHeight)).
WriteString("claimableTime", ufmt.Sprintf("%d", deposit.claimableTime)).
WriteString("rewardAmount", ufmt.Sprintf("%d", deposit.rewardAmount)).
WriteString("rewardCollected", ufmt.Sprintf("%d", deposit.rewardCollected)).
WriteString("rewardCollectHeight", ufmt.Sprintf("%d", deposit.rewardCollectHeight)).
WriteString("rewardCollectTime", ufmt.Sprintf("%d", deposit.rewardCollectTime))
Expand Down
1 change: 0 additions & 1 deletion launchpad/launchpad.gno
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ func TransferLeftFromProjectByAdmin(projectId string, recipient std.Address) uin
common.IsHalted()
en.MintAndDistributeGns()

calculateDepositReward()
project = projects[projectId] // get updated project

leftReward := calculateLeftReward(project)
Expand Down
34 changes: 14 additions & 20 deletions launchpad/reward.gno
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ func CollectProtocolFee() {

// validateRewardCollection validates if reward can be collected
func validateRewardCollection(deposit Deposit, height uint64) error {
if deposit.rewardAmount == 0 {
return errors.New("no reward available")
}

if deposit.rewardCollectTime == 0 && height < deposit.claimableHeight {
return errors.New("reward not yet claimable")
}
Expand Down Expand Up @@ -71,6 +67,7 @@ func calculateTierRewards(tier Tier, currentHeight uint64, lastCalcHeight uint64
return rewardX96, reward, nil
}

/*
// processDepositReward processes reward for a single deposit
func processDepositReward(deposit Deposit, rewardX96 *u256.Uint, tierAmount uint64) (Deposit, error) {
if tierAmount == 0 {
Expand All @@ -86,7 +83,7 @@ func processDepositReward(deposit Deposit, rewardX96 *u256.Uint, tierAmount uint
deposit.rewardAmount += depositReward
return deposit, nil
}

*/
// CollectRewardByProjectId collects reward from entire deposit of certain project by caller
// Returns collected reward amount
// ref: https://docs.gnoswap.io/contracts/launchpad/launchpad_reward.gno#collectrewardbyprojectid
Expand All @@ -99,7 +96,6 @@ func CollectRewardByProjectId(projectId string) uint64 {
return 0
}

calculateDepositReward()
project = projects[projectId] // get updated project

caller := std.PrevRealm().Addr()
Expand All @@ -114,7 +110,8 @@ func CollectRewardByProjectId(projectId string) uint64 {

for _, depositId := range depositIds {
deposit := deposits[depositId]
if deposit.rewardAmount == 0 {
reward := rewardStates.Get(deposit.projectId, deposit.tier).Claim(depositId, height)
if reward == 0 {
continue
}

Expand All @@ -127,37 +124,36 @@ func CollectRewardByProjectId(projectId string) uint64 {
continue
}

totalReward += deposit.rewardAmount
totalReward += reward

std.Emit(
"CollectRewardByProjectId",
"prevAddr", prevAddr,
"prevRealm", prevRealm,
"projectId", projectId,
"depositId", depositId,
"amount", ufmt.Sprintf("%d", deposit.rewardAmount),
"amount", ufmt.Sprintf("%d", reward),
)

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

var tier Tier
switch deposit.tier {
case "30":
tier = project.tiers[30]
tier.userCollectedAmount += deposit.rewardAmount
tier.userCollectedAmount += reward
case "90":
tier = project.tiers[90]
tier.userCollectedAmount += deposit.rewardAmount
tier.userCollectedAmount += reward
case "180":
tier = project.tiers[180]
tier.userCollectedAmount += deposit.rewardAmount
tier.userCollectedAmount += reward
}
project = setTier(project, deposit.tier, tier)

// Update deposit
deposit.rewardCollected += deposit.rewardAmount
deposit.rewardAmount = 0
deposit.rewardCollected += reward
deposit.rewardCollectHeight = height
deposit.rewardCollectTime = uint64(time.Now().Unix())
deposits[depositId] = deposit
Expand Down Expand Up @@ -195,7 +191,6 @@ func CollectRewardByDepositId(depositId string) uint64 {
return 0
}

calculateDepositReward()
project = projects[deposit.projectId]
deposit = deposits[depositId]

Expand All @@ -204,7 +199,7 @@ func CollectRewardByDepositId(depositId string) uint64 {
return 0
}

reward := deposit.rewardAmount
reward := rewardStates.Get(deposit.projectId, deposit.tier).Claim(deposit.id, height)

prevAddr, prevRealm := getPrev()
std.Emit(
Expand Down Expand Up @@ -232,7 +227,6 @@ func CollectRewardByDepositId(depositId string) uint64 {

// Update deposit
deposit.rewardCollected += reward
deposit.rewardAmount = 0
deposit.rewardCollectHeight = height
deposit.rewardCollectTime = uint64(time.Now().Unix())
deposits[depositId] = deposit
Expand All @@ -245,7 +239,7 @@ func CollectRewardByDepositId(depositId string) uint64 {

return reward
}

/*
// calculateProjectRewards calculates rewards for a project's deposits
func calculateProjectRewards(project Project, height uint64) (Project, error) {
if project.started.height > height || project.stats.actualParticipant == 0 {
Expand Down Expand Up @@ -347,7 +341,7 @@ func calculateDepositReward() {
projects[projectId] = updatedProject
}
}

*/
// calcDepositRatioX96 calculates the deposit ratio with Q96 precision
func calcDepositRatioX96(tierAmount uint64, amount uint64) *u256.Uint {
amountX96 := new(u256.Uint).Mul(u256.NewUint(amount), q96.Clone())
Expand Down
Loading

0 comments on commit 0686d2a

Please sign in to comment.