FetchGitHubProfile Contract
The FetchGitHubProfile contract is a GenLayer Intelligent Contract example that fetches a GitHub profile page and stores its content in contract state. The example shows how to use the comparative Equivalence Principle so all nodes agree on the same profile content.
# { "Depends": "py-genlayer:1jb45aa8ynh2a9c9xn3b7qqh8sm5q93hwfp7jqmwsfhh8jpz09h6" }
from genlayer import *
import typing
class FetchGitHubProfile(gl.Contract):
github_profile: str
def __init__(self):
self.github_profile = ""
@gl.public.write
def fetch_github_profile(self, github_handle: str) -> typing.Any:
github_profile_url = "https://github.com/"+github_handle
def fetch_github_profile_page_content() -> str:
response = gl.nondet.web.get(github_profile_url)
return response.body.decode("utf-8")
self.github_profile = gl.eq_principle.strict_eq(fetch_github_profile_page_content)
@gl.public.view
def show_github_profile(self) -> str:
return self.github_profileCode Explanation
- Initialization: The
FetchGitHubProfileclass initializes with an empty string in thegithub_profilevariable. - Write Method:
fetch_github_profile(github_handle)takes a GitHub username and retrieves their profile content.- Constructs the profile URL using the provided handle.
- Uses
gl.eq_principle.strict_eq()to ensure all nodes agree on the same profile content.
- Read Method:
show_github_profile()returns the stored profile content.
Key Components
- GitHub Integration: The contract uses
gl.nondet.web.get()to fetch content from GitHub profiles. - Deterministic Execution:
gl.eq_principle.strict_eq()ensures that all nodes in the network arrive at the same exact content. - State Management: The contract maintains a single string state variable that stores the profile content.
Deploying the Contract
To deploy the FetchGitHubProfile contract:
- Deploy the Contract: No initial parameters are needed for deployment.
- The contract will initialize with an empty profile string.
Checking the Contract State
After deployment, you can:
- Use
show_github_profile()to view the currently stored profile content. - Initially, this will return an empty string.
Executing Transactions
To interact with the deployed contract:
- Call
fetch_github_profile(github_handle)with a GitHub username. - The function will:
- Construct the GitHub profile URL
- Fetch the profile content
- Store the result using the equivalence principle
- Make the content available through
show_github_profile()
Understanding GitHub Integration
This contract demonstrates several important concepts:
- Dynamic URLs: Shows how to construct URLs based on input parameters.
- Web Fetching: Demonstrates safe retrieval of content from GitHub.
- Deterministic Results: Uses the equivalence principle to ensure all nodes reach consensus on profile content.
- State Updates: Shows how external web content can be stored in blockchain state.
Handling Different Scenarios
- Initial State: The profile content starts empty.
- Valid GitHub Handle: The content will contain the text from the GitHub profile page.
- Invalid Handle: May result in a 404 page content or error.
- Network Consensus: All nodes will agree on the same content due to the equivalence principle.
Important Notes
- This example fetches public GitHub profile pages only.
- Profile content may change over time.
- The equivalence principle ensures that all nodes store identical content.
- The
mode="text"parameter ensures only text content is retrieved.
Security Considerations
- Be aware that GitHub profiles are dynamic and can change.
- Consider implementing rate limiting to respect GitHub's terms of service.
- Handle potential errors for non-existent profiles.
- Be mindful of GitHub's robots.txt and usage policies.
You can monitor the contract's behavior through transaction logs, which will show the fetched profile content and state updates as they occur.