{"id":24995391,"url":"https://github.com/compolabs/spark-envio-indexer","last_synced_at":"2025-04-12T04:11:11.918Z","repository":{"id":242069956,"uuid":"808517022","full_name":"compolabs/spark-envio-indexer","owner":"compolabs","description":null,"archived":false,"fork":false,"pushed_at":"2025-03-26T09:18:09.000Z","size":700,"stargazers_count":3,"open_issues_count":1,"forks_count":2,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-02T03:49:10.943Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/compolabs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-31T08:30:03.000Z","updated_at":"2025-03-18T08:45:36.000Z","dependencies_parsed_at":"2024-06-19T19:58:13.178Z","dependency_job_id":"e82b35e3-0dc7-48a8-ae31-cf709f906f71","html_url":"https://github.com/compolabs/spark-envio-indexer","commit_stats":null,"previous_names":["compolabs/spark-envio-indexer"],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/compolabs%2Fspark-envio-indexer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/compolabs%2Fspark-envio-indexer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/compolabs%2Fspark-envio-indexer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/compolabs%2Fspark-envio-indexer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/compolabs","download_url":"https://codeload.github.com/compolabs/spark-envio-indexer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248514221,"owners_count":21116903,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-04T15:37:37.020Z","updated_at":"2025-04-12T04:11:11.889Z","avatar_url":"https://github.com/compolabs.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Spark OrderBook indexer\n\nContract: https://github.com/compolabs/orderbook-contract/tree/master/market-contract\n\n_Please refer to the [documentation website](https://docs.envio.dev) for a thorough guide on all Envio indexer features_\n\n## Local usage\n\n1. Clone the repository\n\n   ```sh\n   git clone git@github.com:compolabs/spark-envio-indexer.git\n   ```\n\n2. Open it locally\n\n   ```sh\n   cd spark-envio-indexer\n   ```\n\n3. Install dependencies (requires [pnpm@8](https://pnpm.io/))\n\n   ```sh\n   pnpm i\n   ```\n\n4. Run envio codegen\n\n   ```sh\n   pnpm envio codegen\n   ```\n\n5. Run envio\n\n   ```sh\n   pnpm envio dev\n   ```\n\n6. Verify it's working correctly by checking the Hasura:\n   1. Open http://localhost:8080\n   2. Enter admin-secret `testing`\n\n7. Query the Indexer\n\n```\nquery MyQuery {\n  Order(where: {status: {_eq: \"Active\"}}) {\n    id\n    initial_amount\n    status\n    price\n    amount\n    order_type\n  }\n}\n```\n\nThis query will return a list of orders with the status \"Active\", including their id, initial_amount, status, price, amount, and order_type.\n\n```\nquery MyQuery {\n  Order(where: {user: {_eq: \"\"}}) {\n    id\n    initial_amount\n    status\n    price\n    amount\n    order_type\n    asset_type\n  }\n}\n```\n\nThis query fetches orders filtered by user, returning their id, initial_amount, status, price, amount, order_type, and asset_type.\n\n```\nquery MyQuery {\n  MatchOrderEvent(where: {owner: {_eq: \"\"}}) {\n    id\n    asset\n    counterparty\n    match_price\n    match_size\n    order_id\n    order_matcher\n    owner\n    timestamp\n    tx_id\n  }\n}\n```\n\nThis query fetches match order events based on the owner, including details such as id, asset, counterparty, match_price, match_size, order_id, order_matcher, owner, timestamp, and tx_id.\n\n## Using WebSockets in Rust and TypeScript\n\nTo interact with the OrderBook Indexer via WebSockets, you can use different libraries and approaches depending on the programming language. Below are examples for Rust and TypeScript.\n\n1. Rust\n\nIn Rust, you can use the tokio-tungstenite library for working with WebSockets. This example demonstrates how to establish a connection to the server and send a request.\n- Add the dependencies to your Cargo.toml:\n\n```\n[dependencies]\ntokio = { version = \"1\", features = [\"full\"] }\ntokio-tungstenite = \"0.15\"\nserde_json = \"1.0\"\n\n```\n- Example code for a WebSocket client in Rust:\n\n```\nuse tokio_tungstenite::connect_async;\nuse tokio_tungstenite::tungstenite::protocol::Message;\nuse futures_util::{StreamExt, SinkExt};\nuse serde_json::json;\n\n#[tokio::main]\nasync fn main() {\n    // WebSocket server URL\n    let url = \"ws://localhost:8080/v1/graphql\";\n\n    // Establish the connection\n    let (mut ws_stream, _) = connect_async(url).await.expect(\"Failed to connect\");\n\n    println!(\"WebSocket connected\");\n\n    // Create a GraphQL subscription request\n    let request = json!({\n        \"type\": \"start\",\n        \"id\": \"1\",\n        \"payload\": {\n            \"query\": r#\"\n                subscription {\n                    Order(where: {status: {_eq: \"Active\"}}) {\n                        id\n                        initial_amount\n                        status\n                        price\n                        amount\n                        order_type\n                    }\n                }\n            \"#\n        }\n    });\n\n    // Send the request to the server\n    ws_stream.send(Message::Text(request.to_string())).await.expect(\"Failed to send message\");\n\n    // Listen for responses from the server\n    while let Some(message) = ws_stream.next().await {\n        let msg = message.expect(\"Failed to read message\");\n        if msg.is_text() {\n            println!(\"Received: {}\", msg.to_text().unwrap());\n        }\n    }\n}\n```\n\nThis example establishes a WebSocket connection to the server and sends a subscription request for orders with the status \"Active.\" The server responses will be printed to the console.\n\n2. TypeScript\n\nIn TypeScript, you can use the standard WebSocket API or libraries like apollo-client for subscriptions via GraphQL.\n\n- Install the necessary packages:\n\n```\npnpm add apollo-client @apollo/client subscriptions-transport-ws graphql\n```\n\n- Example code for a WebSocket client in TypeScript:\n\n```\nimport { WebSocketLink } from '@apollo/client/link/ws';\nimport { ApolloClient, InMemoryCache } from '@apollo/client';\nimport { gql } from 'graphql-tag';\n\n// Establish the WebSocket connection\nconst wsLink = new WebSocketLink({\n  uri: 'ws://localhost:8080/v1/graphql',\n  options: {\n    reconnect: true,\n  },\n});\n\nconst client = new ApolloClient({\n  link: wsLink,\n  cache: new InMemoryCache(),\n});\n\n// GraphQL subscription\nconst ORDER_SUBSCRIPTION = gql`\n  subscription {\n    Order(where: {status: {_eq: \"Active\"}}) {\n      id\n      initial_amount\n      status\n      price\n      amount\n      order_type\n    }\n  }\n`;\n\n// Subscribe to changes\nclient.subscribe({ query: ORDER_SUBSCRIPTION }).subscribe({\n  next(response) {\n    console.log('Received:', response.data);\n  },\n  error(err) {\n    console.error('Error:', err);\n  },\n});\n```\n\nThis example uses Apollo Client to establish a WebSocket connection and subscribe to order updates. The received data is logged to the console.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcompolabs%2Fspark-envio-indexer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcompolabs%2Fspark-envio-indexer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcompolabs%2Fspark-envio-indexer/lists"}