From a2847c2707ba27023bffb89ad22975de9d6ff114 Mon Sep 17 00:00:00 2001 From: zfb7901 Date: Sat, 14 Jun 2025 23:57:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AF=B9V2=E7=9A=84=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zfb7901 --- account_e2e_test.go | 16 ++++++---------- chain_id.go | 15 +++++++++++++++ client.go | 17 +++++++++++------ client_test.go | 4 ++-- setup_e2e_test.go | 9 ++------- 5 files changed, 36 insertions(+), 25 deletions(-) create mode 100644 chain_id.go diff --git a/account_e2e_test.go b/account_e2e_test.go index f7688e3..d247470 100644 --- a/account_e2e_test.go +++ b/account_e2e_test.go @@ -15,20 +15,16 @@ import ( ) func TestClient_AccountBalance(t *testing.T) { - balance, err := api.AccountBalance("0x0000000000000000000000000000000000000000") + balanceInt, err := api.AccountBalance("0x00ee777607Ee4c4Be04f74286BC73C20e3c36aC2") noError(t, err, "api.AccountBalance") - if balance.Int().Cmp(big.NewInt(0)) != 1 { - t.Fatalf("rich man is no longer rich") - } + balance := new(big.Float).Quo(new(big.Float).SetInt(balanceInt.Int()), big.NewFloat(1e18)).String() + fmt.Printf("balance: %s\n", balance) } func TestClient_MultiAccountBalance(t *testing.T) { balances, err := api.MultiAccountBalance( - "0x0000000000000000000000000000000000000000", - "0x0000000000000000000000000000000000000001", - "0x0000000000000000000000000000000000000002", - "0x0000000000000000000000000000000000000003") + "0x00ee777607Ee4c4Be04f74286BC73C20e3c36aC2") noError(t, err, "api.MultiAccountBalance") for i, item := range balances { @@ -44,8 +40,8 @@ func TestClient_MultiAccountBalance(t *testing.T) { func TestClient_NormalTxByAddress(t *testing.T) { const wantLen = 19 - var a, b = 54092, 79728 - txs, err := api.NormalTxByAddress("0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae", &a, &b, 1, 500, false) + var a, b = 0, 9999999999 + txs, err := api.NormalTxByAddress("0x00ee777607Ee4c4Be04f74286BC73C20e3c36aC2", &a, &b, 1, 500, true) noError(t, err, "api.NormalTxByAddress") //j, _ := json.MarshalIndent(txs, "", " ") diff --git a/chain_id.go b/chain_id.go new file mode 100644 index 0000000..c9eb196 --- /dev/null +++ b/chain_id.go @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2018 LI Zhennan + * + * Use of this work is governed by a MIT License. + * You may find a license copy in project root. + */ + +package etherscan + +const ( + MainnetEthereum = "1" + TestnetSepolia = "11155111" + MainnetBNB = "56" + TestnetBNB = "97" +) diff --git a/client.go b/client.go index e69679a..4ec7a40 100644 --- a/client.go +++ b/client.go @@ -24,6 +24,7 @@ type Client struct { coon *http.Client key string baseURL string + ChainID string // Verbose when true, talks a lot Verbose bool @@ -39,11 +40,12 @@ type Client struct { // New initialize a new etherscan API client // please use pre-defined network value -func New(network Network, APIKey string) *Client { +func New(chainID string, APIKey string) *Client { return NewCustomized(Customization{ Timeout: 30 * time.Second, Key: APIKey, - BaseURL: fmt.Sprintf(`https://%s.etherscan.io/api?`, network.SubDomain()), + ChainID: chainID, + BaseURL: fmt.Sprintf(`https://api.etherscan.io/v2/api?`), }) } @@ -60,7 +62,8 @@ type Customization struct { // HTTP Client to be used. Specifying this value will ignore the Timeout value set // Set your own timeout. Client *http.Client - + //chainid + ChainID string // BeforeRequest runs before every client request, in the same goroutine. // May be used in rate limit. // Request will be aborted, if BeforeRequest returns non-nil err. @@ -84,6 +87,7 @@ func NewCustomized(config Customization) *Client { return &Client{ coon: httpClient, key: config.Key, + ChainID: config.ChainID, baseURL: config.BaseURL, Verbose: config.Verbose, BeforeRequest: config.BeforeRequest, @@ -194,9 +198,10 @@ func (c *Client) call(module, action string, param map[string]interface{}, outco // craftURL returns desired URL via param provided func (c *Client) craftURL(module, action string, param map[string]interface{}) (URL string) { q := url.Values{ - "module": []string{module}, - "action": []string{action}, - "apikey": []string{c.key}, + "chainid": []string{c.ChainID}, + "module": []string{module}, + "action": []string{action}, + "apikey": []string{c.key}, } for k, v := range param { diff --git a/client_test.go b/client_test.go index a8d045c..f01beab 100644 --- a/client_test.go +++ b/client_test.go @@ -12,9 +12,9 @@ import ( ) func TestClient_craftURL(t *testing.T) { - c := New(Ropsten, "abc123") + c := New(TestnetBNB, "abc123") - const expected = `https://api-ropsten.etherscan.io/api?action=craftURL&apikey=abc123&four=d&four=e&four=f&module=testing&one=1&three=1&three=2&three=3&two=2` + const expected = `https://api.etherscan.io/v2/api?action=craftURL&apikey=abc123&chainid=97&four=d&four=e&four=f&module=testing&one=1&three=1&three=2&three=3&two=2` output := c.craftURL("testing", "craftURL", M{ "one": 1, "two": "2", diff --git a/setup_e2e_test.go b/setup_e2e_test.go index 64f2702..d98cd23 100644 --- a/setup_e2e_test.go +++ b/setup_e2e_test.go @@ -8,8 +8,6 @@ package etherscan import ( - "fmt" - "os" "testing" "time" ) @@ -26,13 +24,10 @@ var ( ) func init() { - apiKey = os.Getenv(apiKeyEnvName) - if apiKey == "" { - panic(fmt.Sprintf("API key is empty, set env variable %q with a valid API key to proceed.", apiKeyEnvName)) - } + apiKey = "5SPAA3V1AE726C7JG78PNN5WIXGKETVYCX" bucket = NewBucket(500 * time.Millisecond) - api = New(Mainnet, apiKey) + api = New(TestnetBNB, apiKey) api.Verbose = true api.BeforeRequest = func(module string, action string, param map[string]interface{}) error { bucket.Take()