-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add guide for Smart Contracts Tips and Tricks
- Loading branch information
1 parent
215e168
commit 2ef38de
Showing
2 changed files
with
111 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
--- | ||
sidebar_position: 4 | ||
sidebar_label: 'Tips and Tricks' | ||
--- | ||
|
||
# Smart Contracts Tips and Tricks | ||
|
||
:::tip | ||
📝 This article offers insights into **Smart Contracts** with helpful tips and tricks. If you have suggestions or questions, feel free to open an issue. We also welcome contributions through PRs. | ||
::: | ||
|
||
## Calling Smart Contracts Methods with Parameter Overloading | ||
|
||
### Overview of Function Overloading | ||
|
||
Parameter overloading enables smart contracts to define multiple functions bearing the same name, differentiated only by their parameters. While this enhances legibility and organization, it complicates calls due to the need for precise method identification. | ||
|
||
### Example Code | ||
|
||
Below is a demonstration of invoking two versions of the `funcWithParamsOverloading` function in a smart contract, differentiated by their parameter types—`uint256` versus `address`. | ||
|
||
```typescript | ||
import { Web3 } from 'web3'; | ||
|
||
const ABI = [ | ||
{ | ||
inputs: [ | ||
{ | ||
internalType: 'uint256', | ||
name: 'userId', | ||
type: 'uint256', | ||
}, | ||
], | ||
name: 'funcWithParamsOverloading', | ||
outputs: [ | ||
{ | ||
internalType: 'string', | ||
name: '', | ||
type: 'string', | ||
}, | ||
], | ||
stateMutability: 'pure', | ||
type: 'function', | ||
}, | ||
{ | ||
inputs: [ | ||
{ | ||
internalType: 'address', | ||
name: 'userAddress', | ||
type: 'address', | ||
}, | ||
], | ||
name: 'funcWithParamsOverloading', | ||
outputs: [ | ||
{ | ||
internalType: 'string', | ||
name: '', | ||
type: 'string', | ||
}, | ||
], | ||
stateMutability: 'pure', | ||
type: 'function', | ||
} | ||
] as const; | ||
|
||
(async function () { | ||
const web3 = new Web3(provider); | ||
|
||
const contract = new web3.eth.Contract(ABI, contractAddress); | ||
|
||
// Calling the function that accepts an address | ||
const res1 = await contract.methods['funcWithParamsOverloading(address)'](userAddress).call(); | ||
|
||
// Calling the function that accepts a uint256 | ||
const res2 = await contract.methods['funcWithParamsOverloading(uint256)'](userId).call(); | ||
|
||
})(); | ||
``` | ||
|
||
### Handling Ambiguity in Overloaded Methods | ||
|
||
Omitting the explicit specification for overloading, as highlighted earlier, results in the default selection of the first method match in the ABI, along with a warning. Future web3.js releases will address this with an error to enforce stricter specification. | ||
|
||
#### Demonstrating the Importance of Specificity | ||
|
||
To underline specificity's value, here's a scenario of invoking an overloaded function without specifying the parameter type: | ||
|
||
```typescript | ||
// Assuming a contract with overloaded methods: funcWithParamsOverloading(uint256) and funcWithParamsOverloading(string)... | ||
|
||
(async function () { | ||
try { | ||
// A call without specifying overloading results in a warning and choosing the first matched overload | ||
const ambiguousResult = await contract.methods.funcWithParamsOverloading('0x0123').call(); | ||
})(); | ||
``` | ||
This generates a console warning on the ambiguity and auto-selects the first matching function overload found in the ABI: | ||
``` | ||
Multiple methods found that are compatible with the given inputs. Found 2 compatible methods: ["funcWithParamsOverloading(uint256) (signature: 0x...)", "funcWithParamsOverloading(string) (signature: 0x...)"] The first one will be used: funcWithParamsOverloading(uint256) | ||
``` | ||
### Future Considerations | ||
Future releases of web3.js, specifically version 5.x, will replace the warning with an error whenever multiple methods match a call without explicit overloading. This aims to foster greater precision in method invocation. | ||
### Key Takeaway for function overlading: Method Specification | ||
When working with overloaded smart contract methods, it's imperative to specify the intended method by appending its parameter types within parentheses, such as `funcWithParamsOverloading(address)` versus `funcWithParamsOverloading(uint256)`. This ensures the accuracy of method invocation, leading to more efficient and clearer contract interactions. |
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
2ef38de
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Benchmark
processingTx
9343
ops/sec (±4.46%
)9301
ops/sec (±4.81%
)1.00
processingContractDeploy
38526
ops/sec (±7.49%
)39129
ops/sec (±7.62%
)1.02
processingContractMethodSend
19297
ops/sec (±5.85%
)19443
ops/sec (±5.19%
)1.01
processingContractMethodCall
38823
ops/sec (±3.94%
)38971
ops/sec (±6.34%
)1.00
abiEncode
42852
ops/sec (±6.54%
)44252
ops/sec (±6.92%
)1.03
abiDecode
29928
ops/sec (±8.05%
)30419
ops/sec (±8.89%
)1.02
sign
1552
ops/sec (±3.92%
)1656
ops/sec (±4.08%
)1.07
verify
373
ops/sec (±0.44%
)373
ops/sec (±0.78%
)1
This comment was automatically generated by workflow using github-action-benchmark.
2ef38de
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Benchmark
processingTx
9104
ops/sec (±3.40%
)9301
ops/sec (±4.81%
)1.02
processingContractDeploy
37000
ops/sec (±7.79%
)39129
ops/sec (±7.62%
)1.06
processingContractMethodSend
17988
ops/sec (±6.81%
)19443
ops/sec (±5.19%
)1.08
processingContractMethodCall
36066
ops/sec (±5.36%
)38971
ops/sec (±6.34%
)1.08
abiEncode
40613
ops/sec (±6.97%
)44252
ops/sec (±6.92%
)1.09
abiDecode
28716
ops/sec (±6.13%
)30419
ops/sec (±8.89%
)1.06
sign
1501
ops/sec (±3.53%
)1656
ops/sec (±4.08%
)1.10
verify
360
ops/sec (±0.73%
)373
ops/sec (±0.78%
)1.04
This comment was automatically generated by workflow using github-action-benchmark.