-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
319 lines (260 loc) · 7.68 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/stretchr/testify/assert"
)
func TestABIStorage(t *testing.T) {
storage := NewABIStorage()
// Test Set and Get
testItem := StorageItem{
ABI: "test-abi",
Implementation: "0x123",
IsProxy: true,
}
storage.Set("test-key", testItem)
item, ok := storage.Get("test-key")
assert.True(t, ok)
assert.Equal(t, testItem, item)
// Test Get with non-existent key
item, ok = storage.Get("non-existent")
assert.False(t, ok)
assert.Equal(t, StorageItem{}, item)
}
func TestRealEtherscanCall(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
// Load .env file
err := godotenv.Load()
if err != nil {
t.Fatal("Error loading .env file")
}
// Ensure the API key is set
apiKey := os.Getenv("OPTIMISM_API_KEY")
if apiKey == "" {
t.Skip("Skipping test: OPTIMISM_API_KEY not set")
}
// Create a GenericEtherscanAPI instance for Optimism
optimismAPI := &GenericEtherscanAPI{
BaseURL: "https://api-optimistic.etherscan.io/api",
EnvKey: "OPTIMISM_API_KEY",
}
// Test address
address := "0xE575E956757c20b22C5a11eB542F719564c32Fe8"
// Call GetABI
abi, err := optimismAPI.GetABIFromEtherscan(address)
if err != nil {
t.Fatalf("Error getting ABI: %v", err)
}
// Basic check to ensure ABI is not empty
assert.NotEmpty(t, abi, "ABI should not be empty")
// You could add more specific checks here if you know what the ABI should contain
t.Logf("Received ABI: %s", abi)
}
func TestRealUSDCProxyDetection(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
// Load .env file
err := godotenv.Load()
if err != nil {
t.Fatal("Error loading .env file")
}
// Ensure the API key is set
apiKey := os.Getenv("ETHEREUM_API_KEY")
if apiKey == "" {
t.Skip("Skipping test: ETHEREUM_API_KEY not set")
}
// Setup
gin.SetMode(gin.TestMode)
router := gin.Default()
router.GET("/abi/:chainId/:address/*rpcUrl", getABI)
// Create a real EtherscanAPI instance for Ethereum
etherscanAPI := &GenericEtherscanAPI{
BaseURL: "https://api.etherscan.io/api",
EnvKey: "ETHEREUM_API_KEY",
}
etherscanAPIs[1] = etherscanAPI
// USDC contract address
address := "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
rpcURL := "rpc.ankr.com/eth" // Replace with a valid Ethereum RPC URL
// Make the request
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/abi/1/"+address+"/"+rpcURL, nil)
router.ServeHTTP(w, req)
// Check the response
assert.Equal(t, http.StatusOK, w.Code)
var response map[string]interface{}
err = json.Unmarshal(w.Body.Bytes(), &response)
assert.NoError(t, err)
// Check the specific fields
assert.Equal(t, "0x43506849D7C04F9138D1A2050bbF3A0c054402dd", response["implementation"])
assert.Equal(t, false, response["isDecompiled"])
assert.Equal(t, true, response["isProxy"])
// Check if the ABI contains "isBlacklisted"
abi, ok := response["abi"].(string)
assert.True(t, ok)
assert.Contains(t, abi, "isBlacklisted")
t.Logf("Received ABI: %s", abi)
}
func TestHeimdallAPIResponse(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
// Setup
gin.SetMode(gin.TestMode)
router := gin.Default()
router.GET("/abi/:chainId/:address/*rpcUrl", getABI)
// Test contract address
address := "0x759c0e9d7858566df8ab751026bedce462ff42df"
chainID := "11155111" // Sepolia chain ID
rpcURL := "rpc.ankr.com/eth_sepolia"
// Make the request
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/abi/"+chainID+"/"+address+"/"+rpcURL, nil)
router.ServeHTTP(w, req)
// Check the response
assert.Equal(t, http.StatusOK, w.Code)
var response map[string]interface{}
err := json.Unmarshal(w.Body.Bytes(), &response)
assert.NoError(t, err)
// Check the specific fields
assert.Nil(t, response["implementation"])
assert.Equal(t, true, response["isDecompiled"])
assert.Equal(t, false, response["isProxy"])
// Check if the ABI is correct
expectedABI := `[
{
"type": "function",
"name": "changeOwner",
"inputs": [
{
"name": "arg0",
"type": "address"
}
],
"outputs": [],
"stateMutability": "payable"
},
{
"type": "function",
"name": "getOwner",
"inputs": [],
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"stateMutability": "payable"
},
{
"type": "event",
"name": "OwnerSet",
"inputs": [
{
"name": "arg0",
"type": "address",
"indexed": false
},
{
"name": "arg1",
"type": "address",
"indexed": false
}
],
"anonymous": false
}
]`
actualABI, ok := response["abi"].(string)
assert.True(t, ok)
// Normalize the JSON strings for comparison
var expectedJSON, actualJSON interface{}
json.Unmarshal([]byte(expectedABI), &expectedJSON)
json.Unmarshal([]byte(actualABI), &actualJSON)
assert.Equal(t, expectedJSON, actualJSON)
t.Logf("Received ABI: %s", actualABI)
}
func TestParexContractABI(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
// Setup
gin.SetMode(gin.TestMode)
router := gin.Default()
router.GET("/abi/:chainId/:address/*rpcUrl", getABI)
// Test contract address
address := "0x6058518142C6AD506530F5A62dCc58050bf6fC28"
chainID := "322202" // Parex chain ID
rpcURL := "mainnet-rpc.parex.network"
// Make the request
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/abi/"+chainID+"/"+address+"/"+rpcURL, nil)
router.ServeHTTP(w, req)
// Check the response
assert.Equal(t, http.StatusOK, w.Code)
var response map[string]interface{}
err := json.Unmarshal(w.Body.Bytes(), &response)
assert.NoError(t, err)
// Check the specific fields
assert.Equal(t, false, response["isProxy"])
assert.Equal(t, true, response["isDecompiled"])
// Check if the ABI contains "sendValidatorReward"
abi, ok := response["abi"].(string)
assert.True(t, ok)
assert.Contains(t, abi, "sendValidatorReward")
t.Logf("Received ABI: %s", abi)
}
func TestEtherscanFailureHeimdallFallback(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
// Setup
gin.SetMode(gin.TestMode)
router := gin.Default()
router.GET("/abi/:chainId/:address/*rpcUrl", getABI)
// Create a mock EtherscanAPI that always fails
mockEtherscanAPI := &MockEtherscanAPI{
ShouldFail: true,
}
etherscanAPIs[1] = mockEtherscanAPI
// Test contract address (use a real contract address that Heimdall can decompile)
address := "0x6B175474E89094C44Da98b954EedeAC495271d0F" // DAI token
chainID := "1" // Ethereum mainnet
rpcURL := "rpc.ankr.com/eth"
// Make the request
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/abi/"+chainID+"/"+address+"/"+rpcURL, nil)
router.ServeHTTP(w, req)
// Check the response
assert.Equal(t, http.StatusOK, w.Code)
var response map[string]interface{}
err := json.Unmarshal(w.Body.Bytes(), &response)
assert.NoError(t, err)
// Check the specific fields
assert.Nil(t, response["implementation"])
assert.Equal(t, true, response["isDecompiled"])
assert.Equal(t, false, response["isProxy"])
// Check if the ABI contains some expected function
abi, ok := response["abi"].(string)
assert.True(t, ok)
assert.Contains(t, abi, "transfer")
t.Logf("Received ABI: %s", abi)
}
// MockEtherscanAPI is a mock implementation of the ChainAPI interface
type MockEtherscanAPI struct {
ShouldFail bool
}
func (m *MockEtherscanAPI) GetABIFromEtherscan(address string) (string, error) {
if m.ShouldFail {
return "", fmt.Errorf("mock Etherscan API error")
}
return "", nil
}