From f5ce2847cb8d5f10591f0680a66a42b6e2225335 Mon Sep 17 00:00:00 2001 From: Lee ByeongJun Date: Fri, 3 Jan 2025 18:47:42 +0900 Subject: [PATCH] fix rpc --- launchpad/_RPC_api_deposit.gno | 152 +++++++------------------ launchpad/_RPC_api_project.gno | 201 ++++++++------------------------- launchpad/json_builder.gno | 116 +++++++++++++++++++ 3 files changed, 201 insertions(+), 268 deletions(-) create mode 100644 launchpad/json_builder.gno diff --git a/launchpad/_RPC_api_deposit.gno b/launchpad/_RPC_api_deposit.gno index 75c3e70e9..054771c02 100644 --- a/launchpad/_RPC_api_deposit.gno +++ b/launchpad/_RPC_api_deposit.gno @@ -40,126 +40,56 @@ func ApiGetClaimableDepositByAddress(address std.Address) uint64 { } func ApiGetDepositByDepositId(depositId string) string { - calculateDepositReward() + calculateDepositReward() - deposit, exist := deposits[depositId] - if !exist { - return "" - } + deposit, exist := deposits[depositId] + if !exist { + return "" + } + + builder := MetaBuilder(). + WriteString("depositId", depositId) + DepositBuilder(builder, deposit) - depositObj := metaNode() - depositObj.AppendObject("depositId", json.StringNode("depositId", depositId)) - depositObj.AppendObject("projectId", json.StringNode("projectId", deposit.projectId)) - depositObj.AppendObject("tier", json.StringNode("tier", deposit.tier)) - depositObj.AppendObject("depositor", json.StringNode("depositor", deposit.depositor.String())) - depositObj.AppendObject("amount", json.StringNode("amount", ufmt.Sprintf("%d", deposit.amount))) - depositObj.AppendObject("depositHeight", json.StringNode("depositHeight", ufmt.Sprintf("%d", deposit.depositHeight))) - depositObj.AppendObject("depositTime", json.StringNode("depositTime", ufmt.Sprintf("%d", deposit.depositTime))) - depositObj.AppendObject("depositCollectHeight", json.StringNode("depositCollectHeight", ufmt.Sprintf("%d", deposit.depositCollectHeight))) - depositObj.AppendObject("depositCollectTime", json.StringNode("depositCollectTime", ufmt.Sprintf("%d", deposit.depositCollectTime))) - depositObj.AppendObject("claimableHeight", json.StringNode("claimableHeight", ufmt.Sprintf("%d", deposit.claimableHeight))) - depositObj.AppendObject("claimableTime", json.StringNode("claimableTime", ufmt.Sprintf("%d", deposit.claimableTime))) - depositObj.AppendObject("rewardAmount", json.StringNode("rewardAmount", ufmt.Sprintf("%d", deposit.rewardAmount))) - depositObj.AppendObject("rewardCollected", json.StringNode("rewardCollected", ufmt.Sprintf("%d", deposit.rewardCollected))) - depositObj.AppendObject("rewardCollectHeight", json.StringNode("rewardCollectHeight", ufmt.Sprintf("%d", deposit.rewardCollectHeight))) - depositObj.AppendObject("rewardCollectTime", json.StringNode("rewardCollectTime", ufmt.Sprintf("%d", deposit.rewardCollectTime))) - - return marshal(depositObj) + return marshal(builder.Build()) } func ApiGetDepositFullByDepositId(depositId string) string { - calculateDepositReward() + calculateDepositReward() - deposit, exist := deposits[depositId] - if !exist { - return "" - } + deposit, exist := deposits[depositId] + if !exist { + return "" + } - project, exist := projects[deposit.projectId] - if !exist { - return "" - } + project, exist := projects[deposit.projectId] + if !exist { + return "" + } - var tier Tier - tierStr := deposit.tier - switch tierStr { - case "30": - tier = project.tiers[30] - case "90": - tier = project.tiers[90] - case "180": - tier = project.tiers[180] - } + var tier Tier + switch deposit.tier { + case "30": + tier = project.tiers[30] + case "90": + tier = project.tiers[90] + case "180": + tier = project.tiers[180] + } + + builder := MetaBuilder(). + WriteString("depositId", depositId) + + // Add project info + ProjectBuilder(builder, project) + + // Add tier info + TierBuilder(builder, "", tier) + + // Add deposit info + DepositBuilder(builder, deposit) - depositObj := metaNode() - depositObj.AppendObject("depositId", json.StringNode("depositId", depositId)) - - // project info - depositObj.AppendObject("projectId", json.StringNode("projectId", deposit.projectId)) - depositObj.AppendObject("tokenPath", json.StringNode("tokenPath", project.tokenPath)) - depositObj.AppendObject("depositAmount", json.StringNode("depositAmount", ufmt.Sprintf("%d", project.depositAmount))) - depositObj.AppendObject("recipient", json.StringNode("recipient", project.recipient.String())) - tokenPathList, amountList := makeConditionsToStr(project.conditions) - depositObj.AppendObject("conditionsToken", json.StringNode("conditionsToken", tokenPathList)) - depositObj.AppendObject("conditionsAmount", json.StringNode("conditionsAmount", amountList)) - - depositObj.AppendObject("tier30Ratio", json.StringNode("tier30Ratio", ufmt.Sprintf("%d", project.tiers[30].tierAmount))) - depositObj.AppendObject("tier90Ratio", json.StringNode("tier90Ratio", ufmt.Sprintf("%d", project.tiers[90].tierAmount))) - depositObj.AppendObject("tier180Ratio", json.StringNode("tier180Ratio", ufmt.Sprintf("%d", project.tiers[180].tierAmount))) - - depositObj.AppendObject("createdHeight", json.StringNode("createdHeight", ufmt.Sprintf("%d", project.created.height))) - depositObj.AppendObject("createdTime", json.StringNode("createdTime", ufmt.Sprintf("%d", project.created.time))) - depositObj.AppendObject("startHeight", json.StringNode("startHeight", ufmt.Sprintf("%d", project.started.height))) - depositObj.AppendObject("startTime", json.StringNode("startTime", ufmt.Sprintf("%d", project.started.time))) - depositObj.AppendObject("endHeight", json.StringNode("endHeight", ufmt.Sprintf("%d", project.ended.height))) - depositObj.AppendObject("endTime", json.StringNode("endTime", ufmt.Sprintf("%d", project.ended.time))) - - depositObj.AppendObject("totalDeposit", json.StringNode("totalDeposit", ufmt.Sprintf("%d", project.stats.totalDeposit))) - depositObj.AppendObject("actualDeposit", json.StringNode("actualDeposit", ufmt.Sprintf("%d", project.stats.actualDeposit))) - - depositObj.AppendObject("totalParticipant", json.StringNode("totalParticipant", ufmt.Sprintf("%d", project.stats.totalParticipant))) - depositObj.AppendObject("actualParticipant", json.StringNode("actualParticipant", ufmt.Sprintf("%d", project.stats.actualParticipant))) - - depositObj.AppendObject("totalCollected", json.StringNode("totalCollected", ufmt.Sprintf("%d", project.stats.totalCollected))) - - depositObj.AppendObject("refundedAmount", json.StringNode("refundedAmount", ufmt.Sprintf("%d", project.refund.amount))) - depositObj.AppendObject("refundedHeight", json.StringNode("refundedHeight", ufmt.Sprintf("%d", project.refund.height))) - depositObj.AppendObject("refundedTime", json.StringNode("refundedTime", ufmt.Sprintf("%d", project.refund.time))) - - // tier info - depositObj.AppendObject("tier", json.StringNode("tier", deposit.tier)) - depositObj.AppendObject("collectWaitDuration", json.StringNode("collectWaitDuration", ufmt.Sprintf("%d", tier.collectWaitDuration))) - depositObj.AppendObject("tierAmount", json.StringNode("tierAmount", ufmt.Sprintf("%d", tier.tierAmount))) - depositObj.AppendObject("tierAmountPerBlockX96", json.StringNode("tierAmountPerBlockX96", tier.tierAmountPerBlockX96.ToString())) - depositObj.AppendObject("startHeight", json.StringNode("startHeight", ufmt.Sprintf("%d", tier.started.height))) - depositObj.AppendObject("startTime", json.StringNode("startTime", ufmt.Sprintf("%d", tier.started.time))) - depositObj.AppendObject("endHeight", json.StringNode("endHeight", ufmt.Sprintf("%d", tier.ended.height))) - depositObj.AppendObject("endTime", json.StringNode("endTime", ufmt.Sprintf("%d", tier.ended.time))) - - depositObj.AppendObject("totalDepositAmount", json.StringNode("totalDepositAmount", ufmt.Sprintf("%d", tier.totalDepositAmount))) - depositObj.AppendObject("actualDepositAmount", json.StringNode("actualDepositAmount", ufmt.Sprintf("%d", tier.actualDepositAmount))) - - depositObj.AppendObject("totalParticipant", json.StringNode("totalParticipant", ufmt.Sprintf("%d", tier.totalParticipant))) - depositObj.AppendObject("actualParticipant", json.StringNode("actualParticipant", ufmt.Sprintf("%d", tier.actualParticipant))) - - depositObj.AppendObject("userCollectedAmount", json.StringNode("userCollectedAmount", ufmt.Sprintf("%d", tier.userCollectedAmount))) - depositObj.AppendObject("calculatedAmount", json.StringNode("calculatedAmount", ufmt.Sprintf("%d", tier.calculatedAmount))) - - // deposit info - depositObj.AppendObject("depositor", json.StringNode("depositor", deposit.depositor.String())) - depositObj.AppendObject("amount", json.StringNode("amount", ufmt.Sprintf("%d", deposit.amount))) - depositObj.AppendObject("depositHeight", json.StringNode("depositHeight", ufmt.Sprintf("%d", deposit.depositHeight))) - depositObj.AppendObject("depositTime", json.StringNode("depositTime", ufmt.Sprintf("%d", deposit.depositTime))) - depositObj.AppendObject("depositCollectHeight", json.StringNode("depositCollectHeight", ufmt.Sprintf("%d", deposit.depositCollectHeight))) - depositObj.AppendObject("depositCollectTime", json.StringNode("depositCollectTime", ufmt.Sprintf("%d", deposit.depositCollectTime))) - depositObj.AppendObject("claimableHeight", json.StringNode("claimableHeight", ufmt.Sprintf("%d", deposit.claimableHeight))) - depositObj.AppendObject("claimableTime", json.StringNode("claimableTime", ufmt.Sprintf("%d", deposit.claimableTime))) - depositObj.AppendObject("rewardAmount", json.StringNode("rewardAmount", ufmt.Sprintf("%d", deposit.rewardAmount))) - depositObj.AppendObject("rewardCollected", json.StringNode("rewardCollected", ufmt.Sprintf("%d", deposit.rewardCollected))) - depositObj.AppendObject("rewardCollectHeight", json.StringNode("rewardCollectHeight", ufmt.Sprintf("%d", deposit.rewardCollectHeight))) - depositObj.AppendObject("rewardCollectTime", json.StringNode("rewardCollectTime", ufmt.Sprintf("%d", deposit.rewardCollectTime))) - - return marshal(depositObj) + return marshal(builder.Build()) } func makeConditionsToStr(conditions map[string]Condition) (string, string) { diff --git a/launchpad/_RPC_api_project.gno b/launchpad/_RPC_api_project.gno index 93f032008..3a1edbc66 100644 --- a/launchpad/_RPC_api_project.gno +++ b/launchpad/_RPC_api_project.gno @@ -9,85 +9,18 @@ import ( ) func ApiGetProjectAndTierStatisticsByProjectId(projectId string) string { - calculateDepositReward() + calculateDepositReward() - project, exist := projects[projectId] - if !exist { - return "" - } + project, exist := projects[projectId] + if !exist { + return "" + } + + builder := MetaBuilder(). + WriteString("projectId", projectId) + ProjectBuilder(builder, project) - actualDeposit := project.stats.actualDeposit - - totalParticipant := project.stats.totalParticipant - - projectObj := metaNode() - projectObj.AppendObject("projectId", json.StringNode("projectId", projectId)) - projectObj.AppendObject("name", json.StringNode("name", project.name)) - projectObj.AppendObject("tokenPath", json.StringNode("tokenPath", project.tokenPath)) - projectObj.AppendObject("depositAmount", json.StringNode("depositAmount", ufmt.Sprintf("%d", project.depositAmount))) - projectObj.AppendObject("recipient", json.StringNode("recipient", project.recipient.String())) - - tokenPathList, amountList := makeConditionsToStr(project.conditions) - projectObj.AppendObject("conditionsToken", json.StringNode("conditionsToken", tokenPathList)) - projectObj.AppendObject("conditionsAmount", json.StringNode("conditionsAmount", amountList)) - - projectObj.AppendObject("tier30Ratio", json.StringNode("tier30Ratio", ufmt.Sprintf("%d", project.tiersRatios[30]))) - projectObj.AppendObject("tier90Ratio", json.StringNode("tier90Ratio", ufmt.Sprintf("%d", project.tiersRatios[90]))) - projectObj.AppendObject("tier180Ratio", json.StringNode("tier180Ratio", ufmt.Sprintf("%d", project.tiersRatios[180]))) - - projectObj.AppendObject("createdHeight", json.StringNode("createdHeight", ufmt.Sprintf("%d", project.created.height))) - projectObj.AppendObject("createdTime", json.StringNode("createdTime", ufmt.Sprintf("%d", project.created.time))) - projectObj.AppendObject("startHeight", json.StringNode("startHeight", ufmt.Sprintf("%d", project.started.height))) - projectObj.AppendObject("startTime", json.StringNode("startTime", ufmt.Sprintf("%d", project.started.time))) - projectObj.AppendObject("endHeight", json.StringNode("endHeight", ufmt.Sprintf("%d", project.ended.height))) - projectObj.AppendObject("endTime", json.StringNode("endTime", ufmt.Sprintf("%d", project.ended.time))) - - projectObj.AppendObject("totalDeposit", json.StringNode("totalDeposit", ufmt.Sprintf("%d", project.stats.totalDeposit))) - projectObj.AppendObject("actualDeposit", json.StringNode("actualDeposit", ufmt.Sprintf("%d", actualDeposit))) - - projectObj.AppendObject("totalParticipant", json.StringNode("totalParticipant", ufmt.Sprintf("%d", totalParticipant))) - projectObj.AppendObject("actualParticipant", json.StringNode("actualParticipant", ufmt.Sprintf("%d", project.stats.actualParticipant))) - - projectObj.AppendObject("totalCollected", json.StringNode("totalCollected", ufmt.Sprintf("%d", project.stats.totalCollected))) - - projectObj.AppendObject("refundedAmount", json.StringNode("refundedAmount", ufmt.Sprintf("%d", project.refund.amount))) - projectObj.AppendObject("refundedHeight", json.StringNode("refundedHeight", ufmt.Sprintf("%d", project.refund.height))) - projectObj.AppendObject("refundedTime", json.StringNode("refundedTime", ufmt.Sprintf("%d", project.refund.time))) - - projectObj.AppendObject("tier30StartHeight", json.StringNode("tier30StartHeight", ufmt.Sprintf("%d", project.tiers[30].started.height))) - projectObj.AppendObject("tier30StartTime", json.StringNode("tier30StartTime", ufmt.Sprintf("%d", project.tiers[30].started.time))) - projectObj.AppendObject("tier30EndHeight", json.StringNode("tier30EndHeight", ufmt.Sprintf("%d", project.tiers[30].ended.height))) - projectObj.AppendObject("tier30EndTime", json.StringNode("tier30EndTime", ufmt.Sprintf("%d", project.tiers[30].ended.time))) - projectObj.AppendObject("tier30TotalDepositAmount", json.StringNode("tier30TotalDepositAmount", ufmt.Sprintf("%d", project.tiers[30].totalDepositAmount))) - projectObj.AppendObject("tier30ActualDepositAmount", json.StringNode("tier30ActualDepositAmount", ufmt.Sprintf("%d", project.tiers[30].actualDepositAmount))) - projectObj.AppendObject("tier30TotalParticipant", json.StringNode("tier30TotalParticipant", ufmt.Sprintf("%d", project.tiers[30].totalParticipant))) - projectObj.AppendObject("tier30ActualParticipant", json.StringNode("tier30ActualParticipant", ufmt.Sprintf("%d", project.tiers[30].actualParticipant))) - projectObj.AppendObject("tier30UserCollectedAmount", json.StringNode("tier30UserCollectedAmount", ufmt.Sprintf("%d", project.tiers[30].userCollectedAmount))) - projectObj.AppendObject("tier30CalculatedAmount", json.StringNode("tier30CalculatedAmount", ufmt.Sprintf("%d", project.tiers[30].calculatedAmount))) - - projectObj.AppendObject("tier90StartHeight", json.StringNode("tier90StartHeight", ufmt.Sprintf("%d", project.tiers[90].started.height))) - projectObj.AppendObject("tier90StartTime", json.StringNode("tier90StartTime", ufmt.Sprintf("%d", project.tiers[90].started.time))) - projectObj.AppendObject("tier90EndHeight", json.StringNode("tier90EndHeight", ufmt.Sprintf("%d", project.tiers[90].ended.height))) - projectObj.AppendObject("tier90EndTime", json.StringNode("tier90EndTime", ufmt.Sprintf("%d", project.tiers[90].ended.time))) - projectObj.AppendObject("tier90TotalDepositAmount", json.StringNode("tier90TotalDepositAmount", ufmt.Sprintf("%d", project.tiers[90].totalDepositAmount))) - projectObj.AppendObject("tier90ActualDepositAmount", json.StringNode("tier90ActualDepositAmount", ufmt.Sprintf("%d", project.tiers[90].actualDepositAmount))) - projectObj.AppendObject("tier90TotalParticipant", json.StringNode("tier90TotalParticipant", ufmt.Sprintf("%d", project.tiers[90].totalParticipant))) - projectObj.AppendObject("tier90ActualParticipant", json.StringNode("tier90ActualParticipant", ufmt.Sprintf("%d", project.tiers[90].actualParticipant))) - projectObj.AppendObject("tier90UserCollectedAmount", json.StringNode("tier90UserCollectedAmount", ufmt.Sprintf("%d", project.tiers[90].userCollectedAmount))) - projectObj.AppendObject("tier90CalculatedAmount", json.StringNode("tier90CalculatedAmount", ufmt.Sprintf("%d", project.tiers[90].calculatedAmount))) - - projectObj.AppendObject("tier180StartHeight", json.StringNode("tier180StartHeight", ufmt.Sprintf("%d", project.tiers[180].started.height))) - projectObj.AppendObject("tier180StartTime", json.StringNode("tier180StartTime", ufmt.Sprintf("%d", project.tiers[180].started.time))) - projectObj.AppendObject("tier180EndHeight", json.StringNode("tier180EndHeight", ufmt.Sprintf("%d", project.tiers[180].ended.height))) - projectObj.AppendObject("tier180EndTime", json.StringNode("tier180EndTime", ufmt.Sprintf("%d", project.tiers[180].ended.time))) - projectObj.AppendObject("tier180TotalDepositAmount", json.StringNode("tier180TotalDepositAmount", ufmt.Sprintf("%d", project.tiers[180].totalDepositAmount))) - projectObj.AppendObject("tier180ActualDepositAmount", json.StringNode("tier180ActualDepositAmount", ufmt.Sprintf("%d", project.tiers[180].actualDepositAmount))) - projectObj.AppendObject("tier180TotalParticipant", json.StringNode("tier180TotalParticipant", ufmt.Sprintf("%d", project.tiers[180].totalParticipant))) - projectObj.AppendObject("tier180ActualParticipant", json.StringNode("tier180ActualParticipant", ufmt.Sprintf("%d", project.tiers[180].actualParticipant))) - projectObj.AppendObject("tier180UserCollectedAmount", json.StringNode("tier180UserCollectedAmount", ufmt.Sprintf("%d", project.tiers[180].userCollectedAmount))) - projectObj.AppendObject("tier180CalculatedAmount", json.StringNode("tier180CalculatedAmount", ufmt.Sprintf("%d", project.tiers[180].calculatedAmount))) - - return marshal(projectObj) + return marshal(builder.Build()) } func ApiGetProjectStatisticsByProjectId(projectId string) string { @@ -98,90 +31,44 @@ func ApiGetProjectStatisticsByProjectId(projectId string) string { return "" } - totalDeposit := project.stats.totalDeposit - actualDeposit := project.stats.actualDeposit - - totalParticipant := project.stats.totalParticipant - actualParticipant := project.stats.actualParticipant - - totalCollected := project.stats.totalCollected - - projectObj := metaNode() - projectObj.AppendObject("projectId", json.StringNode("projectId", projectId)) - projectObj.AppendObject("name", json.StringNode("name", project.name)) - projectObj.AppendObject("tokenPath", json.StringNode("tokenPath", project.tokenPath)) - projectObj.AppendObject("depositAmount", json.StringNode("depositAmount", ufmt.Sprintf("%d", project.depositAmount))) - projectObj.AppendObject("recipient", json.StringNode("recipient", project.recipient.String())) - - tokenPathList, amountList := makeConditionsToStr(project.conditions) - projectObj.AppendObject("conditionsToken", json.StringNode("conditionsToken", tokenPathList)) - projectObj.AppendObject("conditionsAmount", json.StringNode("conditionsAmount", amountList)) - - projectObj.AppendObject("tier30Ratio", json.StringNode("tier30Ratio", ufmt.Sprintf("%d", project.tiers[30].tierAmount))) - projectObj.AppendObject("tier90Ratio", json.StringNode("tier90Ratio", ufmt.Sprintf("%d", project.tiers[90].tierAmount))) - projectObj.AppendObject("tier180Ratio", json.StringNode("tier180Ratio", ufmt.Sprintf("%d", project.tiers[180].tierAmount))) - - projectObj.AppendObject("createdHeight", json.StringNode("createdHeight", ufmt.Sprintf("%d", project.created.height))) - projectObj.AppendObject("createdTime", json.StringNode("createdTime", ufmt.Sprintf("%d", project.created.time))) - projectObj.AppendObject("startHeight", json.StringNode("startHeight", ufmt.Sprintf("%d", project.started.height))) - projectObj.AppendObject("startTime", json.StringNode("startTime", ufmt.Sprintf("%d", project.started.time))) - projectObj.AppendObject("endHeight", json.StringNode("endHeight", ufmt.Sprintf("%d", project.ended.height))) - projectObj.AppendObject("endTime", json.StringNode("endTime", ufmt.Sprintf("%d", project.ended.time))) - - projectObj.AppendObject("totalDepositAmount", json.StringNode("totalDepositAmount", ufmt.Sprintf("%d", totalDeposit))) - projectObj.AppendObject("actualDepositAmount", json.StringNode("actualDepositAmount", ufmt.Sprintf("%d", actualDeposit))) - - projectObj.AppendObject("totalParticipant", json.StringNode("totalParticipant", ufmt.Sprintf("%d", totalParticipant))) - projectObj.AppendObject("actualParticipant", json.StringNode("actualParticipant", ufmt.Sprintf("%d", actualParticipant))) - - projectObj.AppendObject("totalCollectedAmount", json.StringNode("totalCollectedAmount", ufmt.Sprintf("%d", totalCollected))) + builder := MetaBuilder(). + WriteString("projectId", projectId) + ProjectBuilder(builder, project) - projectObj.AppendObject("refundedAmount", json.StringNode("refundedAmount", ufmt.Sprintf("%d", project.refund.amount))) - projectObj.AppendObject("refundedHeight", json.StringNode("refundedHeight", ufmt.Sprintf("%d", project.refund.height))) - projectObj.AppendObject("refundedTime", json.StringNode("refundedTime", ufmt.Sprintf("%d", project.refund.time))) - - return marshal(projectObj) + return marshal(builder.Build()) } func ApiGetProjectStatisticsByProjectTierId(tierId string) string { - calculateDepositReward() - - projectId, tierStr := getProjectIdAndTierFromTierId(tierId) - project, exist := projects[projectId] - if !exist { - return "" - } - - var tier Tier - switch tierStr { - case "30": - tier = project.tiers[30] - case "90": - tier = project.tiers[90] - case "180": - tier = project.tiers[180] - default: - return "" - } - - tierAmount := tier.tierAmount // project token allocation - - tierTotalDepositAmount := tier.totalDepositAmount - tierActualDepositAmount := tier.actualDepositAmount - - tierTotalParticipant := tier.totalParticipant - tierActualParticipant := tier.actualParticipant - - projectTierObj := metaNode() - projectTierObj.AppendObject("projectId", json.StringNode("projectId", projectId)) - projectTierObj.AppendObject("tierId", json.StringNode("tierId", tierId)) - projectTierObj.AppendObject("tierAmount", json.StringNode("tierAmount", ufmt.Sprintf("%d", tierAmount))) - projectTierObj.AppendObject("tierTotalDepositAmount", json.StringNode("tierTotalDepositAmount", ufmt.Sprintf("%d", tierTotalDepositAmount))) - projectTierObj.AppendObject("tierActualDepositAmount", json.StringNode("tierActualDepositAmount", ufmt.Sprintf("%d", tierActualDepositAmount))) - projectTierObj.AppendObject("tierTotalParticipant", json.StringNode("tierTotalParticipant", ufmt.Sprintf("%d", tierTotalParticipant))) - projectTierObj.AppendObject("tierActualParticipant", json.StringNode("tierActualParticipant", ufmt.Sprintf("%d", tierActualParticipant))) - - return marshal(projectTierObj) + calculateDepositReward() + + projectId, tierStr := getProjectIdAndTierFromTierId(tierId) + project, exist := projects[projectId] + if !exist { + return "" + } + + var tier Tier + switch tierStr { + case "30": + tier = project.tiers[30] + case "90": + tier = project.tiers[90] + case "180": + tier = project.tiers[180] + default: + return "" + } + + builder := MetaBuilder(). + WriteString("projectId", projectId). + WriteString("tierId", tierId). + WriteString("tierAmount", ufmt.Sprintf("%d", tier.tierAmount)). + WriteString("tierTotalDepositAmount", ufmt.Sprintf("%d", tier.totalDepositAmount)). + WriteString("tierActualDepositAmount", ufmt.Sprintf("%d", tier.actualDepositAmount)). + WriteString("tierTotalParticipant", ufmt.Sprintf("%d", tier.totalParticipant)). + WriteString("tierActualParticipant", ufmt.Sprintf("%d", tier.actualParticipant)) + + return marshal(builder.Build()) } func metaNode() *json.Node { diff --git a/launchpad/json_builder.gno b/launchpad/json_builder.gno new file mode 100644 index 000000000..95d0516ee --- /dev/null +++ b/launchpad/json_builder.gno @@ -0,0 +1,116 @@ +package launchpad + +import ( + "gno.land/p/demo/json" + "gno.land/p/demo/ufmt" +) + +// TimeInfoBuilder adds TimeInfo fields to JSON +func TimeInfoBuilder(b *json.NodeBuilder, prefix string, info TimeInfo) *json.NodeBuilder { + return b. + WriteString(prefix+"Height", ufmt.Sprintf("%d", info.height)). + WriteString(prefix+"Time", ufmt.Sprintf("%d", info.time)) +} + +// ProjectStatsBuilder adds ProjectStats fields to JSON +func ProjectStatsBuilder(b *json.NodeBuilder, stats ProjectStats) *json.NodeBuilder { + return b. + WriteString("totalDeposit", ufmt.Sprintf("%d", stats.totalDeposit)). + WriteString("actualDeposit", ufmt.Sprintf("%d", stats.actualDeposit)). + WriteString("totalParticipant", ufmt.Sprintf("%d", stats.totalParticipant)). + WriteString("actualParticipant", ufmt.Sprintf("%d", stats.actualParticipant)). + WriteString("totalCollected", ufmt.Sprintf("%d", stats.totalCollected)) +} + +// RefundInfoBuilder adds RefundInfo fields to JSON +func RefundInfoBuilder(b *json.NodeBuilder, info RefundInfo) *json.NodeBuilder { + return b. + WriteString("refundedAmount", ufmt.Sprintf("%d", info.amount)). + WriteString("refundedHeight", ufmt.Sprintf("%d", info.height)). + WriteString("refundedTime", ufmt.Sprintf("%d", info.time)) +} + +// TierBuilder adds Tier fields to JSON +func TierBuilder(b *json.NodeBuilder, prefix string, tier Tier) *json.NodeBuilder { + b.WriteString(prefix+"CollectWaitDuration", ufmt.Sprintf("%d", tier.collectWaitDuration)) + b.WriteString(prefix+"TierAmount", ufmt.Sprintf("%d", tier.tierAmount)) + if tier.tierAmountPerBlockX96 != nil { + b.WriteString(prefix+"TierAmountPerBlockX96", tier.tierAmountPerBlockX96.ToString()) + } + TimeInfoBuilder(b, prefix+"Started", tier.started) + TimeInfoBuilder(b, prefix+"Ended", tier.ended) + b.WriteString(prefix+"TotalDepositAmount", ufmt.Sprintf("%d", tier.totalDepositAmount)) + b.WriteString(prefix+"ActualDepositAmount", ufmt.Sprintf("%d", tier.actualDepositAmount)) + b.WriteString(prefix+"TotalParticipant", ufmt.Sprintf("%d", tier.totalParticipant)) + b.WriteString(prefix+"ActualParticipant", ufmt.Sprintf("%d", tier.actualParticipant)) + b.WriteString(prefix+"UserCollectedAmount", ufmt.Sprintf("%d", tier.userCollectedAmount)) + b.WriteString(prefix+"CalculatedAmount", ufmt.Sprintf("%d", tier.calculatedAmount)) + return b +} + +// ProjectBuilder adds Project fields to JSON +func ProjectBuilder(b *json.NodeBuilder, project Project) *json.NodeBuilder { + b.WriteString("name", project.name) + b.WriteString("tokenPath", project.tokenPath) + b.WriteString("depositAmount", ufmt.Sprintf("%d", project.depositAmount)) + b.WriteString("recipient", project.recipient.String()) + + tokenPathList, amountList := makeConditionsToStr(project.conditions) + b.WriteString("conditionsToken", tokenPathList) + b.WriteString("conditionsAmount", amountList) + + // Add tier ratios + for _, duration := range []uint64{30, 90, 180} { + b.WriteString(ufmt.Sprintf("tier%dRatio", duration), + ufmt.Sprintf("%d", project.tiersRatios[duration])) + } + + // Add time info + TimeInfoBuilder(b, "created", project.created) + TimeInfoBuilder(b, "started", project.started) + TimeInfoBuilder(b, "ended", project.ended) + + // Add stats + ProjectStatsBuilder(b, project.stats) + + // Add refund info + RefundInfoBuilder(b, project.refund) + + // Add tiers info + for _, duration := range []uint64{30, 90, 180} { + if tier, exists := project.tiers[duration]; exists { + TierBuilder(b, ufmt.Sprintf("tier%d", duration), tier) + } + } + + return b +} + +// DepositBuilder adds Deposit fields to JSON +func DepositBuilder(b *json.NodeBuilder, deposit Deposit) *json.NodeBuilder { + return b. + WriteString("projectId", deposit.projectId). + WriteString("tier", deposit.tier). + WriteString("depositor", deposit.depositor.String()). + WriteString("amount", ufmt.Sprintf("%d", deposit.amount)). + WriteString("depositHeight", ufmt.Sprintf("%d", deposit.depositHeight)). + WriteString("depositTime", ufmt.Sprintf("%d", deposit.depositTime)). + WriteString("depositCollectHeight", ufmt.Sprintf("%d", deposit.depositCollectHeight)). + 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)) +} + +// MetaBuilder adds metadata fields to JSON +func MetaBuilder() *json.NodeBuilder { + height := std.GetHeight() + now := time.Now().Unix() + + return json.Builder(). + WriteString("height", ufmt.Sprintf("%d", height)). + WriteString("now", ufmt.Sprintf("%d", now)) +}