-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetherscan.go
77 lines (69 loc) · 2.02 KB
/
etherscan.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package etherscan
import (
"context"
"encoding/json"
"fmt"
"io"
"math/big"
"net/http"
"github.com/0xPolygonHermez/zkevm-node/encoding"
)
type etherscanResponse struct {
Status string `json:"status"`
Message string `json:"message"`
Result gasPriceEtherscan `json:"result"`
}
// gasPriceEtherscan definition
type gasPriceEtherscan struct {
LastBlock string `json:"LastBlock"`
SafeGasPrice string `json:"SafeGasPrice"`
ProposeGasPrice string `json:"ProposeGasPrice"`
FastGasPrice string `json:"FastGasPrice"`
}
// Config structure
type Config struct {
// Need API key to use etherscan, if it's empty etherscan is not used
ApiKey string `mapstructure:"ApiKey"`
// URL of the etherscan API. Overwritten with a hardcoded URL: "https://api.etherscan.io/api?module=gastracker&action=gasoracle&apikey="
Url string
}
// Client for etherscan
type Client struct {
config Config
Http http.Client
}
// NewEtherscanService is the constructor that creates an etherscanService
func NewEtherscanService(apikey string) *Client {
const url = "https://api.etherscan.io/api?module=gastracker&action=gasoracle&apikey="
return &Client{
config: Config{
Url: url,
ApiKey: apikey,
},
Http: http.Client{},
}
}
// SuggestGasPrice retrieves the gas price estimation from etherscan
func (e *Client) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
var resBody etherscanResponse
url := e.config.Url + e.config.ApiKey
res, err := e.Http.Get(url)
if err != nil {
return big.NewInt(0), err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return big.NewInt(0), err
}
if res.StatusCode != http.StatusOK {
return big.NewInt(0), fmt.Errorf("http response is %d", res.StatusCode)
}
// Unmarshal result
err = json.Unmarshal(body, &resBody)
if err != nil {
return big.NewInt(0), fmt.Errorf("Reading body failed: %w", err)
}
fgp, _ := big.NewInt(0).SetString(resBody.Result.FastGasPrice, encoding.Base10)
return new(big.Int).Mul(fgp, big.NewInt(encoding.Gwei)), nil
}