Skip to main content

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/*

Metadata

Release files for nomisma.contractapi 0.0.3

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

Source distribution (sdist)

Source distribution for nomisma.contractapi 0.0.3
File Size Uploaded Provenance Actions
nomisma.contractapi-0.0.3.tar.gz 81.8 kB Without provenance Details for nomisma.contractapi-0.0.3.tar.gz

Release files / nomisma.contractapi-0.0.3.tar.gz

Download URL nomisma.contractapi-0.0.3.tar.gz
Size 81.8 kB
Tags Source
SHA-256 checksum
How to use checksums
5807b209691ea52c32ee5b7a3c39c64d991e1c6c62f6a4bf074c542b01f68e19
BLAKE2b-256 checksum
How to use checksums
9a198258f75772bf7e456eda74876d751d38257a4af951def049f41441b6ce94
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2

Security for nomisma.contractapi 0.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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