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

Commit

Permalink
Use floats for all gas values (#642)
Browse files Browse the repository at this point in the history
  • Loading branch information
ansermino committed May 17, 2021
1 parent 67ecc1c commit 6f55404
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion connections/ethereum/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (c *Connection) SafeEstimateGas(ctx context.Context) (*big.Int, error) {
if c.egsApiKey != "" {
price, err := egs.FetchGasPrice(c.egsApiKey, c.egsSpeed)
if err != nil {
c.log.Debug("Couldn't fetch gasPrice from GSN", err)
c.log.Error("Couldn't fetch gasPrice from GSN", "err", err)
} else {
suggestedGasPrice = price
}
Expand Down
21 changes: 13 additions & 8 deletions connections/ethereum/egs/egs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ const (
)

type gasPriceResponse struct {
Fast int64 `json:"fast"`
Fastest int64 `json:"fastest"`
SafeLow int64 `json:"safeLow"`
Average int64 `json:"average"`
Fast float32 `json:"fast"`
Fastest float32 `json:"fastest"`
SafeLow float32 `json:"safeLow"`
Average float32 `json:"average"`
BlockTime float32 `json:"block_time"`
BlockNum int64 `json:"blockNum"`
Speed float32 `json:"speed"`
Expand Down Expand Up @@ -92,14 +92,19 @@ func parsePrice(result *gasPriceResponse, speed string) *big.Int {
var res *big.Int
switch speed {
case Fastest:
res = big.NewInt(result.Fastest)
res = big.NewInt(int64(result.Fastest))
case Fast:
res = big.NewInt(result.Fast)
res = big.NewInt(int64(result.Fast))
case Average:
res = big.NewInt(result.Average)
res = big.NewInt(int64(result.Average))
default:
res = big.NewInt(result.Fast)
res = big.NewInt(int64(result.Fast))
}
// Make sure we get at least 10 gwei to avoid prices of 0
if res.Cmp(big.NewInt(1)) == -1 {
res = big.NewInt(1)
}

base := big.NewInt(8) // we are using 8 here but not 9 bcs ethgas station returns values in Gwei * 10
return res.Mul(res, big.NewInt(0).Exp(big.NewInt(10), base, nil))
}
2 changes: 1 addition & 1 deletion connections/ethereum/egs/egs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var exampleResponse = &gasPriceResponse{
}

// Captured by querying endpoint manually
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}}"
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}}"

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

0 comments on commit 6f55404

Please sign in to comment.