The tech behind WardenSwap 2.0 on Optimism

Alchemist Warden
WARDEN Official
Published in
4 min readApr 22, 2022

Representing the best rate and the lowest gas than any other chain!

Since Optimism has a different implementation under the hood compared to other L1 chains e.g. Ethereum, including how gas calculation for each transaction is performed, we have to optimize our smart contract using a different technique than other L1 chains. Therefore, to serve our customers to get the best rate with minimum gas cost, we introduce a new way to further optimize gas usage specifically for L2 chains.

How Optimism Gas Works BTW?

Optimism builds on top of Ethereum to reduce gas costs for transactions by “rollup” multiple transactions into 1 transaction before submitting them to Ethereum chain. For Ethereum chain, there are 3 main layers as a monolithic architecture, which are execution, data availability, and consensus layers. Optimism tries to bring the execution layer out of Ethereum to make it faster and lower gas cost (scalable) while maintaining the same security and data availability as Ethereum.

Nowadays, Optimism has lowered gas costs on the execution layer such that the cost to execute a transaction (L2 gas cost) is lower than 5% of the overall transaction cost. However, 95% of the transaction cost comes from data that have to be stored on Ethereum. The data contains transaction detail that can be used to retry executing the transaction in case the transaction has to be attested. For example, transaction Calldata, signature, etc.

Therefore, in order to lower the gas cost for our WardenSwap contracts as much as possible, we have to lower the cost of data that is stored on Ethereum, which is “transaction Calldata”.

Make Calldata Smol (Small, just for crypto meme)

The smaller data stored on Ethereum, the lower gas cost. Therefore, we introduce a compression technique to make the Calldata smaller using “Address Table” and “Data Serialization”.

Address Table is a smart contract that stores a mapping between contract addresses and their indexes. It can be looked up by put in an index, and it will return the address mapped to it, or vice versa. Therefore, we have developed our WardenSwap contract functions on Optimism to receive the indexes instead, which have smaller data compared to contract addresses, then we convert them to addresses for use in later stages. As you can see that converting between index and address may introduce higher execution cost, but remember that the execution cost on Optimism is only a small percentage of the total gas cost! So, it should be negligible.

Another technique we used is Data Serialization. In order to further reduce the size of Calldata in our smart contract’s functions, we have to encode in such a way that, every variable required by the smart contract function should be a single, serialized value, and each value inside it should be encoded using smallest section as possible. For example, if a value is a number that smaller than 32-bit integer, instead of encoding it as uint256, it should be encoded as uint32. Or, if the number is smaller than 24-bit integer, it should be encoded as uint24. Therefore, the smaller value, the smaller space required.

Optimization Results

We have measured the result by deploying our previous version of WardenSwap smart contracts which is used on other L1 chains, swap them using a specific route, measuring transaction cost, and compare with our new, compressed version.

We tried to swap from ETH to USDC using the following route on both versions of smart contracts:

0.01 ETH — UniswapV3 Fee 0.05% → DAI — UniswapV3 Fee 0.05% → USDC

The previous version of WardenSwap:

https://optimistic.etherscan.io/tx/0xd1e121ce7e517c47d11237bc7dd8bc0beb910a2a49b187cc24dbc0e642d536d2

The new version of WardenSwap (Optimized for Layer-2):

https://optimistic.etherscan.io/tx/0xe6aa6a33d48353231fab149c82b4ea21606c2ccc01419189268c451435219b58

As it is shown that our new version has reduced transaction costs by ~40%, and this is only for swapping a single route. For split trading, the difference in transaction costs is larger (more gas-saving).

With these 2 techniques we used, we can make the transaction Calldata to be much smaller on Layer-2, which significantly reduces transaction cost.

As our best rate algorithm takes gas cost into account when finding the best possible routes, so saving gas fee means finding a better rate at the same time!.

As L2 chains are very early tech and have a high potential for improvements, we will continue optimizing gas costs to always serve our customers the BEST rate with minimum gas COST!

Check it out here 👉 : https://www.wardenswap.finance/#/best-rate-swap?network=optimism

Receive updates and follow us at:
Website | Twitter | Telegram | Discord | Docs| Medium

Warden aims to to accelerate an adoption of a disruptive financial innovations by being the infrastructure of the blockchain technology and community
while serving as an intermediary among people.

--

--