Created
March 5, 2025 22:52
-
-
Save ccerv1/5f8b0a778d9662836709536bec17f643 to your computer and use it in GitHub Desktop.
OP Atlas x OSO Test Queries
This file contains hidden or 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
| import requests | |
| import json | |
| from typing import Dict, Any, List | |
| from requests.exceptions import HTTPError | |
| import re | |
| class OsoClient: | |
| def __init__(self, endpoint: str = "https://www.opensource.observer/api/v1/graphql"): | |
| self.endpoint = endpoint | |
| self.headers = { | |
| "Content-Type": "application/json" | |
| } | |
| def execute_query(self, query: str, variables: Dict[str, Any] = None) -> Dict[str, Any]: | |
| """ | |
| Execute a GraphQL query against the OSO API. | |
| Args: | |
| query: The GraphQL query string | |
| variables: Optional variables for the query | |
| Returns: | |
| Dict containing the API response | |
| Raises: | |
| ValueError: If the query is invalid or the response contains errors | |
| """ | |
| # Clean up the query by removing extra whitespace and newlines | |
| query = re.sub(r'\s+', ' ', query).strip() | |
| payload = {"query": query} | |
| if variables: | |
| payload["variables"] = variables | |
| try: | |
| response = requests.post( | |
| self.endpoint, | |
| headers=self.headers, | |
| json=payload | |
| ) | |
| # Check for HTTP errors | |
| response.raise_for_status() | |
| # Parse the response | |
| result = response.json() | |
| # Check for GraphQL errors | |
| if "errors" in result: | |
| error_msg = "\n".join(error.get("message", "Unknown error") for error in result["errors"]) | |
| raise ValueError(f"GraphQL Error: {error_msg}") | |
| return result | |
| except HTTPError as e: | |
| if e.response.status_code == 400: | |
| try: | |
| error_data = e.response.json() | |
| error_msg = error_data.get("errors", [{}])[0].get("message", str(e)) | |
| raise ValueError(f"GraphQL Validation Error: {error_msg}") | |
| except json.JSONDecodeError: | |
| raise ValueError(f"Bad Request: {str(e)}") | |
| raise | |
| def main(): | |
| try: | |
| # Initialize the client | |
| client = OsoClient() | |
| # Example 1: Get a project by Atlas ID | |
| atlas_id = "0x08df6e20a3cfabbaf8f34d4f4d048fe7da40447c24be0f3ad513db6f13c755dd" | |
| result = client.execute_query( | |
| query=""" | |
| query GetAtlasProject($atlasId: String!) { | |
| oso_projectsV1(limit: 1, where: { projectName: { _eq: $atlasId } }) { | |
| projectId | |
| projectName | |
| description | |
| displayName | |
| projectNamespace | |
| projectSource | |
| } | |
| } | |
| """, | |
| variables={"atlasId": atlas_id} | |
| ) | |
| print("Project:", json.dumps(result, indent=2)) | |
| # Example 2: Get project artifacts (eg, GitHub, DefiLlama, etc) | |
| result = client.execute_query( | |
| query=""" | |
| query GetAtlasArtifacts($atlasId: String!) { | |
| oso_artifactsByProjectV1(where: { projectName: { _eq: $atlasId } }) { | |
| artifactId | |
| artifactName | |
| artifactSource | |
| } | |
| } | |
| """, | |
| variables={"atlasId": atlas_id} | |
| ) | |
| print("Artifacts:", json.dumps(result, indent=2)) | |
| # Example 3: Get metric IDs | |
| result = client.execute_query( | |
| query=""" | |
| query GetMetricIds { | |
| oso_metricsV0 { | |
| metricId | |
| metricName | |
| } | |
| } | |
| """ | |
| ) | |
| print("Metrics:", json.dumps(result, indent=2)) | |
| # Example 4: Get timeseries metrics for a project over a date range | |
| metric_ids = [ | |
| "7PEA33NdLXZ97ichlOnybqpjNAFz84fYFp+Gm7OgGw4=", # transactions | |
| "UtO9UyVu2lYYPzCjiBlIvXGxY+QNVyi+jPHYzVtBoEs=", # gas fees | |
| "+5rLhj0Pg2P/g3AVc2Y7Rvqb90r8SMl+wW3gxFUlejE=" # active addresses | |
| ] | |
| project_id = "H1DdvseIeFYJUwYwfSNvsXvbgxfwasspZw2MT3Apkfg=" | |
| result = client.execute_query( | |
| query=""" | |
| query GetTimeseriesMetrics( | |
| $projectId: String!, | |
| $metricIds: [String!]!, | |
| $startDate: Oso_Date!, | |
| $endDate: Oso_Date! | |
| ) { | |
| oso_timeseriesMetricsByProjectV0( | |
| where: { | |
| projectId: { _eq: $projectId }, | |
| sampleDate: { _gte: $startDate, _lte: $endDate }, | |
| metricId: { _in: $metricIds } | |
| } | |
| ) { | |
| metricId | |
| sampleDate | |
| amount | |
| } | |
| } | |
| """, | |
| variables={ | |
| "projectId": project_id, | |
| "metricIds": metric_ids, | |
| "startDate": "2025-01-01", | |
| "endDate": "2025-02-28" | |
| } | |
| ) | |
| print("Timeseries Metrics:", json.dumps(result, indent=2)) | |
| # Example 5: Get metrics for a specific date (eg, end of a measurement period) | |
| result = client.execute_query( | |
| query=""" | |
| query GetTimeseriesMetricsByDate( | |
| $projectId: String!, | |
| $date: Oso_Date! | |
| ) { | |
| oso_timeseriesMetricsByProjectV0( | |
| where: { | |
| projectId: { _eq: $projectId }, | |
| sampleDate: { _eq: $date } | |
| } | |
| ) { | |
| metricId | |
| sampleDate | |
| amount | |
| } | |
| } | |
| """, | |
| variables={ | |
| "projectId": project_id, | |
| "date": "2025-02-28" | |
| } | |
| ) | |
| print("Metrics by Date:", json.dumps(result, indent=2)) | |
| # Example 6: Get transaction metrics | |
| result = client.execute_query( | |
| query=""" | |
| query GetTransactionMetrics( | |
| $projectId: String!, | |
| $startDate: Oso_Date!, | |
| $endDate: Oso_Date! | |
| ) { | |
| oso_timeseriesMetricsByProjectV0( | |
| where: { | |
| projectId: { _eq: $projectId }, | |
| sampleDate: { _gte: $startDate, _lte: $endDate }, | |
| metricId: { _eq: "7PEA33NdLXZ97ichlOnybqpjNAFz84fYFp+Gm7OgGw4=" } | |
| } | |
| ) { | |
| metricId | |
| sampleDate | |
| amount | |
| } | |
| } | |
| """, | |
| variables={ | |
| "projectId": project_id, | |
| "startDate": "2025-01-01", | |
| "endDate": "2025-02-28" | |
| } | |
| ) | |
| print("Transaction Metrics:", json.dumps(result, indent=2)) | |
| # Example 7: Get active addresses metrics | |
| result = client.execute_query( | |
| query=""" | |
| query GetActiveAddressesMetrics( | |
| $projectId: String!, | |
| $startDate: Oso_Date!, | |
| $endDate: Oso_Date! | |
| ) { | |
| oso_timeseriesMetricsByProjectV0( | |
| where: { | |
| projectId: { _eq: $projectId }, | |
| sampleDate: { _gte: $startDate, _lte: $endDate }, | |
| metricId: { _eq: "+5rLhj0Pg2P/g3AVc2Y7Rvqb90r8SMl+wW3gxFUlejE=" } | |
| } | |
| ) { | |
| metricId | |
| sampleDate | |
| amount | |
| } | |
| } | |
| """, | |
| variables={ | |
| "projectId": project_id, | |
| "startDate": "2025-01-01", | |
| "endDate": "2025-02-28" | |
| } | |
| ) | |
| print("Active Addresses:", json.dumps(result, indent=2)) | |
| # Example 8: Get gas fees metrics | |
| result = client.execute_query( | |
| query=""" | |
| query GetGasFeesMetrics( | |
| $projectId: String!, | |
| $startDate: Oso_Date!, | |
| $endDate: Oso_Date! | |
| ) { | |
| oso_timeseriesMetricsByProjectV0( | |
| where: { | |
| projectId: { _eq: $projectId }, | |
| sampleDate: { _gte: $startDate, _lte: $endDate }, | |
| metricId: { _eq: "UtO9UyVu2lYYPzCjiBlIvXGxY+QNVyi+jPHYzVtBoEs=" } | |
| } | |
| ) { | |
| metricId | |
| sampleDate | |
| amount | |
| } | |
| } | |
| """, | |
| variables={ | |
| "projectId": project_id, | |
| "startDate": "2025-01-01", | |
| "endDate": "2025-02-28" | |
| } | |
| ) | |
| print("Gas Fees:", json.dumps(result, indent=2)) | |
| # Example 9: Look up the onchain projects that depend on a devtooling project | |
| # This is not working yet | |
| result = client.execute_query( | |
| query=""" | |
| query GetDependentProjects($projectId: String!) { | |
| oso_dependenciesV0(where: { toProjectId: { _eq: $projectId } }) { | |
| toProjectId | |
| fromProjectId | |
| dependencyType | |
| } | |
| } | |
| """, | |
| variables={"projectId": project_id} | |
| ) | |
| print("Dependent Projects:", json.dumps(result, indent=2)) | |
| except ValueError as e: | |
| print(f"Error: {str(e)}") | |
| except Exception as e: | |
| print(f"Unexpected error: {str(e)}") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment