December 15, 2022: Python Web3 Testing 🐍🧑💻
The state of Ape, Brownie, and Titanoboa heading into 2023
Imagine it’s 2023, and you need to secure your smart contract. Imagine you also like Python, because… well…
What should you use for your smart contract testing?
Brownie was the first major comprehensive Pythonic smart contract testing suite, and it remains fully featured and wonderful. Several existing smart contract repositories have built tests in Brownie testing suites, so it will remain a useful skill next year and well beyond.
However, even though Brownie is fully featureful, some ongoing maintenance remains necessary to keep it current. For instance, Brownie has issues compiling on newer versions of software due to installation dependencies (toward the end of the article we provide some technical notes for users looking to install brownie using a newer Mac M1).
Additionally, an active community means additional tooling gets created atop the platform. Users therefore flood towards the most active community. With Brownie development slowing, we expect in 2023 the community will move their talents elsewhere unless the project enjoys a resurgence of activity.
If you want to stay current on smart contract testing going into 2023, we therefore recommend updating your repertoire. If you’re new to smart contract development and choosing a test framework, we recommend starting with ApeWorx. We believe Ape Framework is ready, willing, and able to serve as the leading smart contract test suite in 2023.
Ape itself is a Brownie fork, so experienced Brownie users will have almost no learning curve, and are also recommended to become conversant in the suite.
The initial release of ape was intentionally beneath feature parity with Brownie, but the product is developing quite fast and will likely exceed Brownie’s capabilities shortly (if it hasn’t already.)
The newest version released yesterday adds great features like robust gas reporting.
Today is a particularly timely opportunity to pick up Ape, as they’re holding a holiday NFT challenge. You can still participate in if you can crank out some code this weekend. Or just stay tuned for potential NFT drops.
Those who prefer DeFi to NFTs, you’ll be interested to follow how the team is helping sponsor the finalized EIP-4626 tokenized vault standard. They had a recent and highly recommended Twitter Space discussion on the topic, including details on launching your own using Ape.
In 2023, if you’re interested in leveling up, take some time to learn Ape Framework, otherwise you risk NGMI.
Getting Started
With a lot of the world slowing down for the holidays and the weather outside turning frightful, it’s a great time to lock yourself away from your family and level up your coding skills. Everybody can take advantage of any downtime to do some learning.
Getting started with ape is fortunately very easy. If you are new to smart contract testing, Ape provides a robust “Ape Academy” to quickly get new users onto their easy-to-use interface and helpful tutorials on a variety of practical subjects.
If you're ready to try Ape for yourself, getting started is also easy. The first thing you'll need to do is install Ape on your system, which can be as easy as typing pip install eth-ape
(preferably within a virtual environment).
The Bow Tied Jungle, bizarrely judged less influential on crypto than somebody named Matt Damon — has some outstanding resources on smart contract development, as they do on every subject. We recommend making a deal with the BowTiedDevil to subscribe to his excellent Substack:
While you’re collecting knowledge, be sure to also follow BowTiedPickle, who provides good and approachable coding advice on Twitter along with practical lessons via Substack.
For our sake, we’ll be spending the break looking at beefing up our skills around our Achilles heel, which is web3 enabled frontend development. We hope before 2022 ends to add basic React integrations to our repertoire so we have better control over the full stack.
The 2,500 Pound Titanoboa in the Room
We certainly recommend ape, but it’s not necessarily the only potential option. If you want to play on advanced mode, native Vyper parser Titanoboa is quite early, but can already be combined directly with PyTest to form the basis of a functional testing suite.
The disadvantages is that Titanoboa is not yet optimized for this use case, so you’ll need to write some more custom code to replicate the testing environment. The good thing is, by compiling directly in native Python, testing can be much faster for complex contracts!
Recently Curve Finance, which had written its tests almost exclusively on Brownie, has started using both Ape and Boa for its testing for $crvUSD. The repo also serves as the definitive guide to getting started using boa for testing:
Brownie Installation Tips
As noted above — Brownie is still useful given the volume of libraries already written within the framework. A lot of the brownie resources available are also about 90% transferable to ape, so it remains a good idea to become familiar with brownie if you haven’t. We’re biased, but we recommend this tutorial which may also get you some knowledge of how Curve’s backend is constructed.
However, installing on newer Macs has occasionally given trouble. Here’s a few tips from how to get Brownie installed on a new Mac after a recent install gave some trouble.
First up, you have to deal with the fact that Mac by default ship with Python2 for some inexplicable reason. You can upgrade your system Python to Python3 in a few ways — we found using pyenv to manage the system python was best. Using homebrew also worked but was slightly less intuitive.
We haven’t had much luck using the recommended pipx to work due to some dependency conflicts with bitarray
, cytoolz
, or yarl
we could not resolve through several attempts. Nor did we have much luck with Python version 3.11, seeing similar errors as above, but we successfully got it working on 3.8 through 3.10. Therefore you may find it particularly useful to install in a virtual environment.
You also likely need to have some tools within xcode installed on the command line to allow some C libraries to run. After you’ve downloaded xcode (a whoppingly large download), simply run:
xcode-select --install
One of the big problems you’ll likely run into with newer Mac M1 chips: the native solc binaries don’t work natively on M1, so you’ll need to install rosetta
. Any issues with py-solc-x or similar got solved after running the following:
softwareupdate --install-rosetta --agree-to-license
Otherwise, if you hit trouble try installing cython
and cytoolz
before launching Brownie. You may also need to install ganache:
npm install ganache --global
If you run into any issues, please do drop error messages in the comments! Unfortunately we didn’t think to save every error message, but we probably saw them all and will be happy to share our experiences and update the article.
I appreciate the Mac M1 tips at the end! Many of my readers have had issues working with Brownie on their ARM hardware, and I haven't had a good resources to point them to 🙏
I've tried `xcode-select --install`, `softwareupdate --install-rosetta --agree-to-license`, `npm install ganache --global` on my M1 MBP (Python 3.9.6) but still running into an issue where Brownie attempts to download the AMD64 version of solc for OSX. I also tried running it through Docker and with VS Code's Dev Containers, but running into the same underlying ARM issue. Anyone stuck in a similar boat?