Neutron Case Study: Optimizing Gas Usage with Contract Pinning

Misang Ryu
Confio
Published in
3 min readApr 16, 2024

Intro

In the fast-paced world of DeFi, efficiency is paramount. Every transaction executed on a blockchain incurs gas fees, and as the popularity of DeFi platforms grows, so does the demand for cost-effective solutions. Contract pinning, a lesser-known optimization technique, has emerged as a powerful tool to reduce gas usage and improve transaction speed. This case study explores how CosmWasm contract pinning was leveraged to optimize gas usage on Astroport, a leading DEX built on the Injective, Neutron, Osmosis, Sei, and Terra blockchain.

Contract Pinning Mechanics Overview

Contract pinning, a pivotal feature of the CosmWasm virtual machine, expedites the initiation of previously stored compiled contract codes by accessing a dedicated in-memory cache. This process significantly reduces startup time to approximately 45µs, compared to the 1.5ms required when loading from disk, marking a substantial 33x acceleration. Unlike the node-specific Least Recently Used (LRU) memory cache, contract pinning ensures uniform performance enhancements across the network, guaranteeing a streamlined experience for users. Notably, this efficiency allows wasmd to levy discounted gas costs, presenting a compelling incentive for utilizing this innovative functionality.

Problem Statement

Astroport was experiencing rising gas fees due to the increased price of the NTRN token. Complex transactions on the platform were becoming prohibitively expensive for users.

Neutron governance proposal A-31 passed. The link is here.

Neutron Governance Proposal

To address the rising gas fees, the Astroport team proposed using contract pinning for the most frequently used smart contracts on their platform. However, implementing contract pinning required a governance vote on the Neutron blockchain, so proposal A-31 was submitted and passed.

Result

Since the proposal passed, contract pinning was successfully implemented immediately. The Astroport team collected on-chain data to analyze its impact on gas usage, as shown below.

The analysis revealed significant improvements, with gas usage decreasing by 15% to 50% across the pinned contracts.

However, one anomaly was detected: a contract showing increased gas usage. Further analysis is underway to better understand this anomaly.

Best Practices

For further information on contract pinning and its implementation, refer to the documentation provided by the CosmWasm project: Documentation Link

Conclusion

Contract pinning emerges as a simple yet powerful tool to optimize gas usage on blockchain platforms. By caching frequently used smart contracts, developers can achieve up to a 2x reduction in gas costs on the pinned contracts, improving efficiency and cost-effectiveness.

Confio prioritizes CosmWasm development for the CosmWasm Gold and Diamond Subscribers, in this case, Neutron

Neutron’s active involvement as a CosmWasm Subscriber allowed for a direct communication channel with Confio, enabling timely issue resolution and proactive support.

We thank the Neutron team for providing valuable insights and collaboration throughout the contract pinning process. Additionally, we would like to express our special thanks to the P2P team for their contributions to data analysis through their Lambda analytics platform. This is the link to the early alpha version of the P2P Lambda tool a link to the early alpha version of the P2P Lambda tool we can refer to https://lambda-demo.p2p.org/.

For more information on CosmWasm Subscription, please visit here.

--

--