-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a1050b
commit f818148
Showing
8 changed files
with
396 additions
and
0 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
...nt-docs/current/general/build/smart-contracts/gas-optimization/using-payable.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
--- | ||
displayed_sidebar: generalSidebar | ||
--- | ||
|
||
# Using Payable | ||
|
||
In Solidity, leveraging the `payable` keyword can be a subtle yet effective way to optimize gas usage. In this article, we explore two distinct scenarios where using `payable` can lead to gas savings: in constructors and admin functions. | ||
|
||
### Payable Constructor | ||
|
||
The way you write constructors can influence the deployment cost of your contracts, particularly with respect to gas usage. The Ethereum Virtual Machine (EVM) requires gas for all operations, including contract deployment. | ||
|
||
**Demo Code** | ||
|
||
Below, we have two simple contracts, `BasicConstructor` and `AdvancedConstructor`. Both are minimal, but they differ in whether the constructor is marked as `payable`. | ||
|
||
- `BasicConstructor` has a non-payable constructor. | ||
- `AdvancedConstructor` has a payable constructor, allowing it to receive Ether during deployment. | ||
|
||
```solidity | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
contract BasicConstructor { | ||
constructor() {} // Gas: 67161 | ||
} | ||
contract AdvancedConstructor { | ||
constructor() payable {} // Gas: 67102 | ||
} | ||
``` | ||
|
||
When deployed, the `AdvancedConstructor` uses less gas (67,102 gas) compared to the `BasicConstructor` (67,161 gas). Although the difference is minor, marking a constructor as `payable` does not necessarily increase deployment costs and in this case, it marginally reduces them. | ||
|
||
The difference in gas cost can be attributed to how the EVM handles the deployment bytecode. The `payable` modifier might influence the constructor's bytecode slightly differently than a non-payable one, potentially due to optimizations in how storage access and function accessibility are handled during deployment. | ||
|
||
### Payable Admin Functions | ||
|
||
Admin functions can be made payable to save gas. Making admin-specific functions payable reduces gas costs because the compiler won't check the callvalue of the function. This approach also makes the contract smaller and cheaper to deploy as there will be fewer opcodes in the creation and runtime code. | ||
|
||
**Demo Code** | ||
|
||
Below is an example demonstrating how to implement payable admin functions: | ||
|
||
```solidity | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
contract Admin { | ||
address public admin; | ||
constructor() { | ||
admin = msg.sender; | ||
} | ||
modifier onlyAdmin() { | ||
require(msg.sender == admin, "Only admin can call this function"); | ||
_; | ||
} | ||
// gas: 23348 | ||
function adminFunction() external onlyAdmin { | ||
// Admin specific logic here | ||
} | ||
} | ||
contract AdminPayable { | ||
address public admin; | ||
constructor() { | ||
admin = msg.sender; | ||
} | ||
modifier onlyAdmin() { | ||
require(msg.sender == admin, "Only admin can call this function"); | ||
_; | ||
} | ||
// gas: 23324 | ||
function adminFunction() external payable onlyAdmin { | ||
// Admin specific logic here | ||
} | ||
} | ||
``` | ||
|
||
In this example, the `adminFunction` is payable, which can help save gas costs. | ||
|
||
**Recommendations for Gas Optimization** | ||
|
||
🌟 Using a `payable` modifier in a constructor will slightly reduce gas costs during contract deployment. | ||
|
||
🌟 Consider making admin functions payable to reduce gas costs associated with value checks by the compiler. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...nt-docs/current/general/build/smart-contracts/gas-optimization/using-payable.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
--- | ||
displayed_sidebar: generalSidebar | ||
--- | ||
|
||
# Using Payable | ||
|
||
In Solidity, leveraging the `payable` keyword can be a subtle yet effective way to optimize gas usage. In this article, we explore two distinct scenarios where using `payable` can lead to gas savings: in constructors and admin functions. | ||
|
||
### Payable Constructor | ||
|
||
The way you write constructors can influence the deployment cost of your contracts, particularly with respect to gas usage. The Ethereum Virtual Machine (EVM) requires gas for all operations, including contract deployment. | ||
|
||
**Demo Code** | ||
|
||
Below, we have two simple contracts, `BasicConstructor` and `AdvancedConstructor`. Both are minimal, but they differ in whether the constructor is marked as `payable`. | ||
|
||
- `BasicConstructor` has a non-payable constructor. | ||
- `AdvancedConstructor` has a payable constructor, allowing it to receive Ether during deployment. | ||
|
||
```solidity | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
contract BasicConstructor { | ||
constructor() {} // Gas: 67161 | ||
} | ||
contract AdvancedConstructor { | ||
constructor() payable {} // Gas: 67102 | ||
} | ||
``` | ||
|
||
When deployed, the `AdvancedConstructor` uses less gas (67,102 gas) compared to the `BasicConstructor` (67,161 gas). Although the difference is minor, marking a constructor as `payable` does not necessarily increase deployment costs and in this case, it marginally reduces them. | ||
|
||
The difference in gas cost can be attributed to how the EVM handles the deployment bytecode. The `payable` modifier might influence the constructor's bytecode slightly differently than a non-payable one, potentially due to optimizations in how storage access and function accessibility are handled during deployment. | ||
|
||
### Payable Admin Functions | ||
|
||
Admin functions can be made payable to save gas. Making admin-specific functions payable reduces gas costs because the compiler won't check the callvalue of the function. This approach also makes the contract smaller and cheaper to deploy as there will be fewer opcodes in the creation and runtime code. | ||
|
||
**Demo Code** | ||
|
||
Below is an example demonstrating how to implement payable admin functions: | ||
|
||
```solidity | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
contract Admin { | ||
address public admin; | ||
constructor() { | ||
admin = msg.sender; | ||
} | ||
modifier onlyAdmin() { | ||
require(msg.sender == admin, "Only admin can call this function"); | ||
_; | ||
} | ||
// gas: 23348 | ||
function adminFunction() external onlyAdmin { | ||
// Admin specific logic here | ||
} | ||
} | ||
contract AdminPayable { | ||
address public admin; | ||
constructor() { | ||
admin = msg.sender; | ||
} | ||
modifier onlyAdmin() { | ||
require(msg.sender == admin, "Only admin can call this function"); | ||
_; | ||
} | ||
// gas: 23324 | ||
function adminFunction() external payable onlyAdmin { | ||
// Admin specific logic here | ||
} | ||
} | ||
``` | ||
|
||
In this example, the `adminFunction` is payable, which can help save gas costs. | ||
|
||
**Recommendations for Gas Optimization** | ||
|
||
🌟 Using a `payable` modifier in a constructor will slightly reduce gas costs during contract deployment. | ||
|
||
🌟 Consider making admin functions payable to reduce gas costs associated with value checks by the compiler. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...nt-docs/current/general/build/smart-contracts/gas-optimization/using-payable.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
--- | ||
displayed_sidebar: generalSidebar | ||
--- | ||
|
||
# Using Payable | ||
|
||
In Solidity, leveraging the `payable` keyword can be a subtle yet effective way to optimize gas usage. In this article, we explore two distinct scenarios where using `payable` can lead to gas savings: in constructors and admin functions. | ||
|
||
### Payable Constructor | ||
|
||
The way you write constructors can influence the deployment cost of your contracts, particularly with respect to gas usage. The Ethereum Virtual Machine (EVM) requires gas for all operations, including contract deployment. | ||
|
||
**Demo Code** | ||
|
||
Below, we have two simple contracts, `BasicConstructor` and `AdvancedConstructor`. Both are minimal, but they differ in whether the constructor is marked as `payable`. | ||
|
||
- `BasicConstructor` has a non-payable constructor. | ||
- `AdvancedConstructor` has a payable constructor, allowing it to receive Ether during deployment. | ||
|
||
```solidity | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
contract BasicConstructor { | ||
constructor() {} // Gas: 67161 | ||
} | ||
contract AdvancedConstructor { | ||
constructor() payable {} // Gas: 67102 | ||
} | ||
``` | ||
|
||
When deployed, the `AdvancedConstructor` uses less gas (67,102 gas) compared to the `BasicConstructor` (67,161 gas). Although the difference is minor, marking a constructor as `payable` does not necessarily increase deployment costs and in this case, it marginally reduces them. | ||
|
||
The difference in gas cost can be attributed to how the EVM handles the deployment bytecode. The `payable` modifier might influence the constructor's bytecode slightly differently than a non-payable one, potentially due to optimizations in how storage access and function accessibility are handled during deployment. | ||
|
||
### Payable Admin Functions | ||
|
||
Admin functions can be made payable to save gas. Making admin-specific functions payable reduces gas costs because the compiler won't check the callvalue of the function. This approach also makes the contract smaller and cheaper to deploy as there will be fewer opcodes in the creation and runtime code. | ||
|
||
**Demo Code** | ||
|
||
Below is an example demonstrating how to implement payable admin functions: | ||
|
||
```solidity | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
contract Admin { | ||
address public admin; | ||
constructor() { | ||
admin = msg.sender; | ||
} | ||
modifier onlyAdmin() { | ||
require(msg.sender == admin, "Only admin can call this function"); | ||
_; | ||
} | ||
// gas: 23348 | ||
function adminFunction() external onlyAdmin { | ||
// Admin specific logic here | ||
} | ||
} | ||
contract AdminPayable { | ||
address public admin; | ||
constructor() { | ||
admin = msg.sender; | ||
} | ||
modifier onlyAdmin() { | ||
require(msg.sender == admin, "Only admin can call this function"); | ||
_; | ||
} | ||
// gas: 23324 | ||
function adminFunction() external payable onlyAdmin { | ||
// Admin specific logic here | ||
} | ||
} | ||
``` | ||
|
||
In this example, the `adminFunction` is payable, which can help save gas costs. | ||
|
||
**Recommendations for Gas Optimization** | ||
|
||
🌟 Using a `payable` modifier in a constructor will slightly reduce gas costs during contract deployment. | ||
|
||
🌟 Consider making admin functions payable to reduce gas costs associated with value checks by the compiler. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.