Skip to content

Instantly share code, notes, and snippets.

@stablexbt
Created February 12, 2026 12:32
Show Gist options
  • Select an option

  • Save stablexbt/d079d419d468220da2b6c91b778ad1d6 to your computer and use it in GitHub Desktop.

Select an option

Save stablexbt/d079d419d468220da2b6c91b778ad1d6 to your computer and use it in GitHub Desktop.
solidity and erc standard essentials

Token contract

function name() public view returns (string)
function symbol() public view returns (string)
function decimals() public view returns (uint8)
function totalSupply() public view returns (uint256)

function balanceOf(address _owner) public view returns (uint256 balance)
function allowance(address _owner, address _spender) public view returns (uint256 remaining)

function transfer(address _to, uint256 _value) public returns (bool success)
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success)
function approve(address _spender, uint256 _value) public returns (bool success)

event Transfer(address indexed _from, address indexed _to, uint256 _value)
event Approval(address indexed _owner, address indexed _spender, uint256 _value)

ERC-165

check if contract has function

function supportsInterface(bytes4 interfaceID) external view returns (bool);
{
	interface i.function.selector == interfaceID
}

ERC-173

ownership

event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
function owner() view external returns(address);
function transferOwnership(address _newOwner) external;

ERC-223

Basic event handling, mintable and burnable

// sender contract
function transfer(address to, uint value, bytes data) {
        uint codeLength;
        assembly {
            codeLength := extcodesize(_to)
        }
        balances[msg.sender] = balances[msg.sender].sub(_value);
        balances[_to] = balances[_to].add(_value);
        if(codeLength>0) {
            // Require proper transaction handling.
            ERC223Receiver receiver = ERC223Receiver(_to);
            receiver.tokenReceived(msg.sender, _value, _data);
        }
    }
// reciever contract shld implement tokenReceived() or it fails
// reciever contract
function tokenReceived(address _from, uint _value, bytes memory _data)

// mintable and burnable
// onlyOwner functions
function mint(address _to, uint256 _amount)
function burn(address _from, uint256 _amount)

ERC-621

change token supply

// onlyOwner functions
function increaseSupply(uint value, address to)
function decreaseSupply(uint value, address from)

ERC-721

NFTs

function name() external view returns (string _name);
function symbol() external view returns (string _symbol);
function tokenURI(uint256 _tokenId) external view returns (string);

function balanceOf(address _owner) external view returns (uint256);
function ownerOf(uint256 _tokenId) external view returns (address);
function isApprovedForAll(address _owner, address _operator) external view returns (bool);
function getApproved(uint256 _tokenId) external view returns (address);

function safeTransferFrom(address _from, address _to, uint256 _tokenId, bytes data) external payable;
function safeTransferFrom(address _from, address _to, uint256 _tokenId) external payable;
function transferFrom(address _from, address _to, uint256 _tokenId) external payable;
function approve(address _approved, uint256 _tokenId) external payable;
function setApprovalForAll(address _operator, bool _approved) external;

event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId);
event Approval(address indexed _owner, address indexed _approved, uint256 indexed _tokenId);
event ApprovalForAll(address indexed _owner, address indexed _operator, bool _approved);

// reciever contract shld implement for safeTransfers
function onERC721Received(address _operator, address _from, uint256 _tokenId, bytes _data) external returns(bytes4);

// URI
{
    "name": "Thor's hammer",
    "description": "Mjölnir, the legendary hammer of the Norse god of thunder.",
    "image": "<https://game.example/item-id-8u5h2m.png>",
    "strength": 20
}

ERC-777

Essential upgrades on ERC-20 Tokens

Introduces hooks

// default decimal value
decimals = 18

// receive hooks - removes approves + transfers in seprate txn

// the smallest portion of the token that isn’t divisible
// For most token contracts, this value will equal 1.
granularity() -> uint256

// transfer with data
send(recipient, amount, data)

