-
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.
Merge branch 'main' into issue#454uint
- Loading branch information
Showing
531 changed files
with
21,846 additions
and
818 deletions.
There are no files selected for viewing
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
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
docs/general/build/smart-contracts/gas-optimization/constant.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
51 changes: 51 additions & 0 deletions
51
docs/general/build/smart-contracts/gas-optimization/error.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,51 @@ | ||
--- | ||
displayed_sidebar: generalSidebar | ||
--- | ||
# Error | ||
|
||
In Solidity, developers can define errors in three main forms: `revert`, `require`, and `assert`. The main differences between these methods from a functional perspective are two-fold: | ||
|
||
1. Whether they can throw developer-defined error reasons; | ||
2. Whether they can throw developer-defined errors carrying variables; | ||
|
||
The main differences are as follows: | ||
|
||
| Type | Custom Reason | Carries Variable | Example | | ||
|----------|---------------|------------------|----------------------------------------------------------------| | ||
| `revert` | ✅ | ✅ | Reason: UnauthorizedAccess(0x05D01CAF54524A610CCF187082201120757f7AE5) | | ||
| `require`| ✅ | ❌ | Reason: UnauthorizedAccess | | ||
| `assert` | ❌ | ❌ | Reason: Assertion violated | | ||
|
||
**DemoCode** | ||
|
||
Below, we use the three forms of errors to observe the changes in gas usage: | ||
|
||
```solidity | ||
contract Error { | ||
error UnauthorizedAccess(); | ||
// gas: 164 | ||
function errorRevert() external pure { | ||
if (true) revert UnauthorizedAccess(); | ||
} | ||
// gas: 268 | ||
function errorRequire() external pure { | ||
require(false, "UnauthorizedAccess"); | ||
} | ||
// gas: 180 | ||
function errorAssert() external pure { | ||
assert(false); | ||
} | ||
} | ||
``` | ||
|
||
the gas optimization suggestions are as follows: | ||
|
||
🌟1. `revert` is the most recommended as it can throw error messages as well as related variables. | ||
|
||
🌟2. The string in `require` is stored on-chain, which not only consumes more gas but also increases the contract size. It is recommended to choose based on actual needs. | ||
|
||
🌟3. If there's a scenario where `assert` is used, it's suggested to replace it with `revert`. | ||
|
42 changes: 42 additions & 0 deletions
42
docs/general/build/smart-contracts/gas-optimization/memoryAndCalldata.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,42 @@ | ||
--- | ||
displayed_sidebar: generalSidebar | ||
sidebar_position: 2 | ||
--- | ||
# Memory vs Calldata | ||
|
||
1. `memory`: Typically used for function parameters and temporary variables within functions. Stored in memory and not persistent on the blockchain. | ||
|
||
2. `calldata`: Similar to memory, stored in memory and not persistent on the blockchain. The key difference is that calldata variables are immutable and commonly used for function parameters. | ||
|
||
|
||
Learn more: | ||
[Data location and assignment behavior](https://docs.soliditylang.org/en/latest/types.html#data-location) | ||
|
||
Below, we demonstrate how to write data using both `calldata` and `memory` | ||
|
||
```solidity | ||
contract CalldataAndMemory { | ||
struct Confi { | ||
uint16 age; | ||
string name; | ||
string wish; | ||
} | ||
Confi John; | ||
Confi Jane; | ||
function writeToJohn(Confi calldata JohnData) external { | ||
John = JohnData; | ||
} | ||
function writeToJane(Confi memory JaneData) external { | ||
Jane = JaneData; | ||
} | ||
} | ||
``` | ||
|
||
Recommendations for gas optimization: | ||
|
||
🌟 In practical situations, if it's possible to use calldata, it is recommended to use `calldata` instead of `memory`. | ||
|
||
|
42 changes: 42 additions & 0 deletions
42
docs/general/build/smart-contracts/gas-optimization/unchecked.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,42 @@ | ||
--- | ||
displayed_sidebar: generalSidebar | ||
|
||
--- | ||
# Unchecked | ||
|
||
We know that before the Solidity version 0.8, it was necessary to manually import the SafeMath library to ensure data safety and avoid overflow, thereby preventing overflow attacks. | ||
|
||
After Solidity version 0.8, Solidity performs a check every time there is a data change to determine whether there is an overflow, thus deciding whether to throw an exception. | ||
|
||
This also means that the check incurs additional gas costs. By using `unchecked` wisely, it is possible to effectively remove the intermediate checking step, thus achieving the purpose of saving gas. | ||
|
||
Learn more: [Checked or Unchecked Arithmetic](https://docs.soliditylang.org/en/v0.8.25/control-structures.html#checked-or-unchecked-arithmetic) | ||
|
||
**Demo Code** | ||
|
||
Below, we demonstrate using both a conventional for-loop and an `unchecked` for-loop. Note that since `iterations` is already of type `uint256`, there will not be an overflow issue. | ||
|
||
```solidity | ||
contract UncheckedExample { | ||
// gas: 1910309 | ||
function conventionalForLoop(uint256 iterations) external pure returns (uint256 result) { | ||
for (uint256 index = 0; index < iterations; index++) { | ||
result = index + 1; | ||
} | ||
} | ||
// gas: 570287 | ||
function uncheckedForLoop(uint256 iterations) external pure returns (uint256 result) { | ||
for (uint256 index = 0; index < iterations; ) { | ||
unchecked { | ||
result = index + 1; | ||
index++; | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Recommendations for gas optimization: | ||
|
||
🌟 In situations where security is controllable, the unchecked block can be used to save gas. |
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
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
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,40 @@ | ||
--- | ||
sidebar_position: 3 | ||
title: For dApp Developers | ||
keywords: | ||
- dApp | ||
--- | ||
|
||
Decentralized Applications, or dApps, represent the next generation of app development, leveraging blockchain's potential to offer a more transparent, secure, and user-centric digital experience. The key differentiation lies in the backend: instead of relying on centralized servers, dApps use blockchain as their backbone. As dApps allow for direct integration with users' wallets, they can facilitate seamless and trustless peer-to-peer interactions. | ||
|
||
Each step of the journey to build a dApp offers unique considerations, insights, and challenges. Below are several sections with summarized descriptions to guide developers through a wealth of internal resources available: | ||
|
||
## Basics for Conflux Development | ||
|
||
|Title|Description|Key Subjects| | ||
|---|---|---| | ||
| **Getting Started on [Core Space](../core/getting-started/installing-a-wallet.md) & [eSpace](../espace/UserGuide.md)** | User guides for Conflux Core Space and Conflux eSpace | Wallet installation, Testnet faucet, User guide | | ||
| **Network endpoints: [Core](../core/core-endpoints.md) & [eSpace](../espace/network-endpoints.md)** | Public network endpoints of Conflux Core Space and Conflux eSpace, enabling you to connect to blockchain via JSON-RPC | Chain id, Network endpoints | | ||
| **SDKs or Tools for [Core Space](/docs/category/sdks-and-tools) & [eSpace](../espace/DeveloperQuickstart.md)** | SDKs and Tools to assist in building web3 applications | SDK, Toolchain, Hardhat | | ||
|
||
## Become Acquainted With Smart Contracts | ||
|
||
|Title|Description|Key Subjects| | ||
|---|---|---| | ||
| [**Introduction to Smart Contracts**](../general/conflux-basics/contracts.md)| An overview and basic introduction to smart contracts | Concept | | ||
| [**Smart Contracts Development**](/docs/category/smart-contracts) | Basic understanding of how to develop smart contracts, including the smart contracts development workflow, elementary Solidity syntax, and prevalent token standards | Solidity, Token Standard | | ||
|[**How to Deploy Contract on eSpace**](/docs/category/how-to-deploy-contract)|Guidelines on how to utilize build tools (like hardhat, remix) to deploy contracts in **Conflux eSpace** | Hardhat, Remix, Thirdweb | | ||
|
||
## Frontend Development | ||
|
||
|Title|Description|Key Subjects| | ||
|---|---|---| | ||
| [**Fluent Integration**](../core/build/fluent.md)| Guidance on integrating Fluent wallet into your frontend | Wallet Connection, Fluent wallet | | ||
| [**use-wallet**](/docs/category/smart-contracts)| `use-wallet` is a front-end oriented wallet hooks library designed to provide swift development support for lightweight dapps. `use-wallet` is compatible with conflux chains and ethereum chains, with integration support for the React and Vue3 frameworks. | Wallet Connection, Fluent wallet, MetaMask, React, Vue3 | | ||
|[**Scaffold Conflux**](../espace/tutorials/scaffoldCfx/scaffold.md)| Instructions for Scaffold Conflux, the adaptation of [Scaffold-ETH-2](https://scaffoldeth.io/) template, enabling you to deploy the contract on **Conflux eSpace** and utilize the components, integration of hardhat, and the swift deployment of Scaffold-ETH-2. | Scaffold, Hardhat, Conflux eSpace | | ||
|
||
## Tools | ||
|
||
|Title|Description|Key Subjects| | ||
|---|---|---| | ||
|[**IPFS Overview**](../general/build/nfts/ipfs.md) &<br/>[**Pinata Tutorial**](../general/build/nfts/pinata.md)|What IPFS is and how to "pin" files to IPFS using Pinata. Valuable for NFT development if you're looking to upload files to IPFS| NFT development, IPFS| |
Oops, something went wrong.