halving.gno
6.58 Kb · 243 lines
1package gns
2
3import (
4 gnsmath "gno.land/p/gnoswap/gnsmath"
5)
6
7// HalvingData stores emission data for each halving period.
8// Contains timestamps, amounts, and rates for the 12-year emission schedule.
9type HalvingData struct {
10 startTimestamps []int64
11 endTimestamps []int64
12 maxAmount []int64
13 mintedAmount []int64
14 leftAmount []int64
15 accumAmount []int64
16 amountPerSecond []int64
17}
18
19// getStartTimestamp returns the start timestamp for the specified halving year.
20// Returns 0 if year is invalid.
21func (h *HalvingData) getStartTimestamp(year int64) int64 {
22 if validYear(year) != nil {
23 return 0
24 }
25
26 return h.startTimestamps[year-1]
27}
28
29// getEndTimestamp returns the end timestamp for the specified halving year.
30// Returns 0 if year is invalid.
31func (h *HalvingData) getEndTimestamp(year int64) int64 {
32 if validYear(year) != nil {
33 return 0
34 }
35
36 return h.endTimestamps[year-1]
37}
38
39// getMaxAmount returns the maximum emission amount for the specified halving year.
40// Returns 0 if year is invalid.
41func (h *HalvingData) getMaxAmount(year int64) int64 {
42 if validYear(year) != nil {
43 return 0
44 }
45
46 return h.maxAmount[year-1]
47}
48
49// getMintedAmount returns the amount already minted for the specified halving year.
50func (h *HalvingData) getMintedAmount(year int64) int64 {
51 if validYear(year) != nil {
52 return 0
53 }
54 return h.mintedAmount[year-1]
55}
56
57// getAccumAmount returns the accumulated emission amount for the specified halving year.
58func (h *HalvingData) getAccumAmount(year int64) int64 {
59 if validYear(year) != nil {
60 return 0
61 }
62 return h.accumAmount[year-1]
63}
64
65// getLeftAmount returns the remaining emission amount for the specified halving year.
66func (h *HalvingData) getLeftAmount(year int64) int64 {
67 if validYear(year) != nil {
68 return 0
69 }
70 return h.leftAmount[year-1]
71}
72
73// getAmountPerSecond returns the emission rate per second for the specified halving year.
74// Returns 0 if year is invalid.
75func (h *HalvingData) getAmountPerSecond(year int64) int64 {
76 if validYear(year) != nil {
77 return 0
78 }
79 return h.amountPerSecond[year-1]
80}
81
82// setStartTimestamp sets the start timestamp for the specified halving year.
83// Returns error if year is invalid.
84func (h *HalvingData) setStartTimestamp(year int64, timestamp int64) error {
85 err := validYear(year)
86 if err != nil {
87 return err
88 }
89
90 h.startTimestamps[year-1] = timestamp
91
92 return nil
93}
94
95// setEndTimestamp sets the end timestamp for the specified halving year.
96// Returns error if year is invalid.
97func (h *HalvingData) setEndTimestamp(year int64, timestamp int64) error {
98 err := validYear(year)
99 if err != nil {
100 return err
101 }
102
103 h.endTimestamps[year-1] = timestamp
104
105 return nil
106}
107
108// setMaxAmount sets the maximum emission amount for the specified halving year.
109// Returns error if year is invalid.
110func (h *HalvingData) setMaxAmount(year, amount int64) error {
111 err := validYear(year)
112 if err != nil {
113 return err
114 }
115
116 h.maxAmount[year-1] = amount
117
118 return nil
119}
120
121// setMintedAmount sets the minted amount for the specified halving year.
122// Returns error if year is invalid.
123func (h *HalvingData) setMintedAmount(year, amount int64) error {
124 err := validYear(year)
125 if err != nil {
126 return err
127 }
128
129 h.mintedAmount[year-1] = amount
130
131 return nil
132}
133
134// setAccumAmount sets the accumulated amount for the specified halving year.
135// Returns error if year is invalid.
136func (h *HalvingData) setAccumAmount(year, amount int64) error {
137 err := validYear(year)
138 if err != nil {
139 return err
140 }
141
142 h.accumAmount[year-1] = amount
143
144 return nil
145}
146
147// setLeftAmount sets the remaining amount for the specified halving year.
148// Returns error if year is invalid.
149func (h *HalvingData) setLeftAmount(year, amount int64) error {
150 err := validYear(year)
151 if err != nil {
152 return err
153 }
154
155 h.leftAmount[year-1] = amount
156
157 return nil
158}
159
160// addAccumAmount adds to the accumulated amount for the specified halving year.
161// Returns error if year is invalid.
162func (h *HalvingData) addAccumAmount(year, amount int64) error {
163 err := validYear(year)
164 if err != nil {
165 return err
166 }
167
168 h.accumAmount[year-1] = gnsmath.SafeAddInt64(h.accumAmount[year-1], amount)
169
170 return nil
171}
172
173// setAmountPerSecond sets the emission rate per second for the specified halving year.
174// Returns error if year is invalid.
175func (h *HalvingData) setAmountPerSecond(year, amount int64) error {
176 err := validYear(year)
177 if err != nil {
178 return err
179 }
180
181 h.amountPerSecond[year-1] = amount
182
183 return nil
184}
185
186func (h *HalvingData) Clone() *HalvingData {
187 startTimestamps := make([]int64, len(h.startTimestamps))
188 endTimestamps := make([]int64, len(h.endTimestamps))
189 maxAmount := make([]int64, len(h.maxAmount))
190 mintedAmount := make([]int64, len(h.mintedAmount))
191 leftAmount := make([]int64, len(h.leftAmount))
192 accumAmount := make([]int64, len(h.accumAmount))
193 amountPerSecond := make([]int64, len(h.amountPerSecond))
194
195 copy(startTimestamps, h.startTimestamps)
196 copy(endTimestamps, h.endTimestamps)
197 copy(maxAmount, h.maxAmount)
198 copy(mintedAmount, h.mintedAmount)
199 copy(leftAmount, h.leftAmount)
200 copy(accumAmount, h.accumAmount)
201 copy(amountPerSecond, h.amountPerSecond)
202
203 return &HalvingData{
204 startTimestamps: startTimestamps,
205 endTimestamps: endTimestamps,
206 maxAmount: maxAmount,
207 mintedAmount: mintedAmount,
208 leftAmount: leftAmount,
209 accumAmount: accumAmount,
210 amountPerSecond: amountPerSecond,
211 }
212}
213
214// NewHalvingData creates a new HalvingData instance with emission schedule.
215// Initializes 12 years of halving periods with timestamps, amounts, and rates based on startTimestamp.
216func NewHalvingData(startTimestamp int64) *HalvingData {
217 halvingData := &HalvingData{
218 startTimestamps: make([]int64, HALVING_END_YEAR),
219 endTimestamps: make([]int64, HALVING_END_YEAR),
220 maxAmount: make([]int64, HALVING_END_YEAR),
221 mintedAmount: make([]int64, HALVING_END_YEAR),
222 leftAmount: make([]int64, HALVING_END_YEAR),
223 accumAmount: make([]int64, HALVING_END_YEAR),
224 amountPerSecond: make([]int64, HALVING_END_YEAR),
225 }
226
227 for year := HALVING_START_YEAR; year <= HALVING_END_YEAR; year++ {
228 yearStartTimestamp := startTimestamp + (SECONDS_IN_YEAR * (year - 1))
229 yearEndTimestamp := yearStartTimestamp + SECONDS_IN_YEAR - 1
230 yearDistributionAmount := GetHalvingAmountsPerYear(year)
231 yearAmountPerSecond := yearDistributionAmount / SECONDS_IN_YEAR
232
233 halvingData.setStartTimestamp(year, yearStartTimestamp)
234 halvingData.setEndTimestamp(year, yearEndTimestamp)
235 halvingData.setMaxAmount(year, yearDistributionAmount)
236 halvingData.setMintedAmount(year, 0)
237 halvingData.setAccumAmount(year, 0)
238 halvingData.setAmountPerSecond(year, yearAmountPerSecond)
239 halvingData.setLeftAmount(year, yearDistributionAmount)
240 }
241
242 return halvingData
243}