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