Search Apps Documentation Source Content File Folder Download Copy Actions Download

project.gno

6.20 Kb ยท 229 lines
  1package launchpad
  2
  3import (
  4	"gno.land/p/nt/ufmt"
  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.
 97func (p *Project) SetConditions(conditions map[string]*ProjectCondition) {
 98	p.conditions = conditions
 99}
100
101// GetTiers returns the tiers map of the project.
102func (p *Project) Tiers() map[int64]*ProjectTier {
103	tiers := make(map[int64]*ProjectTier)
104
105	for duration, tier := range p.tiers {
106		tiers[duration] = tier.Clone()
107	}
108
109	return tiers
110}
111
112// SetTiers sets the tiers map of the project.
113func (p *Project) SetTiers(tiers map[int64]*ProjectTier) {
114	p.tiers = tiers
115}
116
117// GetTiersRatios returns the tiers ratios map of the project.
118func (p *Project) TiersRatios() map[int64]int64 {
119	return p.tiersRatios
120}
121
122// SetTiersRatios sets the tiers ratios map of the project.
123func (p *Project) SetTiersRatios(tiersRatios map[int64]int64) {
124	p.tiersRatios = tiersRatios
125}
126
127// GetCreatedHeight returns the created height of the project.
128func (p *Project) CreatedHeight() int64 {
129	return p.createdHeight
130}
131
132// SetCreatedHeight sets the created height of the project.
133func (p *Project) SetCreatedHeight(height int64) {
134	p.createdHeight = height
135}
136
137// GetCreatedAt returns the created time of the project.
138func (p *Project) CreatedAt() int64 {
139	return p.createdAt
140}
141
142// SetCreatedAt sets the created time of the project.
143func (p *Project) SetCreatedAt(time int64) {
144	p.createdAt = time
145}
146
147func (p *Project) GetTier(duration int64) (*ProjectTier, error) {
148	tier, exists := p.tiers[duration]
149	if !exists {
150		return nil, ufmt.Errorf("tier(%d) not found", duration)
151	}
152
153	return tier.Clone(), nil
154}
155
156func (p *Project) SetTier(duration int64, tier *ProjectTier) {
157	p.tiers[duration] = tier
158}
159
160// IsRecipient returns true if the project recipient is the given address.
161func (p *Project) IsRecipient(recipient address) bool {
162	return p.recipient == recipient
163}
164
165func NewProject(
166	name string,
167	tokenPath string,
168	depositAmount int64,
169	recipient address,
170	createdHeight int64,
171	createdAt int64,
172) *Project {
173	return &Project{
174		id:            MakeProjectID(tokenPath, createdHeight),
175		name:          name,
176		tokenPath:     tokenPath,
177		depositAmount: depositAmount,
178		recipient:     recipient,
179		conditions:    make(map[string]*ProjectCondition),
180		tiers:         make(map[int64]*ProjectTier),
181		tiersRatios:   make(map[int64]int64),
182		createdHeight: createdHeight,
183		createdAt:     createdAt,
184	}
185}
186
187func (p Project) Clone() *Project {
188	conditions := make(map[string]*ProjectCondition)
189	for k, v := range p.conditions {
190		conditions[k] = v.Clone()
191	}
192
193	tiers := make(map[int64]*ProjectTier)
194	for k, v := range p.tiers {
195		tiers[k] = v.Clone()
196	}
197
198	tiersRatios := make(map[int64]int64)
199	for k, v := range p.tiersRatios {
200		tiersRatios[k] = v
201	}
202
203	return &Project{
204		id:            p.id,
205		name:          p.name,
206		tokenPath:     p.tokenPath,
207		depositAmount: p.depositAmount,
208		recipient:     p.recipient,
209		conditions:    conditions,
210		tiers:         tiers,
211		tiersRatios:   tiersRatios,
212		createdHeight: p.createdHeight,
213		createdAt:     p.createdAt,
214	}
215}
216
217// MakeProjectID generates a unique project ID based on the given token path and the current block height.
218//
219// The generated ID combines the `tokenPath` and the current block height in the following format:
220// "{tokenPath}:{height}"
221//
222// Parameters:
223// - tokenPath (string): The path of the token associated with the project.
224//
225// Returns:
226// - string: A unique project ID in the format "tokenPath:height".
227func MakeProjectID(tokenPath string, createdHeight int64) string {
228	return ufmt.Sprintf("%s:%d", tokenPath, createdHeight)
229}