-
Notifications
You must be signed in to change notification settings - Fork 1
/
methods_metadata_test.go
85 lines (75 loc) · 1.95 KB
/
methods_metadata_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
package sourcify
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
)
func TestGetContractMetadata(t *testing.T) {
// Create a mock HTTP server
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/server/repository/contracts/full_match/1/0x0000000000000000000000001234567890aBcdEF/metadata.json" {
// Simulate a successful response with sample metadata
metadata := &Metadata{
Compiler: Compiler{Version: "0.1.0"},
Language: "Solidity",
Output: Output{
Abi: []Abi{
{
Inputs: []any{},
Type: "function",
Name: "myFunction",
Outputs: []OutputDetail{
{
Type: "uint256",
Name: "result",
},
},
},
},
},
Version: 1,
}
err := json.NewEncoder(w).Encode(metadata)
if err != nil {
t.Errorf("failed to encode mock metadata: %v", err)
}
} else {
http.NotFound(w, r)
}
}))
defer mockServer.Close()
// Create a client for the mock server
client := NewClient(WithBaseURL(mockServer.URL + "/server"))
// Define test data
chainID := 1
contractAddress := common.HexToAddress("0x1234567890abcdef")
matchType := MethodMatchTypeFull
// Call the function to get contract metadata
metadata, err := GetContractMetadata(client, chainID, contractAddress, matchType)
// Verify the results
assert.NoError(t, err, "GetContractMetadata returned an error")
expectedMetadata := &Metadata{
Compiler: Compiler{Version: "0.1.0"},
Language: "Solidity",
Output: Output{
Abi: []Abi{
{
Inputs: []any{},
Type: "function",
Name: "myFunction",
Outputs: []OutputDetail{
{
Type: "uint256",
Name: "result",
},
},
},
},
},
Version: 1,
}
assert.Equal(t, expectedMetadata, metadata, "GetContractMetadata returned unexpected metadata")
}