Skip to main content

Contract Factory Design: Reducing Blockchain Gas Costs through Templates and Tight Packing

· 4 min read
Pay Protocol Dev
Pay Protocol Dev
Pay Protocol Development Team

In the past decade, decentralized payments typically used EOA (Externally Owned Accounts) wallet solutions, including existing MPC solutions based on EOA wallet sharding. However, achieving fully decentralized payments without any custodial servers requires an all-contract solution for fund custody. The optimization of gas costs in full-contract solutions has been a core challenge for developers.

While full-contract solutions offer high security, optimizing their gas costs remains a core challenge for developers. This article will explore how to significantly reduce gas costs and promote the application of sub-contracts in real scenarios through contract factory template design and tight packing inline assembly solutions.

1. Contract Factory Template Design: Dynamic Generation and Cost Optimization

Traditional Problems

Traditional contract deployment requires deploying complete code for each sub-contract separately, resulting in high gas consumption and redundant on-chain code storage. To address this issue, we designed and implemented the contract factory template pattern.

Core Concepts

Contract factory templates are mechanisms that dynamically generate sub-contracts through predefined template contracts. By mapping template contract addresses to generated sub-contracts, code reuse can be achieved. This design has several key features:

  • Dynamic Generation: Factory contracts dynamically create sub-contracts based on needs, avoiding repeated template logic deployment.
  • Reduced Redundancy: All sub-contracts share the same template code, significantly reducing on-chain storage costs.
  • Gas Savings: Sub-contract generation only requires passing minimal parameters, avoiding full code deployment and reducing gas consumption.

Practical Application Scenarios

For example, in multi-signature wallet scenarios, users can dynamically generate sub-contracts to execute tasks based on transaction requirements. Compared to traditional sub-wallet solutions, this approach is more flexible and efficient, making it possible for sub-contracts to replace traditional sub-wallet solutions.

2. Tight Packing Inline Assembly: Ultimate Optimization for Batch Transfers

Transfer operations are among the most common and gas-intensive scenarios in blockchain. To further optimize, we adopted a tight packing inline assembly solution that effectively reduces gas costs by minimizing transaction payload size and streamlining code logic.

Solution Details

  1. Parameter Tight Packing
    • Use minimum byte representation for parameters (such as addresses, amounts) off-chain.
    • Compress parameter storage size to reduce batch transfer payload volume.
  2. Inline Assembly Optimization
    • Extensive use of inline assembly instructions provided by EVM (Ethereum Virtual Machine) in transfer logic, these low-level operations directly manipulate the EVM stack, avoiding overhead from high-level languages.
  3. Batch Transfers
    • Combine multiple transfers into a single call, reducing transaction count and further lowering total gas consumption.

3. Practical Results

Gas Cost Reduction

Through contract factory templates and tight packing solutions, test results show:

  • Gas consumption for contract deployment and calls reduced by approximately 40%-70% compared to traditional methods.

Example: Sub-contract Consolidation Gas Comparison Table

Batch Operations(USDT)Total Gas LimitAverage Gas Limit per OperationTraditional 1.0 Transfer Gas LimitEstimated Savings vs 1.0 Method
Create 10 Sub-contracts409,38340,938Requires small ETH for gasOnly needs 1 creation
Create 50 Sub-contracts386,44038,644
Consolidate 10 Sub-contracts156,01515,60154,17771.2%
Consolidate 50 Sub-contracts135,58013,55854,17774.9%

Test Date: July 29, 2024

Enhanced User Experience

  • Significantly reduced user operation costs and gas fees
  • More efficient on-chain logic enables faster transaction confirmation

Support for Complex Scenarios

This optimization solution is particularly suitable for:

  • Multi-signature wallets
  • Large-scale DApp batch operations
  • High-frequency smart contract interaction scenarios

Through contract factory templates and tight packing inline assembly solutions, we have not only solved the gas cost optimization challenge but also provided more flexible and efficient solutions for practical application scenarios. In the future, this design will play a crucial role in more complex blockchain scenarios.