Skip to main content

PyAutoBlockchain (PAB)

Python 3.10 Integration Tests Unit Tests Documentation Status codecov

PAB is a framework that helps with development and automation of periodic tasks on blockchains.

With PAB, you quickstart your blockchain development and prototyping. After running pab init to create a new project, you can jump right into developing your own strategies.

With little more configuration, you can connect to any Web3 compatible network using an RPC, load contracts from the network, and use any account you have the Private Key of to authenticate against the network (if you need to make transactions).

PAB allows you to quickly implement Strategies without worring about some Web3 implementation details, like connecting to a blockchain, retrieving contracts and sending transactions.

Check out our documentation here!

Installation

Using pip:

$ pip install PyAutoBlockchain

With PABUI

Install with PABUI extension:

$ pip install PyAutoBlockchain[ui]

And run PABUI from your projects directory:

$ ls
abis/  config.json  contracts.json  strategies.py  tasks.json  venv/
$ pabui

Usage

Create project in current directory:

(venv) $ pab init 

Run project:

(venv) $ pab run

For a substantially more complete guide, head over to our Official Documentation's Guide section.

Sample Strategy

PAB will load custom strategies at startup from a strategies module in the current working directory. This module can be a single strategies.py file or a strategies directory with an __init__.py file.

All subclasses of pab.strategy.BaseStrategy are loaded as available strategies for tasks, and all must implement the run() method.

Here's a basic sample strategy to give you an idea of the Strategy API:

import csv
from datetime import datetime
from pab.strategy import BaseStrategy

class CompoundAndLog(BaseStrategy):
    """ Finds pool in `masterchef` for `token`, compounds the given pool for 
    `self.accounts[account_index]` and logs relevant data into some csv file. """

    def __init__(self, *args, filepath: str = "compound.csv", token: str = '', masterchef: str = '', account_index: int = 0):
        super().__init__(*args)
        self.filepath = filepath
        self.account = self.accounts[account_index]
        self.token = self.contracts.get(token)
        self.masterchef = self.contracts.get(masterchef)
        self.pool_id = self.masterchef.functions.getPoolId(self.token.address).call()
        if not self.pool_id:
            raise Exception(f"Pool not found for {self.token} in {self.masterchef}")

    def run(self):
        """ Strategy entrypoint. """
        balance = self.get_balance()
        new_balance = self.compound()
        self.write_to_file(balance, new_balance)
        self.logger.info(f"Current balance is {balance}")

    def compound(self) -> int:
        self.transact(self.account, self.masterchef.functions.compound, (self.pool_id, ))
        return self.get_balance()

    def get_balance(self) -> int:
        return self.masterchef.functions.balanceOf(
            self.account.address,
            self.pool_id
        ).call()
    
    def write_to_file(self, old_balance: int, new_balance: int):
        now = datetime.now().strftime('%Y-%m-%d %I:%M:%S')
        diff = new_balance - old_balance
        with open(self.filepath, 'a') as fp:
            writer = csv.writer(fp)
            writer.writerow([now, new_balance, diff])

For more details, read our Official Documentation's Strategies In-Depth section.

Development

Testing

Unit Tests

To run unit tests:

(venv) $ pip install -e requirements-dev.txt
(venv) $ ./tests.sh

Integration tests

The recommended way to run integration-tests is with act.

With act you can run:

$ act -j integration-tests

to run integration from the github actions tests inside a docker container.

The other way is to use local installations of truffle and ganache and run:

$ ./integration-tests.sh

For more information on integration tests see Integration Tests README.

Release files for PyAutoBlockchain 0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for PyAutoBlockchain 0.5
File Size Uploaded
PyAutoBlockchain-0.5.tar.gz 28.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for PyAutoBlockchain 0.5
File Interpreter ABI Platform
PyAutoBlockchain-0.5-py3-none-any.whl Python 3 none any Details

Total release size: 60.0 kB

Release files / PyAutoBlockchain-0.5.tar.gz

Download URL PyAutoBlockchain-0.5.tar.gz
Size 28.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d050d6dd0c1decec677dc2f323dd97539e8c4bc8f35192024881a69fcd5b3a71
BLAKE2b-256 checksum
How to use checksums
a0a0bbdc09bc483be31b332d0c0e0ec09cf8dc7d559225fe92f1c6bf034d16a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

Release files / PyAutoBlockchain-0.5-py3-none-any.whl

Download URL PyAutoBlockchain-0.5-py3-none-any.whl
Size 32.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f64fed762e2b09de600f1c4ea1d032d37ada007a2229bb65df757387661e790f
BLAKE2b-256 checksum
How to use checksums
c11ae8793fd8772d3a7abc84b9d46982fc8ed57b19ece7d9eaf98ed5b218d4a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

Release history Release notifications | RSS feed

This release

0.5 This release

2 release files

0.4

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page