Skip to content

Commit

Permalink
Add etherscan gas price (#182)
Browse files Browse the repository at this point in the history
* Add etherscan gas price

* Update Changelog
  • Loading branch information
ferranbt authored Apr 7, 2022
1 parent 6181cfb commit c0024dd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

# 0.1.1 (Not released)

- Add `etherscan.GasPrice` function to return last block gas price [[GH-182](https://github.com/umbracle/ethgo/issues/182)]
- Add `4byte` package and cli [[GH-178](https://github.com/umbracle/ethgo/issues/178)]
- Install and use `ethers.js` spec tests for wallet private key decoding [[GH-177](https://github.com/umbracle/ethgo/issues/177)]
- Add `GetLogs` function Etherscan to return logs by filter [[GH-170](https://github.com/umbracle/ethgo/issues/170)]
Expand Down
14 changes: 14 additions & 0 deletions etherscan/etherscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,20 @@ func (e *Etherscan) GetContractCode(addr ethgo.Address) (*ContractCode, error) {
return out[0], nil
}

func (e *Etherscan) GasPrice() (uint64, error) {
var out struct {
LastBlock string `json:"LastBlock"`
}
if err := e.Query("gastracker", "gasoracle", &out, map[string]string{}); err != nil {
return 0, err
}
num, err := strconv.Atoi(out.LastBlock)
if err != nil {
return 0, err
}
return uint64(num), nil
}

func (e *Etherscan) GetLogs(filter *ethgo.LogFilter) ([]*ethgo.Log, error) {
if len(filter.Address) == 0 {
return nil, fmt.Errorf("an address to filter is required")
Expand Down
8 changes: 8 additions & 0 deletions etherscan/etherscan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,11 @@ func TestGetLogs(t *testing.T) {
assert.NoError(t, err)
assert.NotEmpty(t, logs)
}

func TestGasPrice(t *testing.T) {
e := testEtherscanMainnet(t)

gas, err := e.GasPrice()
assert.NoError(t, err)
assert.NotZero(t, gas)
}
8 changes: 8 additions & 0 deletions website/pages/integrations/etherscan.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,11 @@ Input:
Output:

- `logs` <Log text="[]Log"/>: List of logs that match the filter.

## GasPrice

<GoDocLink href="etherscan#Etherscan.GasPrice">GasPrice</GoDocLink> returns the gas price of the latest block.

Output:

- `gasPrice` (`uint64`): Gas price of the latest block.

1 comment on commit c0024dd

@vercel
Copy link

@vercel vercel bot commented on c0024dd Apr 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.