project.gno
7.12 Kb · 248 lines
1package launchpad
2
3import (
4 ufmt "gno.land/p/nt/ufmt/v0"
5)
6
7// Project represents a launchpad project.
8//
9// This struct contains the necessary data and methods to manage and distribute
10// rewards for a specific project.
11//
12// Fields:
13// - id (string): The unique identifier for the project, formatted as "{tokenPath}:{createdHeight}".
14// - name (string): The name of the project.
15// - tokenPath (string): The path of the token associated with the project.
16// - depositAmount (int64): The total amount of tokens deposited for the project.
17// - recipient (std.Address): The address to receive the project's rewards.
18// - conditions (map[string]*ProjectCondition): A map of token paths to their associated conditions.
19// - tiers (map[int64]*ProjectTier): A map of tier durations to their associated tiers.
20// - tiersRatios (map[int64]int64): A map of tier durations to their associated ratios.
21// - createdBlockTimeInfo (BlockTimeInfo): The block time and height information for the creation of the project.
22type Project struct {
23 id string // 'tokenPath:createdHeight'
24 name string
25 tokenPath string
26 depositAmount int64
27 recipient address // string
28 conditions map[string]*ProjectCondition // tokenPath -> Condition
29 tiers map[int64]*ProjectTier
30 tiersRatios map[int64]int64
31 createdHeight int64
32 createdAt int64
33}
34
35// GetID returns the ID of the project.
36func (p *Project) ID() string {
37 return p.id
38}
39
40// SetID sets the ID of the project.
41func (p *Project) SetID(id string) {
42 p.id = id
43}
44
45// GetName returns the name of the project.
46func (p *Project) Name() string {
47 return p.name
48}
49
50// SetName sets the name of the project.
51func (p *Project) SetName(name string) {
52 p.name = name
53}
54
55// GetTokenPath returns the token path of the project.
56func (p *Project) TokenPath() string {
57 return p.tokenPath
58}
59
60// SetTokenPath sets the token path of the project.
61func (p *Project) SetTokenPath(tokenPath string) {
62 p.tokenPath = tokenPath
63}
64
65// GetDepositAmount returns the deposit amount of the project.
66func (p *Project) DepositAmount() int64 {
67 return p.depositAmount
68}
69
70// SetDepositAmount sets the deposit amount of the project.
71func (p *Project) SetDepositAmount(amount int64) {
72 p.depositAmount = amount
73}
74
75// GetRecipient returns the recipient address of the project.
76func (p *Project) Recipient() address {
77 return p.recipient
78}
79
80// SetRecipient sets the recipient address of the project.
81func (p *Project) SetRecipient(recipient address) {
82 p.recipient = recipient
83}
84
85// GetConditions returns the conditions map of the project.
86func (p *Project) Conditions() map[string]*ProjectCondition {
87 conditions := make(map[string]*ProjectCondition)
88
89 for tokenPath, condition := range p.conditions {
90 conditions[tokenPath] = condition.Clone()
91 }
92
93 return conditions
94}
95
96// SetConditions sets the conditions map of the project. The map is rebuilt so
97// it is owned by this realm, allowing later in-domain mutations (e.g.
98// SetCondition) without hitting the readonly taint on a foreign map.
99func (p *Project) SetConditions(conditions map[string]*ProjectCondition) {
100 owned := make(map[string]*ProjectCondition, len(conditions))
101 for tokenPath, condition := range conditions {
102 owned[tokenPath] = condition
103 }
104 p.conditions = owned
105}
106
107// SetCondition sets a single condition by token path. Mutating the map inside
108// the domain method avoids the readonly taint that arises when a caller in
109// another realm round-trips Conditions()/SetConditions().
110func (p *Project) SetCondition(tokenPath string, condition *ProjectCondition) {
111 p.conditions[tokenPath] = condition
112}
113
114// GetTiers returns the tiers map of the project.
115func (p *Project) Tiers() map[int64]*ProjectTier {
116 tiers := make(map[int64]*ProjectTier)
117
118 for duration, tier := range p.tiers {
119 tiers[duration] = tier.Clone()
120 }
121
122 return tiers
123}
124
125// SetTiers sets the tiers map of the project. The map is rebuilt so it is owned
126// by this realm, allowing later in-domain mutations (e.g. SetTier) without
127// hitting the readonly taint on a map passed in from another realm.
128func (p *Project) SetTiers(tiers map[int64]*ProjectTier) {
129 owned := make(map[int64]*ProjectTier, len(tiers))
130 for duration, tier := range tiers {
131 owned[duration] = tier
132 }
133 p.tiers = owned
134}
135
136// GetTiersRatios returns the tiers ratios map of the project.
137func (p *Project) TiersRatios() map[int64]int64 {
138 return p.tiersRatios
139}
140
141// SetTiersRatios sets the tiers ratios map of the project.
142func (p *Project) SetTiersRatios(tiersRatios map[int64]int64) {
143 p.tiersRatios = tiersRatios
144}
145
146// GetCreatedHeight returns the created height of the project.
147func (p *Project) CreatedHeight() int64 {
148 return p.createdHeight
149}
150
151// SetCreatedHeight sets the created height of the project.
152func (p *Project) SetCreatedHeight(height int64) {
153 p.createdHeight = height
154}
155
156// GetCreatedAt returns the created time of the project.
157func (p *Project) CreatedAt() int64 {
158 return p.createdAt
159}
160
161// SetCreatedAt sets the created time of the project.
162func (p *Project) SetCreatedAt(time int64) {
163 p.createdAt = time
164}
165
166func (p *Project) GetTier(duration int64) (*ProjectTier, error) {
167 tier, exists := p.tiers[duration]
168 if !exists {
169 return nil, ufmt.Errorf("tier(%d) not found", duration)
170 }
171
172 return tier.Clone(), nil
173}
174
175func (p *Project) SetTier(duration int64, tier *ProjectTier) {
176 p.tiers[duration] = tier
177}
178
179// IsRecipient returns true if the project recipient is the given address.
180func (p *Project) IsRecipient(recipient address) bool {
181 return p.recipient == recipient
182}
183
184func NewProject(
185 name string,
186 tokenPath string,
187 depositAmount int64,
188 recipient address,
189 createdHeight int64,
190 createdAt int64,
191) *Project {
192 return &Project{
193 id: MakeProjectID(tokenPath, createdHeight),
194 name: name,
195 tokenPath: tokenPath,
196 depositAmount: depositAmount,
197 recipient: recipient,
198 conditions: make(map[string]*ProjectCondition),
199 tiers: make(map[int64]*ProjectTier),
200 tiersRatios: make(map[int64]int64),
201 createdHeight: createdHeight,
202 createdAt: createdAt,
203 }
204}
205
206func (p Project) Clone() *Project {
207 conditions := make(map[string]*ProjectCondition)
208 for k, v := range p.conditions {
209 conditions[k] = v.Clone()
210 }
211
212 tiers := make(map[int64]*ProjectTier)
213 for k, v := range p.tiers {
214 tiers[k] = v.Clone()
215 }
216
217 tiersRatios := make(map[int64]int64)
218 for k, v := range p.tiersRatios {
219 tiersRatios[k] = v
220 }
221
222 return &Project{
223 id: p.id,
224 name: p.name,
225 tokenPath: p.tokenPath,
226 depositAmount: p.depositAmount,
227 recipient: p.recipient,
228 conditions: conditions,
229 tiers: tiers,
230 tiersRatios: tiersRatios,
231 createdHeight: p.createdHeight,
232 createdAt: p.createdAt,
233 }
234}
235
236// MakeProjectID generates a unique project ID based on the given token path and the current block height.
237//
238// The generated ID combines the `tokenPath` and the current block height in the following format:
239// "{tokenPath}:{height}"
240//
241// Parameters:
242// - tokenPath (string): The path of the token associated with the project.
243//
244// Returns:
245// - string: A unique project ID in the format "tokenPath:height".
246func MakeProjectID(tokenPath string, createdHeight int64) string {
247 return ufmt.Sprintf("%s:%d", tokenPath, createdHeight)
248}