Categorygithub.com/unpackdev/sourcify-go
modulepackage
1.0.3
Repository: https://github.com/unpackdev/sourcify-go.git
Documentation: pkg.go.dev

# README

Build Status Coverage Status License PkgGoDev

Sourcify Go

Sourcify Go is a Golang package that provides tools for interacting with the Sourcify API.

It allows you to access various API endpoints and perform operations such as checking server status, retrieving chains information, obtaining contract information from supported chains and more.

Installation

To use Sourcify in your Go project, you can simply import the package:

import "github.com/unpackdev/sourcify-go"

Usage

Creating a Client

To interact with the Sourcify API, you need to create a client using the NewClient function. You can provide optional configuration options using ClientOption functions. For example, you can specify a custom base URL or a custom HTTP client, set retry configuration in case sourcify servers are temporairly unavailable and set rate limits in order to control the rate at which HTTP requests are sent to the sourcify servers.

client := sourcify.NewClient(
	sourcify.WithHTTPClient(httpClient),
	sourcify.WithBaseURL("https://sourcify.dev/server"),
	sourcify.WithRetryOptions(
		sourcify.WithMaxRetries(3),
		sourcify.WithDelay(2*time.Second),
	),
	sourcify.WithRateLimit(10, 1*time.Second),
)

Calling Raw API Endpoints

Sourcify provides various API endpoints as Method objects. You can call these endpoints using the CallMethod function on the client and do your own method parsers if you wish to.

However, if there are methods that we do not provide yet, you can do something like this to extend package.

customMethod := sourcify.Method{...}

customMethod.SetParams(
	MethodParam{Key: ":chain", Value: chainId},
)

if err := customMethod.Verify(); err != nil {
	return nil, err
}

response, statusCode, err := client.CallMethod(customMethod)
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()

// Process the response

Supported API Endpoints

Sourcify provides the following API endpoints that you can call that are currently supported by this package:

  • MethodHealth: Check the server status. More information
  • MethodGetChains: Retrieve the chains (networks) added to Sourcify. More information
  • MethodCheckByAddresses: Check if contracts with the desired chain and addresses are verified and in the repository. More information
  • MethodCheckAllByAddresses: Check if contracts with the desired chain and addresses are verified and in the repository. More information
  • MethodGetFileTreeFullOrPartialMatch: Get the file tree with full or partial match for the desired chain and address. More information
  • MethodGetFileTreeFullMatch: Get the file tree with full match for the desired chain and address. More information
  • MethodSourceFilesFullOrPartialMatch: Get the source files with full or partial match for the desired chain and address, including metadata.json. More information
  • MethodSourceFilesFullMatch: Get the source files with full match for the desired chain and address, including metadata.json. More information
  • MethodGetContractAddressesFullOrPartialMatch: Get the verified contract addresses for the chain with full or partial match. More information
  • MethodGetFileFromRepositoryFullMatch: Retrieve statically served files over the server for full match contract. More information
  • MethodGetFileFromRepositoryPartialMatch: Retrieve statically served files over the server for partial match contract. More information

For more information on each endpoint, including the parameters they require and the expected responses, refer to the Sourcify API documentation.

Examples

You can find endpoint examples under the examples directory.

Contributing

Contributions to Sourcify are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on GitHub.

License

Sourcify is released under the Apache 2.0 License. See the LICENSE file for more details.

# Packages

Package examples provides example code snippets demonstrating the usage of the Sourcify Go client.

# Functions

