project.gno
2.30 Kb · 92 lines
1package launchpad
2
3import (
4 "errors"
5
6 gnsmath "gno.land/p/gnoswap/gnsmath"
7 ufmt "gno.land/p/nt/ufmt/v0"
8 "gno.land/r/gnoswap/launchpad"
9)
10
11func isProjectActive(p *launchpad.Project, currentTime int64) bool {
12 return getStandardTier(p).IsActivated(currentTime)
13}
14
15func isProjectEnded(p *launchpad.Project, currentTime int64) bool {
16 return getStandardTier(p).IsEnded(currentTime)
17}
18
19func getProjectRemainingAmount(p *launchpad.Project) int64 {
20 remainingAmount := int64(0)
21
22 for _, tier := range p.Tiers() {
23 remainingAmount = gnsmath.SafeAddInt64(remainingAmount, getCalculatedLeftReward(tier))
24 }
25
26 return remainingAmount
27}
28
29func checkProjectConditions(p *launchpad.Project, caller address, balanceOfFunc func(tokenPath string, caller address) int64) error {
30 conditions := p.Conditions()
31 if conditions == nil {
32 return makeErrorWithDetails(errInvalidData, "conditions is nil")
33 }
34
35 for _, condition := range conditions {
36 // xGNS(or GNS) may have a zero condition
37 if !condition.IsAvailable() {
38 continue
39 }
40
41 tokenPath := condition.TokenPath()
42 balance := balanceOfFunc(tokenPath, caller)
43
44 if err := condition.CheckBalanceCondition(tokenPath, balance); err != nil {
45 return err
46 }
47 }
48
49 return nil
50}
51
52func getProjectTier(p *launchpad.Project, duration int64) (*launchpad.ProjectTier, error) {
53 tier, err := p.GetTier(duration)
54 if err != nil {
55 return nil, makeErrorWithDetails(errDataNotFound, err.Error())
56 }
57
58 return tier, nil
59}
60
61func getStandardTier(p *launchpad.Project) *launchpad.ProjectTier {
62 projectTier, err := p.GetTier(projectTier180)
63 if err != nil {
64 panic(makeErrorWithDetails(errDataNotFound, err.Error()))
65 }
66
67 return projectTier
68}
69
70func validateRefundRemainingAmount(p *launchpad.Project, currentTime int64) error {
71 if !isProjectEnded(p, currentTime) {
72 return errors.New(
73 ufmt.Sprintf("project not ended yet(current:%d, endTime: %d)", currentTime, getStandardTier(p).EndTime()),
74 )
75 }
76
77 if getProjectRemainingAmount(p) == 0 {
78 return errors.New(
79 ufmt.Sprintf("project has no remaining amount"),
80 )
81 }
82
83 return nil
84}
85
86func addProjectTier(p *launchpad.Project, tierDuration int64, projectTier *launchpad.ProjectTier) {
87 p.SetTier(tierDuration, projectTier)
88}
89
90func addProjectCondition(p *launchpad.Project, tokenPath string, condition *launchpad.ProjectCondition) {
91 p.SetCondition(tokenPath, condition)
92}