Skip to main content

mev-commit python sdk for on-chain data retrieval for mev-commit chain

Project description

mev-commit-sdk-py

Introduction

The mev-commit-sdk-py is a Python-based SDK designed to interact with the MEV Commit protocol, enabling developers to efficiently query blockchain data such as events, transactions, and blocks. This SDK leverages Envio's Hypersync Indexer to streamline data retrieval from the MEV Commit chain.

Features

  • Event Querying: Retrieve and analyze specific events on the mev-commit blockchain.
  • Block and Transaction Retrieval: Access detailed block and transaction data across a specified range of blocks.
  • Data Transformation: Utilize Polars DataFrames to handle and process data efficiently. Customizable Queries: Flexibility in creating custom queries based on specific needs, including event filtering by block range or address.

Installation

Install with pip install mev-commit-sdk-py

This library uses rye as the project manager. First install rye. Then clone the repository and run rye sync to install the dependencies and get setup. Current Python version is 3.12.2. Alternatively a different virtual environment manager can be used as long as it can use Python 3.12.2.

Testing

Run the unit tests with python -m unittest discover -s tests command.

Usage

This SDK is designed for ease of use in querying blockchain data. Below are some examples to help you get started:

Event Querying

You can query specific events using event names:

  • "NewL1Block": Tracks new L1 block events.
  • "CommitmentProcessed": Tracks when commitments are processed.
  • "BidderRegistered": Tracks bidder registration with deposited amount.
  • "BidderWithdrawal": Tracks bidder withdrawals.
  • "OpenedCommitmentStored": Tracks when opened commitments are stored.
  • "FundsRetrieved": Tracks when funds are retrieved from a bidder.
  • "FundsRewarded": Tracks when funds are rewarded from a bidder.
  • "FundsSlashed": Tracks when funds are slashed from a provider.
  • "FundsDeposited": Tracks when funds are deposited by a provider.
  • "Withdraw": Tracks provider withdrawals.
  • "ProviderRegistered": Tracks when a provider is registered.
  • "UnopenedCommitmentStored": Tracks when unopened commitments are stored.

Block and Transaction Retrieval

To retrieve transactions and blocks for a specific range:

import asyncio
import polars as pl
from mev_commit_sdk_py.hypersync_client import Hypersync

client = Hypersync(url='https://mev-commit.hypersync.xyz')

txs, blocks = asyncio.run(client.get_blocks_txs(from_block=0, to_block=100000))
print(txs.head())
print(blocks.head())

Query Preconf Commitment Data:

To query and build a DataFrame of precommitment data:

import asyncio
import polars as pl
from mev_commit_sdk_py.hypersync_client import Hypersync

client = Hypersync(url='https://mev-commit.hypersync.xyz')

# Encrypted commits have the dispatchTimestamp, which is the time when the provider decides to open the commitment to reveal the data. 
encrypted_stores: pl.DataFrame = await client.execute_event_query('UnopenedCommitmentStored', from_block=from_block)

# Opened commits have all of the bidding data such as bidder, bid amount, and decay function parameters.
commit_stores: pl.DataFrame = await client.execute_event_query('OpenedCommitmentStored', from_block=from_block)

# Get commitment slashing
commits_processed = await client.execute_event_query('CommitmentProcessed', from_block=from_block)

# Polars join log data to get comprehensive commitment data
commitments_df: pl.DataFrame = (
    encrypted_stores
    .join(commit_stores, on='commitmentIndex', how='left')
    .with_columns(('0x' + pl.col("txnHash")).alias('txnHash'))
    .join(commits_processed.select('commitmentIndex', 'isSlash'), on='commitmentIndex', how='inner')
).select(
    'block_number', 'blockNumber', 'txnHash', 'bid', 'commiter', 'bidder',
    'isSlash', 'decayStartTimeStamp', 'decayEndTimeStamp', 'dispatchTimestamp',
    'commitmentHash', 'commitmentIndex', 'commitmentDigest', 'commitmentSignature',
    'revertingTxHashes', 'bidHash', 'bidSignature', 'sharedSecretKey'
)

print(commitments_df.head(5))

Query Provider Slashing

import asyncio
import polars as pl
from mev_commit_sdk_py.hypersync_client import Hypersync

client = Hypersync(url='https://mev-commit.hypersync.xyz')

provider_slashes = asyncio.run(client.execute_event_query('FundsSlashed'))
# print(provider_slashes.tail(10)) # tail gets most recent events

provider_table = provider_slashes.with_columns(
    (pl.col('amount')/ 10**18).alias('slash_amt_eth')
).group_by('provider').agg(
    pl.col('slash_amt_eth').sum().alias('total_amt_slashed_eth'),
    pl.len().alias('slash_count')
)

print(provider_table)

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

mev_commit_sdk_py-0.4.6.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

mev_commit_sdk_py-0.4.6-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file mev_commit_sdk_py-0.4.6.tar.gz.

File metadata

  • Download URL: mev_commit_sdk_py-0.4.6.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for mev_commit_sdk_py-0.4.6.tar.gz
Algorithm Hash digest
SHA256 6257078a4c5c9478ccf50b8b830e0b5108568ca9c5732615f3e56bbc66547312
MD5 3803ef8b8ad76c3480c72d586301b12c
BLAKE2b-256 b40a50946fad5e75b8432402c8fc9c30031f40d0aead6c8f359b365d8e6740c0

See more details on using hashes here.

File details

Details for the file mev_commit_sdk_py-0.4.6-py3-none-any.whl.

File metadata

File hashes

Hashes for mev_commit_sdk_py-0.4.6-py3-none-any.whl
Algorithm Hash digest
SHA256 dc9c3702f7b3db547fbff39c36a349f0dd4d7b3e094ef74a3c03df5cb053d86f
MD5 af50881845673a8210b7b2d2d2e78f7d
BLAKE2b-256 8f0550dbefc3b296c7e3aeec792d74e1897e40f049dadb8750356a4ace25aca7

See more details on using hashes here.

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