-
Notifications
You must be signed in to change notification settings - Fork 1
/
methods_check_by_address.go
147 lines (128 loc) · 4.51 KB
/
methods_check_by_address.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
package sourcify
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/ethereum/go-ethereum/common"
)
var (
// MethodCheckByAddresses represents the API endpoint for checking by addresses in the Sourcify service.
// It includes the name, the HTTP method, the URI, and the parameters necessary for the request.
// Checks if contract with the desired chain and address is verified and in the repository.
// More information: https://docs.sourcify.dev/docs/api/server/check-by-addresses/
MethodCheckByAddresses = Method{
Name: "Check By Addresses",
URI: "/check-by-addresses",
MoreInfo: "https://docs.sourcify.dev/docs/api/server/check-by-addresses/",
Method: "GET",
ParamType: MethodParamTypeQueryString,
RequiredParams: []string{"addresses", "chainIds"},
Params: []MethodParam{
{
Key: "addresses",
Value: []string{},
},
{
Key: "chainIds",
Value: []int{},
},
},
}
// MethodCheckAllByAddresses represents the API endpoint for checking all addresses in the Sourcify service.
// It includes the name, the HTTP method, the URI, and the parameters necessary for the request.
// Checks if contract with the desired chain and address is verified and in the repository.
// More information: https://docs.sourcify.dev/docs/api/server/check-all-by-addresses/
MethodCheckAllByAddresses = Method{
Name: "Check All By Addresses",
URI: "/check-all-by-addresses",
MoreInfo: "https://docs.sourcify.dev/docs/api/server/check-all-by-addresses/",
Method: "GET",
ParamType: MethodParamTypeQueryString,
RequiredParams: []string{"addresses", "chainIds"},
Params: []MethodParam{
{
Key: "addresses",
Value: []string{},
},
{
Key: "chainIds",
Value: []int{},
},
},
}
)
// CheckContractAddress represents the contract address and associated chain IDs and statuses.
type CheckContractAddress struct {
Address common.Address `json:"address"` // The contract address.
Status string `json:"status"` // The status of the contract.
ChainIDs []string `json:"chainIds"` // The chain ID.
}
// CheckContractAddressMore represents the contract address and associated chain IDs and statuses.
type CheckContractAddressMore struct {
Address common.Address `json:"address"` // The contract address.
Info []CheckContractAddressMoreInfo `json:"chainIds"` // The chain ID.
}
// CheckContractAddressMoreInfo represents the contract address and associated chain IDs and statuses.
type CheckContractAddressMoreInfo struct {
Status string `json:"status"` // The status of the contract.
ChainID string `json:"chainId"` // The chain ID.
}
// CheckContractByAddresses retrieves the available verified contract addresses for the given chain ID.
func CheckContractByAddresses(client *Client, addresses []string, chainIds []int, matchType MethodMatchType) ([]*CheckContractAddress, error) {
var method Method
switch matchType {
case MethodMatchTypeFull:
method = MethodCheckByAddresses
case MethodMatchTypePartial:
method = MethodCheckAllByAddresses
case MethodMatchTypeAny:
method = MethodCheckAllByAddresses
default:
return nil, fmt.Errorf("invalid match type: %s", matchType)
}
method.SetParams(
MethodParam{Key: "addresses", Value: addresses},
MethodParam{Key: "chainIds", Value: chainIds},
)
if err := method.Verify(); err != nil {
return nil, err
}
response, statusCode, err := client.CallMethod(method)
if err != nil {
return nil, err
}
// Close the io.ReadCloser interface.
// This is important as CallMethod is NOT closing the response body!
// You'll have memory leaks if you don't do this!
defer response.Close()
if statusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", statusCode)
}
body, err := io.ReadAll(response)
if err != nil {
return nil, err
}
var toReturn []*CheckContractAddress
if err := json.Unmarshal(body, &toReturn); err != nil {
if strings.Contains(err.Error(), "cannot unmarshal object into Go struct field CheckContractAddress.chainIds") {
var toReturnMore []*CheckContractAddressMore
if err := json.Unmarshal(body, &toReturnMore); err != nil {
return nil, err
}
for _, v := range toReturnMore {
for _, info := range v.Info {
toReturn = append(toReturn, &CheckContractAddress{
Address: v.Address,
Status: info.Status,
ChainIDs: []string{info.ChainID},
})
}
}
return toReturn, nil
}
return nil, err
}
return toReturn, nil
}