Skip to main content

No project description provided

Project description

hypersync-client-python

Python package for Envio's HyperSync client written in Rust

feedback and issues are appreciated :)

Can find more examples in examples/

Ex: run examples/all-erc20.py

$ python3 examples/all-erc20.py

Example usage

import hypersync
import asyncio

async def main():

    # Create hypersync client using the mainnet hypersync endpoint
    client = hypersync.hypersync_client(
        "https://eth.hypersync.xyz",
    )

    height = await client.get_height()
    print("Height:", height)

    # The address we want to get all ERC20 transfers and transactions for
    addr = "1e037f97d730Cc881e77F01E409D828b0bb14de0"

    # The query to run
    query = {
        # start from block 0 and go to the end of the chain (we don't specify a toBlock).   
        "from_block": 0,
        # The logs we want. We will also automatically get transactions and blocks relating to these logs (the query implicitly joins them)
        "logs": [
            {
                "topics": [
                    # We want ERC20 transfers
                    ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"],
                    [],
                    # We want the transfers that go to this address.
                    # appending zeroes because topic is 32 bytes but address is 20 bytes
                    ["0x000000000000000000000000" + addr],
                ],
            },
            {
                "topics": [
                    # We want ERC20 transfers
                    ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"],
                    # We want the transfers that go to this address.
                    # appending zeroes because topic is 32 bytes but address is 20 bytes
                    ["0x000000000000000000000000" + addr],
                    [],
                ],
            },
        ],
        "transactions": [
            # We want all the transactions that come from this address
            {"from": ["0x" + addr]},
            # We want all the transactions that went to this address
            {"to": ["0x" + addr]},
        ],
        # Select the fields we are interested in
        "field_selection": {
            "block": ["number", "timestamp", "hash"],
            "log": [
                "block_number",
                "log_index",
                "transaction_index",
                "data",
                "address",
                "topic0",
                "topic1",
                "topic2",
                "topic3",
            ],
            "transaction": [
                "block_number",
                "transaction_index",
                "hash",
                "from",
                "to",
                "value",
                "input",
            ],
        },
    }

    print("query: ", query)

    # run the query once
    res = await client.send_req(query)
    print("res: ", res)

    # read json abi file for erc20
    with open('./erc20.abi.json', 'r') as json_file:
        abi = json_file.read()

    # every log we get should be decodable by this abi but we don't know
    # the specific contract addresses since we are indexing all erc20 transfers.
    abis = {}
    for log in res.data.logs:
        abis[log.address] = abi

    # create a decoder based on our abi file
    decoder = hypersync.Decoder(abis)
    
    # decode the logs using the decoder
    decoded_logs = decoder.decode_logs_sync(res.data.logs)

    print("decoded logs:")
    for decoded_log in decoded_logs:
        print(decoded_log.indexed)
        print(decoded_log.body)

    # Create a parquet folder by running this query and writing the contents to disk
    await client.create_parquet_folder(query, "data")
    print("finished writing parquet folder")


asyncio.run(main())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hypersync-0.5.4.tar.gz (44.2 kB view hashes)

Uploaded Source

Built Distributions

hypersync-0.5.4-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.4 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

hypersync-0.5.4-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.4 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

hypersync-0.5.4-cp312-none-win_amd64.whl (4.9 MB view hashes)

Uploaded CPython 3.12 Windows x86-64

hypersync-0.5.4-cp312-none-win32.whl (4.3 MB view hashes)

Uploaded CPython 3.12 Windows x86

hypersync-0.5.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.4 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

hypersync-0.5.4-cp312-cp312-macosx_11_0_arm64.whl (5.3 MB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

hypersync-0.5.4-cp312-cp312-macosx_10_12_x86_64.whl (5.6 MB view hashes)

Uploaded CPython 3.12 macOS 10.12+ x86-64

hypersync-0.5.4-cp311-none-win_amd64.whl (4.9 MB view hashes)

Uploaded CPython 3.11 Windows x86-64

hypersync-0.5.4-cp311-none-win32.whl (4.3 MB view hashes)

Uploaded CPython 3.11 Windows x86

hypersync-0.5.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.3 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

hypersync-0.5.4-cp311-cp311-macosx_11_0_arm64.whl (5.3 MB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

hypersync-0.5.4-cp311-cp311-macosx_10_12_x86_64.whl (5.6 MB view hashes)

Uploaded CPython 3.11 macOS 10.12+ x86-64

hypersync-0.5.4-cp310-none-win_amd64.whl (4.9 MB view hashes)

Uploaded CPython 3.10 Windows x86-64

hypersync-0.5.4-cp310-none-win32.whl (4.3 MB view hashes)

Uploaded CPython 3.10 Windows x86

hypersync-0.5.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.3 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

hypersync-0.5.4-cp310-cp310-macosx_11_0_arm64.whl (5.3 MB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

hypersync-0.5.4-cp310-cp310-macosx_10_12_x86_64.whl (5.6 MB view hashes)

Uploaded CPython 3.10 macOS 10.12+ x86-64

hypersync-0.5.4-cp39-none-win_amd64.whl (4.9 MB view hashes)

Uploaded CPython 3.9 Windows x86-64

hypersync-0.5.4-cp39-none-win32.whl (4.3 MB view hashes)

Uploaded CPython 3.9 Windows x86

hypersync-0.5.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.3 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page