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 SelfDestruct to general/build/smart-contracts/gas-optimization #602

Merged
merged 2 commits into from
Jun 6, 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,51 @@
---
displayed_sidebar: generalSidebar
---

# Using selfdestruct in Constructors

In Solidity, optimizing gas usage is crucial for creating efficient smart contracts. One technique involves using the `selfdestruct` function within the constructor for contracts designed for one-time use. This approach can reduce gas costs by eliminating the contract from the blockchain once its purpose is fulfilled.

Contracts are sometimes used to deploy several contracts in a single transaction. In cases where the only function of a contract is the code within the constructor, invoking `selfdestruct` at the end of the constructor can save gas. This is because `selfdestruct` removes the contract from the blockchain, thus reclaiming the storage and returning any remaining Cfx to a specified address.

**Demo Code**

To demonstrate this, the contract `SelfDestructExample` deploys another contract in its constructor and then self-destructs, optimizing gas usage.

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

contract DeployedContract {
uint256 public value;

constructor(uint256 _value) {
value = _value;
}
}

contract SelfDestructExample {
constructor(uint256 _value) {
// Deploy another contract
DeployedContract newContract = new DeployedContract(_value);

// Perform any required operations here...

// Self-destruct the contract to save gas
selfdestruct(payable(msg.sender));
}
}
```

**Using `selfdestruct` in the Constructor**

1. **Deploy another contract**: In the example, the `SelfDestructExample` contract deploys an instance of `DeployedContract` in the constructor.
2. **Perform required operations**: Any operations that need to be done during the deployment can be performed.
3. **Invoke `selfdestruct`**: Finally, the `selfdestruct` function is called to remove the contract from the blockchain, returning any remaining Cfx to the deployer's address.


Using `selfdestruct` in the constructor for one-time-use contracts is a effective technique for gas optimization in Solidity, it helps in reducing gas costs and cleaning up the blockchain state.

Recommendations for gas optimization:

🌟 For contracts that only perform operations within the constructor and do not need to persist on the blockchain, use `selfdestruct` at the end of the constructor to save on gas costs and clean up the state.
Loading