Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
353 changes: 263 additions & 90 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# hypersync-client-python

Python package for [Envio's](https://envio.dev/) HyperSync client written in Rust

## Setup
Expand All @@ -10,6 +11,7 @@ python -m venv .venv
```

Then activate the venv before use.

```bash
source .venv/bin/activate
```
Expand All @@ -22,14 +24,11 @@ pip install -e .

### Examples (`examples/`)

The `examples/` folder contains a set of examples you can explore. Before running any example, install the required dependencies with:

```bash
pip install -e .[examples]
```
There are a collection of self-contained examples you can look through. To run them run `python examples/<example>.py`.

Next, add your HyperSync token to the `.env` file. You can then run an example using:
For examples that call the HyperSync API, set the Envio API token via the environment:

```bash
python examples/<example>.py
export ENVIO_API_TOKEN="your-token"
python examples/chain_id.py
```
14 changes: 2 additions & 12 deletions examples/all_erc20.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import os
from dotenv import load_dotenv
import hypersync
import asyncio
from hypersync import BlockField, TransactionField, LogField, ClientConfig

# Load environment variables from a .env file
load_dotenv()

async def main():
bearer_token = os.getenv("ENVIO_API_TOKEN")
if not bearer_token:
raise ValueError("ENVIO_API_TOKEN environment variable is required. Please set it in your .env file.")

client = hypersync.HypersyncClient(ClientConfig(
url="https://eth.hypersync.xyz/",
bearer_token=bearer_token
))
cfg = ClientConfig(bearer_token=os.environ.get("ENVIO_API_TOKEN"))
client = hypersync.HypersyncClient(cfg)

# The query to run
query = hypersync.Query(
Expand Down
16 changes: 3 additions & 13 deletions examples/all_erc20_transfer_and_approve.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
import os
from dotenv import load_dotenv
import hypersync
import asyncio
from hypersync import BlockField, TransactionField, LogField, ClientConfig

# Load environment variables from a .env file
load_dotenv()

# For a simpler example with a single event, see all-erc20-transfers.py
# For a simpler example with a single event, see all_erc20_transfers.py

transfer_topic = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
approval_topic = "0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925"

async def main():
bearer_token = os.getenv("ENVIO_API_TOKEN")
if not bearer_token:
raise ValueError("ENVIO_API_TOKEN environment variable is required. Please set it in your .env file.")

client = hypersync.HypersyncClient(ClientConfig(
url="https://eth.hypersync.xyz/",
bearer_token=bearer_token
))
cfg = ClientConfig(bearer_token=os.environ.get("ENVIO_API_TOKEN"))
client = hypersync.HypersyncClient(cfg)

# The query to run
query = hypersync.Query(
Expand Down
16 changes: 3 additions & 13 deletions examples/all_erc20_transfers.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
import os
from dotenv import load_dotenv
import hypersync
import asyncio
from hypersync import BlockField, TransactionField, LogField, ClientConfig

# Load environment variables from a .env file
load_dotenv()

# For a more complex example with multiple events, see all-erc20-transfer-and-approve.py
# For a more complex example with multiple events, see all_erc20_transfer_and_approve.py

async def main():
bearer_token = os.getenv("ENVIO_API_TOKEN")
if not bearer_token:
raise ValueError("ENVIO_API_TOKEN environment variable is required. Please set it in your .env file.")

client = hypersync.HypersyncClient(ClientConfig(
url="https://eth.hypersync.xyz/",
bearer_token=bearer_token
))
cfg = ClientConfig(bearer_token=os.environ.get("ENVIO_API_TOKEN"))
client = hypersync.HypersyncClient(cfg)

# The query to run
query = hypersync.Query(
Expand Down
16 changes: 3 additions & 13 deletions examples/block_data.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import os
from dotenv import load_dotenv
import hypersync
import asyncio
from hypersync import BlockField, JoinMode, TransactionField, LogField, ClientConfig

# Load environment variables from a .env file
load_dotenv()

async def main():
bearer_token = os.getenv("ENVIO_API_TOKEN")
if not bearer_token:
raise ValueError("ENVIO_API_TOKEN environment variable is required. Please set it in your .env file.")

client = hypersync.HypersyncClient(ClientConfig(
url="https://eth.hypersync.xyz/",
bearer_token=bearer_token
))
cfg = ClientConfig(bearer_token=os.environ.get("ENVIO_API_TOKEN"))
client = hypersync.HypersyncClient(cfg)

# The query to run
query = hypersync.Query(
Expand Down Expand Up @@ -63,4 +53,4 @@ async def main():
print(len(res.data.transactions))
print(len(res.data.logs))

asyncio.run(main())
asyncio.run(main())
16 changes: 4 additions & 12 deletions examples/block_rewards.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
import os
from dotenv import load_dotenv
import hypersync
import asyncio
import polars

# Load environment variables from a .env file
load_dotenv()

async def run():
bearer_token = os.getenv("ENVIO_API_TOKEN")
if not bearer_token:
raise ValueError("ENVIO_API_TOKEN environment variable is required. Please set it in your .env file.")

client = hypersync.HypersyncClient(
hypersync.ClientConfig(
url="https://eth.hypersync.xyz/",
bearer_token=bearer_token
)
cfg = hypersync.ClientConfig(
url="http://167.235.0.227:2104",
bearer_token=os.environ.get("ENVIO_API_TOKEN"),
)
client = hypersync.HypersyncClient(cfg)

res = await client.collect_arrow(
hypersync.Query(
Expand Down
15 changes: 3 additions & 12 deletions examples/call_watch.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
import os
from dotenv import load_dotenv
import hypersync
import asyncio
import time
from hypersync import TransactionField

# Load environment variables from a .env file
load_dotenv()

DAI_ADDRESS = "0x6B175474E89094C44Da98b954EedeAC495271d0F"

async def main():
bearer_token = os.getenv("ENVIO_API_TOKEN")
if not bearer_token:
raise ValueError("ENVIO_API_TOKEN environment variable is required. Please set it in your .env file.")

client = hypersync.HypersyncClient(hypersync.ClientConfig(
url="https://eth.hypersync.xyz/",
bearer_token=bearer_token
))
# Create hypersync client using the mainnet hypersync endpoint (default)
cfg = hypersync.ClientConfig(bearer_token=os.environ.get("ENVIO_API_TOKEN"))
client = hypersync.HypersyncClient(cfg)

# The query to run
query = hypersync.Query(
Expand Down
16 changes: 16 additions & 0 deletions examples/chain-id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import os
import hypersync
import asyncio
from hypersync import BlockField, TransactionField, LogField, ClientConfig


async def main():
cfg = ClientConfig(bearer_token=os.environ.get("ENVIO_API_TOKEN"))
client = hypersync.HypersyncClient(cfg)

chain_id = await client.get_chain_id()

print(chain_id)


asyncio.run(main())
13 changes: 2 additions & 11 deletions examples/parquet_blocks_and_transactions.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import os
from dotenv import load_dotenv
import hypersync
import asyncio

# Load environment variables from a .env file
load_dotenv()

async def main():
bearer_token = os.getenv("ENVIO_API_TOKEN")
if not bearer_token:
raise ValueError("ENVIO_API_TOKEN environment variable is required. Please set it in your .env file.")

client = hypersync.HypersyncClient(hypersync.ClientConfig(
url="https://eth.hypersync.xyz/",
bearer_token=bearer_token
))
cfg = hypersync.ClientConfig(bearer_token=os.environ.get("ENVIO_API_TOKEN"))
client = hypersync.HypersyncClient(cfg)
height = await client.get_height()

query = hypersync.preset_query_blocks_and_transactions(
Expand Down
15 changes: 3 additions & 12 deletions examples/simple_blocks_and_transaction_hashes.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import os
from dotenv import load_dotenv
import hypersync
import asyncio

# Load environment variables from a .env file
load_dotenv()

# returns all blocks and the hashes of the transactions (not entire transaction objects) within a block range

async def main():
bearer_token = os.getenv("ENVIO_API_TOKEN")
if not bearer_token:
raise ValueError("ENVIO_API_TOKEN environment variable is required. Please set it in your .env file.")

client = hypersync.HypersyncClient(hypersync.ClientConfig(
url="https://eth.hypersync.xyz/",
bearer_token=bearer_token
))
# Create hypersync client using the mainnet hypersync endpoint (default)
cfg = hypersync.ClientConfig(bearer_token=os.environ.get("ENVIO_API_TOKEN"))
client = hypersync.HypersyncClient(cfg)

query = hypersync.preset_query_blocks_and_transaction_hashes(17_000_000, 17_000_050)

Expand Down
15 changes: 3 additions & 12 deletions examples/simple_blocks_and_transactions.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import os
from dotenv import load_dotenv
import hypersync
import asyncio

# Load environment variables from a .env file
load_dotenv()

# returns all block and transaction objects within a block range

async def main():
bearer_token = os.getenv("ENVIO_API_TOKEN")
if not bearer_token:
raise ValueError("ENVIO_API_TOKEN environment variable is required. Please set it in your .env file.")

client = hypersync.HypersyncClient(hypersync.ClientConfig(
url="https://eth.hypersync.xyz/",
bearer_token=bearer_token
))
# Create hypersync client using the mainnet hypersync endpoint (default)
cfg = hypersync.ClientConfig(bearer_token=os.environ.get("ENVIO_API_TOKEN"))
client = hypersync.HypersyncClient(cfg)

query = hypersync.preset_query_blocks_and_transactions(17_000_000, 17_000_050)

Expand Down
15 changes: 3 additions & 12 deletions examples/simple_logs.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import os
from dotenv import load_dotenv
import hypersync
import asyncio

# Load environment variables from a .env file
load_dotenv()

# returns all logs from a contract within a block range

async def main():
bearer_token = os.getenv("ENVIO_API_TOKEN")
if not bearer_token:
raise ValueError("ENVIO_API_TOKEN environment variable is required. Please set it in your .env file.")

client = hypersync.HypersyncClient(hypersync.ClientConfig(
url="https://eth.hypersync.xyz/",
bearer_token=bearer_token
))
# Create hypersync client using the mainnet hypersync endpoint (default)
cfg = hypersync.ClientConfig(bearer_token=os.environ.get("ENVIO_API_TOKEN"))
client = hypersync.HypersyncClient(cfg)

usdt_contract = "0xdAC17F958D2ee523a2206206994597C13D831ec7"

Expand Down
15 changes: 3 additions & 12 deletions examples/simple_logs_of_event.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import os
from dotenv import load_dotenv
import hypersync
import asyncio

# Load environment variables from a .env file
load_dotenv()

# returns all logs of a specific event from a contract within a block range

async def main():
bearer_token = os.getenv("ENVIO_API_TOKEN")
if not bearer_token:
raise ValueError("ENVIO_API_TOKEN environment variable is required. Please set it in your .env file.")

client = hypersync.HypersyncClient(hypersync.ClientConfig(
url="https://eth.hypersync.xyz/",
bearer_token=bearer_token
))
# Create hypersync client using the mainnet hypersync endpoint (default)
cfg = hypersync.ClientConfig(bearer_token=os.environ.get("ENVIO_API_TOKEN"))
client = hypersync.HypersyncClient(cfg)

usdt_contract = "0xdAC17F958D2ee523a2206206994597C13D831ec7"

Expand Down
14 changes: 2 additions & 12 deletions examples/stream_with_progress_bar.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
from dotenv import load_dotenv
import hypersync
import asyncio
import time
Expand All @@ -9,9 +8,6 @@
from tqdm_loggable.auto import tqdm
from tqdm_loggable.tqdm_logging import tqdm_logging

# Load environment variables from a .env file
load_dotenv()

# Set up logging
logger = logging.getLogger(__name__)
fmt = "%(filename)-20s:%(lineno)-4d %(asctime)s %(message)s"
Expand All @@ -24,14 +20,8 @@


async def main():
bearer_token = os.getenv("ENVIO_API_TOKEN")
if not bearer_token:
raise ValueError("ENVIO_API_TOKEN environment variable is required. Please set it in your .env file.")

client = hypersync.HypersyncClient(ClientConfig(
url="https://eth.hypersync.xyz/",
bearer_token=bearer_token
))
cfg = ClientConfig(bearer_token=os.environ.get("ENVIO_API_TOKEN"))
client = hypersync.HypersyncClient(cfg)
height = await client.get_height()
start_block = height - 8000
total_blocks = height - start_block
Expand Down
Loading