Gas Optimization Strategies in Cross-Chain Environments | Analog Insights

  • Gas Optimization Strategies in Cross-Chain Environments
  • Gas estimation in cross-chain environments
  • How Analog GMP optimizes cross-chain gas fees
  • Get started with Analog GMP
  • Build with us!
Linkedintwitter
TABLE OF CONTENTS

As the Web3 ecosystem expands, the need for cross-chain interoperability solutions has significantly increased. However, like any technology, cross-chain interoperability solutions present several challenges, with the chief one being the need for a cost-effective gas payment mechanism.

This is especially true in the Ethereum ecosystem, which has long been associated with high gas fees. In a multi-chain world where many blockchains interact with Ethereum, the cross-chain solution you choose should not only abstract gas payments but also allow users to pay as cheaply as possible.

Analog GMP is a secure messaging protocol that allows users to interact with any asset or DApp on any supported chain with a one-click UX. It is designed so that users only pay the gas fees once in native currency on the origin chain without requiring them to post additional transactions on the target chains or perform relays.

In this post, we explore how Analog has applied practical optimization strategies to achieve gas efficiency in a cross-chain environment.

Gas estimation in cross-chain environments

Insufficient gas on target chains is a major reason why DApp users often abandon cross-chain transactions. To solve this challenge, some cross-chain solutions allow users to pay gas fees in multiple tokens. However, this process can become cumbersome and costly for users.

In an ideal setup, you want to build your DApp on an ecosystem that completely abstracts the issue of gas payment from users. The idea behind the cross-chain gas abstraction mechanism is simple: gas fees should be abstracted away from the UX. In other words, DApp users should only pay the gas fees in native currency on the origin chain and leave the cross-chain solution to handle the execution process on the destination chains.

To achieve gas abstraction, the cross-chain solution must implement accurate gas estimation mechanisms, enabling users to leverage a prepaid fee payment model. This mechanism should help ensure the reliable execution of the cross-chain transaction on the destination chain regardless of gas spikes on that chain.

However, in a cross-chain environment, estimating cross-chain gas fees is more complicated than gas computation, which you’re accustomed to in a single blockchain ecosystem. The true cost of the cross-chain gas consists of three fees:


  • The gas fee a user is required to pay on the origin chain.
  • The gas fee it takes for the validators to attest to the transaction on the source chain.
  • The gas fee it takes to relay and execute the transaction on the destination chain.

Fee (3) is the most complex to estimate because of the different tokens that may exist on the destination chains. In Analog GMP, for example, a decentralized oracle service adjusts the gas fees for each deployed Gateway Smart Contract (GSC) based on Ethereum gas prices whenever possible.

Analog's Public Testnet

Be among the first to experience Analog’s Timechain and the tools that power up the next generation of omnichain DApps.

How Analog GMP optimizes cross-chain gas fees

When designing Analog GMP, we meticulously scrutinized the code to optimize cross-chain gas consumption as much as possible. Here are some optimizations that bring the GMP on-chain logic close to its optimal performance:

Optimizing the upgradable proxy

The upgradable proxy is a proxy contract that DApp users use to interact with the deployed GSCs. It is EIP-1167 and EIP-1967-compliant (i.e., popular Solidity patterns that create cheap and minimal proxy clones of contracts). This provides users with as many efficient mechanisms for gas consumption as possible. In addition, the upgradable proxy is ERC-1967 compliant, so Etherscan and other block explorers can recognize it as a proxy.

Use of branchless algorithms to replace conditionals and loops

Using many branches in Solidity can become more expensive because the EVM needs to predict the correct execution path for the code, leading to unpredictable gas costs. For a cross-chain solution like Analog GMP, unpredictable gas costs are problematic for the following reasons:

  • Users are required to buy a specific quantity of gas on the origin chain to execute a message on the target chain. As such, the GSC must be certain that the amount of gas is sufficient to execute the message on that chain.
  • Blockchain networks often charge users based on the worst-case scenario for gas costs. As a result, users will incur high gas costs when sending cross-chain messages if your code has many branches.

