Skip to content

Commit

Permalink
Refs #454 -- Added monolithic-architecture.md to general/build/smart-…
Browse files Browse the repository at this point in the history
…contracts/gas-optimization (#620)
  • Loading branch information
jackleeio authored Jun 27, 2024
1 parent 23be4b5 commit 6a1050b
Showing 1 changed file with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
displayed_sidebar: generalSidebar
---

# Use Monolithic Contract Architecture

This tutorial explores how making the architecture of your smart contracts monolithic, rather than having several contracts that communicate with each other, can result in gas savings. Inter-contract calls can be expensive, and by consolidating logic into a single contract, you can avoid these costs, albeit with some trade-offs in terms of complexity and modularity.

In Solidity, the default approach might be to modularize your code into multiple contracts that interact with each other. While this is great for maintainability and separation of concerns, it can lead to increased gas costs due to the overhead of contract calls. By designing a monolithic architecture, where most of the logic resides within a single contract, you can reduce these gas costs.

**Demo Code**

Below are two versions of a contract system. The first one uses multiple contracts that call each other, while the optimized version consolidates the logic into a single contract to minimize contract calls.

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
// Multiple contracts approach
contract ContractA {
uint256 public data;
function setData(uint256 _data) public {
data = _data;
}
function getData() public view returns (uint256) {
return data;
}
}
contract ContractB {
ContractA public contractA;
constructor(address _contractA) {
contractA = ContractA(_contractA);
}
function updateAndFetchData(uint256 _data) public returns (uint256) {
contractA.setData(_data);
return contractA.getData();
}
}
// Monolithic contract approach
contract MonolithicContract {
uint256 public data;
function setData(uint256 _data) public {
data = _data;
}
function updateAndFetchData(uint256 _data) public returns (uint256) {
data = _data;
return data;
}
}
```

In the optimized monolithic version, the `MonolithicContract` handles both setting and getting data within the same contract, eliminating the need for inter-contract calls. This approach can save approximately 20,000 gas units per call compared to the multiple contracts approach.

**Benefits of Monolithic Architecture**

- **Reduced Gas Costs**: Eliminates the overhead of contract calls, leading to significant gas savings.
- **Simplified Gas Management**: Easier to estimate and manage gas consumption when all logic resides in a single contract.

**Trade-offs**

- **Increased Complexity**: The contract might become more complex and harder to maintain.
- **Reduced Modularity**: Harder to reuse components in other contracts or projects.

**Recommendations for Gas Optimization**

🌟 Consolidate logic into fewer contracts to reduce inter-contract call overhead. Aim to implement most of your functionalities within a single contract where feasible. But you need to carefully evaluate the trade-offs between maintainability and gas costs.

0 comments on commit 6a1050b

Please sign in to comment.