One of the most straightforward ways to save gas is by making your code simpler. The less code there is to execute, the less gas is consumed.
Consider the following function:
Solidity
pragma solidity ^0.8.9;
contract OptimizationExample {
address[] public admins;
function isUserAdmin(address user) public view returns (bool) {
for (uint i = 0; i < admins.length; i++) {
if (admins[i] == user) return true;
}
return false;
}
}
The above function uses a loop, which becomes more costly as more admins are added. A more gas-efficient structure would use a mapping:
Solidity
contract OptimizationExampleOptimized {
mapping(address => bool) public admins;
function isUserAdmin(address user) public view returns (bool) {
return admins[user];
}
}
When working with Ethereum, remember that you’re paying for every bit of storage. If you find yourself storing the same data in multiple places, consider how you might store it once and reference it elsewhere.
Libraries in Solidity are reusable pieces of code. They can help you write modular code, and since they are deployed only once and linked to other contracts, they can save gas in the long run.
Solidity
library SafeMath {
function add(uint a, uint b) internal pure returns (uint) {
uint c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
//... other math functions
}
contract UsingSafeMath {
using SafeMath for uint;
uint public value;
function increment(uint _value) public {
value = value.add(_value);
}
}
The delete
keyword in Solidity allows you to nullify variables, which refund some of the gas. If you’re finished with a particular piece of storage, delete it to claim this refund.
While storing data on the blockchain can be costly, logging events is comparatively cheaper. If you have data that doesn’t need to be read from the contract but should be verifiable, consider using event logs.
Some operations are more gas-intensive than others. For instance:
Smart contract optimisation for gas is both an art and a science. The approaches discussed in this course, when combined, have the potential to drastically lower the gas costs of your contracts. Keep in mind that the Ethereum ecosystem and its tools, including Remix, are always changing. Keep up to date, and test and improve your contracts on a regular basis for optimisation. The Ethereum community is large and helpful, so never be afraid to seek guidance or best practices. Have fun coding!
One of the most straightforward ways to save gas is by making your code simpler. The less code there is to execute, the less gas is consumed.
Consider the following function:
Solidity
pragma solidity ^0.8.9;
contract OptimizationExample {
address[] public admins;
function isUserAdmin(address user) public view returns (bool) {
for (uint i = 0; i < admins.length; i++) {
if (admins[i] == user) return true;
}
return false;
}
}
The above function uses a loop, which becomes more costly as more admins are added. A more gas-efficient structure would use a mapping:
Solidity
contract OptimizationExampleOptimized {
mapping(address => bool) public admins;
function isUserAdmin(address user) public view returns (bool) {
return admins[user];
}
}
When working with Ethereum, remember that you’re paying for every bit of storage. If you find yourself storing the same data in multiple places, consider how you might store it once and reference it elsewhere.
Libraries in Solidity are reusable pieces of code. They can help you write modular code, and since they are deployed only once and linked to other contracts, they can save gas in the long run.
Solidity
library SafeMath {
function add(uint a, uint b) internal pure returns (uint) {
uint c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
//... other math functions
}
contract UsingSafeMath {
using SafeMath for uint;
uint public value;
function increment(uint _value) public {
value = value.add(_value);
}
}
The delete
keyword in Solidity allows you to nullify variables, which refund some of the gas. If you’re finished with a particular piece of storage, delete it to claim this refund.
While storing data on the blockchain can be costly, logging events is comparatively cheaper. If you have data that doesn’t need to be read from the contract but should be verifiable, consider using event logs.
Some operations are more gas-intensive than others. For instance:
Smart contract optimisation for gas is both an art and a science. The approaches discussed in this course, when combined, have the potential to drastically lower the gas costs of your contracts. Keep in mind that the Ethereum ecosystem and its tools, including Remix, are always changing. Keep up to date, and test and improve your contracts on a regular basis for optimisation. The Ethereum community is large and helpful, so never be afraid to seek guidance or best practices. Have fun coding!