Search Apps Documentation Source Content File Folder Download Copy Actions Download

project.gno

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