// mint, burn
mint(amount, data
burn(amount, data)

// operator functions - to give ownership permissions
isOperatorFor(operator, tokenHolder)
defaultOperators()

authorizeOperator(operator)
revokeOperator(operator)
operatorSend(sender, recipient, amount, data, operatorData)
operatorBurn(account, amount, data, operatorData)

// Events
Sent(operator, from, to, amount, data, operatorData)
Minted(operator, to, amount, data, operatorData)
Burned(operator, from, amount, data, operatorData)
AuthorizedOperator(operator, tokenHolder)
RevokedOperator(operator, tokenHolder)

// Reciever contract need to implement for send - ERC777Recipient
tokensReceived(operator, from, to, amount, userData, operatorData)

ERC-677,827

ERC20 with direct triggers

// ERC-677
function transferAndCall(address _to, uint256 _value, bytes memory _data) public payable returns (bool) {
    super.transfer(_to, _value);
    _call(_to, _data);
    return true;
}

// ERC-827
function transferFromAndCall(address _from, address _to, uint256 _value, bytes memory _data) public payable returns (bool) {
    super.transferFrom(_from, _to, _value);
    _call(_to, _data);
    return true;
}

function approveAndCall(address _spender, uint256 _value, bytes memory _data) public payable returns (bool) {
    super.approve(_spender, _value);
    _call(_spender, _data);
    return true;
}

ERC-865

sign offchain and let someone else send the tokens

// Pay transfers in tokens instead of gas, in one transaction
// One standard function a token contract can implement to allow a user to delegate transfer of tokens to a third party
// The third party pays for the gas, and takes a fee in tokens.
// delegatedTransfer is called by the delegate, and performs the transfer.

function transferPreSigned( bytes _signature, address _to, uint256 _value,uint256 _fee,uint256 _nonce) public returns (bool);

ERC-884

Delaware compliant ERC20 - each token is a share

// events
event VerifiedAddressAdded( address indexed addr,bytes32 hash, address indexed sender);
event VerifiedAddressRemoved(address indexed addr, address indexed sender);
event VerifiedAddressUpdated( address indexed addr,bytes32 oldHash,bytes32 hash, address indexed sender);
event VerifiedAddressSuperseded(address indexed original,address indexed replacement,address indexed sender);

// functions
function addVerified(address addr, bytes32 hash) public;
function removeVerified(address addr) public;
function updateVerified(address addr, bytes32 hash) public;
function cancelAndReissue(address original,address replacement) public;

function isVerified(address addr) public view returns (bool);
function hasHash(address addr,bytes32 hash) public view returns (bool);
function holderCount() public view returns (uint);
function holderAt(uint256 index) public view returns (address);
function isSuperseded(address addr) public view returns (bool);
function getCurrentFor(address addr5) public view returns (address);

ERC-1155

Multi token standard, semi fungible tokens

// Batch Transfer: Transfer multiple assets in a single call.
// Batch Balance: Get the balances of multiple assets in a single call.
// Batch Approval: Approve all tokens to an address.
// Hooks: Receive tokens hook.
// NFT Support: If supply is only 1, treat it as NFT.
// Safe Transfer Rules: Set of rules for secure transfer.

// function
balanceOf(account, id)
balanceOfBatch(accounts, ids)
isApprovedForAll(account, operator)

setApprovalForAll(operator, approved)
safeTransferFrom(from, to, id, value, data)
safeBatchTransferFrom(from, to, ids, values, data)

// events
TransferSingle(operator, from, to, id, value)
TransferBatch(operator, from, to, ids, values)
ApprovalForAll(account, operator, approved)
URI(value, id)

ERC-1400,1404

Security Tokens with compliance

// Enforcing Token Lock-Up Periods
// Enforcing Passed AML/KYC Checks
// Private Real-Estate Investment Trusts
// Delaware General Corporations Law Shares
function detectTransferRestriction (address from, address to, uint256 value) public view returns (uint8);
function messageForTransferRestriction (uint8 restrictionCode) public view returns (string);

ERC-2981

royalties for NFTs

struct RoyaltyReceiver {
    address creator;
    uint8 royaltyPercent;
}
mapping(uint256 => RoyaltyReceiver) royalties;

ERC-4626

Token yield-bearing vaults

for lending markets, aggregators, and intrinsically interest-bearing tokens

// functions
function asset() public view returns (address) // underlying asset
function totalAssets() public view returns (uint256)
function convertToShares(uint256 assets) public view returns (uint256 shares)
function convertToAssets(uint256 shares) public view returns (uint256 assets)
function maxDeposit(address receiver) public view returns (uint256)
function previewDeposit(uint256 assets) public view returns (uint256)
function deposit(uint256 assets, address receiver) public returns (uint256 shares)
function maxMint(address receiver) public view returns (uint256)
function previewMint(uint256 shares) public view returns (uint256)
function mint(uint256 shares, address receiver) public returns (uint256 assets)
function maxWithdraw(address owner) public view returns (uint256)
function previewWithdraw(uint256 assets) public view returns (uint256)
function withdraw(uint256 assets, address receiver, address owner) public returns (uint256 shares)
function maxRedeem(address owner) public view returns (uint256)
function previewRedeem(uint256 shares) public view returns (uint256)
function redeem(uint256 shares, address receiver, address owner) public returns (uint256 assets)
function totalSupply() public view returns (uint256)
function balanceOf(address owner) public view returns (uint256)

// events
event Deposit(address indexed sender, address indexed owner, uint256 assets, uint256 shares)
event Withdraw(address indexed sender, address indexed receiver, address indexed owner, uint256 assets, uint256 share)

ERC-4671

soul-bound NFTs

event Minted(address owner, uint256 tokenId);
event Revoked(address owner, uint256 tokenId);

function balanceOf(address owner) external view returns (uint256);
function ownerOf(uint256 tokenId) external view returns (address);
function isValid(uint256 tokenId) external view returns (bool);
function hasValid(address owner) external view returns (bool);

ERC-6551

Token-bound account

event TransactionExecuted(address indexed target, uint256 indexed value, bytes data);

receive() external payable;
function executeCall(address to, uint256 value, bytes calldata data) external payable returns (bytes memory);
function token() external view returns (uint256 chainId, address tokenContract, uint256 tokenId);
function owner() external view returns (address);
function nonce() external view returns (uint256);

Factory

contract Hello {
	function func() public {}
}

contract Factory {
	function create() public {
		Hello h = new Hello();
	}
	function callfuncFromhere(address adr) public view {
		Hello(adr).func()
	}
}

Ownable

// modifier
onlyOwner()

// functions
constructor(initialOwner)
owner()
_checkOwner()
renounceOwnership()
transferOwnership(newOwner)
_transferOwnership(newOwner)

// events
OwnershipTransferred(previousOwner, newOwner)

AccessControl

// functions
hasRole(role, account)
getRoleAdmin(role)

grantRole(role, account)
revokeRole(role, account)
renounceRole(role, callerConfirmation)

// events
RoleAdminChanged(role, previousAdminRole, newAdminRole)
RoleGranted(role, account, sender)
RoleRevoked(role, account, sender)

Proxy & Upgradable contracts

// The proxy contract uses delegatecall function call where that the code at the target address 
// is executed in the context of the calling contract, if the logic contract’s code changes storage variables, 
// those changes are reflected in the proxy contract’s storage variables—i.e. in the proxy contract’s state.

// delegate function sits in fallback function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment