Skip to content

Commit

Permalink
Merge pull request #5 from hrharder/refactor/remove-unnecessary-pointers
Browse files Browse the repository at this point in the history
gas: remove unnecessary internal usage of pointers
  • Loading branch information
Henry Harder committed Feb 6, 2020
2 parents 8d9b0d9 + 7057cd1 commit a5332f4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
24 changes: 14 additions & 10 deletions gas_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ func NewGasPriceSuggester(maxResultAge time.Duration) (GasPriceSuggester, error)
if err != nil {
return nil, err
}

m := gasPriceManager{
latestResponse: prices,
fetchedAt: time.Now(),
maxResultAge: maxResultAge,
}

return func(priority GasPriority) (*big.Int, error) {
return m.suggestCachedGasPrice(priority)
}, nil
Expand All @@ -79,9 +81,10 @@ func NewGasPriceSuggester(maxResultAge time.Duration) (GasPriceSuggester, error)
type gasPriceManager struct {
sync.Mutex

latestResponse *ethGasStationResponse
fetchedAt time.Time
maxResultAge time.Duration
fetchedAt time.Time
maxResultAge time.Duration

latestResponse ethGasStationResponse
}

func (m *gasPriceManager) suggestCachedGasPrice(priority GasPriority) (*big.Int, error) {
Expand Down Expand Up @@ -113,21 +116,22 @@ type ethGasStationResponse struct {
Average float64 `json:"average"`
}

func loadGasPrices() (*ethGasStationResponse, error) {
func loadGasPrices() (ethGasStationResponse, error) {
var prices ethGasStationResponse

res, err := http.Get(ETHGasStationURL)
if err != nil {
return nil, err
return prices, err
}

var body ethGasStationResponse
if err := json.NewDecoder(res.Body).Decode(&body); err != nil {
return nil, err
if err := json.NewDecoder(res.Body).Decode(&prices); err != nil {
return prices, err
}

return &body, nil
return prices, nil
}

func parseSuggestedGasPrice(priority GasPriority, prices *ethGasStationResponse) (*big.Int, error) {
func parseSuggestedGasPrice(priority GasPriority, prices ethGasStationResponse) (*big.Int, error) {
switch priority {
case GasPriorityFast:
return parseGasPriceToWei(prices.Fast)
Expand Down
2 changes: 1 addition & 1 deletion gas_price_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestLoadGasPrices(t *testing.T) {

func TestGasPriceManager(t *testing.T) {
// create "phony" negative price result so we know the cache is being used
prices := &ethGasStationResponse{
prices := ethGasStationResponse{
Fast: -1.0,
Fastest: -1.0,
SafeLow: -1.0,
Expand Down

0 comments on commit a5332f4

Please sign in to comment.