|
7 | 7 |
|
8 | 8 | package etherscan
|
9 | 9 |
|
10 |
| -import "encoding/json" |
| 10 | +import ( |
| 11 | + "encoding/json" |
| 12 | + "fmt" |
| 13 | + "strconv" |
| 14 | + "strings" |
| 15 | +) |
11 | 16 |
|
12 | 17 | // Envelope is the carrier of nearly every response
|
13 | 18 | type Envelope struct {
|
@@ -175,3 +180,65 @@ type Log struct {
|
175 | 180 | Removed bool `json:"removed"`
|
176 | 181 | }
|
177 | 182 |
|
| 183 | +//GasPrices holds info for Gas Oracle queries |
| 184 | +//Gas Prices are returned in Gwei |
| 185 | +type GasPrices struct { |
| 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 |
| 244 | +} |
0 commit comments