How Bancor Dramatically Cut Ethereum Gas Costs

The Bancor Network for decentralized liquidity is fundamentally different than traditional and decentralized exchanges. Whereas trading on exchanges requires matching between buyers and sellers, the Bancor Protocol enables users to convert tokens without a counterparty. Another key differentiator is the Bancor Protocol doesn’t rely on added fees, but rather on the growth of BNT (and thus network usage) for its expansion. The only fees incurred by users are those required to transact with the underlying blockchain (e.g., gas on Ethereum). That said, the smart contracts Bancor uses to enable on-chain liquidity (matching users to smart contract-owned liquidity pools, rather than each other) are computationally intensive and can cause the cost of gas to be relatively higher than other services which simply swap tokens between buyers and sellers (and usually charge added fees).

We were pleased when Ethereum released the Byzantium upgrade and reduced their network gas prices by a landslide. Unfortunately, soon after the release, we noticed a continuous increase in rates which encouraged us to continue evolving independent solutions to this issue as the next Ethereum upgrade could be far out.

We began reviewing gas estimations provided by popular tools and compared these with actual data from the latest blocks and transactions. Our goal was to see if there was any way for us to reduce our gas estimation (which we provide as a helpful tool for users in the Bancor Network UX to prevent transactions from failing) while keeping execution times low.

Our research uncovered that in most blocks, several transactions had a very high gas price relative to ~90% of the other transactions in each block, sometimes having a gwei even upwards of 1000x the next highest transaction. We realized the standard Web3 method for estimating gasPrice uses the median of the last block’s gas price transactions. Bingo! This meant that if in the previous block users happened to send several transactions with unusually high gas prices to the network, the gasPrice method would be fooled into returning a much higher estimation than needed.

This is a back door that hackers can abuse if they want to manipulate the gas prices on the network. Additionally, if this process is done by Ethereum miners, they can push prices up and inflate their own profits with just a handful of overpriced transactions performed every few hours.

As a first step to addressing this issue, Bancor implemented a new gas price calculation method that excludes outsized gas prices from each block. This immediately reduced our estimations to more realistic numbers, much lower than the gasPrice method estimation would yield.

Next, we cross-referenced our numbers against the figures provided by ETH Gas Station and made sure to stay close to their standard (<5 minutes) to ensure Bancor Network transactions would still be executed within 5 minutes or less.

The new gas estimation calculation was pushed to the Bancor Web App on Jan 14th at 4pm (GMT) and immediately cut average gas prices by 50%!

This solution was sound but we knew we could do even better. We initiated a process to predict the gas rates of subsequent blocks based on the gas prices from previous blocks. This prediction process allowed Bancor to reduce rates by an additional 30% on average.

We are monitoring these processes closely and constantly updating them in order to stay ahead of the curve and provide Bancor Network users the best possible rates while converting between any token on our decentralized on-chain liquidity network.

Thanks to Or Dadush, Bancor Solidity developer, for researching, conceiving and developing this new approach.