1
1
/*
2
2
* Copyright (c) 2018 LI Zhennan
3
+ * Copyright (c) 2022 Łukasz Rżanek
3
4
*
4
5
* Use of this work is governed by a MIT License.
5
6
* You may find a license copy in project root.
@@ -10,37 +11,79 @@ package etherscan
10
11
import (
11
12
"encoding/json"
12
13
"testing"
14
+
15
+ "github.com/stretchr/testify/assert"
13
16
)
14
17
15
18
func TestClient_BlockReward (t * testing.T ) {
16
- const ans = `{"blockNumber":"2165403","timeStamp":"1472533979","blockMiner":"0x13a06d3dfe21e0db5c016c03ea7d2509f7f8d1e3","blockReward":"5314181600000000000","uncles":[{"miner":"0xbcdfc35b86bedf72f0cda046a3c16829a2ef41d1","unclePosition":"0","blockreward":"3750000000000000000"},{"miner":"0x0d0c9855c722ff0c78f21e43aa275a5b8ea60dce","unclePosition":"1","blockreward":"3750000000000000000"}],"uncleInclusionReward":"312500000000000000"}`
19
+ const (
20
+ ethAns = `{"blockNumber":"2165403","timeStamp":"1472533979","blockMiner":"0x13a06d3dfe21e0db5c016c03ea7d2509f7f8d1e3","blockReward":"5314181600000000000","uncles":[{"miner":"0xbcdfc35b86bedf72f0cda046a3c16829a2ef41d1","unclePosition":"0","blockreward":"3750000000000000000"},{"miner":"0x0d0c9855c722ff0c78f21e43aa275a5b8ea60dce","unclePosition":"1","blockreward":"3750000000000000000"}],"uncleInclusionReward":"312500000000000000"}`
21
+ maticAns = `{"blockNumber":"2165403","timeStamp":"1595322344","blockMiner":"0x0375b2fc7140977c9c76d45421564e354ed42277","blockReward":"0","uncles":[],"uncleInclusionReward":"0"}`
22
+ bscAns = `{"blockNumber":"2165403","timeStamp":"1605169045","blockMiner":"0x78f3adfc719c99674c072166708589033e2d9afe","blockReward":"0","uncles":[],"uncleInclusionReward":"0"}`
23
+ avaxAns = `{"blockNumber":"2165403","timeStamp":"1622557183","blockMiner":"0x0100000000000000000000000000000000000000","blockReward":"7963290000000000","uncles":[],"uncleInclusionReward":"0"}`
24
+ ftmAns = `{"blockNumber":"2165403","timeStamp":"1612983193","blockMiner":"0x0000000000000000000000000000000000000000","blockReward":"5225066000000000","uncles":[],"uncleInclusionReward":"0"}`
25
+ cronosAns = `{"blockNumber":"2165403","timeStamp":"1648843458","blockMiner":"0x4f87a3f99bd1e58d01de1c38b7f83cb967e816c2","blockReward":"49532402000000000000","uncles":[],"uncleInclusionReward":"0"}`
26
+ arbitrumAns = `{"blockNumber":"2165403","timeStamp":"1634069963","blockMiner":"0x0000000000000000000000000000000000000000","blockReward":"2065103660121552","uncles":[],"uncleInclusionReward":"0"}`
27
+ )
17
28
18
- reward , err := api .BlockReward (2165403 )
19
- noError (t , err , "api.BlockReward" )
29
+ type Data struct {
30
+ BlockNumber int
31
+ Ans string
32
+ }
20
33
21
- j , err := json .Marshal (reward )
22
- noError (t , err , "json.Marshal" )
23
- if string (j ) != ans {
24
- t .Errorf ("api.BlockReward not working, got %s, want %s" , j , ans )
34
+ testData := map [string ]Data {
35
+ EthMainnet .CommonName : {2165403 , ethAns },
36
+ MaticMainnet .CommonName : {2165403 , maticAns },
37
+ BscMainnet .CommonName : {2165403 , bscAns },
38
+ AvaxMainnet .CommonName : {2165403 , avaxAns },
39
+ FantomMainnet .CommonName : {2165403 , ftmAns },
40
+ CronosMainnet .CommonName : {2165403 , cronosAns },
41
+ ArbitrumMainnet .CommonName : {2165403 , arbitrumAns },
42
+ }
43
+
44
+ for _ , network := range TestNetworks {
45
+ if td , ok := testData [network .Network .CommonName ]; ok {
46
+ t .Run (network .Network .Name , func (t * testing.T ) {
47
+ reward , err := network .client .BlockReward (td .BlockNumber )
48
+ assert .NoError (t , err )
49
+
50
+ j , err := json .Marshal (reward )
51
+ assert .NoError (t , err )
52
+ assert .Equalf (t , td .Ans , string (j ), "api.BlockReward not working, got %s, want %s" , j , ethAns )
53
+ })
54
+ }
25
55
}
26
56
}
27
57
28
58
func TestClient_BlockNumber (t * testing.T ) {
29
- // Note: All values taken from docs.etherscan.io/api-endpoints/blocks
30
- const ansBefore = 9251482
31
- const ansAfter = 9251483
32
-
33
- blockNumber , err := api .BlockNumber (1578638524 , "before" )
34
- noError (t , err , "api.BlockNumber" )
59
+ type Data struct {
60
+ Timestamp int64
61
+ AnsBefore int
62
+ AnsAfter int
63
+ }
35
64
36
- if blockNumber != ansBefore {
37
- t .Errorf (`api.BlockNumber(1578638524, "before") not working, got %d, want %d` , blockNumber , ansBefore )
65
+ testData := map [string ]Data {
66
+ // Note: All values taken from docs.etherscan.io/api-endpoints/blocks
67
+ EthMainnet .CommonName : {1578638524 , 9251482 , 9251483 },
68
+ MaticMainnet .CommonName : {1601510400 , 5164199 , 5164200 },
69
+ BscMainnet .CommonName : {1601510400 , 946206 , 946207 },
70
+ AvaxMainnet .CommonName : {1609455600 , 18960 , 18961 },
71
+ FantomMainnet .CommonName : {1609455600 , 1632921 , 1632921 }, // it is the same!
72
+ CronosMainnet .CommonName : {1654034400 , 3023556 , 3023557 },
73
+ ArbitrumMainnet .CommonName : {1656626400 , 16655953 , 16655954 },
38
74
}
39
75
40
- blockNumber , err = api .BlockNumber (1578638524 , "after" )
41
- noError (t , err , "api.BlockNumber" )
76
+ for _ , network := range TestNetworks {
77
+ if td , ok := testData [network .Network .CommonName ]; ok {
78
+ t .Run (network .Network .Name , func (t * testing.T ) {
79
+ blockNumber , err := network .client .BlockNumber (td .Timestamp , "before" )
80
+ assert .NoError (t , err )
81
+ assert .Equalf (t , td .AnsBefore , blockNumber , `api.BlockNumber(%d, "before") not working, got %d, want %d` , td .Timestamp , blockNumber , td .AnsBefore )
42
82
43
- if blockNumber != ansAfter {
44
- t .Errorf (`api.BlockNumber(1578638524,"after") not working, got %d, want %d` , blockNumber , ansAfter )
83
+ blockNumber , err = network .client .BlockNumber (td .Timestamp , "after" )
84
+ assert .NoError (t , err )
85
+ assert .Equalf (t , td .AnsAfter , blockNumber , `api.BlockNumber(%d, "after") not working, got %d, want %d` , td .Timestamp , blockNumber , td .AnsAfter )
86
+ })
87
+ }
45
88
}
46
89
}
0 commit comments