The gas costs of executing transactions on the Ethereum network are an inherent limitation of dApp development. In Ethereum smart contracts, each instruction that is executed runs on every node in the network. When nodes verify the integrity of each new block, they validate every transaction. For an application as complex as Matryx, computation costs can quickly become prohibitively expensive.

This July, the Matryx platform was ready for deployment on the Ropsten Testnet, but our heaviest transactions required a significant amount of gas, often exceeding the Ropsten gas limit (about 4.8 million at the time). To deploy a platform with realistic prospects for major scalability and maintenance, we needed to cut computation costs. Our team launched an all-hands-on-deck contract and transaction optimization effort.

Here’s how we did it:

Libraries

A library in Solidity is similar to a contract, but it acts as an isolated piece of source code and doesn’t hold any tokens or storage. A library only needs to be deployed to the blockchain once. Other Solidity contracts can then make calls to the library in the context of the calling contract, modifying the caller’s internal storage, exclusively.

Previously, a new tournament on the Matryx platform created a new tournament contract, containing all necessary code. Multiple tournaments lead to repetitive code on the blockchain. Using libraries, however, we can store shared tournament functions in a single library that tournaments can call. Thus, individual tournaments only need to store their own unique data.

too much gas

The latest Matryx platform release stores most code for tournament, round and submission contracts in libraries. This saved a ton of gas, but was not enough improvement for robust scalability. We needed efficiency, so we decided to go low-level.

Enter Yul

Yul is a low-level inline assembly language for the Ethereum Virtual Machine (EVM). Direct access to the EVM allows us to circumvent some redundancy created by the Solidity compiler for our production code.

The new Matryx platform runs Yul versions of the tournament, round, and submission contracts, putting deployment costs comfortably below the gas limit. After optimization, the submission contract bytecode decreased to 21,406 characters from an original 33,682 — a factor of 1.57.

The substantial reduction of bytecode size means that running transactions is cheaper and faster. Before Yul optimization, tournament creation cost 5,040,063 gas. Now it’s less than half that, at 2,105,859 gas. With a gas price of 1, creating a tournament costs about $1. With truly minimal costs for even the heaviest contracts, Matryx is cheaply adoptable and ready to scale.

Sample Gas Statistics:

(for Platform.createTournament )

Old Platform Local: 5040063 gas

Local: 5040063 gas New Platform Local: 2105859 gas

New Platform Ropsten: 2112832 gas

Next Up

The Ethereum computing platform is still a very new technology. We are thrilled to be working on the cutting edge of possibility and push the limit of Ethereum dApp development. The Matryx team now faces new upcoming challenges that will require creative solutions. Currently, we’re creating a reputation system model to incentivize honest citations and submission attributions. Additionally, we continue to tackle the complications of dApp upgradeability. We aim to create a system that can be updated seamlessly while maintaining the platform data in a decentralized environment.

The Matryx team, led by Sam Hessenauer and Max Howard, continues to use cutting-edge methodologies and frameworks in blockchain engineering to push the limit of what is traditionally common. Through these innovative techniques such as library-driven development, Yul (inline-assembly), and novel upgrade/migration system techniques, the Matryx code is one of the most advanced blockchain smart contract systems to date.

Nanome’s CEO, Steve McCloskey, shares the vision of Nanome/Matryx in his upcoming series of blog posts. Please check out the Matryx git and Steve’s Blog, try the alpha, and stay tuned for our next release!