Verified contractPartialMIT
PegToken
Address0xac810ea5efd4cd2dda9d48268e7f32cd652b67cb
- Balance
0 ETH
- Transactions
32
- 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
- Via IR
- yes
- License
- MIT
- Match
- Partialcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,147,001
- Creation transaction
- 0x5ac3145b1a…27bf9f1e2c
src/PegToken.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;
interface IPegRenderer {
function svg(uint64 id) external view returns (string memory);
function uri(uint64 id) external view returns (string memory);
}
/* PegToken — an ERC-20 where every whole token is a pixel hook.
Supply is fixed (10,000 by default). Balances are ordinary ERC-20 balances and trade in fractions, but each
whole unit (1.0, 2.0, 3.0 …) a wallet holds is pegged to one pixel hook, drawn 100% on-chain by the renderer.
· A wallet crosses up to the next whole number → a hook is born. Its look comes from a seed mixed from the
token's entropy, which the Uniswap v4 hook stirs on every swap, so every trade reshapes what gets born next.
· A wallet drops below a whole number → its newest hook leaves: it moves to the receiver if the receiver
gains a whole unit in the same transfer, otherwise it dies.
· Moving whole tokens wallet-to-wallet moves the hooks themselves (newest first).
Contracts that only hold tokens in bulk — the Uniswap PoolManager, the hook contract, this contract, the dead
address, and any router the owner marks — are "exempt": they hold tokens but no art.
An art id is a uint64: the high 32 bits are its birth number (#1, #2 …), the low 32 bits its seed. */
contract PegToken {
string public name;
string public symbol;
uint8 public constant decimals = 18;
uint256 public constant UNIT = 1e18;
uint256 public immutable totalSupply;
address constant DEAD = 0x000000000000000000000000000000000000dEaD;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
address public owner;
address public hook; // set once; may stir entropy
address public nft; // set once; the ERC-721 wrapper (PegNFT) for OpenSea
IPegRenderer public renderer; // owner may swap it until `lockRenderer`
bool public rendererLocked;
mapping(address => bool) public exempt;
bytes32 public entropy;
uint32 public born; // hooks ever born (= the last birth number)
uint32 public died; // hooks that died
// packed hook ids per wallet: 4 × uint64 per storage word; count = balanceOf / UNIT (non-exempt wallets)
mapping(address => mapping(uint256 => uint256)) internal _ids;
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
event Born(address indexed holder, uint64 id);
event Died(address indexed holder, uint64 id);
event Moved(address indexed from, address indexed to, uint64 id);
event ExemptSet(address indexed account, bool exempt);
event OwnerSet(address owner);
event HookSet(address hook);
event RendererSet(address renderer);
event NftSet(address nft);
event Wrapped(address indexed holder, uint64 id);
event Unwrapped(address indexed holder, uint64 id);
modifier onlyOwner() { require(msg.sender == owner, "owner"); _; }
constructor(string memory n, string memory s, uint256 wholeSupply, address poolManager, IPegRenderer r) {
name = n; symbol = s; owner = msg.sender; renderer = r;
totalSupply = wholeSupply * UNIT;
exempt[address(this)] = true; exempt[poolManager] = true; exempt[DEAD] = true; exempt[address(0)] = true;
emit ExemptSet(poolManager, true);
entropy = keccak256(abi.encode(n, s, block.prevrandao, block.timestamp, msg.sender));
balanceOf[address(this)] = totalSupply; // held here (exempt) until the hook takes it for the pool
emit Transfer(address(0), address(this), totalSupply);
}
/* ================= ERC-20 ================= */
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) { require(a >= amount, "allowance"); allowance[from][msg.sender] = a - amount; }
_transfer(from, to, amount); return true;
}
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 from, address to, uint256 amount) internal {
require(to != address(0), "to");
uint256 fb = balanceOf[from]; require(fb >= amount, "balance");
if (from == to || amount == 0) { emit Transfer(from, to, amount); return; }
uint256 tb = balanceOf[to];
unchecked { balanceOf[from] = fb - amount; }
balanceOf[to] = tb + amount;
emit Transfer(from, to, amount);
uint256 lost = exempt[from] ? 0 : fb / UNIT - (fb - amount) / UNIT;
uint256 gained = exempt[to] ? 0 : (tb + amount) / UNIT - tb / UNIT;
if (lost == 0 && gained == 0) return;
uint256 fromCount = fb / UNIT; uint256 toCount = tb / UNIT;
uint256 move = lost < gained ? lost : gained;
// newest hooks of the sender move to the receiver
for (uint256 i = 0; i < move; i++) {
uint64 id = _get(from, fromCount - 1 - i);
_set(to, toCount + i, id);
emit Moved(from, to, id);
}
// the rest of what the sender lost dies
for (uint256 i = move; i < lost; i++) {
uint64 id = _get(from, fromCount - 1 - i);
emit Died(from, id);
}
if (lost > move) died += uint32(lost - move);
// the rest of what the receiver gained is born
if (gained > move) {
bytes32 e = keccak256(abi.encode(entropy, from, to, amount, block.prevrandao, block.number));
uint32 n = born;
for (uint256 i = move; i < gained; i++) {
n += 1;
uint32 seed = uint32(uint256(keccak256(abi.encode(e, i))));
uint64 id = (uint64(n) << 32) | uint64(seed);
_set(to, toCount + i, id);
emit Born(to, id);
}
born = n; entropy = e;
}
}
/* ================= hooks ================= */
function _get(address a, uint256 i) internal view returns (uint64) {
return uint64(_ids[a][i >> 2] >> ((i & 3) * 64));
}
function _set(address a, uint256 i, uint64 id) internal {
uint256 w = _ids[a][i >> 2]; uint256 sh = (i & 3) * 64;
_ids[a][i >> 2] = (w & ~(uint256(type(uint64).max) << sh)) | (uint256(id) << sh);
}
/// how many hooks a wallet has (whole tokens held; 0 for exempt contracts)
function hooksOf(address a) public view returns (uint256) { return exempt[a] ? 0 : balanceOf[a] / UNIT; }
/// a wallet's hook ids, oldest first. `start`/`limit` page through big wallets.
function idsOf(address a, uint256 start, uint256 limit) external view returns (uint64[] memory ids) {
uint256 n = hooksOf(a); if (start >= n) return new uint64[](0);
uint256 end = start + limit > n ? n : start + limit;
ids = new uint64[](end - start);
for (uint256 i = start; i < end; i++) ids[i - start] = _get(a, i);
}
function hookOf(address a, uint256 index) external view returns (uint64) { require(index < hooksOf(a), "index"); return _get(a, index); }
/// on-chain art
function svgOf(uint64 id) external view returns (string memory) { return renderer.svg(id); }
function uriOf(uint64 id) external view returns (string memory) { return renderer.uri(id); }
/// hooks alive right now
function alive() external view returns (uint256) { return uint256(born) - uint256(died); }
/* ================= hook & admin ================= */
/// the hook mixes swap data into the entropy on every trade
function stir(bytes32 x) external { require(msg.sender == hook, "hook"); entropy = keccak256(abi.encode(entropy, x)); }
/// one time: connect the Uniswap v4 hook and hand it the whole supply for the pool
function setHook(address h) external onlyOwner {
require(hook == address(0) && h != address(0), "hook set");
hook = h; exempt[h] = true; emit ExemptSet(h, true); emit HookSet(h);
uint256 b = balanceOf[address(this)];
balanceOf[address(this)] = 0; balanceOf[h] += b; emit Transfer(address(this), h, b);
}
/// mark a router / exchange contract as exempt (no hooks). Its hooks (if any) die or are reborn accordingly.
function setExempt(address a, bool on) external onlyOwner {
require(a != address(this) && a != hook && a != nft && a != address(0) && a != DEAD, "fixed");
if (exempt[a] == on) return;
uint256 n = balanceOf[a] / UNIT;
if (on) {
for (uint256 i = n; i > 0; i--) emit Died(a, _get(a, i - 1));
died += uint32(n);
exempt[a] = true;
} else {
exempt[a] = false;
bytes32 e = keccak256(abi.encode(entropy, a, block.prevrandao, block.number)); uint32 b = born;
for (uint256 i = 0; i < n; i++) {
b += 1; uint64 id = (uint64(b) << 32) | uint64(uint32(uint256(keccak256(abi.encode(e, i)))));
_set(a, i, id); emit Born(a, id);
}
born = b; entropy = e;
}
emit ExemptSet(a, on);
}
/* ================= NFT wrapper (OpenSea) ================= */
/// one time: connect the PegNFT wrapper. It holds 1 whole token for every wrapped hook (exempt, so no art of its own).
function setNft(address n) external onlyOwner {
require(nft == address(0) && n != address(0) && n != hook, "nft set");
nft = n; exempt[n] = true; emit ExemptSet(n, true); emit NftSet(n);
}
/// wrapper only: take the hook at `index` (any position) plus exactly 1 whole token out of `a`.
/// The hook stays alive; it now lives as an ERC-721 held by the wrapper's NFT owner.
function pegOut(address a, uint256 index) external returns (uint64 id) {
require(msg.sender == nft, "nft");
require(!exempt[a], "exempt");
uint256 fb = balanceOf[a]; uint256 n = fb / UNIT;
require(index < n, "index");
id = _get(a, index);
if (index != n - 1) _set(a, index, _get(a, n - 1)); // fill the gap with the newest hook
unchecked { balanceOf[a] = fb - UNIT; } // floor drops by exactly one
balanceOf[nft] += UNIT;
emit Transfer(a, nft, UNIT);
emit Moved(a, nft, id); emit Wrapped(a, id);
}
/// wrapper only: give `to` exactly 1 whole token back together with that same hook.
function pegIn(address to, uint64 id) external {
require(msg.sender == nft, "nft");
require(to != address(0) && !exempt[to], "exempt");
uint256 tb = balanceOf[to];
balanceOf[nft] -= UNIT;
balanceOf[to] = tb + UNIT; // floor rises by exactly one
_set(to, tb / UNIT, id);
emit Transfer(nft, to, UNIT);
emit Moved(nft, to, id); emit Unwrapped(to, id);
}
function setRenderer(IPegRenderer r) external onlyOwner { require(!rendererLocked, "locked"); renderer = r; emit RendererSet(address(r)); }
function lockRenderer() external onlyOwner { rendererLocked = true; }
function setOwner(address o) external onlyOwner { owner = o; emit OwnerSet(o); }
}
Compiler settingsShow
{
"viaIR": true,
"metadata": {
"appendCBOR": true,
"bytecodeHash": "none",
"useLiteralContent": false
},
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "cancun",
"remappings": [
"@v4/=lib/v4-core/src/",
"forge-std/=lib/forge-std/src/",
"@uniswap/v4-core/=lib/v4-core/",
"@ensdomains/=/home/claude/defib-v4/lib/v4-core/node_modules/@ensdomains/",
"@openzeppelin/=/home/claude/defib-v4/lib/v4-core/lib/openzeppelin-contracts/",
"ds-test/=/home/claude/defib-v4/lib/v4-core/lib/forge-std/lib/ds-test/src/",
"hardhat/=/home/claude/defib-v4/lib/v4-core/node_modules/hardhat/",
"openzeppelin-contracts/=/home/claude/defib-v4/lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/",
"solmate/=/home/claude/defib-v4/lib/v4-core/lib/solmate/",
"v4-core/=/home/claude/defib-v4/lib/v4-core/src/",
"v4-periphery/=/home/claude/defib-v4/lib/v4-periphery/src/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.