Skip to content

Commit 115bac1

Browse files
authoredOct 25, 2022
Merge pull request #74 from avislash/feature/gas_tracker
Free Gas Tracker Endpoints
2 parents 108d273 + 384329f commit 115bac1

File tree

3 files changed

+124
-1
lines changed

3 files changed

+124
-1
lines changed
 

‎gas_tracker.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2022 Avi Misra
3+
*
4+
* Use of this work is governed by a MIT License.
5+
* You may find a license copy in project root.
6+
*/
7+
8+
package etherscan
9+
10+
import "time"
11+
12+
// GasEstiamte gets estiamted confirmation time (in seconds) at the given gas price
13+
func (c *Client) GasEstimate(gasPrice int) (confirmationTimeInSec time.Duration, err error) {
14+
params := M{"gasPrice": gasPrice}
15+
var confTime string
16+
err = c.call("gastracker", "gasestimate", params, &confTime)
17+
if err != nil {
18+
return
19+
}
20+
return time.ParseDuration(confTime + "s")
21+
}
22+
23+
// GasOracle gets suggested gas prices (in Gwei)
24+
func (c *Client) GasOracle() (gasPrices GasPrices, err error) {
25+
err = c.call("gastracker", "gasoracle", M{}, &gasPrices)
26+
return
27+
}

‎gas_tracker_e2e_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2018 LI Zhennan
3+
*
4+
* Use of this work is governed by a MIT License.
5+
* You may find a license copy in project root.
6+
*/
7+
8+
package etherscan
9+
10+
import (
11+
"testing"
12+
)
13+
14+
//GasEstiamte generates dynamic data. Best we can do is ensure all fields are populated
15+
func TestClient_GasEstimate(t *testing.T) {
16+
_, err := api.GasEstimate(20000000)
17+
noError(t, err, "api.GasEstimate")
18+
}
19+
20+
//GasOracle generates dynamic data. Best we can do is ensure all fields are populated
21+
func TestClient_GasOracle(t *testing.T) {
22+
gasPrice, err := api.GasOracle()
23+
noError(t, err, "api.GasOrcale")
24+
25+
if 0 == len(gasPrice.GasUsedRatio) {
26+
t.Errorf("gasPrice.GasUsedRatio empty")
27+
}
28+
29+
}

‎response.go

+68-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77

88
package etherscan
99

10-
import "encoding/json"
10+
import (
11+
"encoding/json"
12+
"fmt"
13+
"strconv"
14+
"strings"
15+
)
1116

1217
// Envelope is the carrier of nearly every response
1318
type Envelope struct {
@@ -175,3 +180,65 @@ type Log struct {
175180
Removed bool `json:"removed"`
176181
}
177182

183+
//GasPrices holds info for Gas Oracle queries
184+
//Gas Prices are returned in Gwei
185+
type GasPrices struct {
186+
LastBlock int
187+
SafeGasPrice float64
188+
ProposeGasPrice float64
189+
FastGasPrice float64
190+
SuggestBaseFeeInGwei float64 `json:"suggestBaseFee"`
191+
GasUsedRatio []float64 `json:"gasUsedRatio"`
192+
}
193+
194+
func (gp *GasPrices) UnmarshalJSON(data []byte) error {
195+
_gp := struct {
196+
LastBlock string
197+
SafeGasPrice string
198+
ProposeGasPrice string
199+
FastGasPrice string
200+
SuggestBaseFeeInGwei string `json:"suggestBaseFee"`
201+
GasUsedRatio string `json:"gasUsedRatio"`
202+
}{}
203+
204+
err := json.Unmarshal(data, &_gp)
205+
if err != nil {
206+
return err
207+
}
208+
209+
gp.LastBlock, err = strconv.Atoi(_gp.LastBlock)
210+
if err != nil {
211+
return fmt.Errorf("Unable to convert LastBlock %s to int: %w", _gp.LastBlock, err)
212+
}
213+
214+
gp.SafeGasPrice, err = strconv.ParseFloat(_gp.SafeGasPrice, 64)
215+
if err != nil {
216+
return fmt.Errorf("Unable to convert SafeGasPrice %s to float64: %w", _gp.SafeGasPrice, err)
217+
}
218+
219+
gp.ProposeGasPrice, err = strconv.ParseFloat(_gp.ProposeGasPrice, 64)
220+
if err != nil {
221+
return fmt.Errorf("Unable to convert ProposeGasPrice %s to float64: %w", _gp.ProposeGasPrice, err)
222+
}
223+
224+
gp.FastGasPrice, err = strconv.ParseFloat(_gp.FastGasPrice, 64)
225+
if err != nil {
226+
return fmt.Errorf("Unable to convert FastGasPrice %s to float64: %w", _gp.FastGasPrice, err)
227+
}
228+
229+
gp.SuggestBaseFeeInGwei, err = strconv.ParseFloat(_gp.SuggestBaseFeeInGwei, 64)
230+
if err != nil {
231+
return fmt.Errorf("Unable to convert SuggestBaseFeeInGwei %s to float64: %w", _gp.SuggestBaseFeeInGwei, err)
232+
}
233+
234+
gasRatios := strings.Split(_gp.GasUsedRatio, ",")
235+
gp.GasUsedRatio = make([]float64, len(gasRatios))
236+
for i, gasRatio := range gasRatios {
237+
gp.GasUsedRatio[i], err = strconv.ParseFloat(gasRatio, 64)
238+
if err != nil {
239+
return fmt.Errorf("Unable to convert gasRatio %s to float64: %w", gasRatio, err)
240+
}
241+
}
242+
243+
return nil
244+
}

0 commit comments

Comments
 (0)
Please sign in to comment.