Skip to content
This repository was archived by the owner on Jul 13, 2022. It is now read-only.

Commit 6f55404

Browse files
authored
Use floats for all gas values (#642)
1 parent 67ecc1c commit 6f55404

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

connections/ethereum/connection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (c *Connection) SafeEstimateGas(ctx context.Context) (*big.Int, error) {
138138
if c.egsApiKey != "" {
139139
price, err := egs.FetchGasPrice(c.egsApiKey, c.egsSpeed)
140140
if err != nil {
141-
c.log.Debug("Couldn't fetch gasPrice from GSN", err)
141+
c.log.Error("Couldn't fetch gasPrice from GSN", "err", err)
142142
} else {
143143
suggestedGasPrice = price
144144
}

connections/ethereum/egs/egs.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ const (
2424
)
2525

2626
type gasPriceResponse struct {
27-
Fast int64 `json:"fast"`
28-
Fastest int64 `json:"fastest"`
29-
SafeLow int64 `json:"safeLow"`
30-
Average int64 `json:"average"`
27+
Fast float32 `json:"fast"`
28+
Fastest float32 `json:"fastest"`
29+
SafeLow float32 `json:"safeLow"`
30+
Average float32 `json:"average"`
3131
BlockTime float32 `json:"block_time"`
3232
BlockNum int64 `json:"blockNum"`
3333
Speed float32 `json:"speed"`
@@ -92,14 +92,19 @@ func parsePrice(result *gasPriceResponse, speed string) *big.Int {
9292
var res *big.Int
9393
switch speed {
9494
case Fastest:
95-
res = big.NewInt(result.Fastest)
95+
res = big.NewInt(int64(result.Fastest))
9696
case Fast:
97-
res = big.NewInt(result.Fast)
97+
res = big.NewInt(int64(result.Fast))
9898
case Average:
99-
res = big.NewInt(result.Average)
99+
res = big.NewInt(int64(result.Average))
100100
default:
101-
res = big.NewInt(result.Fast)
101+
res = big.NewInt(int64(result.Fast))
102102
}
103+
// Make sure we get at least 10 gwei to avoid prices of 0
104+
if res.Cmp(big.NewInt(1)) == -1 {
105+
res = big.NewInt(1)
106+
}
107+
103108
base := big.NewInt(8) // we are using 8 here but not 9 bcs ethgas station returns values in Gwei * 10
104109
return res.Mul(res, big.NewInt(0).Exp(big.NewInt(10), base, nil))
105110
}

connections/ethereum/egs/egs_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var exampleResponse = &gasPriceResponse{
2020
}
2121

2222
// Captured by querying endpoint manually
23-
var rawResponse = "{\"fast\":590,\"fastest\":700,\"safeLow\":490,\"average\":500,\"block_time\":15.9,\"blockNum\":12389059,\"speed\":0.995538884040233,\"safeLowWait\":13.6,\"avgWait\":2.1,\"fastWait\":0.9,\"fastestWait\":0.6,\"gasPriceRange\":{\"4\":265,\"6\":265,\"8\":265,\"10\":265,\"20\":265,\"30\":265,\"40\":265,\"50\":265,\"60\":265,\"70\":265,\"80\":265,\"90\":265,\"100\":265,\"110\":265,\"120\":265,\"130\":265,\"140\":265,\"150\":265,\"160\":265,\"170\":265,\"180\":265,\"190\":265,\"200\":265,\"220\":265,\"240\":265,\"260\":265,\"280\":265,\"300\":265,\"320\":265,\"340\":265,\"360\":265,\"380\":265,\"400\":265,\"420\":265,\"440\":265,\"460\":265,\"480\":15,\"490\":13.6,\"500\":2.1,\"520\":1.7,\"540\":1.2,\"560\":1,\"580\":0.9,\"590\":0.9,\"600\":0.8,\"620\":0.7,\"640\":0.7,\"660\":0.7,\"680\":0.7,\"700\":0.6}}"
23+
var rawResponse = "{\"fast\":590,\"fastest\":700,\"safeLow\":490,\"average\":0.5,\"block_time\":15.9,\"blockNum\":12389059,\"speed\":0.995538884040233,\"safeLowWait\":13.6,\"avgWait\":2.1,\"fastWait\":0.9,\"fastestWait\":0.6,\"gasPriceRange\":{\"4\":265,\"6\":265,\"8\":265,\"10\":265,\"20\":265,\"30\":265,\"40\":265,\"50\":265,\"60\":265,\"70\":265,\"80\":265,\"90\":265,\"100\":265,\"110\":265,\"120\":265,\"130\":265,\"140\":265,\"150\":265,\"160\":265,\"170\":265,\"180\":265,\"190\":265,\"200\":265,\"220\":265,\"240\":265,\"260\":265,\"280\":265,\"300\":265,\"320\":265,\"340\":265,\"360\":265,\"380\":265,\"400\":265,\"420\":265,\"440\":265,\"460\":265,\"480\":15,\"490\":13.6,\"500\":2.1,\"520\":1.7,\"540\":1.2,\"560\":1,\"580\":0.9,\"590\":0.9,\"600\":0.8,\"620\":0.7,\"640\":0.7,\"660\":0.7,\"680\":0.7,\"700\":0.6}}"
2424

2525
func TestParsePrice(t *testing.T) {
2626
assert.Equal(t, parsePrice(exampleResponse, Fastest), big.NewInt(200000000))

0 commit comments

Comments
 (0)