-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding tree methods including example and unit test
- Loading branch information
Showing
3 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package examples | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/txpull/sourcify-go" | ||
) | ||
|
||
func Example_GetFiles() { | ||
// Create a custom HTTP client with timeout | ||
httpClient := &http.Client{ | ||
Timeout: 30 * time.Second, | ||
} | ||
|
||
// Create a new Sourcify client with custom options | ||
client := sourcify.NewClient( | ||
sourcify.WithHTTPClient(httpClient), | ||
sourcify.WithBaseURL("https://sourcify.dev/server"), | ||
sourcify.WithRetryOptions( | ||
sourcify.WithMaxRetries(3), | ||
sourcify.WithDelay(2*time.Second), | ||
), | ||
) | ||
|
||
// Get 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) | ||
} | ||
|
||
fmt.Printf("Status: %+v\n", files.Status) | ||
|
||
for _, file := range files.Files { | ||
fmt.Printf("Path: %+v\n", file) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package sourcify | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetContractFiles(t *testing.T) { | ||
// Create a mock HTTP server | ||
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.URL.Path == "/files/tree/any/1/0x0000000000000000000000001234567890aBcdEF" { | ||
// Simulate a successful response with sample file tree | ||
fileTree := &FileTree{ | ||
Status: "success", | ||
Files: []string{"/path/to/file1.sol", "/path/to/file2.sol"}, | ||
} | ||
|
||
err := json.NewEncoder(w).Encode(fileTree) | ||
if err != nil { | ||
t.Errorf("failed to encode mock file tree: %v", err) | ||
} | ||
} else { | ||
http.NotFound(w, r) | ||
} | ||
})) | ||
defer mockServer.Close() | ||
|
||
// Create a client for the mock server | ||
client := NewClient(WithBaseURL(mockServer.URL)) | ||
|
||
// Define test data | ||
chainID := 1 | ||
contractAddress := common.HexToAddress("0x1234567890abcdef") | ||
matchType := MethodMatchTypeAny | ||
|
||
// Call the function to get contract files | ||
fileTree, err := GetContractFiles(client, chainID, contractAddress, matchType) | ||
|
||
// Verify the results | ||
assert.NoError(t, err, "GetContractFiles returned an error") | ||
|
||
expectedFileTree := &FileTree{ | ||
Status: "success", | ||
Files: []string{"/path/to/file1.sol", "/path/to/file2.sol"}, | ||
} | ||
|
||
assert.Equal(t, expectedFileTree, fileTree, "GetContractFiles returned unexpected file tree") | ||
} | ||
|
||
func TestGetContractFiles_Error(t *testing.T) { | ||
// Create a mock HTTP server that always returns 404 Not Found | ||
mockServer := httptest.NewServer(http.NotFoundHandler()) | ||
defer mockServer.Close() | ||
|
||
// Create a client for the mock server | ||
client := NewClient(WithBaseURL(mockServer.URL)) | ||
|
||
// Define test data | ||
chainID := 1 | ||
contractAddress := common.HexToAddress("0x1234567890abcdef") | ||
matchType := MethodMatchTypeAny | ||
|
||
// Call the function to get contract files | ||
fileTree, err := GetContractFiles(client, chainID, contractAddress, matchType) | ||
|
||
// Verify the results | ||
assert.Error(t, err, "GetContractFiles should return an error") | ||
assert.Nil(t, fileTree, "File tree should be nil") | ||
} |