Reference for all built-in functions and global objects.
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 |
function deposit() public payable {
address caller = msg.sender;
uint256 amount = msg.value;
}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");
}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;
}| Function | Returns | Description |
|---|---|---|
address(0) |
address |
Zero address |
address(x) |
address |
Convert to address |
address zero = address(0);
require(recipient != address(0), "Invalid address");// Explicit conversions
uint8 small = uint8(256); // Truncates to 0
uint256 large = uint256(100);
int256 signed = int256(unsignedValue);Concatenate strings:
string memory greeting = string.concat("Hello, ", name, "!");Append element to dynamic array:
uint256[] storage arr;
arr.push(42);Remove and return last element:
uint256 last = arr.pop();Get array length:
uint256 len = arr.length;Concatenate byte arrays:
bytes memory result = bytes.concat(data1, data2);Compute Keccak-256 hash:
bytes32 hash = keccak256(abi.encodePacked("data"));
bytes32 hash2 = keccak256(abi.encode(value1, value2));Compute SHA-256 hash:
bytes32 hash = sha256(abi.encodePacked("data"));Standard ABI encoding with padding:
bytes memory encoded = abi.encode(
uint256(100),
address(0x123...),
"hello"
);Packed encoding (no padding):
bytes memory packed = abi.encodePacked(
uint8(1),
uint16(2),
uint32(3)
);Encode with function selector:
bytes memory data = abi.encodeWithSelector(
IERC20.transfer.selector,
recipient,
amount
);Decode ABI-encoded data:
(uint256 value, address addr) = abi.decode(
data,
(uint256, address)
);Revert if condition is false:
require(condition, "Error message");
require(amount > 0, "Amount must be positive");
require(msg.sender == owner); // No messageExplicitly revert execution:
// With message
revert("Something went wrong");
// With custom error
revert InsufficientBalance(available, required);
// Plain revert
revert();Check invariants (should never fail):
assert(totalBefore == totalAfter);| Operator | Description |
|---|---|
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Integer division |
% |
Modulo (remainder) |
** |
Exponentiation |
uint256 sum = a + b;
uint256 power = base ** exponent;
uint256 remainder = dividend % divisor;| Operator | Description |
|---|---|
< |
Less than |
<= |
Less than or equal |
> |
Greater than |
>= |
Greater than or equal |
== |
Equal |
!= |
Not equal |
| Operator | Description |
|---|---|
& |
Bitwise AND |
| |
Bitwise OR |
^ |
Bitwise XOR |
~ |
Bitwise NOT |
<< |
Left shift |
>> |
Right shift |
uint256 flags = a & b;
uint256 combined = a | b;
uint256 shifted = value << 8;Get type bounds:
uint256 maxUint = type(uint256).max;
int256 minInt = type(int256).min;Reference to current contract:
address contractAddr = address(this);
uint256 balance = address(this).balance;Reference to parent contract:
function foo() public override {
super.foo(); // Call parent implementation
}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);
}Create new array in memory:
uint256[] memory arr = new uint256[](10);
bytes memory data = new bytes(32);Reset value to default:
delete balances[user]; // Reset to 0
delete myStruct; // Reset all fields
delete myArray; // Clear array// Access current program ID
address programId = program.id;// Derive PDA
(address pda, uint8 bump) = findProgramAddress(
seeds,
programId
);