getter.gno
7.02 Kb · 204 lines
1package gns
2
3import (
4 "time"
5
6 gnsmath "gno.land/p/gnoswap/gnsmath"
7)
8
9// GetMaxEmissionAmount returns the maximum amount of emission allowed.
10func GetMaxEmissionAmount() int64 {
11 return MAX_EMISSION_AMOUNT
12}
13
14// GetMaximumSupply returns the maximum supply of gns tokens.
15func GetMaximumSupply() int64 {
16 return MAXIMUM_SUPPLY
17}
18
19// GetInitialMintAmount returns the initial amount of gns tokens to be minted.
20func GetInitialMintAmount() int64 {
21 return INITIAL_MINT_AMOUNT
22}
23
24// IsEmissionInitialized returns true if emission schedule has been initialized.
25func IsEmissionInitialized() bool {
26 return getEmissionState().isInitialized()
27}
28
29// IsEmissionActive returns true if emission is currently active based on current time.
30func IsEmissionActive() bool {
31 return getEmissionState().isActive(time.Now().Unix())
32}
33
34// IsEmissionEnded returns true if emission schedule has completed.
35func IsEmissionEnded() bool {
36 return getEmissionState().isEnded(time.Now().Unix())
37}
38
39// GetHalvingYear returns the halving year (1-12) for a given timestamp.
40func GetHalvingYear(timestamp int64) int64 {
41 return getEmissionState().getCurrentYear(timestamp)
42}
43
44// GetCurrentYear returns the current halving year (1-12) or 0 if emission is not active.
45func GetCurrentYear() int64 {
46 return getEmissionState().getCurrentYear(time.Now().Unix())
47}
48
49// GetEmissionAmountPerSecondInRange returns halving timestamps and emission rates for the given time range.
50// Returns two slices: timestamps when halving periods start and corresponding emission rates per second.
51func GetEmissionAmountPerSecondInRange(fromTime, toTime int64) ([]int64, []int64) {
52 if fromTime > toTime {
53 return nil, nil
54 }
55
56 halvingData := getEmissionState().getHalvingData()
57 halvingTimes := make([]int64, 0, HALVING_END_YEAR+1)
58 halvingEmissions := make([]int64, 0, HALVING_END_YEAR+1)
59
60 for year := HALVING_START_YEAR; year <= HALVING_END_YEAR; year++ {
61 startTimestamp := halvingData.getStartTimestamp(year)
62 if startTimestamp < fromTime {
63 continue
64 }
65
66 if toTime < startTimestamp {
67 break
68 }
69
70 halvingTimes = append(halvingTimes, startTimestamp)
71 halvingEmissions = append(halvingEmissions, halvingData.getAmountPerSecond(year))
72 }
73
74 emissionEndTimestamp := halvingData.getEndTimestamp(HALVING_END_YEAR)
75 if fromTime <= emissionEndTimestamp && emissionEndTimestamp < toTime {
76 halvingTimes = append(halvingTimes, gnsmath.SafeAddInt64(emissionEndTimestamp, 1))
77 halvingEmissions = append(halvingEmissions, 0)
78 }
79
80 return halvingTimes, halvingEmissions
81}
82
83// GetEmissionAmountPerSecondByTimestamp returns the emission rate per second for a given timestamp.
84// Returns 0 if timestamp is outside emission period.
85func GetEmissionAmountPerSecondByTimestamp(timestamp int64) int64 {
86 state := getEmissionState()
87 year := state.getCurrentYear(timestamp)
88 return state.getHalvingYearAmountPerSecond(year)
89}
90
91// GetEmissionLeftAmountByTimestamp returns the remaining emission amount for the halving year at given timestamp.
92// Returns 0 if timestamp is outside emission period.
93func GetEmissionLeftAmountByTimestamp(timestamp int64) int64 {
94 state := getEmissionState()
95 year := state.getCurrentYear(timestamp)
96 return state.getHalvingYearLeftAmount(year)
97}
98
99// GetEmissionAccumulatedAmountByTimestamp returns the accumulated emission amount for the halving year at given timestamp.
100// Returns 0 if timestamp is outside emission period.
101func GetEmissionAccumulatedAmountByTimestamp(timestamp int64) int64 {
102 state := getEmissionState()
103 year := state.getCurrentYear(timestamp)
104 return state.getHalvingYearAccumulatedAmount(year)
105}
106
107// GetHalvingYearStartTimestamp returns the start timestamp for the specified halving year.
108func GetHalvingYearStartTimestamp(year int64) int64 {
109 halvingData := getEmissionState().getHalvingData()
110 return halvingData.getStartTimestamp(year)
111}
112
113// GetHalvingYearEndTimestamp returns the end timestamp for the specified halving year.
114func GetHalvingYearEndTimestamp(year int64) int64 {
115 halvingData := getEmissionState().getHalvingData()
116 return halvingData.getEndTimestamp(year)
117}
118
119// GetHalvingYearMaxAmount returns the maximum token issuance for the specified halving year.
120func GetHalvingYearMaxAmount(year int64) int64 {
121 halvingData := getEmissionState().getHalvingData()
122 return halvingData.getMaxAmount(year)
123}
124
125// GetHalvingYearMintAmount returns the amount of tokens minted for the specified halving year.
126func GetHalvingYearMintAmount(year int64) int64 {
127 halvingData := getEmissionState().getHalvingData()
128 return halvingData.getMintedAmount(year)
129}
130
131// GetHalvingYearLeftAmount returns the remaining token issuance for the specified halving year.
132func GetHalvingYearLeftAmount(year int64) int64 {
133 halvingData := getEmissionState().getHalvingData()
134 return halvingData.getLeftAmount(year)
135}
136
137// GetHalvingYearAccuAmount returns the accumulated token issuance for the specified halving year.
138func GetHalvingYearAccuAmount(year int64) int64 {
139 halvingData := getEmissionState().getHalvingData()
140 return halvingData.getAccumAmount(year)
141}
142
143// GetAmountPerSecondPerHalvingYear returns the emission rate per second for the specified halving year.
144func GetAmountPerSecondPerHalvingYear(year int64) int64 {
145 halvingData := getEmissionState().getHalvingData()
146 return halvingData.getAmountPerSecond(year)
147}
148
149// GetHalvingAmountsPerYear returns the total emission amount allocated for the specified year.
150// Returns 0 if year is outside the valid range (1-12).
151func GetHalvingAmountsPerYear(year int64) int64 {
152 if validYear(year) != nil {
153 return 0
154 }
155 return halvingAmountsPerYear[year-1]
156}
157
158// GetEmissionCreatedHeight returns the block height when emission schedule was created.
159func GetEmissionCreatedHeight() int64 {
160 return getEmissionState().getCreatedHeight()
161}
162
163// GetEmissionStartTimestamp returns the timestamp when emission schedule begins.
164func GetEmissionStartTimestamp() int64 {
165 return getEmissionState().getStartTimestamp()
166}
167
168// GetEmissionEndTimestamp returns the timestamp when emission schedule ends.
169func GetEmissionEndTimestamp() int64 {
170 return getEmissionState().getEndTimestamp()
171}
172
173// GetHalvingYearInfo returns the halving year, start timestamp, and end timestamp for a given timestamp.
174// Returns (year, startTimestamp, endTimestamp). Year is 0 if outside emission period.
175func GetHalvingYearInfo(timestamp int64) (int64, int64, int64) {
176 state := getEmissionState()
177 year := state.getCurrentYear(timestamp)
178
179 // If outside emission period, return 0 values
180 if year == 0 {
181 return 0, 0, 0
182 }
183
184 // Use cached timestamps from HalvingData
185 halvingData := state.getHalvingData()
186 return year, halvingData.getStartTimestamp(year), halvingData.getEndTimestamp(year)
187}
188
189// GetHalvingInfo returns a clone of the halving data.
190func GetHalvingInfo() *HalvingData {
191 return getEmissionState().getHalvingData().Clone()
192}
193
194// CalculateMintGnsAmount returns the amount of GNS that would be minted for the given timestamp range.
195func CalculateMintGnsAmount(fromTimestamp, toTimestamp int64) int64 {
196 state := getEmissionState().Clone()
197
198 amountToMint, err := calculateAmountToMint(state, fromTimestamp, toTimestamp)
199 if err != nil {
200 return 0
201 }
202
203 return amountToMint
204}