Because of the above reasons, Analog GMP is designed with branchless algorithms that consider the worst-case scenario costs. Our BranchlessMath.sol is an extreme optimization library that consumes constant gas cost for any input and generates significant gas savings compared to OpenZeppelin’s Math library.

In addition, Analog GMP is the only granular cross-chain solution when it comes to cross-chain gas costs. As of this writing, Analog GMP is the only solution considering Ethereum calldata cost in gas cost computations.
For example, Analog GMP can calculate the calldata
cost as 21000 + (16 * non_zeros) + (4 * zeros), while others calculate
21000 + (zeros + non_zeros) * 16, as illustrated here.

Minimal on-chain storage

Store and read operations are by far the most expensive on Ethereum (and any other blockchain). To minimize gas costs for Analog GMP, we compressed as much data as possible into a single 256-bit storage slot. Executing a cross-chain message with a single storage slot is gas efficient because each additional slot costs users between $0.05 and $1.00 per message, making it expensive.

Get started with Analog GMP

You can quickly get started with Analog GMP by visiting this link, where you’ll learn how to dispatch messages using the deployed GSCs.

Here are other quick links to get you started with Analog GMP:

Each deployed GSC comes bundled with the estimateMessageCost method, which you can implement or directly call to receive a quote for the gas fees required to execute your message on the destination chain.

function estimateMessageCost(uint16 networkid, uint256 messageSize, uint256 gasLimit)
external
view
returns (uint256)

NetworkInfo storage network = _networkInfo[networkid];
uint256 baseFee = uint256(network.baseFee);
UFloat9x56 relativeGasPrice = network.relativeGasPrice;

// Verify if the network exists
require(baseFee > 0 || UFloat9x56.unwrap(relativeGasPrice) > 0, "unsupported network");

// if the message data is too large, we use the maximum base fee.
baseFee = BranchlessMath.ternary(messageSize > MAX_PAYLOAD_SIZE, 2 ** 256 - 1, baseFee);

// Estimate the cost
return GasUtils.estimateWeiCost(relativeGasPrice, baseFee, uint16(messageSize), 0, gasLimit);

estimateMessageCost has the following parameters:


  • networkid: The destination chain where the contract call will be made.
  • messageSize: The size of the payload in bytes.
  • gasLimit: The gas limit required to execute the payload on the destination

The estimateMessageCost returns a value that serves as a msg.value parameter of the gateway’s submitMessage function. You can use this value (i.e., msg.value) to pass in the native token of your origin chain as the gas fee. For example, you can call the submitMessage function like this:


gateway.submitMessage{value: msg.value}(dest_address, dest_network, GMP_GAS_LIMIT, payload);

Build with us!

Join us on our cross-chain journey and become part of the hundreds of developers building unique projects with Analog GMP. Stay up-to-date with Analog:

Discord: https://discord.com/invite/analog

X: https://x.com/OneAnalog

Telegram: @analogannouncements |  https://t.me/analogtimer

Subscribe to our blog

You Might Also Enjoy

Exploring Cross-Chain Decentralized Exchanges: A Comprehensive Overview

Decentralized Exchanges — or more simply DEXs — are peer-to-peer (P2P) marketplaces that allow crypto traders to trade their tokens directly without handing over the management of their assets to an intermediary.

Read more rightArrow

Bridges are soon obsolete, what comes next? | Analog Insights

Blockchain interoperability has increasingly become essential to how the Web3 ecosystem evolves. As Layer-1s (L1s) and Layer-2s (L2s) increasingly become specialized, their accrued value becomes more fragmented, effectively addressing one challenge but giving rise to another.

Read more rightArrow

Web2 To Web3: Bridging The Gap From The Developers’ Perspective

The true essence of the web is the democratization of communication and knowledge. From its initial roots as Advanced Research Projects Agency Network (ARPANET), the web has been leveraged to close…

Read more rightArrow
Contact Us
Go to Top