Skip to content

Commit

Permalink
add guide for Smart Contracts Tips and Tricks
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad-Altabba committed Mar 27, 2024
1 parent 215e168 commit 2ef38de
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 1 deletion.
110 changes: 110 additions & 0 deletions docs/docs/guides/smart_contracts/tips_and_tricks.md
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.
2 changes: 1 addition & 1 deletion packages/web3-eth-contract/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,7 @@ export class Contract<Abi extends ContractAbi>
} compatible methods: ${JSON.stringify(
applicableMethodAbi.map(
m =>
`${(m as { methodNameWithInputs: string }).methodNameWithInputs} (${
`${(m as { methodNameWithInputs: string }).methodNameWithInputs} (signature: ${
(m as { signature: string }).signature
})`,
),
Expand Down

2 comments on commit 2ef38de

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 2ef38de Previous: 6c075db Ratio
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.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 2ef38de Previous: 6c075db Ratio
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.

Please sign in to comment.