Verified contractExactMIT
Chunck
Address0x8a74fd2e29e8fa96a5ee8e53b5d0b195a1178f43
- Balance
0 ETH
- Transactions
24
- Compiler
solc 0.8.26
- Same code
1 more contract
Details
- Compiler
- solc 0.8.26+commit.8a97fa7a
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- cancun
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 25,727,828
- Creation transaction
- 0xa5a0fa4431…a878c0dd97
- Main copy (most transactions)
- 0x1ab076aa…94416a12
src/Chunck.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
/// @title Chunck — the ERC-404 hybrid ledger.
/// @notice One ERC-20 balance, and one Chunk NFT for every whole token in it. Crossing a whole
/// unit upwards mints a Chunk; crossing downwards burns one.
///
/// @dev WHY TWO CONTRACTS. The original ERC-404 put both interfaces on one address, where
/// `approve(address,uint256)` and `transferFrom(address,address,uint256)` are the same
/// selector for both standards and have to be told apart by guessing whether the number
/// looks like a token id. That guess is the source of most ERC-404 accidents. Here the
/// ERC-721 surface lives on a separate mirror, so no selector is ever ambiguous and no
/// heuristic is needed. This contract owns the state; the mirror owns the interface.
contract Chunck {
/* ── ERC-20 ─────────────────────────────────────────────────────────── */
string public constant name = "Chunck";
string public constant symbol = "CHUNK";
uint8 public constant decimals = 18;
/// @notice Tokens per Chunk. NOT one token: at a ~3 ETH opening a 0.1 ETH buy takes 3% of the
/// float, and one Chunk per token would ask a single swap to mint tens of millions of
/// NFTs. Sizing UNIT so the collection caps in the low thousands is what makes the
/// hybrid survive its own launch.
uint256 public immutable UNIT;
/// @notice Most Chunks a single transfer will mint. Anything beyond this is not lost: it
/// becomes `owedOf` and can be claimed later, in batches the claimer pays for. A swap
/// must never revert because the art was expensive.
uint256 public constant MAX_MINT_PER_TX = 64;
uint256 public immutable totalSupply;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
event Transfer(address indexed from, address indexed to, uint256 amount);
event Approval(address indexed owner, address indexed spender, uint256 amount);
/* ── the NFT side, stored here, exposed by the mirror ────────────────── */
address public mirror;
address public owner;
mapping(uint256 => address) public ownerOfId;
/// @notice The epoch a Chunk was minted in. This is the whole lifecycle: a Chunk freezes one
/// cell per epoch it survives, and a Chunk that moves is burned and re-minted, so the
/// buyer starts again from zero.
mapping(uint256 => uint256) public bornAt;
mapping(address => uint256[]) internal _owned;
mapping(uint256 => uint256) internal _ownedIndex;
/// @notice Addresses that hold the token without holding Chunks: the v4 PoolManager, the LP
/// manager, routers. Contracts default to skipping, which is almost always what they
/// want; anyone can change their own setting.
mapping(address => bool) internal _skip;
mapping(address => bool) internal _skipSet;
uint256 public nextId = 1;
uint256 public mintedTotal;
uint256 public burnedTotal;
/// The clock. Read, never written, by this contract.
address public state;
event MirrorSet(address mirror);
event SkipNFTSet(address indexed account, bool skip);
event ChunkMinted(uint256 indexed id, address indexed to, uint256 bornAt);
event ChunkBurned(uint256 indexed id, address indexed from);
error NotOwner();
error NotMirror();
error AlreadySet();
error ZeroAddress();
error InsufficientBalance();
error InsufficientAllowance();
error NotChunkOwner();
error SenderSkips();
error NothingOwed();
modifier onlyOwner() { if (msg.sender != owner) revert NotOwner(); _; }
constructor(uint256 supply, uint256 unit, address _state, address to) {
if (_state == address(0) || to == address(0)) revert ZeroAddress();
if (unit == 0) revert ZeroAddress();
owner = msg.sender;
state = _state;
totalSupply = supply;
UNIT = unit;
// the recipient holds the whole float before the pool exists; materialising Chunks for it
// would be pointless and enormous, so the deployer starts skipping
_skip[to] = true;
_skipSet[to] = true;
emit SkipNFTSet(to, true);
balanceOf[to] = supply;
emit Transfer(address(0), to, supply);
}
/// @notice How many Chunks this collection can ever hold at once.
function maxChunks() external view returns (uint256) {
return totalSupply / UNIT;
}
function setMirror(address m) external onlyOwner {
if (mirror != address(0)) revert AlreadySet();
if (m == address(0)) revert ZeroAddress();
mirror = m;
emit MirrorSet(m);
}
/// @notice Whether an address materialises Chunks. Contracts default to skipping, because a
/// pool or a router holding NFTs helps nobody and costs everybody gas.
function skipsNFT(address a) public view returns (bool) {
if (_skipSet[a]) return _skip[a];
return a.code.length != 0;
}
/// @notice Anyone may set their own. Turning it off does not retroactively mint: call
/// `mintOwed` for that, in batches you choose.
function setSkipNFT(bool v) external {
_skip[msg.sender] = v;
_skipSet[msg.sender] = true;
emit SkipNFTSet(msg.sender, v);
}
/// @notice The owner may only set this for infrastructure before the pool opens; it is a
/// convenience for deployment, not a power over holders.
function setSkipNFTFor(address a, bool v) external onlyOwner {
_skip[a] = v;
_skipSet[a] = true;
emit SkipNFTSet(a, v);
}
/// @notice Chunks this address could materialise but has not.
function owedOf(address a) public view returns (uint256) {
if (skipsNFT(a)) return 0;
uint256 due = balanceOf[a] / UNIT;
uint256 have = _owned[a].length;
return due > have ? due - have : 0;
}
/// @notice Claim up to `n` of the Chunks you are owed. Bounded, and you pay for it.
function mintOwed(uint256 n) external {
uint256 owed = owedOf(msg.sender);
if (owed == 0) revert NothingOwed();
if (n == 0 || n > owed) n = owed;
if (n > MAX_MINT_PER_TX) n = MAX_MINT_PER_TX;
for (uint256 i = 0; i < n; ++i) _mintOne(msg.sender);
}
function renounceOwnership() external onlyOwner { owner = address(0); }
/* ── ERC-20 surface ─────────────────────────────────────────────────── */
function approve(address spender, uint256 amount) external returns (bool) {
allowance[msg.sender][spender] = amount;
emit Approval(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) {
uint256 a = allowance[from][msg.sender];
if (a != type(uint256).max) {
if (a < amount) revert InsufficientAllowance();
allowance[from][msg.sender] = a - amount;
}
_transfer(from, to, amount);
return true;
}
/* ── the NFT view, read by the mirror ───────────────────────────────── */
function erc721BalanceOf(address a) external view returns (uint256) {
return _owned[a].length;
}
function ownedIds(address a) external view returns (uint256[] memory) {
return _owned[a];
}
function ownedAt(address a, uint256 i) external view returns (uint256) {
return _owned[a][i];
}
/// @notice Chunks alive right now.
function chunkSupply() external view returns (uint256) {
return mintedTotal - burnedTotal;
}
/* ── moving one specific Chunk, on the mirror's authority ───────────── */
/// @notice Move exactly one whole token AND the named Chunk with it. Called only by the mirror,
/// which has already checked that `spender` may move `id`.
/// @dev The Chunk keeps its id and its birth epoch here — an ERC-721 transfer is a deliberate
/// move of that specific Chunk, not a burn-and-remint. Selling through the pool goes
/// the other way, through `_transfer`, and does reset the clock.
function mirrorTransfer(address from, address to, uint256 id) external {
if (msg.sender != mirror) revert NotMirror();
if (ownerOfId[id] != from) revert NotChunkOwner();
if (to == address(0)) revert ZeroAddress();
if (skipsNFT(from) || skipsNFT(to)) revert SenderSkips();
if (balanceOf[from] < UNIT) revert InsufficientBalance();
unchecked {
balanceOf[from] -= UNIT;
balanceOf[to] += UNIT;
}
emit Transfer(from, to, UNIT);
_unlink(from, id);
_link(to, id);
// the mirror emits the ERC-721 Transfer
}
/* ── internals ──────────────────────────────────────────────────────── */
function _transfer(address from, address to, uint256 amount) internal {
if (to == address(0)) revert ZeroAddress();
uint256 fromBefore = balanceOf[from];
if (fromBefore < amount) revert InsufficientBalance();
/* Write the sender FIRST, then read the receiver. Reading `balanceOf[to]` before the
sender is debited looks harmless until `from == to`: the second write then uses a stale
value and credits `amount` out of nothing, doubling the balance on every self-transfer.
That is a mint, and it was live. */
unchecked {
balanceOf[from] = fromBefore - amount;
}
uint256 toBefore = balanceOf[to];
unchecked {
balanceOf[to] = toBefore + amount;
}
emit Transfer(from, to, amount);
/* Burn first, then mint: a Chunk never survives a move through the ERC-20 side. That is
the whole tension of the design, and it is where the buyer's clock starts at zero.
The burn is NOT capped. Owning more Chunks than the balance allows would break the
invariant in the unsafe direction, so it always runs to completion; it is bounded by
how many the sender holds, which is bounded by the collection size.
The mint IS capped, and the remainder becomes `owedOf`. A swap must never revert
because the art was expensive. */
uint256 held = _owned[from].length;
if (held > 0) {
uint256 allowed = balanceOf[from] / UNIT;
uint256 burnCount = held > allowed ? held - allowed : 0;
for (uint256 i = 0; i < burnCount; ++i) _burnOne(from);
}
if (!skipsNFT(to)) {
uint256 due = balanceOf[to] / UNIT;
uint256 have = _owned[to].length;
uint256 mintCount = due > have ? due - have : 0;
if (mintCount > MAX_MINT_PER_TX) mintCount = MAX_MINT_PER_TX;
for (uint256 i = 0; i < mintCount; ++i) _mintOne(to);
}
}
function _mintOne(address to) internal {
uint256 id = nextId++;
// read the clock once per mint; a Chunk minted now is born now
(bool ok, bytes memory data) = state.staticcall(abi.encodeWithSignature("epoch()"));
bornAt[id] = ok && data.length == 32 ? abi.decode(data, (uint256)) : 0;
_link(to, id);
++mintedTotal;
emit ChunkMinted(id, to, bornAt[id]);
IChunckMirror(mirror).emitTransfer(address(0), to, id);
}
function _burnOne(address from) internal {
uint256[] storage list = _owned[from];
uint256 id = list[list.length - 1]; // LIFO: the newest Chunk goes first
_unlink(from, id);
delete bornAt[id];
++burnedTotal;
emit ChunkBurned(id, from);
IChunckMirror(mirror).emitTransfer(from, address(0), id);
}
function _link(address to, uint256 id) internal {
ownerOfId[id] = to;
_ownedIndex[id] = _owned[to].length;
_owned[to].push(id);
}
function _unlink(address from, uint256 id) internal {
uint256[] storage list = _owned[from];
uint256 i = _ownedIndex[id];
uint256 last = list.length - 1;
if (i != last) {
uint256 moved = list[last];
list[i] = moved;
_ownedIndex[moved] = i;
}
list.pop();
delete _ownedIndex[id];
delete ownerOfId[id];
}
}
interface IChunckMirror {
function emitTransfer(address from, address to, uint256 id) external;
}
Compiler settingsShow
{
"viaIR": false,
"metadata": {
"appendCBOR": true,
"bytecodeHash": "ipfs",
"useLiteralContent": false
},
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "cancun",
"remappings": [
"forge-std/=lib/forge-std/src/",
"v4-core/=lib/v4-core/",
"v4-periphery/=lib/v4-periphery/",
"@openzeppelin/=lib/openzeppelin-contracts/",
"permit2/=lib/permit2/",
"solmate/=lib/solmate/",
"@ensdomains/=/Users/pro/prism-v4-contracts/lib/v4-core/node_modules/@ensdomains/",
"@uniswap/v4-core/=/Users/pro/prism-v4-contracts/lib/v4-periphery/lib/v4-core/",
"base-std/=lib/base-std/src/",
"ds-test/=/Users/pro/prism-v4-contracts/lib/v4-core/lib/forge-std/lib/ds-test/src/",
"erc4626-tests/=/Users/pro/prism-v4-contracts/lib/openzeppelin-contracts/lib/erc4626-tests/",
"forge-gas-snapshot/=/Users/pro/prism-v4-contracts/lib/v4-periphery/lib/permit2/lib/forge-gas-snapshot/src/",
"halmos-cheatcodes/=/Users/pro/prism-v4-contracts/lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
"hardhat/=/Users/pro/prism-v4-contracts/lib/v4-core/node_modules/hardhat/",
"openzeppelin-contracts/=lib/openzeppelin-contracts/",
"solady/=/Users/pro/prism-v4-contracts/lib/solady/src/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.