Skip to content

Commit caf06a3

Browse files
committed
Implemented Free Gas Tracker Endpoints
1 parent 108d273 commit caf06a3

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

gas_tracker.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
// GasEstiamte gets estiamted confirmation time (in seconds) at the given gas price
11+
func (c *Client) GasEstimate(gasPrice int) (confirmationTimeInSec string, err error) {
12+
params := M{"gasPrice": gasPrice}
13+
err = c.call("gastracker", "gasestimate", params, &confirmationTimeInSec)
14+
return
15+
}
16+
17+
// GasOracle gets suggested gas prices (in Gwei)
18+
func (c *Client) GasOracle() (gasPrices GasPrices, err error) {
19+
err = c.call("gastracker", "gasoracle", M{}, &gasPrices)
20+
return
21+
}

gas_tracker_e2e_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
confirmationTime, err := api.GasEstimate(20000000)
17+
noError(t, err, "api.GasEstimate")
18+
19+
if 0 == len(confirmationTime) {
20+
t.Errorf("confirmationTime empty string")
21+
}
22+
}
23+
24+
//GasOracle generates dynamic data. Best we can do is ensure all fields are populated
25+
func TestClient_GasOracle(t *testing.T) {
26+
gasPrice, err := api.GasOracle()
27+
noError(t, err, "api.GasOrcale")
28+
29+
if 0 == len(gasPrice.LastBlock) {
30+
t.Errorf("gasPrice.LastBlock empty string")
31+
}
32+
33+
if 0 == len(gasPrice.SafeGasPrice) {
34+
t.Errorf("gasPrice.SafeGasPrice empty string")
35+
}
36+
37+
if 0 == len(gasPrice.ProposeGasPrice) {
38+
t.Errorf("gasPrice.ProposeGasPrice empty string")
39+
}
40+
41+
if 0 == len(gasPrice.FastGasPrice) {
42+
t.Errorf("gasPrice.FastGasPrice empty string")
43+
}
44+
45+
if 0 == len(gasPrice.SuggestBaseFeeInGwei) {
46+
t.Errorf("gasPrice.SuggestBaseFeeInGwei empty string")
47+
}
48+
49+
if 0 == len(gasPrice.GasUsedRatio) {
50+
t.Errorf("gasPrice.GasUsedRatio empty string")
51+
}
52+
53+
}

response.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,13 @@ type Log struct {
175175
Removed bool `json:"removed"`
176176
}
177177

178+
//GasPrices holds info for Gas Oracle queries
179+
//Gas Prices are returned in Gwei
180+
type GasPrices struct {
181+
LastBlock string
182+
SafeGasPrice string
183+
ProposeGasPrice string
184+
FastGasPrice string
185+
SuggestBaseFeeInGwei string `json:"suggestBaseFee"`
186+
GasUsedRatio string `json:"gasUsedRatio"`
187+
}

0 commit comments

Comments
 (0)