Skip to main content

Smart contract Python API generator.

Project description

pytract

PyPI download month

Smart contract Python API generator.

It works by interpreting smart contract ABI and generate corresponding Python API code.

Usage

Install from PyPI:

pip install pytract

Create Python API code for your brownie project:

usage: pytract process [-h] project_path

positional arguments:
  project_path  Path of the project to process

optional arguments:
  -h, --help    show this help message and exit

And you shall import all available contracts and programmatically.

If your project folder name is brownie_project then write:

from brownie_project.api import project_info, contracts

print(dir(contracts)) # list available contract classes
print(dir(project_info)) # list project info attributes

Note the difference of using it with brownie alone, you now have a fully portable python package, which you can write static typed code without the need for copying and pasting from brownie console.

You can view all generated API code under folder <your brownie project>/api.

Example generated contract API class is given below:

class Faucet(abi2api.ContractInstance):
    _project = project_info.project
    _contract_info = project_info.contracts_info["Faucet"]
    _contract_name = "Faucet"

    class Function(abi2api.FunctionBase):
        def returnVars(
            self,
        ):
            """
            Function Type: pure

            Inputs:
                (No parameters)

            Outputs:
                (uint256, uint256, uint256)
            """
            values = self._contract.returnVars()
            return values

        def withdraw(
            self,
            withdraw_amount,
            _txparams: Optional[abi2api.TransactionParameters] = None,
        ):
            """
            Function Type: nonpayable

            Inputs:
                withdraw_amount: uint256

            Outputs:
                (No parameters)
            """
            values = self._contract.withdraw(
                withdraw_amount, self.txparams_with_fallback(_txparams).dict()
            )
            return values

    def __init__(
        self,
        contract: Union[Contract, ProjectContract],
        issuer: Optional[Account] = None,
        _txparams: Optional[abi2api.TransactionParameters] = None,
    ):  # to create you need to either deploy or load contract by address
        super().__init__(contract, issuer)
        self.function = self.Function(contract, _txparams)
        """
        Available functions of this smart contract.
        """

    @classmethod
    def deploy(cls, _txparams: abi2api.TransactionParameters):
        """
        Inputs:
            transaction_parameters: TransactionParameters

        Output:
            contract: Faucet
        """
        args = []

        parameters = _txparams.to_contract_deploy_parameters(args)

        deployed_contract: ProjectContract = cls._contract_info.contract_container.deploy(*parameters.to_args())  # type: ignore

        return cls(deployed_contract, _txparams.issuer, _txparams)

Roadmap

  • Create a binding to smart contract and APIs for Brownie
  • Create a simple pure Python transaction and smart contract platform without blockchain
  • Create a TypeChain-like commandline tool for generating standalone package from a list of contract ABI files

Related projects

Stargazers

Stargazers over time

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

pytract-0.0.4.tar.gz (15.4 kB view details)

Uploaded Source

File details

Details for the file pytract-0.0.4.tar.gz.

File metadata

  • Download URL: pytract-0.0.4.tar.gz
  • Upload date:
  • Size: 15.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for pytract-0.0.4.tar.gz
Algorithm Hash digest
SHA256 64e190b76853c8f82e2b31ad0e702b78fddd1eb90f715af9253da022487e259a
MD5 1a19071117e2167e97f7cf8a7b02e0bf
BLAKE2b-256 99fec63524b3548c135ec3d3c3bb953b4616d67dbbfbf1be3c110464d85a8f6b

See more details on using hashes here.

Supported by

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