CheckContractByAddresses retrieves the available verified contract addresses for the given chain ID.
GetAvailableContractAddresses retrieves the available verified contract addresses for the given chain ID.
GetChains gets the chains (networks) added to Sourcify by calling the MethodGetChains endpoint using the provided client.
GetContractFiles retrieves the repository URLs for every file in the source tree for the given chain ID and contract address.
GetContractMetadata fetches the metadata of a contract from a given client, chain ID, contract address, and match type.
GetContractMetadataAsBytes retrieves the metadata of a smart contract as a byte slice.
GetContractSourceCode retrieves the source code files for a contract with the given chain ID and address, based on the match type.
GetHealth checks the server status by calling the MethodHealth endpoint using the provided client.
NewClient initializes a new Sourcify client with optional configurations.
NewRateLimiter creates a new rate limiter.
WithBaseURL allows you to provide your own base URL for the Sourcify client.
WithDelay sets the delay between retries.
WithHTTPClient allows you to provide your own http.Client for the Sourcify client.
WithMaxRetries sets the maximum number of retries.
WithRateLimit allows you to configure rate limits for the Sourcify client.
WithRetryOptions allows you to configure retry settings for the Sourcify client.

# Constants

MethodMatchTypePartial denotes the type of match which is partial.
MethodMatchTypeFull denotes the type of match which is full.
MethodMatchTypePartial denotes the type of match which is partial.
1.
0.

# Variables

ErrInvalidParamType represents an error when a parameter of an invalid type is encountered.
MethodCheckAllByAddresses represents the API endpoint for checking all addresses in the Sourcify service.
MethodCheckByAddresses represents the API endpoint for checking by addresses in the Sourcify service.
MethodGetChains represents the API endpoint for getting the chains (networks) added to Sourcify in the Sourcify service.
MethodGetContractAddressesFullOrPartialMatch represents the API endpoint for getting the contract addresses with full or partial match in the Sourcify service.
MethodGetFileFromRepositoryFullMatch represents the API endpoint for retrieving staticly served files over the server for full match contract in the Sourcify service.
MethodGetFileFromRepositoryPartialMatch represents the API endpoint for retrieving staticly served files over the server for partial match contract in the Sourcify service.
MethodGetFileTreeFullMatch represents the API endpoint for getting the file tree with full match in the Sourcify service.
MethodGetFileTreeFullOrPartialMatch represents the API endpoint for getting the file tree with full or partial match in the Sourcify service.
MethodHealth represents the API endpoint for checking the server status in the Sourcify service.
MethodSourceFilesFullMatch represents the API endpoint for getting the source files with full match in the Sourcify service.
MethodSourceFilesFullOrPartialMatch represents the API endpoint for getting the source files with full or partial match in the Sourcify service.

# Structs

Abi holds the Application Binary Interface (ABI) of the compiled code.
Chain represents a chain (network) added to Sourcify.
ChainEns represents the ENS (Ethereum Name Service) configuration of a chain.
ChainExplorer represents an explorer configuration for a chain.
ChainFeature represents a feature of a chain.
ChainNativeCurrency represents the native currency of a chain.
ChainParent represents the parent chain information for a chain.
CheckContractAddress represents the contract address and associated chain IDs and statuses.
CheckContractAddressMore represents the contract address and associated chain IDs and statuses.
CheckContractAddressMoreInfo represents the contract address and associated chain IDs and statuses.
No description provided by the author
Compiler provides information about the compiler used for the smart contract.
Devdoc provides details about the developer documentation.
DevMethod contains information about a method in the developer documentation.
FileTree represents the file tree response from the Sourcify service.
Libraries represent the libraries used in the source code.
Metadata represents the top-level structure for compiler metadata for Ethereum smart contracts.
MetadataDetail provides additional metadata.
Method represents an API endpoint in the Sourcify service.
MethodParam represents a parameter key-value pair.
Optimizer contains details about the compiler optimization settings.
Output contains details about the output of the compiled code.
OutputDetail holds information about the output parameters of the functions.
RateLimiter represents a rate limiter that controls the rate of actions using the token bucket algorithm.
RetryOptions represents options for configuring retry settings.
Settings includes details about the compiler settings used.
SourceCode represents the source code details for a file.
SourceCodes represents the source code details for multiple files.
SourceDetails holds the details of the main contract source code.
Userdoc provides information about the user documentation.
VerifiedContractAddresses represents the structure for the verified contract addresses response.

# Type aliases

No description provided by the author
CompilationTarget holds the details of the compilation target.
No description provided by the author
No description provided by the author
RetryOption sets a configuration option for retry settings.
Sources provides details about the source code.