@@ -115,10 +115,10 @@ func (api *API) GetHashrate() uint64 {
115115 return uint64 (api .xhash .Hashrate ())
116116}
117117
118- func (api * API ) GetCirculatingSupply () * big. Int {
118+ func (api * API ) GetTotalSupply () string {
119119 header := api .chain .CurrentHeader ()
120120 if header == nil {
121- return big . NewInt ( 0 )
121+ return "0"
122122 }
123123
124124 // Number of blocks including genesis
@@ -155,5 +155,65 @@ func (api *API) GetCirculatingSupply() *big.Int {
155155 emissions .Add (emissions , tmp )
156156 }
157157
158- return emissions
158+ return emissions .String ()
159+ }
160+
161+ func (api * API ) GetCirculatingSupply () string {
162+ header := api .chain .CurrentHeader ()
163+ if header == nil {
164+ return "0"
165+ }
166+
167+ height := header .Number .Uint64 ()
168+
169+ const (
170+ halvingInterval uint64 = 210_000
171+ coinbaseMaturity uint64 = 100
172+ )
173+
174+ // No matured rewards yet
175+ if height <= coinbaseMaturity {
176+ return "0"
177+ }
178+
179+ // Highest block whose coinbase is spendable
180+ maturedHeight := height - coinbaseMaturity
181+
182+ // Number of rewarded & matured blocks (1..maturedHeight)
183+ n := maturedHeight
184+
185+ emissions := new (big.Int )
186+ tmp := new (big.Int )
187+
188+ fullEras := n / halvingInterval
189+ remainder := n % halvingInterval
190+
191+ // Full eras
192+ for era := range fullEras {
193+ // pick a representative block in this era
194+ sampleBlock := era * halvingInterval
195+ if sampleBlock == 0 {
196+ sampleBlock = 1 // avoid genesis if it has no reward
197+ }
198+ reward := calcBlockReward (sampleBlock )
199+
200+ tmp .SetUint64 (halvingInterval )
201+ tmp .Mul (tmp , reward )
202+ emissions .Add (emissions , tmp )
203+ }
204+
205+ // Partial current era
206+ if remainder > 0 {
207+ sampleBlock := fullEras * halvingInterval
208+ if sampleBlock == 0 {
209+ sampleBlock = 1
210+ }
211+ reward := calcBlockReward (sampleBlock )
212+
213+ tmp .SetUint64 (remainder )
214+ tmp .Mul (tmp , reward )
215+ emissions .Add (emissions , tmp )
216+ }
217+
218+ return emissions .String ()
159219}
0 commit comments