project_tier.gno
6.99 Kb ยท 197 lines
1package launchpad
2
3import (
4 "gno.land/p/nt/ufmt"
5
6 u256 "gno.land/p/gnoswap/uint256"
7)
8
9// ProjectTier represents a tier within a project.
10//
11// This struct contains the necessary data and methods to manage and distribute
12// rewards for a specific tier of a project.
13//
14// Fields:
15// - distributeAmountPerSecondX128 (u256.Uint): The amount of tokens to be distributed per second, represented as a Q128 fixed-point number.
16// - startTime (int64): The time for the start of the tier.
17// - endTime (int64): The time for the end of the tier.
18// - id (string): The unique identifier for the tier, formatted as "{projectID}:duration".
19// - totalDistributeAmount (int64): The total amount of tokens to be distributed for the tier.
20// - totalDepositAmount (int64): The total amount of tokens deposited for the tier.
21// - totalWithdrawAmount (int64): The total amount of tokens withdrawn from the tier.
22// - totalDepositCount (int64): The total number of deposits made to the tier.
23// - totalWithdrawCount (int64): The total number of withdrawals from the tier.
24// - totalCollectedAmount (int64): The total amount of tokens collected as rewards for the tier.
25type ProjectTier struct {
26 distributeAmountPerSecondX128 *u256.Uint // distribute amount per second, Q128
27 id string // '{projectId}:duration' // duartion == 30, 90, 180
28 totalDistributeAmount int64
29 totalDepositAmount int64 // accumulated deposit amount
30 totalWithdrawAmount int64 // accumulated withdraw amount
31 totalDepositCount int64 // accumulated deposit count
32 totalWithdrawCount int64 // accumulated withdraw count
33 totalCollectedAmount int64 // total collected amount by user (reward)
34 startTime int64
35 endTime int64
36}
37
38// DistributeAmountPerSecondX128 returns the distribute amount per second (Q128) of the project tier.
39func (pt *ProjectTier) DistributeAmountPerSecondX128() *u256.Uint {
40 return pt.distributeAmountPerSecondX128
41}
42
43// SetDistributeAmountPerSecondX128 sets the distribute amount per second (Q128) of the project tier.
44func (pt *ProjectTier) SetDistributeAmountPerSecondX128(amount *u256.Uint) {
45 pt.distributeAmountPerSecondX128 = amount
46}
47
48// ID returns the ID of the project tier.
49func (pt *ProjectTier) ID() string {
50 return pt.id
51}
52
53// SetID sets the ID of the project tier.
54func (pt *ProjectTier) SetID(id string) {
55 pt.id = id
56}
57
58// TotalDistributeAmount returns the total distribute amount of the project tier.
59func (pt *ProjectTier) TotalDistributeAmount() int64 {
60 return pt.totalDistributeAmount
61}
62
63// SetTotalDistributeAmount sets the total distribute amount of the project tier.
64func (pt *ProjectTier) SetTotalDistributeAmount(amount int64) {
65 pt.totalDistributeAmount = amount
66}
67
68// TotalDepositAmount returns the total deposit amount of the project tier.
69func (pt *ProjectTier) TotalDepositAmount() int64 {
70 return pt.totalDepositAmount
71}
72
73// SetTotalDepositAmount sets the total deposit amount of the project tier.
74func (pt *ProjectTier) SetTotalDepositAmount(amount int64) {
75 pt.totalDepositAmount = amount
76}
77
78// TotalWithdrawAmount returns the total withdraw amount of the project tier.
79func (pt *ProjectTier) TotalWithdrawAmount() int64 {
80 return pt.totalWithdrawAmount
81}
82
83// SetTotalWithdrawAmount sets the total withdraw amount of the project tier.
84func (pt *ProjectTier) SetTotalWithdrawAmount(amount int64) {
85 pt.totalWithdrawAmount = amount
86}
87
88// TotalDepositCount returns the total deposit count of the project tier.
89func (pt *ProjectTier) TotalDepositCount() int64 {
90 return pt.totalDepositCount
91}
92
93// SetTotalDepositCount sets the total deposit count of the project tier.
94func (pt *ProjectTier) SetTotalDepositCount(count int64) {
95 pt.totalDepositCount = count
96}
97
98// TotalWithdrawCount returns the total withdraw count of the project tier.
99func (pt *ProjectTier) TotalWithdrawCount() int64 {
100 return pt.totalWithdrawCount
101}
102
103// SetTotalWithdrawCount sets the total withdraw count of the project tier.
104func (pt *ProjectTier) SetTotalWithdrawCount(count int64) {
105 pt.totalWithdrawCount = count
106}
107
108// TotalCollectedAmount returns the total collected amount of the project tier.
109func (pt *ProjectTier) TotalCollectedAmount() int64 {
110 return pt.totalCollectedAmount
111}
112
113// SetTotalCollectedAmount sets the total collected amount of the project tier.
114func (pt *ProjectTier) SetTotalCollectedAmount(amount int64) {
115 pt.totalCollectedAmount = amount
116}
117
118// StartTime returns the start time of the project tier.
119func (pt *ProjectTier) StartTime() int64 {
120 return pt.startTime
121}
122
123// SetStartTime sets the start time of the project tier.
124func (pt *ProjectTier) SetStartTime(time int64) {
125 pt.startTime = time
126}
127
128// EndTime returns the end time of the project tier.
129func (pt *ProjectTier) EndTime() int64 {
130 return pt.endTime
131}
132
133// SetEndTime sets the end time of the project tier.
134func (pt *ProjectTier) SetEndTime(time int64) {
135 pt.endTime = time
136}
137
138func (pt *ProjectTier) IsActivated(currentTime int64) bool {
139 return pt.startTime <= currentTime && currentTime < pt.endTime
140}
141
142// IsEnded returns true if the project tier has ended.
143func (pt *ProjectTier) IsEnded(currentTime int64) bool {
144 return pt.endTime < currentTime
145}
146
147func (pt ProjectTier) Clone() *ProjectTier {
148 return &ProjectTier{
149 id: pt.id,
150 totalDistributeAmount: pt.totalDistributeAmount,
151 distributeAmountPerSecondX128: pt.distributeAmountPerSecondX128.Clone(),
152 startTime: pt.startTime,
153 endTime: pt.endTime,
154 totalDepositAmount: pt.totalDepositAmount,
155 totalWithdrawAmount: pt.totalWithdrawAmount,
156 totalDepositCount: pt.totalDepositCount,
157 totalWithdrawCount: pt.totalWithdrawCount,
158 totalCollectedAmount: pt.totalCollectedAmount,
159 }
160}
161
162// NewProjectTier returns a pointer to a new ProjectTier with the given values.
163func NewProjectTier(
164 projectID string,
165 tierDuration int64,
166 totalDistributeAmount int64,
167 startTime int64,
168 endTime int64,
169) *ProjectTier {
170 return &ProjectTier{
171 id: MakeProjectTierID(projectID, tierDuration),
172 totalDistributeAmount: totalDistributeAmount,
173 distributeAmountPerSecondX128: u256.Zero(),
174 startTime: startTime,
175 endTime: endTime,
176 totalDepositAmount: 0,
177 totalWithdrawAmount: 0,
178 totalDepositCount: 0,
179 totalWithdrawCount: 0,
180 totalCollectedAmount: 0,
181 }
182}
183
184// MakeProjectTierID generates a unique tier ID based on the given project ID and the tier duration.
185//
186// The generated ID combines the `projectId` and the `duration` in the following format:
187// "{projectId}:{duration}"
188//
189// Parameters:
190// - projectId (string): The unique ID of the project associated with the tier.
191// - duration (uint64): The duration of the tier (e.g., 30, 90, 180 days).
192//
193// Returns:
194// - string: A unique tier ID in the format "projectId:duration".
195func MakeProjectTierID(projectID string, duration int64) string {
196 return ufmt.Sprintf("%s:%d", projectID, duration)
197}