Skip to main content

Python API wrappers for Nomisma contracts

Project description

Using the Nomisma Contract API

Requirements:

  • Python 3.7.x
  • Web3 >= 4.9

The API consists of a set of classes that wrap and enhance the Solidity smart contracts. An asynchronous multi-threaded runtime is included and used by these classes. Additionally, a test resource is available to make testing your code simpler.

Testing your code that uses the API

The test_util package includes the following test resource classes that should be used in your tests.

CapitalStructureTestResource

These test resources have methods for deploying the contracts and loading python instances of these contracts. The contracts are split into two sets: contracts that have no storage (just code) and should be deployed just once, and contracts that have storage, which should be deployed with each independent test case.

You have to choose an ethereum test implementation for your the tests. Two choices are: ganache or eth_tester (native python).

Ganache

Run ganache-cli with these parameters: ganache-cli --account '0x0000000000000000000000000000000000000000000000000000000000000001',10000000000000000000000

This creates a locally running Ethereum test chain and creates 100 accounts with known addresses. Adding -b 1 to the command line args will result in some transaction batching, which is more realistic, but slows down testing.

In the test resource initialization, point to the ganache instance like this:

CapitalStructureTestResource(web3=Web3(Web3.HTTPProvider("http://127.0.0.1:8545")))

Eth-Tester

As of 0.0.3, Eth-tester seems to be broken. Don't use for now. You'll have to install eth-tester (via pip or similar). eth-tester seems limited to 10 accounts, so don't use any more accounts than 10 in your tests (the test resource uses some accounts, configurable in its constructor).

In the test resource initialization, use this form:

CapitalStructureTestResource(web3=Web3(EthereumTesterProvider()))

Why code generation?

Web3.py comes with various ways to invoke contracts and it does it without code generation (using python's highly dynamic object structure). With code generation, we get the following benefits, as compared to web3.py out of the box:

  • The contract functions are visible and readable. This is ultimately much better for developers, because both them and their tools (IDE with auto-completion) have access to the method declarations.
  • python 3 type hinting for parameters and return types
  • payable functions have an extra parameter (to_pay_ether) with a sensible unit (ether)
  • The contract functions can be documented from the solidity docs.
  • idiomatic python snake case names, e.g. getAddress -> get_address
  • user friendly parameter checking, e.g. correct length for unit256[4] (python doesn't have fixed length arrays)
  • performant(async)/user friendly call/transaction wrapping

Development Tool Setup

Requirements:

  • Python 3.7.x
  • Pipenv: pip install pipenv
  • ganache-cli for testing (which usually means installing npm, then npm install ganache-cli)

Set PIPENV_VENV_IN_PROJECT=true in your environment variables to have a .venv directory inside the project folder.

pipenv shell opens a shell prompt within the virtual environment for the project. pipenv install will install the project dependencies. pipenv install --dev installs the development dependencies.

Running Tests

For dev purposes, we run most tests with ganache (see above).

Technical Notes

Web3.py converts Solidity address type to Python str, but we don't type hint addresses as such in the generated code, to make it clear they are not arbitrary strings.

Releasing

Make sure setup.py has a new version, based on semantic versioning. Add a git tag: git tag nomisma.contractapi-x.y.z -m "Release x.y.z" git push upstream nomisma.contractapi-x.y.z Clean the dist directory: rm -rf dist Create a distribution (in the project's virtual env, usually via pipenv shell): python setup.py sdist Upload to pypi (requires username/pw): twine upload dist/*

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

nomisma.contractapi-0.0.3.tar.gz (81.8 kB view hashes)

Uploaded Source

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