Skip to main content

Tools to help with the common problems of Forta bot development

Project description

Forta Toolkit

Various tools to help with the common problems of Forta bot development.

Table of Contents

Installation

# globally
pip install forta_toolkit

# in a local environment
poetry add forta_toolkit

Usage

Bot setup

The Forta often require initialization steps to adapt to a given chain or use external tools.

Alert statistics

This is an alternative to querying the Zetta API for alert statistics. It saves a local history of the alerts in memory and use it to calculate the rates. The main motivation is to improve performance by avoiding web requests.

To use it, just wrap handle_block / handle_transaction / handle_alert as follows:

import forta_toolkit

@forta_toolkit.alerts.alert_history(size=10000)
def handle_block(log: BlockEvent) -> list:
    pass

@forta_toolkit.alerts.alert_history(size=10000)
def handle_transaction(log: TransactionEvent) -> list:
    pass

@forta_toolkit.alerts.alert_history(size=10000)
def handle_alert(log: AlertEvent) -> list:
    pass

The decorator will automatically add the anomaly_score in the metadata of the Finding objects. It will use the field alert_id from the Finding objects to identify them.

make sure the history size is big enough to contain occurences of the bot alerts!

For example, if your bot triggers ALERT-1 every 2k transactions and ALERT-2 every 10k on average: @alert_history(size=100000) would gather enough alerts to have a relevant estimation of the rate of both alerts.

Logging execution events

Improving performances

Load balancing

Profiling

The bots have to follow the pace of the blockchain, so they need to process transactions relatively quickly.

You can leverage the profiling tools to find the performance bottlenecks in your bots:

from forta_toolkit.profiling import test_performances, display_performances

test_performances(func=handle_transaction, data=some_tx_log)
display_performances(logpath='./test_performances')

Otherwise, you can monitor the performances directly when processing mainnet transactions. Just decorate the handle_block / handle_transaction / handle_alert as follows:

@forta_toolkit.alerts.profile
def handle_transaction(tx: TransactionEvent) -> list:
    pass

Then you can parse the profile logs manually with pstats or:

display_performances(logpath='some/path/to/the/logs/handle_transaction')

Development

Contributions welcome!

Changelog

See CHANGELOG.

Todo

See TODO.

Credits

The RPC request queue was inspired by the TS module forta-helpers by Artem Kovalchuk.

License

Licensed under the aGPL v3.

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

forta_toolkit-0.1.21.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

forta_toolkit-0.1.21-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file forta_toolkit-0.1.21.tar.gz.

File metadata

  • Download URL: forta_toolkit-0.1.21.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.5 Linux/6.6.1-arch1-1

File hashes

Hashes for forta_toolkit-0.1.21.tar.gz
Algorithm Hash digest
SHA256 c268a98fe1da3744a54845bc2ce09bb2c73dbdceb4c5a9794c15adf68dac095f
MD5 4997a2931f487e250bc3311b2da6aa88
BLAKE2b-256 2b0082c29aad5b6322d01ca89c742783ebb1e8dcda5ed4096a26ed25857c3094

See more details on using hashes here.

File details

Details for the file forta_toolkit-0.1.21-py3-none-any.whl.

File metadata

  • Download URL: forta_toolkit-0.1.21-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.5 Linux/6.6.1-arch1-1

File hashes

Hashes for forta_toolkit-0.1.21-py3-none-any.whl
Algorithm Hash digest
SHA256 052b4787d010b9396f15ea297a672101799fb4cf349ac692b7ef5f32ce561f42
MD5 3b2ac731a2823c6f66d82e4005fc4481
BLAKE2b-256 121489df3162c4d037d87c7e32f9ebcd45f63702d9d786d9864ddaf3bd755514

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