Skip to content

Commit 384329f

Browse files
committed
Use numeric data types for gas prices, confirmation times, and block numbers
1 parent caf06a3 commit 384329f

File tree

3 files changed

+75
-36
lines changed

3 files changed

+75
-36
lines changed

gas_tracker.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@
77

88
package etherscan
99

10+
import "time"
11+
1012
// GasEstiamte gets estiamted confirmation time (in seconds) at the given gas price
11-
func (c *Client) GasEstimate(gasPrice int) (confirmationTimeInSec string, err error) {
13+
func (c *Client) GasEstimate(gasPrice int) (confirmationTimeInSec time.Duration, err error) {
1214
params := M{"gasPrice": gasPrice}
13-
err = c.call("gastracker", "gasestimate", params, &confirmationTimeInSec)
14-
return
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")
1521
}
1622

1723
// GasOracle gets suggested gas prices (in Gwei)

gas_tracker_e2e_test.go

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,17 @@ import (
1313

1414
//GasEstiamte generates dynamic data. Best we can do is ensure all fields are populated
1515
func TestClient_GasEstimate(t *testing.T) {
16-
confirmationTime, err := api.GasEstimate(20000000)
16+
_, err := api.GasEstimate(20000000)
1717
noError(t, err, "api.GasEstimate")
18-
19-
if 0 == len(confirmationTime) {
20-
t.Errorf("confirmationTime empty string")
21-
}
2218
}
2319

2420
//GasOracle generates dynamic data. Best we can do is ensure all fields are populated
2521
func TestClient_GasOracle(t *testing.T) {
2622
gasPrice, err := api.GasOracle()
2723
noError(t, err, "api.GasOrcale")
2824

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-
4925
if 0 == len(gasPrice.GasUsedRatio) {
50-
t.Errorf("gasPrice.GasUsedRatio empty string")
26+
t.Errorf("gasPrice.GasUsedRatio empty")
5127
}
5228

5329
}

response.go

Lines changed: 64 additions & 7 deletions
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 {
@@ -178,10 +183,62 @@ type Log struct {
178183
//GasPrices holds info for Gas Oracle queries
179184
//Gas Prices are returned in Gwei
180185
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"`
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
187244
}

0 commit comments

Comments
 (0)