Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refs #454 -- Added predicting-contract-address to general/build/smart-contracts/gas-optimization #596

Merged
merged 3 commits into from
May 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
displayed_sidebar: generalSidebar
---

# Predicting Contract Addresses Using Account Nonce

In Solidity, predicting contract addresses before their deployment can save substantial gas, especially when deploying interdependent contracts. This method eliminates the need for setter functions and storage variables, which are costly in terms of gas usage. We can use the [LibRLP](https://github.com/Vectorized/solady/blob/6c54795ef69838e233020e9ab29f3f6288efdf06/src/utils/LibRLP.sol#L27) library from Solady to deterministically compute the addresses based on the deployer's nonce.

In scenarios where two contracts are interdependent, like a `DataStorage` contract and a `DataWriter` contract, managing their addresses effectively is crucial. Typically, after deploying both contracts, you would set the address of one contract in the other using a setter function. However, this approach involves multiple transactions and storage operations, which are expensive.

**DemoCode**

Instead of using setter functions, we can compute the address of each contract before deployment and pass these addresses to the constructors, reducing gas costs.

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

import {LibRLP} from "https://github.com/vectorized/solady/blob/main/src/utils/LibRLP.sol";

contract DataStorage {
address immutable public dataWriter;
uint256 public value;

constructor(address _dataWriter) {
dataWriter = _dataWriter;
}

// cost: 47158 gas
function storeValue(uint256 newValue) external {
require(msg.sender == address(dataWriter), "Only DataWriter can update value");
value = newValue;
}
}

contract DataWriter {
DataStorage immutable public dataStorage;

constructor(DataStorage _dataStorage) {
dataStorage = _dataStorage;
}

function writeValue(uint256 newValue) external {
dataStorage.storeValue(newValue);
}
}

contract DeploymentManager {
using LibRLP for address;

function deployContracts() public returns(DataStorage dataStorage, address dataWriter) {
DataStorage precomputedDataStorage = DataStorage(address(this).computeAddress(2));
dataWriter = address(new DataWriter(precomputedDataStorage));
dataStorage = new DataStorage(dataWriter);

require(dataStorage == precomputedDataStorage, "Computed address mismatch");
}
}
```

By precomputing addresses and using immutable storage for contract references, we can reduce the gas cost of writeValue(). This method not only lowers gas consumption but also simplifies the deployment process by eliminating the need for post-deployment setup calls.


Recommendations for gas optimization:

🌟 Use the account nonce to predict the addresses of interdependent smart contracts, reducing gas consumption and simplifying deployment by avoiding storage variables and post-deployment setup calls.
Loading