Skip to content

Commit

Permalink
support etherscan v2 (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
ductm54 authored Jan 13, 2025
1 parent 0cdcbfe commit c265880
Show file tree
Hide file tree
Showing 19 changed files with 62 additions and 78 deletions.
10 changes: 4 additions & 6 deletions networks/arbitrum.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ type arbitrumMainnet struct {
}

func NewArbitrumMainnet() *arbitrumMainnet {
result := &arbitrumMainnet{NewEtherscanLikeExplorer(
"https://api.arbiscan.io",
"IE1GI7ZUG4HTRUI1AHX51IW2KR41D96EWN",
)}
result := &arbitrumMainnet{NewEtherscanV2()}
result.ChainID = result.GetChainID()
apiKey := strings.Trim(os.Getenv(result.GetBlockExplorerAPIKeyVariableName()), " ")
if apiKey != "" {
result.EtherscanLikeExplorer.APIKey = apiKey
Expand Down Expand Up @@ -56,14 +54,14 @@ func (self *arbitrumMainnet) GetNodeVariableName() string {

func (self *arbitrumMainnet) GetDefaultNodes() map[string]string {
return map[string]string{
"infura": "https://arb1.arbitrum.io/rpc",
"infura": "https://arb1.arbitrum.io/rpc",
// "alchemy-arbitrum": "https://arb-mainnet.g.alchemy.com/v2/PGAWvp9KLZbqjvap-iingGj-Id7HM_Yn",
// "arbitrum.io": "https://arb1.arbitrum.io/rpc",
}
}

func (self *arbitrumMainnet) GetBlockExplorerAPIKeyVariableName() string {
return "ARBISCAN_API_KEY"
return "ETHERSCAN_API_KEY"
}

func (self *arbitrumMainnet) GetBlockExplorerAPIURL() string {
Expand Down
8 changes: 3 additions & 5 deletions networks/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ type baseMainnet struct {
}

func NewBaseMainnet() *baseMainnet {
result := &baseMainnet{NewEtherscanLikeExplorer(
"https://api.basescan.org",
"KGU9B686UEDCRMMMMEDA3Q4M4EM9GMTVKA",
)}
result := &baseMainnet{NewEtherscanV2()}
result.ChainID = result.GetChainID()
apiKey := strings.Trim(os.Getenv(result.GetBlockExplorerAPIKeyVariableName()), " ")
if apiKey != "" {
result.EtherscanLikeExplorer.APIKey = apiKey
Expand Down Expand Up @@ -61,7 +59,7 @@ func (self *baseMainnet) GetDefaultNodes() map[string]string {
}

func (self *baseMainnet) GetBlockExplorerAPIKeyVariableName() string {
return "BASESCAN_API_KEY"
return "ETHERSCAN_API_KEY"
}

func (self *baseMainnet) GetBlockExplorerAPIURL() string {
Expand Down
3 changes: 2 additions & 1 deletion networks/bsc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type bscMainnet struct {

func NewBSCMainnet() *bscMainnet {
result := &bscMainnet{NewBscscan()}
result.ChainID = result.GetChainID()
apiKey := strings.Trim(os.Getenv(result.GetBlockExplorerAPIKeyVariableName()), " ")
if apiKey != "" {
result.EtherscanLikeExplorer.APIKey = apiKey
Expand Down Expand Up @@ -60,7 +61,7 @@ func (self *bscMainnet) GetDefaultNodes() map[string]string {
}

func (self *bscMainnet) GetBlockExplorerAPIKeyVariableName() string {
return "BSCSCAN_API_KEY"
return "ETHERSCAN_API_KEY"
}

func (self *bscMainnet) GetBlockExplorerAPIURL() string {
Expand Down
3 changes: 2 additions & 1 deletion networks/bsc_testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type bscTestnet struct {

func NewBSCTestnet() *bscTestnet {
result := &bscTestnet{NewTestnetBscscan()}
result.ChainID = result.GetChainID()
apiKey := strings.Trim(os.Getenv(result.GetBlockExplorerAPIKeyVariableName()), " ")
if apiKey != "" {
result.EtherscanLikeExplorer.APIKey = apiKey
Expand Down Expand Up @@ -63,7 +64,7 @@ func (self *bscTestnet) GetDefaultNodes() map[string]string {
}

func (self *bscTestnet) GetBlockExplorerAPIKeyVariableName() string {
return "BSCSCAN_API_KEY"
return "ETHERSCAN_API_KEY"
}

func (self *bscTestnet) GetBlockExplorerAPIURL() string {
Expand Down
8 changes: 3 additions & 5 deletions networks/bttc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ type bttcMainnet struct {
}

func NewBttcMainnet() *bttcMainnet {
result := &bttcMainnet{NewEtherscanLikeExplorer(
"https://api.bttcscan.com",
"W56TCSJ96BRMTU7ZZ4HNEETMIDM8CHCJYR",
)}
result := &bttcMainnet{NewEtherscanV2()}
result.ChainID = result.GetChainID()
apiKey := strings.Trim(os.Getenv(result.GetBlockExplorerAPIKeyVariableName()), " ")
if apiKey != "" {
result.EtherscanLikeExplorer.APIKey = apiKey
Expand Down Expand Up @@ -61,7 +59,7 @@ func (self *bttcMainnet) GetDefaultNodes() map[string]string {
}

func (self *bttcMainnet) GetBlockExplorerAPIKeyVariableName() string {
return "BTTCSCAN_API_KEY"
return "ETHERSCAN_API_KEY"
}

func (self *bttcMainnet) GetBlockExplorerAPIURL() string {
Expand Down
1 change: 1 addition & 0 deletions networks/ethereum_mainnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type ethereumMainnet struct {

func NewEthereumMainnet() *ethereumMainnet {
result := &ethereumMainnet{NewMainnetEtherscan()}
result.ChainID = result.GetChainID()
apiKey := strings.Trim(os.Getenv(result.GetBlockExplorerAPIKeyVariableName()), " ")
if apiKey != "" {
result.EtherscanLikeExplorer.APIKey = apiKey
Expand Down
3 changes: 2 additions & 1 deletion networks/ethereum_pow.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type ethereumPOW struct {

func NewEthereumPOW() *ethereumPOW {
result := &ethereumPOW{NewMainnetEtherscan()}
result.ChainID = result.GetChainID()
apiKey := strings.Trim(os.Getenv(result.GetBlockExplorerAPIKeyVariableName()), " ")
if apiKey != "" {
result.EtherscanLikeExplorer.APIKey = apiKey
Expand Down Expand Up @@ -58,7 +59,7 @@ func (self *ethereumPOW) GetDefaultNodes() map[string]string {
}

func (self *ethereumPOW) GetBlockExplorerAPIKeyVariableName() string {
return "ETHERPOWSCAN_API_KEY"
return "ETHERSCAN_API_KEY"
}

func (self *ethereumPOW) GetBlockExplorerAPIURL() string {
Expand Down
3 changes: 2 additions & 1 deletion networks/fantom.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type fantom struct {

func NewFantom() *fantom {
result := &fantom{NewFtmscan()}
result.ChainID = result.GetChainID()
apiKey := strings.Trim(os.Getenv(result.GetBlockExplorerAPIKeyVariableName()), " ")
if apiKey != "" {
result.EtherscanLikeExplorer.APIKey = apiKey
Expand Down Expand Up @@ -58,7 +59,7 @@ func (self *fantom) GetDefaultNodes() map[string]string {
}

func (self *fantom) GetBlockExplorerAPIKeyVariableName() string {
return "FTMSCAN_API_KEY"
return "ETHERSCAN_API_KEY"
}

func (self *fantom) GetBlockExplorerAPIURL() string {
Expand Down
1 change: 1 addition & 0 deletions networks/kovan.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type kovan struct {

func NewKovan() *kovan {
result := &kovan{NewKovanEtherscan()}
result.ChainID = result.GetChainID()
apiKey := strings.Trim(os.Getenv(result.GetBlockExplorerAPIKeyVariableName()), " ")
if apiKey != "" {
result.EtherscanLikeExplorer.APIKey = apiKey
Expand Down
8 changes: 3 additions & 5 deletions networks/linea.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ type lineaMainnet struct {
}

func NewlineaMainnet() *lineaMainnet {
result := &lineaMainnet{NewEtherscanLikeExplorer(
"https://api.lineascan.build",
"97MYPVEHAHR7YFRBHB27YQZSQFM6RJH54C",
)}
result := &lineaMainnet{NewEtherscanV2()}
result.ChainID = result.GetChainID()
apiKey := strings.Trim(os.Getenv(result.GetBlockExplorerAPIKeyVariableName()), " ")
if apiKey != "" {
result.EtherscanLikeExplorer.APIKey = apiKey
Expand Down Expand Up @@ -61,7 +59,7 @@ func (self *lineaMainnet) GetDefaultNodes() map[string]string {
}

func (self *lineaMainnet) GetBlockExplorerAPIKeyVariableName() string {
return "LINEASCAN_API_KEY"
return "ETHERSCAN_API_KEY"
}

func (self *lineaMainnet) GetBlockExplorerAPIURL() string {
Expand Down
5 changes: 3 additions & 2 deletions networks/matic.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type matic struct {
func NewMatic() *matic {
result := &matic{NewPolygonscan()}
apiKey := strings.Trim(os.Getenv(result.GetBlockExplorerAPIKeyVariableName()), " ")
result.ChainID = result.GetChainID()
if apiKey != "" {
result.EtherscanLikeExplorer.APIKey = apiKey
}
Expand Down Expand Up @@ -53,12 +54,12 @@ func (self *matic) GetNodeVariableName() string {

func (self *matic) GetDefaultNodes() map[string]string {
return map[string]string{
"kyber": "https://polygon.kyberengineering.io",
"kyber": "https://polygon.kyberengineering.io",
}
}

func (self *matic) GetBlockExplorerAPIKeyVariableName() string {
return "POLYGONSCAN_API_KEY"
return "ETHERSCAN_API_KEY"
}

func (self *matic) GetBlockExplorerAPIURL() string {
Expand Down
2 changes: 1 addition & 1 deletion networks/mumbai.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (self *mumbai) GetDefaultNodes() map[string]string {
}

func (self *mumbai) GetBlockExplorerAPIKeyVariableName() string {
return "POLYGONSCAN_API_KEY"
return "ETHERSCAN_API_KEY"
}

func (self *mumbai) GetBlockExplorerAPIURL() string {
Expand Down
2 changes: 1 addition & 1 deletion networks/optimism.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type optimismMainnet struct {
}

func NewOptimismMainnet() *optimismMainnet {
result := &optimismMainnet{NewEtherscanLikeExplorer("https://api-optimistic.etherscan.io", "RU33HVN77Q51YNQIANP7GXZRAZ423CETB8")}
result := &optimismMainnet{NewEtherscanV2()}
apiKey := strings.Trim(os.Getenv(result.GetBlockExplorerAPIKeyVariableName()), " ")
if apiKey != "" {
result.EtherscanLikeExplorer.APIKey = apiKey
Expand Down
6 changes: 2 additions & 4 deletions networks/polygon_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ type polygonZkevmMainnet struct {
}

func NewPolygonZkevmMainnet() *polygonZkevmMainnet {
result := &polygonZkevmMainnet{NewEtherscanLikeExplorer(
"https://api.zkevm.polygonscan.com",
"DCMI8XG5WU2W1GX52ZEYN14PY9894YTW9A",
)}
result := &polygonZkevmMainnet{NewEtherscanV2()}
result.ChainID = result.GetChainID()
apiKey := strings.Trim(os.Getenv(result.GetBlockExplorerAPIKeyVariableName()), " ")
if apiKey != "" {
result.EtherscanLikeExplorer.APIKey = apiKey
Expand Down
1 change: 1 addition & 0 deletions networks/rinkeby.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type rinkeby struct {

func NewRinkeby() *rinkeby {
result := &rinkeby{NewRinkebyEtherscan()}
result.ChainID = result.GetChainID()
apiKey := strings.Trim(os.Getenv(result.GetBlockExplorerAPIKeyVariableName()), " ")
if apiKey != "" {
result.EtherscanLikeExplorer.APIKey = apiKey
Expand Down
1 change: 1 addition & 0 deletions networks/ropsten.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type ropsten struct {

func NewRopsten() *ropsten {
result := &ropsten{NewRopstenEtherscan()}
result.ChainID = result.GetChainID()
apiKey := strings.Trim(os.Getenv(result.GetBlockExplorerAPIKeyVariableName()), " ")
if apiKey != "" {
result.EtherscanLikeExplorer.APIKey = apiKey
Expand Down
6 changes: 2 additions & 4 deletions networks/scroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ type scrollMainnet struct {
}

func NewScrollMainnet() *scrollMainnet {
result := &scrollMainnet{NewEtherscanLikeExplorer(
"https://api.scrollscan.com",
"PY7TDTR7V8FHZSUCGI2R12WZZYGFIAF9QT",
)}
result := &scrollMainnet{NewEtherscanV2()}
result.ChainID = result.GetChainID()
apiKey := strings.Trim(os.Getenv(result.GetBlockExplorerAPIKeyVariableName()), " ")
if apiKey != "" {
result.EtherscanLikeExplorer.APIKey = apiKey
Expand Down
8 changes: 6 additions & 2 deletions util/explorers/etherscan_alike_explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type EtherscanLikeExplorer struct {
gpmu sync.Mutex
latestGasPrice float64
gasPriceTimestamp int64
ChainID uint64

Domain string
APIKey string
Expand All @@ -31,8 +32,9 @@ func NewEtherscanLikeExplorer(domain string, apiKey string) *EtherscanLikeExplor

func (ee *EtherscanLikeExplorer) RecommendedGasPriceAPIURL() string {
return fmt.Sprintf(
"%s/api?module=gastracker&action=gasoracle&apikey=%s",
"%s/api?chainid=%dmodule=gastracker&action=gasoracle&apikey=%s",
ee.Domain,
ee.ChainID,
ee.APIKey,
)
}
Expand Down Expand Up @@ -100,9 +102,11 @@ func (ee *EtherscanLikeExplorer) RecommendedGasPrice() (float64, error) {

func (ee *EtherscanLikeExplorer) GetABIStringAPIURL(address string) string {
return fmt.Sprintf(
"%s/api?module=contract&action=getabi&address=%s",
"%s/api?chainid=%d&module=contract&action=getabi&address=%s&apikey=%s",
ee.Domain,
ee.ChainID,
address,
ee.APIKey,
)
}

Expand Down
61 changes: 22 additions & 39 deletions util/explorers/explorers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,73 +5,56 @@ type BlockExplorer interface {
GetABIString(address string) (string, error)
}

func NewMainnetEtherscan() *EtherscanLikeExplorer {
func NewEtherscanV2() *EtherscanLikeExplorer {
return NewEtherscanLikeExplorer(
"https://api.etherscan.io",
"https://api.etherscan.io/v2",
"UBB257TI824FC7HUSPT66KZUMGBPRN3IWV",
)
}

func NewMainnetEtherscan() *EtherscanLikeExplorer {
return NewEtherscanV2()
}

func NewRopstenEtherscan() *EtherscanLikeExplorer {
return NewEtherscanLikeExplorer(
"https://api-ropsten.etherscan.io",
"UBB257TI824FC7HUSPT66KZUMGBPRN3IWV",
)
return NewEtherscanV2()
}

func NewRinkebyEtherscan() *EtherscanLikeExplorer {
return NewEtherscanLikeExplorer(
"https://api-rinkeby.etherscan.io",
"UBB257TI824FC7HUSPT66KZUMGBPRN3IWV",
)
return NewEtherscanV2()
}

func NewKovanEtherscan() *EtherscanLikeExplorer {
return NewEtherscanLikeExplorer(
"https://api-kovan.etherscan.io",
"UBB257TI824FC7HUSPT66KZUMGBPRN3IWV",
)
return NewEtherscanV2()
}

func NewBscscan() *EtherscanLikeExplorer {
return NewEtherscanLikeExplorer(
"https://api.bscscan.com",
"62TU8Z81F7ESNJT38ZVRBSX7CNN4QZSP5I",
)
return NewEtherscanV2()
}

func NewTestnetBscscan() *EtherscanLikeExplorer {
return NewEtherscanLikeExplorer(
"https://api-testnet.bscscan.com",
"62TU8Z81F7ESNJT38ZVRBSX7CNN4QZSP5I",
)
return NewEtherscanV2()
}

func NewPolygonscan() *EtherscanLikeExplorer {
return NewEtherscanLikeExplorer(
"https://api.polygonscan.com",
"AE61PSRHNZ7WS1R1BZUXZXDGE52MMNC22U",
)
return NewEtherscanV2()
}

func NewFtmscan() *EtherscanLikeExplorer {
return NewEtherscanLikeExplorer(
"https://api.ftmscan.com",
"IDMTDBFTDKE7SSHX2V2FYKWWQ9WTVW73HR",
)
}

func NewSnowtrace() *EtherscanLikeExplorer {
return NewEtherscanLikeExplorer(
"https://api.snowtrace.io",
"NWT5MMCQMAPYH47DGD8K7QIJGP2DHZRMW3",
)
return NewEtherscanV2()
}

func NewMumbaiPolygonscan() *EtherscanLikeExplorer {
return nil
return NewEtherscanV2()
}

func NewTomoBlockExplorer() *EtherscanLikeExplorer {
return nil
return NewEtherscanV2()
}

func NewSnowtrace() *EtherscanLikeExplorer {
return NewEtherscanLikeExplorer(
"https://api.routescan.io/v2/network/mainnet/evm/43114/etherscan/",
"NWT5MMCQMAPYH47DGD8K7QIJGP2DHZRMW3",
)
}

0 comments on commit c265880

Please sign in to comment.