Skip to content

Commit

Permalink
Fixing up the examples package
Browse files Browse the repository at this point in the history
  • Loading branch information
0x19 committed Jun 20, 2023
1 parent 5408482 commit c0aebc4
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 7 deletions.
4 changes: 2 additions & 2 deletions examples/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/txpull/sourcify-go"
)

// Example_GetChains demonstrates how to retrieve chains using the Sourcify client.
func Example_GetChains() {
// Create a custom HTTP client with timeout
httpClient := &http.Client{
Expand All @@ -24,13 +25,12 @@ func Example_GetChains() {
),
)

// Call the API method
// Call get chains to retrieve all chains from Sourcify
chains, err := sourcify.GetChains(client)
if err != nil {
panic(err)
}

// Process the response
for _, chain := range chains {
fmt.Printf("Chain: %+v\n\n", chain)
}
Expand Down
5 changes: 5 additions & 0 deletions examples/check_addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/txpull/sourcify-go"
)

// Example_CheckAddresses demonstrates how to check contract addresses using the Sourcify client.
func Example_CheckAddresses() {
// Create a custom HTTP client with timeout
httpClient := &http.Client{
Expand All @@ -24,21 +25,25 @@ func Example_CheckAddresses() {
),
)

// Define contract addresses to check
contractAddresses := []string{
"0x054B2223509D430269a31De4AE2f335890be5C8F",
}

// Define chain IDs to check
chainIds := []int{
56, // Binance Smart Chain
1, // Ethereum Mainnet
137, // Polygon
}

// Call the API method to check contract addresses
checks, err := sourcify.CheckContractByAddresses(client, contractAddresses, chainIds, sourcify.MethodMatchTypeAny)
if err != nil {
panic(err)
}

// Process the response
for _, check := range checks {
fmt.Printf("Contract Addresses Check Response: %+v\n", check)
}
Expand Down
11 changes: 11 additions & 0 deletions examples/examples.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Package examples provides example code snippets demonstrating the usage of the Sourcify Go client.
//
// These examples showcase various API methods and illustrate how to interact with the Sourcify server
// to retrieve contract information, source code, metadata, file trees, and more.
//
// The examples demonstrate the configuration of a custom HTTP client, setting base URLs, retry options,
// and making API calls to retrieve data from the Sourcify server.
//
// Each example function provides a self-contained code snippet that can be executed independently.
// Simply copy the desired example function and execute it in your Go environment to see the output.
package examples
6 changes: 5 additions & 1 deletion examples/get_all_addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/txpull/sourcify-go"
)

// Example_GetAllAddresses demonstrates how to retrieve all available contract addresses using the Sourcify client.
func Example_GetAllAddresses() {
// Create a custom HTTP client with timeout
httpClient := &http.Client{
Expand All @@ -24,12 +25,15 @@ func Example_GetAllAddresses() {
),
)

// Call the API method
// Call the API method to retrieve all available contract addresses
addresses, err := sourcify.GetAvailableContractAddresses(client, 56)
if err != nil {
panic(err)
}

// Print the number of full match addresses
fmt.Printf("Full Match Addresses: %d\n", len(addresses.Full))

// Print the number of partial match addresses
fmt.Printf("Partial Match Addresses: %d\n\n", len(addresses.Partial))
}
5 changes: 4 additions & 1 deletion examples/get_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/txpull/sourcify-go"
)

// Example_GetFiles demonstrates how to retrieve source files for a contract using the Sourcify client.
func Example_GetFiles() {
// Create a custom HTTP client with timeout
httpClient := &http.Client{
Expand All @@ -25,14 +26,16 @@ func Example_GetFiles() {
),
)

// Get files for the Binance Smart Chain with the address of the R3T contract
// Get source files for the Binance Smart Chain with the address of the R3T contract
files, err := sourcify.GetContractFiles(client, 56, common.HexToAddress("0x054B2223509D430269a31De4AE2f335890be5C8F"), sourcify.MethodMatchTypeAny)
if err != nil {
panic(err)
}

// Print the status of the response
fmt.Printf("Status: %+v\n", files.Status)

// Print the paths of the retrieved files
for _, file := range files.Files {
fmt.Printf("Path: %+v\n", file)
}
Expand Down
2 changes: 2 additions & 0 deletions examples/get_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/txpull/sourcify-go"
)

// Example_GetMetadata demonstrates how to retrieve full metadata for a contract using the Sourcify client.
func Example_GetMetadata() {
// Create a custom HTTP client with timeout
httpClient := &http.Client{
Expand All @@ -31,5 +32,6 @@ func Example_GetMetadata() {
panic(err)
}

// Print the full match metadata
fmt.Printf("Full Match Metadata: %+v\n", fullMetadata)
}
5 changes: 4 additions & 1 deletion examples/get_source_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/txpull/sourcify-go"
)

// Example_GetSourceCode demonstrates how to retrieve source code for a contract using the Sourcify client.
func Example_GetSourceCode() {
// Create a custom HTTP client with timeout
httpClient := &http.Client{
Expand All @@ -25,14 +26,16 @@ func Example_GetSourceCode() {
),
)

// Get full metadata for the Binance Smart Chain with the address of the R3T contract
// Get source code for the Binance Smart Chain with the address of the R3T contract
sources, err := sourcify.GetContractSourceCode(client, 56, common.HexToAddress("0x054B2223509D430269a31De4AE2f335890be5C8F"), sourcify.MethodMatchTypeAny)
if err != nil {
panic(err)
}

// Print the status of the source code retrieval
fmt.Printf("Status: %+v\n", sources.Status)

// Iterate over the source code files and print their names and paths
for _, source := range sources.Code {
fmt.Printf("Name: %+v\n", source.Name)
fmt.Printf("Path: %+v\n", source.Path)
Expand Down
5 changes: 3 additions & 2 deletions examples/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/txpull/sourcify-go"
)

// Example_GetHealth demonstrates how to check the health status of the Sourcify server using the Sourcify client.
func Example_GetHealth() {
// Create a custom HTTP client with timeout
httpClient := &http.Client{
Expand All @@ -24,12 +25,12 @@ func Example_GetHealth() {
),
)

// Call the API method
// Call the API method to get the health status
status, err := sourcify.GetHealth(client)
if err != nil {
panic(err)
}

// Process the response
// Print the server's health status
fmt.Printf("Is server alive and ready to receive requests: %v\n", status)
}

0 comments on commit c0aebc4

Please sign in to comment.