-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmethods_contract.go
More file actions
204 lines (178 loc) · 7.22 KB
/
methods_contract.go
File metadata and controls
204 lines (178 loc) · 7.22 KB
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
package sourcify
import (
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/common"
"net/http"
"strings"
"time"
)
var (
// MethodGetContractByChainIdAndAddress represents the API endpoint for retrieving contract details by chain ID and address.
// It returns all verified sources from the repository for the specified contract address and chain, including metadata.json.
// This endpoint searches only for full matches.
// HTTP Method: GET
// URI: /v2/contract/:chain/:address
// Documentation: https://docs.sourcify.dev/docs/api/#/Contract%20Lookup/get-contract
MethodGetContractByChainIdAndAddress = Method{
Name: "Get contract by chain id and address",
URI: "/v2/contract/:chain/:address",
MoreInfo: "https://docs.sourcify.dev/docs/api/#/Contract%20Lookup/get-contract",
Method: "GET",
ParamType: MethodParamTypeUriAndQueryString,
RequiredParams: []string{":chain", ":address"},
Params: []MethodParam{
{
Key: "fields",
Value: "",
},
{
Key: "omit",
Value: ""},
},
}
// MethodGetContractByChainId represents the API endpoint for listing all verified contracts for a specific chain.
// It returns all verified contract addresses and basic details for the specified chain ID.
// HTTP Method: GET
// URI: /v2/contracts/:chain
// Documentation: https://docs.sourcify.dev/docs/api/#/Contract%20Lookup/get-v2-contracts-chainId
MethodGetContractByChainId = Method{
Name: "List through all contracts for the chain",
URI: "/v2/contracts/:chain",
MoreInfo: "https://docs.sourcify.dev/docs/api/#/Contract%20Lookup/get-v2-contracts-chainId",
Method: "GET",
ParamType: MethodParamTypeUriAndQueryString,
RequiredParams: []string{":chain"},
Params: []MethodParam{
{
Key: "fields",
Value: "",
},
},
}
)
// ContractResponse represents the detailed response from the Sourcify API when retrieving complete contract information.
// Contains full contract data including ABI, bytecode, sources, and metadata.
type ContractResponse struct {
Abi []ABIEntry `json:"abi"`
Address string `json:"address"`
ChainID string `json:"chainId"`
Compilation Compilation `json:"compilation"`
CreationBytecode Bytecode `json:"creationBytecode"`
CreationMatch string `json:"creationMatch"`
Deployment Deployment `json:"deployment"`
DevDoc DevDoc `json:"devdoc"`
Match string `json:"match"`
MatchID string `json:"matchId"`
Metadata Metadata `json:"metadata"`
ProxyResolution ProxyResolution `json:"proxyResolution"`
RuntimeBytecode Bytecode `json:"runtimeBytecode"`
RuntimeMatch string `json:"runtimeMatch"`
SourceIds SourceIds `json:"sourceIds"`
Sources Sources `json:"sources"`
StdJSONInput StdJSONInput `json:"stdJsonInput"`
StdJSONOutput StdJSONOutput `json:"stdJsonOutput"`
StorageLayout *StorageLayout `json:"storageLayout"`
UserDoc UserDoc `json:"userdoc"`
VerifiedAt time.Time `json:"verifiedAt"`
}
// ContractBaseResponse represents the basic response from the Sourcify API when retrieving contract information.
// Contains only essential contract verification details without the full source code or ABI.
type ContractBaseResponse struct {
Address string `json:"address"`
ChainID string `json:"chainId"`
CreationMatch string `json:"creationMatch"`
Match string `json:"match"`
MatchID string `json:"matchId"`
RuntimeMatch string `json:"runtimeMatch"`
VerifiedAt time.Time `json:"verifiedAt"`
}
// ContractsResponse wraps a collection of ContractBaseResponse objects returned when listing multiple contracts.
type ContractsResponse struct {
Results []ContractBaseResponse `json:"results"`
}
// GetContractsByChainId retrieves a paginated list of verified contract addresses for the given chain ID.
// Parameters:
// - client: The Sourcify API client
// - chainId: The blockchain network ID
// - sort: Sorting option for results
// - afterMatchId: Pagination parameter; returns results after this match ID
// - limit: Maximum number of results to return
// Returns a ContractsResponse containing basic information about each contract or an error.
func GetContractsByChainId(client *Client, chainId int, sort string, afterMatchId string, limit int) (*ContractsResponse, error) {
method := MethodGetContractByChainId
method.SetParams(
MethodParam{Key: ":chain", Value: chainId},
MethodParam{Key: "sort", Value: sort},
MethodParam{Key: "afterMatchId", Value: afterMatchId},
MethodParam{Key: "limit", Value: limit},
)
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 {
if rErr := ToErrorResponse(response); rErr != nil {
return nil, rErr
}
return nil, fmt.Errorf("unexpected status code: %d", statusCode)
}
var toReturn *ContractsResponse
if jdErr := json.NewDecoder(response).Decode(&toReturn); jdErr != nil {
return nil, jdErr
}
return toReturn, nil
}
// GetContractByChainIdAndAddress retrieves the complete details of a verified contract by its chain ID and address.
// Parameters:
// - client: The Sourcify API client
// - chainId: The blockchain network ID
// - address: The Ethereum contract address
// - fields: Specific fields to include in the response (use []string{"all"} for complete data)
// - omit: Fields to exclude from the response
// Note: fields and omit parameters are mutually exclusive; if both are empty, fields defaults to ["all"].
// Returns a ContractResponse containing detailed contract information or an error.
func GetContractByChainIdAndAddress(client *Client, chainId int, address common.Address, fields []string, omit []string) (*ContractResponse, error) {
method := MethodGetContractByChainIdAndAddress
// Omit and fields cannot co-exist together
if len(omit) == 0 && len(fields) == 0 {
fields = []string{"all"}
}
pFields := strings.Join(fields, ",")
pOmit := strings.Join(omit, ",")
method.SetParams(
MethodParam{Key: ":chain", Value: chainId},
MethodParam{Key: ":address", Value: address.Hex()},
MethodParam{Key: "fields", Value: pFields},
MethodParam{Key: "omit", Value: pOmit},
)
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 {
if rErr := ToErrorResponse(response); rErr != nil {
return nil, rErr
}
return nil, fmt.Errorf("unexpected status code: %d", statusCode)
}
var toReturn ContractResponse
if jdErr := json.NewDecoder(response).Decode(&toReturn); jdErr != nil {
return nil, jdErr
}
return &toReturn, nil
}