Python API wrappers for Nomisma contracts
Using the Nomisma Contract API
- 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
test_util package includes the following test resource classes that should be used in your tests.
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).
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.
-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:
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:
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.
- user friendly parameter checking, e.g. correct length for
unit256(python doesn't have fixed length arrays)
- performant(async)/user friendly call/transaction wrapping
Development Tool Setup
- Python 3.7.x
pip install pipenv
- ganache-cli for testing (which usually means installing npm, then
npm install ganache-cli)
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.
For dev purposes, we run most tests with ganache (see above).
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.
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
python setup.py sdist
Upload to pypi (requires username/pw):
twine upload dist/*
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Hashes for nomisma.contractapi-0.0.3.tar.gz