30 Solidity Snippets

For smart contract development · Copy-paste ready

Battle-tested Solidity snippets covering ERC-20, ERC-721, access control, upgrades, events, payments, utilities, and Yul assembly. Each snippet includes an explanation and gas notes. Compatible with Solidity 0.8.20+ and OpenZeppelin v5.

Categories

ERC-20 (3)ERC-721 (4)Access Control (5)Upgrades (4)Events (3)Payments (4)Utilities (4)Assembly (3)

Live Demo

Terminal demo

Sample Snippets

Safe transfer with check — ERC-20

function safeTransfer(address token, address to, uint256 amount)
    internal
    returns (bool)
{
    (bool success, bytes memory data) = token.call(
        abi.encodeWithSelector(IERC20.transfer.selector, to, amount)
    );
    return success && (data.length == 0 || abi.decode(data, (bool)));
}

Role-based modifier — Access Control

modifier onlyRole(bytes32 role) {
    require(hasRole(role, msg.sender), "Access denied");
    _;
}

bytes32 public constant ADMIN = keccak256("ADMIN");
bytes32 public constant MINTER = keccak256("MINTER");

Split payment — Payments

function splitPayment(address[] calldata recipients, uint256[] calldata shares)
    external
    payable
{
    uint256 total = 0;
    for (uint256 i = 0; i < shares.length; i++) total += shares[i];
    require(msg.value == total, "Mismatch");
    for (uint256 i = 0; i < recipients.length; i++)
        payable(recipients[i]).transfer(shares[i]);
}

Lazy mint signature — ERC-721

function lazyMint(
    address to,
    uint256 tokenId,
    bytes calldata signature
) external {
    bytes32 msgHash = keccak256(abi.encode(to, tokenId));
    bytes32 ethSignedHash = ECDSA.toEthSignedMessageHash(msgHash);
    address signer = ECDSA.recover(ethSignedHash, signature);
    require(signer == minter, "Invalid sig");
    _safeMint(to, tokenId);
}

UUPS upgrade guard — Upgrades

function _authorizeUpgrade(address newImplementation)
    internal
    override
    onlyRole(UPGRADER)
{}

uint256[50] private __gap;

+25 more snippets across 8 categories.

FAQ

What do I get?

A PDF with 30 copy-paste-ready Solidity snippets covering ERC-20, ERC-721, access control, upgrades, events, payments, utilities, and Yul assembly. Each snippet includes an explanation and gas notes.

How do I pay?

Send exactly $1 in ETH to the address below. After the transaction confirms, email me your wallet address and I'll send you the PDF within 24 hours.

What Solidity version?

0.8.20+. Snippets avoid deprecated patterns and are compatible with OpenZeppelin v5.

Refund policy?

Full refund within 7 days if the snippets don't save you time. Just show me the tx hash.

Get the full collection

Send exactly $1 in ETH to:

0x0D42B32D6E21F496082104BFC8d7213081474577

Ethereum mainnet · ERC-20 compatible

Other tools

© 2025 · 30 Solidity Snippets