From c44a816769a748c12eb3ec375ac82ada98ffa5fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20R=C5=BCanek?= Date: Mon, 23 May 2022 01:10:24 +0200 Subject: [PATCH 1/4] feat: changes to network config, more complex scenario --- client.go | 1 + network.go | 35 ++++++++++++++++++----------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/client.go b/client.go index c12a3ed..a418d71 100644 --- a/client.go +++ b/client.go @@ -23,6 +23,7 @@ import ( type Client struct { coon *http.Client key string + network Network baseURL string // Verbose when true, talks a lot diff --git a/network.go b/network.go index 34d4ccf..5da08c9 100644 --- a/network.go +++ b/network.go @@ -7,36 +7,37 @@ package etherscan -const ( - // // Ethereum public networks - +var ( // EthMainnet Ethereum mainnet for production - EthMainnet Network = "https://api.etherscan.io/api?" + EthMainnet Network = Network{"Ethereum", "main", "https://api.etherscan.io/api?"} // EthRopsten Testnet(POW) - EthRopsten Network = "https://api-ropsten.etherscan.io/api?" + EthRopsten Network = Network{"Ethereum Ropsten", "test", "https://api-ropsten.etherscan.io/api?"} // EthKovan Testnet(POA) - EthKovan Network = "https://api-kovan.etherscan.io/api?" + EthKovan Network = Network{"Ethereum Kovan", "test", "https://api-kovan.etherscan.io/api?"} // EthRinkby Testnet(CLIQUE) - EthRinkby Network = "https://api-rinkeby.etherscan.io/api?" + EthRinkby Network = Network{"Ethereum Rinkby", "test", "https://api-rinkeby.etherscan.io/api?"} // EthGoerli Testnet(CLIQUE) - EthGoerli Network = "https://api-goerli.etherscan.io/api?" + EthGoerli Network = Network{"Ethereum Goerli", "test", "https://api-goerli.etherscan.io/api?"} // EthTobalaba Testnet - EthTobalaba Network = "https://api-tobalaba.etherscan.io/api?" + EthTobalaba Network = Network{"Ethereum Tobalaba", "test", "https://api-tobalaba.etherscan.io/api?"} // MaticMainnet Matic mainnet for production - MaticMainnet Network = "https://api.polygonscan.com/api?" + MaticMainnet Network = Network{"Polygon", "main", "https://api.polygonscan.com/api?"} // MaticTestnet Matic testnet for development - MaticTestnet Network = "https://api-testnet.polygonscan.com/api?" + MaticTestnet Network = Network{"Polygon Mumbai", "test", "https://api-testnet.polygonscan.com/api?"} // BscMainnet Bsc mainnet for production - BscMainnet Network = "https://api.bscscan.com/api?" + BscMainnet Network = Network{"Binance", "main", "https://api.bscscan.com/api?"} // BscTestnet Bsc testnet for development - BscTestnet Network = "https://api-testnet.bscscan.com/api?" + BscTestnet Network = Network{"Binance test", "test", "https://api-testnet.bscscan.com/api?"} ) // Network is ethereum network type (mainnet, ropsten, etc) -type Network string +type Network struct { + Name string + Type string + BaseURL string +} -// SubDomain returns the subdomain of etherscan API -// via n provided. +// Domain returns the subdomain of etherscan API via n provided. func (n Network) Domain() (sub string) { - return string(n) + return n.BaseURL } From a2cb094b09ef7c8e09904915aa0ebc5a6a2e3d65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20R=C5=BCanek?= Date: Mon, 23 May 2022 01:14:18 +0200 Subject: [PATCH 2/4] feat: visibility of the network in the client --- client.go | 4 ++++ network.go | 10 +++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/client.go b/client.go index a418d71..950c532 100644 --- a/client.go +++ b/client.go @@ -38,6 +38,10 @@ type Client struct { AfterRequest func(module, action string, param map[string]interface{}, outcome interface{}, requestErr error) } +func (c *Client) GetNetwork() Network { + return c.network +} + // New initialize a new etherscan API client // please use pre-defined network value func New(network Network, APIKey string) *Client { diff --git a/network.go b/network.go index 5da08c9..0aacfdd 100644 --- a/network.go +++ b/network.go @@ -32,12 +32,12 @@ var ( // Network is ethereum network type (mainnet, ropsten, etc) type Network struct { - Name string - Type string - BaseURL string + Name string // Name of the network or chain + Type string // Type of the network, either main or test + baseURL string // baseURL for the API client } -// Domain returns the subdomain of etherscan API via n provided. +// Domain returns the subdomain of etherscan API via n provided. func (n Network) Domain() (sub string) { - return n.BaseURL + return n.baseURL } From 025f7352b57f81bc4551bd970bc3cb7b28b5723e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20R=C5=BCanek?= Date: Mon, 23 May 2022 23:00:38 +0200 Subject: [PATCH 3/4] feat: method to get network by name, tests --- network.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ network_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 network_test.go diff --git a/network.go b/network.go index 0aacfdd..06dfe04 100644 --- a/network.go +++ b/network.go @@ -7,6 +7,11 @@ package etherscan +import ( + "fmt" + "strings" +) + var ( // EthMainnet Ethereum mainnet for production EthMainnet Network = Network{"Ethereum", "main", "https://api.etherscan.io/api?"} @@ -28,8 +33,37 @@ var ( BscMainnet Network = Network{"Binance", "main", "https://api.bscscan.com/api?"} // BscTestnet Bsc testnet for development BscTestnet Network = Network{"Binance test", "test", "https://api-testnet.bscscan.com/api?"} + + networks = map[string]*Network{ + "ethmainnet": &EthMainnet, + "ethereum": &EthMainnet, + "eth": &EthMainnet, + "ethropsten": &EthRopsten, + "ropsten": &EthRopsten, + "ethkovan": &EthKovan, + "ethrinkby": &EthRinkby, + "ethgoerli": &EthGoerli, + "ethtobalaba": &EthTobalaba, + "maticmainnet": &MaticMainnet, + "polygon": &MaticMainnet, + "matic": &MaticMainnet, + "matictestnet": &MaticTestnet, + "mumbai": &MaticTestnet, + "bscmainnet": &BscMainnet, + "binance": &BscMainnet, + "bsctestnet": &BscTestnet, + } + + networkNames []string ) +func init() { + for name, _ := range networks { + networkNames = append(networkNames, name) + } + +} + // Network is ethereum network type (mainnet, ropsten, etc) type Network struct { Name string // Name of the network or chain @@ -41,3 +75,15 @@ type Network struct { func (n Network) Domain() (sub string) { return n.baseURL } + +func ParseNetworkName(network string) (Network, error) { + if x, ok := networks[network]; ok { + return *x, nil + } + // Case insensitive parse, do a separate lookup to prevent unnecessary cost of lowercasing a string if we don't need to. + if x, ok := networks[strings.ToLower(network)]; ok { + return *x, nil + } + return Network{}, fmt.Errorf("%s is not a valid ETHNetworkType, try one of [%s]", network, strings.Join(networkNames, ", ")) + +} diff --git a/network_test.go b/network_test.go new file mode 100644 index 0000000..8ed4dc7 --- /dev/null +++ b/network_test.go @@ -0,0 +1,45 @@ +package etherscan + +import ( + "reflect" + "testing" +) + +func TestParseNetworkName(t *testing.T) { + tests := []struct { + name string + want Network + wantErr bool + }{ + {"ethmainnet", EthMainnet, false}, + {"eth main net", Network{}, true}, + {"ethereum", EthMainnet, false}, + {"eth", EthMainnet, false}, + {"ethropsten", EthRopsten, false}, + {"ropsten", EthRopsten, false}, + {"ethkovan", EthKovan, false}, + {"ethrinkby", EthRinkby, false}, + {"ethgoerli", EthGoerli, false}, + {"ethtobalaba", EthTobalaba, false}, + {"maticmainnet", MaticMainnet, false}, + {"polygon", MaticMainnet, false}, + {"matic", MaticMainnet, false}, + {"matictestnet", MaticTestnet, false}, + {"mumbai", MaticTestnet, false}, + {"bscmainnet", BscMainnet, false}, + {"binance", BscMainnet, false}, + {"bsctestnet", BscTestnet, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := ParseNetworkName(tt.name) + if (err != nil) != tt.wantErr { + t.Errorf("ParseNetworkName() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("ParseNetworkName() got = %v, want %v", got, tt.want) + } + }) + } +} From e3d5c9712877020e306c998ee17b1995308b6069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20R=C5=BCanek?= Date: Sat, 28 May 2022 23:11:04 +0200 Subject: [PATCH 4/4] feat: avalanche network added --- network.go | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/network.go b/network.go index 06dfe04..0c0618a 100644 --- a/network.go +++ b/network.go @@ -33,25 +33,37 @@ var ( BscMainnet Network = Network{"Binance", "main", "https://api.bscscan.com/api?"} // BscTestnet Bsc testnet for development BscTestnet Network = Network{"Binance test", "test", "https://api-testnet.bscscan.com/api?"} + // AvaxMainnet Avalanche mainnet for production + AvaxMainnet Network = Network{"Avax", "main", "https://api.snowtrace.io/api?"} + // AvaxTestnet Avalanche testnet for development + AvaxTestnet Network = Network{"Avax test", "test", "https://api-testnet.snowtrace.io/api?"} networks = map[string]*Network{ - "ethmainnet": &EthMainnet, - "ethereum": &EthMainnet, - "eth": &EthMainnet, - "ethropsten": &EthRopsten, - "ropsten": &EthRopsten, - "ethkovan": &EthKovan, - "ethrinkby": &EthRinkby, - "ethgoerli": &EthGoerli, - "ethtobalaba": &EthTobalaba, - "maticmainnet": &MaticMainnet, - "polygon": &MaticMainnet, - "matic": &MaticMainnet, - "matictestnet": &MaticTestnet, - "mumbai": &MaticTestnet, - "bscmainnet": &BscMainnet, - "binance": &BscMainnet, - "bsctestnet": &BscTestnet, + "ethmainnet": &EthMainnet, + "ethereum": &EthMainnet, + "eth": &EthMainnet, + "ethropsten": &EthRopsten, + "ropsten": &EthRopsten, + "ethkovan": &EthKovan, + "ethrinkby": &EthRinkby, + "ethgoerli": &EthGoerli, + "ethtobalaba": &EthTobalaba, + "maticmainnet": &MaticMainnet, + "polygon": &MaticMainnet, + "matic": &MaticMainnet, + "matictestnet": &MaticTestnet, + "mumbai": &MaticTestnet, + "bscmainnet": &BscMainnet, + "binance": &BscMainnet, + "bsctestnet": &BscTestnet, + "avalanche": &AvaxMainnet, + "avax": &AvaxMainnet, + "avaxmainnet": &AvaxMainnet, + "avalanchemainnet": &AvaxMainnet, + "avaxtestnet": &AvaxTestnet, + "avalanchetestnet": &AvaxTestnet, + "avaxfuji": &AvaxTestnet, + "avalanchefuji": &AvaxTestnet, } networkNames []string