You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Focusing on key improvements to enhance the gas efficiency and clarity of the smart contract:
1. Using require Instead of if for Reverts:
Instead of using if statements followed by manual revert calls to handle errors, we switch to using require directly. The require statement is a standard Solidity way of validating conditions before proceeding with function execution. It automatically reverts with a specified message when the condition fails.
This approach saves gas as require is optimized for validation checks in Solidity, reducing the overhead associated with custom if and revert logic.
Example:
require(msg.value >= rides[rideId].rideFare, "Money sent not equal to ride Fare");
if (msg.value < rides[rideId].rideFare)
revert ERROR_CarPooling({
message: "Money sent not equal to ride Fare"
});
Conditionally Emitting Events:
To prevent emitting events unnecessarily, we introduce conditional checks before calling emit statements. Specifically, we verify if a state change has actually occurred before broadcasting it as an event.
This reduces gas consumption by avoiding the creation of event logs when no change has taken place, as events consume a considerable amount of gas.
File: CarPooling.sol
Focusing on key improvements to enhance the gas efficiency and clarity of the smart contract:
1. Using require Instead of if for Reverts:
Instead of using if statements followed by manual revert calls to handle errors, we switch to using require directly. The require statement is a standard Solidity way of validating conditions before proceeding with function execution. It automatically reverts with a specified message when the condition fails.
This approach saves gas as require is optimized for validation checks in Solidity, reducing the overhead associated with custom if and revert logic.
Example:
This replaces:
Ether-Wheels/blockchain/contracts/CarPooling.sol
Line 88 in de4cfae
To prevent emitting events unnecessarily, we introduce conditional checks before calling emit statements. Specifically, we verify if a state change has actually occurred before broadcasting it as an event.
This reduces gas consumption by avoiding the creation of event logs when no change has taken place, as events consume a considerable amount of gas.
Example:
Here, we only emit the
StatusUpdate
event when the completion status of a ride has changed, optimizing the event handling process.These Enhancements would definitely optimize the contract and will save us some gas prize
The text was updated successfully, but these errors were encountered: