Skip to content
Verified contractExactMIT

DefiProtocol

Address0x5ce44494159d86d6a9f0a8ae87fca2143d15075e
Balance

0 ETH

 

Transactions

5

 

Compiler

solc 0.8.35

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.35+commit.47b9dedd
Language
solidity
Optimization
Yes, 1 runs
EVM version
cancun
Via IR
yes
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
25,691,921
Creation transaction
0x8fa200b31d…e09c04eeea

src/DefiProtocol.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;

///                 No proxy. No selfdestruct. No delegatecall. No pause. No blacklist.
///                 No upgrade path. Fully immutable after ownership renounce.
///                 Solidity ^0.8.26, via-ir, optimizer, cancun.

contract DefiProtocol {

    // ── Types ───────────────────────────────────────────────────────

    struct Checkpoint {
        uint32  fromBlock;
        uint224 votes;
    }

    // ── Errors ──────────────────────────────────────────────────────

    error Unauthorized();
    error NullAddress();
    error InsufficientBalance();
    error InsufficientAllowance();
    error AlreadyMinted();
    error ExpiredDeadline();
    error InvalidSignature();
    error ZeroAmount();
    error BlockNotYetMined();

    // ── Events ──────────────────────────────────────────────────────

    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);
    event OwnershipTransferred(address indexed prev, address indexed next);
    event DelegateChanged(
        address indexed delegator,
        address indexed fromDelegate,
        address indexed toDelegate
    );
    event DelegateVotesChanged(
        address indexed delegate,
        uint256 previousBalance,
        uint256 newBalance
    );

    // ── Constants ───────────────────────────────────────────────────

    string public constant name     = "Defi Protocol";
    string public constant symbol   = "Defi";
    uint8  public constant decimals = 18;

    address public constant POOL_MANAGER = 0x000000000004444c5dc75cB358380D2e3dE08A90;
    address public constant PERMIT2      = 0x000000000022D473030F116dDEE9F6B43aC78BA3;
    address public constant WETH9        = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
    uint24  public constant POOL_FEE     = 0x800000;

    // ── State ───────────────────────────────────────────────────────

    uint256 public totalSupply;
    uint256 public burned;
    address public owner;

    mapping(address => uint256)                     public balanceOf;
    mapping(address => mapping(address => uint256)) public allowance;
    mapping(address => uint256)                     public nonces;

    mapping(address => address)      public delegates;
    mapping(address => Checkpoint[]) private _checkpoints;
    Checkpoint[]                     private _totalSupplyCheckpoints;

    bool private _minted;

    // ── EIP-712 ─────────────────────────────────────────────────────

    uint256 private immutable _CACHED_CHAIN_ID;
    bytes32 private immutable _CACHED_DOMAIN_SEPARATOR;

    bytes32 private constant _EIP712_TYPEHASH =
        keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");

    bytes32 private constant _PERMIT_TYPEHASH =
        keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");

    bytes32 private constant _DELEGATION_TYPEHASH =
        keccak256("Delegation(address delegatee,uint256 nonce,uint256 expiry)");

    bytes32 private constant _HASHED_NAME    = keccak256(bytes("UniCurve"));
    bytes32 private constant _HASHED_VERSION = keccak256(bytes("1"));

    // ── Constructor ─────────────────────────────────────────────────

    constructor(address _deployer) {
        if (_deployer == address(0)) revert NullAddress();

        owner = _deployer;
        emit OwnershipTransferred(address(0), _deployer);

        _CACHED_CHAIN_ID = block.chainid;
        _CACHED_DOMAIN_SEPARATOR = _computeDomainSeparator();
    }

    // ── Genesis Mint ────────────────────────────────────────────────

    function mintInitialSupply(address to, uint256 amount) external {
        if (msg.sender != owner) revert Unauthorized();
        if (_minted) revert AlreadyMinted();
        if (to == address(0)) revert NullAddress();
        if (amount == 0) revert ZeroAmount();

        _minted = true;
        totalSupply = amount;

        unchecked {
            balanceOf[to] = amount;
        }

        emit Transfer(address(0), to, amount);

        _writeCheckpoint(_totalSupplyCheckpoints, amount);
        _moveVotingPower(address(0), delegates[to], amount);
    }

    // ── ERC-20 ──────────────────────────────────────────────────────

    function approve(address spender, uint256 amount) external returns (bool) {
        _approve(msg.sender, spender, amount);
        return true;
    }

    function transfer(address to, uint256 amount) external returns (bool) {
        _transfer(msg.sender, to, amount);
        return true;
    }

    function transferFrom(address from, address to, uint256 amount) external returns (bool) {
        _spendAllowance(from, msg.sender, amount);
        _transfer(from, to, amount);
        return true;
    }

    function increaseAllowance(address spender, uint256 added) external returns (bool) {
        _approve(msg.sender, spender, allowance[msg.sender][spender] + added);
        return true;
    }

    function decreaseAllowance(address spender, uint256 reduced) external returns (bool) {
        uint256 cur = allowance[msg.sender][spender];
        if (cur < reduced) revert InsufficientAllowance();

        unchecked {
            _approve(msg.sender, spender, cur - reduced);
        }

        return true;
    }

    // ── Burn ────────────────────────────────────────────────────────

    function burn(uint256 amount) external {
        _burn(msg.sender, amount);
    }

    function burnFrom(address account, uint256 amount) external {
        _spendAllowance(account, msg.sender, amount);
        _burn(account, amount);
    }

    // ── EIP-2612 Permit ─────────────────────────────────────────────

    function permit(
        address _owner,
        address spender,
        uint256 value,
        uint256 deadline,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) external {
        if (block.timestamp > deadline) revert ExpiredDeadline();

        bytes32 structHash = keccak256(
            abi.encode(
                _PERMIT_TYPEHASH,
                _owner,
                spender,
                value,
                nonces[_owner]++,
                deadline
            )
        );

        bytes32 digest = keccak256(
            abi.encodePacked("\x19\x01", DOMAIN_SEPARATOR(), structHash)
        );

        address recovered = ecrecover(digest, v, r, s);

        if (recovered == address(0) || recovered != _owner) revert InvalidSignature();

        _approve(_owner, spender, value);
    }

    function DOMAIN_SEPARATOR() public view returns (bytes32) {
        return block.chainid == _CACHED_CHAIN_ID
            ? _CACHED_DOMAIN_SEPARATOR
            : _computeDomainSeparator();
    }

    // ── EIP-5267 ────────────────────────────────────────────────────

    function eip712Domain()
        external
        view
        returns (
            bytes1   fields,
            string memory _name,
            string memory _version,
            uint256 chainId,
            address verifyingContract,
            bytes32 salt,
            uint256[] memory extensions
        )
    {
        return (
            hex"0f",
            name,
            "1",
            block.chainid,
            address(this),
            bytes32(0),
            new uint256[](0)
        );
    }

    // ── Delegation ──────────────────────────────────────────────────

    function delegate(address delegatee) external {
        _delegate(msg.sender, delegatee);
    }

    function delegateBySig(
        address delegatee,
        uint256 nonce,
        uint256 expiry,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) external {
        if (block.timestamp > expiry) revert ExpiredDeadline();

        bytes32 structHash = keccak256(
            abi.encode(_DELEGATION_TYPEHASH, delegatee, nonce, expiry)
        );

        bytes32 digest = keccak256(
            abi.encodePacked("\x19\x01", DOMAIN_SEPARATOR(), structHash)
        );

        address signer = ecrecover(digest, v, r, s);

        if (signer == address(0)) revert InvalidSignature();
        if (nonce != nonces[signer]++) revert InvalidSignature();

        _delegate(signer, delegatee);
    }

    // ── Voting Power ────────────────────────────────────────────────

    function getVotes(address account) external view returns (uint256) {
        uint256 len = _checkpoints[account].length;
        return len == 0 ? 0 : _checkpoints[account][len - 1].votes;
    }

    function getPastVotes(address account, uint256 blockNumber) external view returns (uint256) {
        if (blockNumber >= block.number) revert BlockNotYetMined();
        return _checkpointsLookup(_checkpoints[account], blockNumber);
    }

    function getPastTotalSupply(uint256 blockNumber) external view returns (uint256) {
        if (blockNumber >= block.number) revert BlockNotYetMined();
        return _checkpointsLookup(_totalSupplyCheckpoints, blockNumber);
    }

    function numCheckpoints(address account) external view returns (uint32) {
        return uint32(_checkpoints[account].length);
    }

    function checkpoints(address account, uint32 pos) external view returns (Checkpoint memory) {
        return _checkpoints[account][pos];
    }

    // ── Views ───────────────────────────────────────────────────────

    function maxSupply() external view returns (uint256) {
        return totalSupply + burned;
    }

    function circulatingSupply() external view returns (uint256) {
        return totalSupply;
    }

    function isMinted() external view returns (bool) {
        return _minted;
    }

    // ── Ownership ───────────────────────────────────────────────────

    function renounceOwnership() external {
        if (msg.sender != owner) revert Unauthorized();

        emit OwnershipTransferred(owner, address(0));
        owner = address(0);
    }

    // ── ERC-165 ─────────────────────────────────────────────────────

    function supportsInterface(bytes4 interfaceId) external pure returns (bool) {
        return interfaceId == 0x36372b07
            || interfaceId == 0x01ffc9a7;
    }

    // ── Fallback ────────────────────────────────────────────────────

    receive() external payable {
        revert();
    }

    // ── Internal ────────────────────────────────────────────────────

    function _transfer(address from, address to, uint256 amount) internal {
        if (from == address(0)) revert NullAddress();
        if (to == address(0)) revert NullAddress();

        uint256 bal = balanceOf[from];
        if (bal < amount) revert InsufficientBalance();

        unchecked {
            balanceOf[from] = bal - amount;
        }

        balanceOf[to] += amount;

        emit Transfer(from, to, amount);

        _moveVotingPower(delegates[from], delegates[to], amount);
    }

    function _burn(address account, uint256 amount) internal {
        if (account == address(0)) revert NullAddress();
        if (amount == 0) revert ZeroAmount();

        uint256 bal = balanceOf[account];
        if (bal < amount) revert InsufficientBalance();

        unchecked {
            balanceOf[account] = bal - amount;
            totalSupply -= amount;
        }

        burned += amount;

        emit Transfer(account, address(0), amount);

        _moveVotingPower(delegates[account], address(0), amount);
        _writeCheckpoint(_totalSupplyCheckpoints, totalSupply);
    }

    function _approve(address _owner, address spender, uint256 amount) internal {
        if (_owner == address(0)) revert NullAddress();
        if (spender == address(0)) revert NullAddress();

        allowance[_owner][spender] = amount;

        emit Approval(_owner, spender, amount);
    }

    function _spendAllowance(address _owner, address spender, uint256 amount) internal {
        uint256 cur = allowance[_owner][spender];

        if (cur != type(uint256).max) {
            if (cur < amount) revert InsufficientAllowance();

            unchecked {
                allowance[_owner][spender] = cur - amount;
            }
        }
    }

    function _delegate(address delegator, address delegatee) internal {
        address currentDelegate = delegates[delegator];
        uint256 delegatorBalance = balanceOf[delegator];

        delegates[delegator] = delegatee;

        emit DelegateChanged(delegator, currentDelegate, delegatee);

        _moveVotingPower(currentDelegate, delegatee, delegatorBalance);
    }

    function _moveVotingPower(address src, address dst, uint256 amount) internal {
        if (src != dst && amount > 0) {
            if (src != address(0)) {
                uint256 srcLen = _checkpoints[src].length;
                uint256 oldWeight = srcLen == 0 ? 0 : _checkpoints[src][srcLen - 1].votes;
                uint256 newWeight = oldWeight - amount;

                _writeCheckpoint(_checkpoints[src], newWeight);

                emit DelegateVotesChanged(src, oldWeight, newWeight);
            }

            if (dst != address(0)) {
                uint256 dstLen = _checkpoints[dst].length;
                uint256 oldWeight = dstLen == 0 ? 0 : _checkpoints[dst][dstLen - 1].votes;
                uint256 newWeight = oldWeight + amount;

                _writeCheckpoint(_checkpoints[dst], newWeight);

                emit DelegateVotesChanged(dst, oldWeight, newWeight);
            }
        }
    }

    function _writeCheckpoint(Checkpoint[] storage ckpts, uint256 newWeight) private {
        uint256 length = ckpts.length;

        if (length > 0 && ckpts[length - 1].fromBlock == block.number) {
            ckpts[length - 1].votes = uint224(newWeight);
        } else {
            ckpts.push(Checkpoint({
                fromBlock: uint32(block.number),
                votes: uint224(newWeight)
            }));
        }
    }

    function _checkpointsLookup(
        Checkpoint[] storage ckpts,
        uint256 blockNumber
    ) private view returns (uint256) {
        uint256 length = ckpts.length;

        if (length == 0) return 0;

        if (ckpts[length - 1].fromBlock <= blockNumber) {
            return ckpts[length - 1].votes;
        }

        if (ckpts[0].fromBlock > blockNumber) return 0;

        uint256 low = 0;
        uint256 high = length - 1;

        while (low < high) {
            uint256 mid = low + (high - low + 1) / 2;

            if (ckpts[mid].fromBlock <= blockNumber) {
                low = mid;
            } else {
                high = mid - 1;
            }
        }

        return ckpts[low].votes;
    }

    function _computeDomainSeparator() private view returns (bytes32) {
        return keccak256(
            abi.encode(
                _EIP712_TYPEHASH,
                _HASHED_NAME,
                _HASHED_VERSION,
                block.chainid,
                address(this)
            )
        );
    }
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "optimizer": {
    "runs": 1,
    "details": {
      "yul": true
    },
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": [
    "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
    "@uniswap/v4-core/=lib/v4-core/",
    "@uniswap/v4-periphery/=lib/v4-periphery/",
    "permit2/=lib/permit2/",
    "forge-std/=lib/forge-std/src/",
    "solady/=lib/solady/",
    "@ensdomains/=lib/v4-core/node_modules/@ensdomains/",
    "ds-test/=lib/v4-core/lib/forge-std/lib/ds-test/src/",
    "erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
    "forge-gas-snapshot/=lib/permit2/lib/forge-gas-snapshot/src/",
    "halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
    "hardhat/=lib/v4-core/node_modules/hardhat/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/",
    "solmate/=lib/v4-core/lib/solmate/",
    "v4-core/=lib/v4-core/src/",
    "v4-periphery/=lib/v4-periphery/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.