Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
397 lines (272 loc) · 5.87 KB

File metadata and controls

397 lines (272 loc) · 5.87 KB
Copy raw file
Download raw file
Outline
Edit and raw actions

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
function deposit() public payable {
    address caller = msg.sender;
    uint256 amount = msg.value;
}

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
address zero = address(0);
require(recipient != address(0), "Invalid 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:

string memory greeting = string.concat("Hello, ", name, "!");

Array Functions

push

Append element to dynamic array:

uint256[] storage arr;
arr.push(42);

pop

Remove and return last element:

uint256 last = arr.pop();

length

Get array length:

uint256 len = arr.length;

Bytes Functions

bytes.concat

Concatenate byte arrays:

bytes memory result = bytes.concat(data1, data2);

keccak256

Compute Keccak-256 hash:

bytes32 hash = keccak256(abi.encodePacked("data"));
bytes32 hash2 = keccak256(abi.encode(value1, value2));

sha256

Compute SHA-256 hash:

bytes32 hash = sha256(abi.encodePacked("data"));

ABI Encoding

abi.encode

Standard ABI encoding with padding:

bytes memory encoded = abi.encode(
    uint256(100),
    address(0x123...),
    "hello"
);

abi.encodePacked

Packed encoding (no padding):

bytes memory packed = abi.encodePacked(
    uint8(1),
    uint16(2),
    uint32(3)
);

abi.encodeWithSelector

Encode with function selector:

bytes memory data = abi.encodeWithSelector(
    IERC20.transfer.selector,
    recipient,
    amount
);

abi.decode

Decode ABI-encoded data:

(uint256 value, address addr) = abi.decode(
    data,
    (uint256, address)
);

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):

assert(totalBefore == totalAfter);

Math Functions

Operators

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Integer division
% Modulo (remainder)
** Exponentiation
uint256 sum = a + b;
uint256 power = base ** exponent;
uint256 remainder = dividend % divisor;

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
uint256 flags = a & b;
uint256 combined = a | b;
uint256 shifted = value << 8;

Type Information

type(T).min / type(T).max

Get type bounds:

uint256 maxUint = type(uint256).max;
int256 minInt = type(int256).min;

Special Values

this

Reference to current contract:

address contractAddr = address(this);
uint256 balance = address(this).balance;

super

Reference to parent contract:

function foo() public override {
    super.foo();  // Call parent implementation
}

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:

uint256[] memory arr = new uint256[](10);
bytes memory data = new bytes(32);

delete

Reset value to default:

delete balances[user];  // Reset to 0
delete myStruct;        // Reset all fields
delete myArray;         // Clear array

Solana-Specific

Program Context

// Access current program ID
address programId = program.id;

PDA Derivation

// Derive PDA
(address pda, uint8 bump) = findProgramAddress(
    seeds,
    programId
);

See Also

Morty Proxy This is a proxified and sanitized view of the page, visit original site.