Built-in Functions & Objects¶
Reference for all built-in functions and global objects.
Global Objects¶
msg¶
Transaction message context:
| Property | Type | Description |
|---|---|---|
msg.sender | address | Address of the caller |
msg.value | uint256 | Amount of SOL sent (in lamports) |
msg.data | bytes | Complete calldata |
block¶
Current block information:
| Property | Type | Description |
|---|---|---|
block.timestamp | uint256 | Current block timestamp (Unix seconds) |
block.number | uint256 | Current slot number |
function checkExpiry(uint256 deadline) public view {
require(block.timestamp < deadline, "Expired");
}
tx¶
Transaction context:
| Property | Type | Description |
|---|---|---|
tx.origin | address | Original transaction sender |
// Note: Avoid using tx.origin for authorization
function getOriginalSender() public view returns (address) {
return tx.origin;
}
Type Functions¶
address¶
| Function | Returns | Description |
|---|---|---|
address(0) | address | Zero address |
address(x) | address | Convert to address |
uint/int Conversions¶
// Explicit conversions
uint8 small = uint8(256); // Truncates to 0
uint256 large = uint256(100);
int256 signed = int256(unsignedValue);
String Functions¶
string.concat¶
Concatenate strings:
Array Functions¶
push¶
Append element to dynamic array:
pop¶
Remove and return last element:
length¶
Get array length:
Bytes Functions¶
bytes.concat¶
Concatenate byte arrays:
keccak256¶
Compute Keccak-256 hash:
bytes32 hash = keccak256(abi.encodePacked("data"));
bytes32 hash2 = keccak256(abi.encode(value1, value2));
sha256¶
Compute SHA-256 hash:
ABI Encoding¶
abi.encode¶
Standard ABI encoding with padding:
abi.encodePacked¶
Packed encoding (no padding):
abi.encodeWithSelector¶
Encode with function selector:
abi.decode¶
Decode ABI-encoded data:
Control Flow¶
require¶
Revert if condition is false:
require(condition, "Error message");
require(amount > 0, "Amount must be positive");
require(msg.sender == owner); // No message
revert¶
Explicitly revert execution:
// With message
revert("Something went wrong");
// With custom error
revert InsufficientBalance(available, required);
// Plain revert
revert();
assert¶
Check invariants (should never fail):
Math Functions¶
Operators¶
| Operator | Description |
|---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Integer division |
% | Modulo (remainder) |
** | Exponentiation |
Comparison¶
| Operator | Description |
|---|---|
< | Less than |
<= | Less than or equal |
> | Greater than |
>= | Greater than or equal |
== | Equal |
!= | Not equal |
Bitwise Operations¶
| Operator | Description |
|---|---|
& | Bitwise AND |
\| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise NOT |
<< | Left shift |
>> | Right shift |
Type Information¶
type(T).min / type(T).max¶
Get type bounds:
Special Values¶
this¶
Reference to current contract:
super¶
Reference to parent contract:
Events¶
emit¶
Emit an event:
event Transfer(address indexed from, address indexed to, uint256 amount);
function transfer(address to, uint256 amount) public {
// ... transfer logic ...
emit Transfer(msg.sender, to, amount);
}
Memory Allocation¶
new¶
Create new array in memory:
delete¶
Reset value to default:
delete balances[user]; // Reset to 0
delete myStruct; // Reset all fields
delete myArray; // Clear array