Image for post
Image for post

Testing EOSIO smart contracts with Hydra

Testing EOSIO smart contracts was always a big pain point for me and many other developers, especially newcomers. I probably spent more time setting up and managing local testnets, or writing shell scripts to automate this process, than actually writing the smart contract.

There are currently some testing frameworks that try to make this process easier, but they are all just a wrapper on top of your local EOSIO node’s chain_api. This means you are still required to have EOSIO installed locally, correctly set up, and ensure it is running whenever you run your tests. This comes with all the restrictions, limitations and disadvantages of running a local blockchain:

  • Require EOSIO and/or Docker to be installed on your system, making it infeasible to run on low-resource systems like CI pipelines.
  • Running a local blockchain requires significant CPU and storage resources, slowing down the rest of your system.
  • Cannot run tests in parallel because all operate on the same local blockchain node.
  • You are restricted to the 500ms block production time, even though your transactions might run in microseconds.

Over a year ago I had the idea of creating a testing environment that just uses a WebAssembly interpreter like EOSVM to execute the compiled smart contract — removing all features that are not necessary for executing transactions, such as block production, CPU/NET/RAM and other resource billing.

I’m excited to announce that I finally had the opportunity to work on this with the Klevoya Team and release the first version of Hydra today. 🎊

Hydra Features

Hydra is a simple and fast EOSIO smart contract test and execution environment. With Hydra you can quickly create and execute test cases without needing to maintain and run a local blockchain. All within minutes. — Hydra

Some of Hydra’s features include:

  • Test EOSIO smart contracts without running a local node
  • Run tests in parallel. Run integration tests in CI.
  • Bootstrap command to quickly get started with testing your smart contracts
  • Load initial contract table data through JSON files

The way Hydra works is by creating a local EOSIO-compatible snapshot whenever an action like creating accounts or deploying contracts is done. To run a transaction it communicates with the backend to simulate all EOSIO checks and execute the transaction in the EOSVM with the snapshot’s context. The updated snapshot, which can contain updated contract tables or even new account creations from running the contract code, is returned and usable by the client.

Example Test

Hydra is written in NodeJS and tests are written in JavaScript or TypeScript. The Hydra CLI can be used to quickly bootstrap tests for smart contracts through the init command, which allows you to select which contracts you want to test and what tables you want to fill with initial data.

Let’s test something a bit more complicated than an eosio.token contract to showcase the strength of Hydra. In reality, most dapps consist of several smart contracts that interact with each other in a certain way. For example, the open-source Bancor protocol allows anyone to trade tokens without an order-book and finding a trading counterparty. The price of the trade is set by the Bancor protocol and changes based on the liquidity of the tokens provided. It consists of several contracts:

  1. A multi-token contract: This is just a standard eosio.token contract that manages multiple tokens.
  2. The Bancor Network token (BNT) which acts as a base token for every trade.
  3. Some token we want to be able to trade for BNT, let's call it the AAAA token.
  4. The Bancor Network contract: It’s the entry point for any Bancor trade and the funds need to be sent to this contract.
  5. A multi-converter contract: This is the brain of all contracts that holds the funds (called “reserve”) for all tradable tokens and does the conversion. We can’t directly trade BNT tokens for our AAAA tokens though, there is an intermediate token for each trading pair (called a smart token), in our case BNTAAAA. I won't go into details why this token exists, it's enough to know that it exists and trading BNT for AAAA theoretically trades BNT -> BNTAAAA -> AAAA.

For comparison, the way Bancor is currently tested is by first creating all the accounts, tokens, and setting the code by running three shell scripts which themself run cleos commands. You'll need to reset your local blockchain after every test and run these scripts again. The actual tests are written in JavaScript using eosjs to communicate with the local nodeos. A lot of helper functions were defined to make working with eosjs easier.

Let’s write a test using Hydra that sets all of this up, and then trades BNT for AAAA and we'll see if the math checks out. ✅

After compiling the Bancor contracts, we can run the hydra init command, selecting the BancorConverter and BancorNetwork contracts as the contracts to scaffold tests for. The init command creates a tests/BancorConverter.test.js file, the hydra.yml config file, and installs jest, a modern JavaScript testing framework.

We edit the example test file to create the accounts and set the contracts:

Creating initial tokens

Equipping accounts with some initial token balances is needed a lot in testing and Hydra makes this especially easy with its initial table loading feature.

We will create JSON files for the eosio.token’s accounts and stat table and provide our test accounts with initial balances. The JSON fixures must be placed in the tests/fixtures/eosio.token directory:

Here, we created the AAAA and BNT tokens, and we can now define the actual balances for accounts in the accounts.json file:

To load this JSON data, we create the token account, deploy the eosio.token contract, one of the pre-defined contract templates, and call loadFixtures(). This is a lot more convenient than writing down all the create, issue, and transfer actions required to bring the blockchain into the same state.

The feature to load table data from JSON files requires adding a helper function to your smart contract. Read more about it here.

Setting up Bancor

As the next step, we can write our first test to set up the Bancor converter.

First, we run some actions on the converter contract, which can be done by accessing the converter.contract object and specifying the action to invoke with its data. The authorization defaults to the contract account's active permission and can be changed by passing a second authorization argument. After running these actions, we check the contract's table data using the account.getTableRowsScoped method. As the name suggests, it returns the table data for the specified table name as a mapping from scopes to data entries.

Submitting the trade

We can add another test executing a trade and check if the Bancor algorithm was correctly implemented.

So far, we have 10 BNT and 10 AAAA tokens in the converter's funds. If a user puts in 20 more BNT, they are responsible for 2/3 of the BNT funds. This allows them to take out 2/3 of the AAAA funds, so we'd expect 6.6666 AAAA to be returned.

The test passes and we indeed get back the expected amount of AAAA tokens, increasing the price of AAAA tokens for the next user. The complete test file used for this example can be seen in this GitHub gist and turns out to be less than 200 LoC.

Hydra allowed us to easily write tests without any additional setup scripts or local blockchain.
If you want to learn more about Hydra check out the FAQ or dive right into the documentation.

Written by

Full Stack Software Engineer #javascript #EOS. Into Recreational Math / CS 🤯 Just message me about anything, my mind is open.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store