Introduction to Function Overloading
Solidity supports function overloading, allowing multiple functions with the same name but different parameter types to coexist. These are treated as distinct functions. Key considerations:
- Modifiers cannot be overloaded
- Compiled functions receive unique selectors based on parameter types
- Ambiguous matches between overloaded functions trigger compiler errors
Library Functions in Solidity
Libraries are specialized contracts designed for code reuse and gas optimization. They differ from regular contracts by:
- Prohibiting state variables
- Disallowing inheritance
- Rejecting Ether transfers
- Being indestructible
Library Implementation Example
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
library Strings {
function toHexString(uint256 value) public pure returns (string memory) {
// Implementation logic
return "0x00";
}
}
contract UseLib {
using Strings for uint256;
function getString1(uint256 _number) public pure returns(string memory) {
return _number.toHexString();
}
function getString2(uint256 _number) public pure returns(string memory) {
return Strings.toHexString(_number);
}
}Popular Solidity Libraries
- Strings: Converts uint256 to String
- Address: Validates contract addresses
- Create2: Safer usage of EVM opcode
- Arrays: Utility functions for array operations
👉 Explore more library implementations
Importing Contracts
Solidity provides multiple import methods:
- Relative path:
import './Yeye.sol'; - URL import:
import 'https://github.com/.../Address.sol'; - NPM package:
import '@openzeppelin/../Ownable.sol'; - Specific symbol:
import {Yeye} from './Yeye.sol';
Import Syntax Variations
import {Yeye as Wowo} from "./Yeye.sol";
import * as Wowo from "./Yeye.sol";ETH Transaction Handling
Receive Function
- Triggered when contract receives ETH
- Must be
external payable - Limited to 2300 gas when using
transfer/send
receive() external payable {
emit Received(msg.sender, msg.value);
}Fallback Function
Executes when:
- Nonexistent functions are called
- ETH is sent without data
- Also requires
external payable
fallback() external payable {
emit fallbackCalled(msg.sender, msg.value, msg.data);
}ETH Transfer Methods Comparison
| Method | Gas Limit | Auto Revert | Recommended |
|---|---|---|---|
| transfer() | 2300 | Yes | Medium |
| send() | 2300 | No | No |
| call() | Flexible | No | Yes |
Contract Interaction Patterns
Direct Contract Calls
contract CallContract {
function callSetX(address _Address, uint256 x) external {
OtherContract(_Address).setX(x);
}
}Low-Level Call Functions
(bool success, bytes memory data) = _addr.call(
abi.encodeWithSignature("mint(uint256)", _num)
);Advanced Deployment Techniques
CREATE2 Opcode
Deterministic address generation formula:
new_address = hash("0xFF", creator_address, salt, bytecode)Factory Contract Example
contract PairFactory {
function createPair() external returns (Pair) {
return new Pair(msg.sender);
}
}Error Handling with Try-Catch
Introduced in Solidity 0.6 for external call exception handling:
try externalContract.f() returns (returnType val) {
// Success logic
} catch Error(string memory reason) {
// Handle revert/require failures
} catch (bytes memory reason) {
// Handle assert failures
}FAQ Section
Q: When should I use delegatecall?
A: Primarily for proxy contracts where storage and logic are separated, allowing logic upgrades without storage migration.
Q: What's the main advantage of CREATE2?
A: It enables deterministic address calculation before deployment, crucial for layer-2 solutions and pre-computed contract addresses.
Q: Why is call() preferred for ETH transfers?
A: It provides flexible gas limits and better control compared to transfer()/send(), though requires manual success checking.
Q: How does function overloading affect selector generation?
A: Each parameter combination creates a unique selector, ensuring proper function resolution during calls.