Verified contractExactMIT
QdayPad
Address0x34a5984d4936c4fb112ea5bbbf7bca53d3bb62cc
- Balance
0.010199 ETH
- Transactions
3
- Compiler
solc 0.8.28
- Same code
Only this contract
Details
- Compiler
- solc 0.8.28+commit.7893614a
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- cancun
- Via IR
- yes
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,350,301
- Creation transaction
- 0xa2f1acae6c…a825148b1a
3 files
- QdayPad.solmain19.44 KB
- QdayCoin.sol3.99 KB
- QdayTypes.sol2.75 KB
QdayPad.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
// QDAY PREPRINT 2 . A launch curve that hands each coin to its own Uniswap v4 pool
// web https://quantumday.fun . x https://x.com/qday_fun
//
// Result. One contract launches coins, sells them on a bonding curve, opens each coin's v4 pool when the
// curve is sold out, and is the hook of every one of those pools, so one ledger holds every fee.
// Construction. The curve is a constant product on virtual reserves of 1,125,000,000 coins and 1.125 ETH, so each
// coin starts at a fully diluted value of exactly 1 ETH. 750,000,000 coins are for sale. The curve
// closes when 2.25 ETH net has come in, at nine times the opening price; the buy that crosses the
// end is cut there and refunded the ETH it did not need.
// Promotion. That same buy creates the pool ETH/coin (LP fee 0, tick spacing 200, this contract as hook) at the
// curve's closing price, and places the 2.25 ETH and the 250,000,000 unsold coins in it as one
// full-range position through the PositionManager, whose NFT goes to the OWNER. Whoever holds that
// NFT can take the liquidity out, until Q-Day (below).
// Fees. 1% of the ETH side of every trade, on the curve and in the pool, in all four swap shapes (an exact
// output pays 1% of its gross). Of that 1%: 0.50% to the OWNER, 0.25% to the coin's creator, 0.15% to
// the coin's vault holders, 0.10% to the siren. Vault holders share in proportion to the coin's
// circulating supply: holding x of the circulating coins in the vault earns x of that 0.15%; the part
// belonging to coins outside the vault goes to the siren at once, so no pot waits for a late claimant.
// Circulating means sold by the curve, then, after promotion, every coin not held by the PoolManager,
// read from the coin's own balance sheet, so added or removed liquidity cannot skew it.
// payOwner, payCreatorShare and feedSiren may be called by anyone and pay only the recorded
// recipient; a creator's payee, and nobody else, may move that coin's creator share elsewhere.
// After Q-Day. Coins move only between the pad, the vault and the PoolManager: curve buys have to land in the
// vault, and this hook refuses to let liquidity leave any pool.
// Measured. qday/measured.json, from sim_qday.mjs on a mainnet fork.
import {QdayCoin} from "./QdayCoin.sol";
import {PoolKey, SwapParams, ModifyLiquidityParams, IPoolManager, IPositionManager, IAllowanceTransfer} from "./QdayTypes.sol";
interface ISirenAlarm {
function alarm() external view returns (address);
}
contract QdayPad {
// ---------------------------------------------------------------- the curve (econ_qday.js)
uint256 internal constant SUPPLY = 1_000_000_000e18;
uint256 internal constant CURVE = 750_000_000e18;
uint256 internal constant POOL_COINS = SUPPLY - CURVE;
uint256 internal constant T0 = 1_125_000_000e18;
uint256 internal constant V0 = 1.125 ether;
uint256 internal constant K = V0 * T0;
uint256 internal constant RAISE = 2.25 ether;
uint256 internal constant FEE_BPS = 100;
uint256 internal constant ACC = 1e27;
uint256 internal constant MIN_VAULTED = 1e18;
// ---------------------------------------------------------------- the pool (qday/tools/pool_constants.js)
address internal constant POOL_MANAGER = 0x000000000004444c5dc75cB358380D2e3dE08A90;
address internal constant POSITIONS = 0xbD216513d74C8cf14cf4747E6AaA6420FF64ee9e;
address internal constant PERMIT2 = 0x000000000022D473030F116dDEE9F6B43aC78BA3;
int24 internal constant SPACING = 200;
int24 internal constant LOW = -887200;
int24 internal constant HIGH = 887200;
uint160 internal constant OPEN_SQRT = 835138161250159770621379338565354;
uint128 internal constant BAND_L = 23717082451262844989991;
address public immutable OWNER;
address public immutable VAULT;
address public immutable SIREN;
address public immutable ALARM;
/// phase 0 not a QDAY coin, 1 on the curve, 2 in its pool. sold = coins the curve has sold (frozen at 750M once
/// the coin is in its pool; see circulatingOf).
struct Book {
uint96 sold;
uint88 creatorOwed;
uint64 raised;
uint8 phase;
}
struct Yield {
uint128 acc;
uint128 vaulted;
}
struct Owed {
uint96 owner;
uint80 siren;
uint80 yieldHeld;
}
mapping(address => Book) public books;
mapping(address => Yield) public yields;
mapping(address => address) public payeeOf;
Owed public owed;
uint256 public launched;
event Launched(address indexed coin, address indexed creator, address payee, string name, string symbol, string image);
event Bought(address indexed coin, address indexed buyer, address to, uint256 eth, uint256 fee, uint256 coins, uint256 raised, uint256 sold);
event Sold(address indexed coin, address indexed seller, address to, uint256 coins, uint256 eth, uint256 fee, uint256 raised, uint256 sold);
event Promoted(address indexed coin, uint256 tokenId);
event HookFee(address indexed coin, uint256 fee);
event Paid(uint8 indexed kind, address indexed to, uint256 amount);
event PayeeMoved(address indexed coin, address indexed from, address indexed to);
error CurveClosed();
error Slippage();
error BadLaunch();
error NotPoolManager();
error NotVault();
error PartialFill();
error QDayFrozen();
error Occupied();
error PayFailed();
error NotPayee();
constructor(address owner_, address vault_, address siren_) {
OWNER = owner_;
VAULT = vault_;
SIREN = siren_;
ALARM = ISirenAlarm(siren_).alarm();
}
receive() external payable {}
modifier exclusive() {
bool busy;
assembly {
busy := tload(0)
}
if (busy) revert Occupied();
assembly {
tstore(0, 1)
}
_;
assembly {
tstore(0, 0)
}
}
// ================================================================ launch, buy, sell
/// Launches a coin. ETH sent with it is the creator's first buy, priced like anyone's.
function create(string calldata name, string calldata symbol, string calldata image, address payee)
external
payable
exclusive
returns (address coin)
{
if (bytes(name).length == 0 || bytes(name).length > 32 || bytes(symbol).length == 0 || bytes(symbol).length > 12) {
revert BadLaunch();
}
if (bytes(image).length > 256) revert BadLaunch();
if (payee == address(0)) payee = msg.sender;
coin = address(new QdayCoin(name, symbol, VAULT, ALARM));
books[coin].phase = 1;
payeeOf[coin] = payee;
unchecked {
++launched;
}
emit Launched(coin, msg.sender, payee, name, symbol, image);
if (msg.value != 0) _buy(coin, msg.value, 0, msg.sender);
}
function buy(address coin, uint256 minOut, address to) external payable exclusive returns (uint256 out) {
out = _buy(coin, msg.value, minOut, to);
}
function sell(address coin, uint256 amount, uint256 minEth, address to) external exclusive returns (uint256 net) {
Book memory b = books[coin];
if (b.phase != 1) revert CurveClosed();
uint256 v = V0 + b.raised;
uint256 t = T0 - b.sold;
uint256 gross = v - _ceilDiv(K, t + amount);
uint256 fee = _onePercent(gross);
net = gross - fee;
if (net < minEth || amount == 0) revert Slippage();
b.raised -= uint64(gross);
b.sold -= uint96(amount);
_accrue(coin, b, fee, b.sold);
books[coin] = b;
QdayCoin(coin).drawFrom(msg.sender, amount);
emit Sold(coin, msg.sender, to, amount, net, fee, b.raised, b.sold);
_send(to, net);
}
function _buy(address coin, uint256 gross, uint256 minOut, address to) private returns (uint256 out) {
Book memory b = books[coin];
if (b.phase != 1) revert CurveClosed();
uint256 fee = _onePercent(gross);
uint256 net = gross - fee;
uint256 v = V0 + b.raised;
out = (T0 - b.sold) - _ceilDiv(K, v + net);
uint256 refund;
bool last = b.sold + out >= CURVE;
if (last) {
// stop at the end of the curve: the exact net that reaches V0 + RAISE, the smallest gross paying it
out = CURVE - b.sold;
uint256 need = V0 + RAISE - v;
uint256 g = _grossFor(need);
refund = gross - g;
fee = g - need; // 1% of g, plus the at most 1 wei that rounding leaves over
net = need;
gross = g;
}
if (out < minOut || out == 0) revert Slippage();
b.raised += uint64(net);
b.sold += uint96(out);
_accrue(coin, b, fee, b.sold);
books[coin] = b;
QdayCoin(coin).transfer(to, out);
emit Bought(coin, msg.sender, to, gross, fee, out, b.raised, b.sold);
if (last) _promote(coin);
if (refund != 0) _send(msg.sender, refund);
}
function _promote(address coin) private {
books[coin].phase = 2;
PoolKey memory key = poolFor(coin);
IPoolManager(POOL_MANAGER).initialize(key, OPEN_SQRT);
QdayCoin(coin).approve(PERMIT2, POOL_COINS);
IAllowanceTransfer(PERMIT2).approve(coin, POSITIONS, uint160(POOL_COINS), 0);
uint256 tokenId = IPositionManager(POSITIONS).nextTokenId();
bytes[] memory params = new bytes[](3);
params[0] = abi.encode(key, LOW, HIGH, uint256(BAND_L), uint128(RAISE), uint128(POOL_COINS), OWNER, bytes(""));
params[1] = abi.encode(address(0), coin);
params[2] = abi.encode(address(0), address(this));
// MINT_POSITION, SETTLE_PAIR, SWEEP: the 1,290 wei the position leaves come straight back
IPositionManager(POSITIONS).modifyLiquidities{value: RAISE}(abi.encode(hex"020d14", params), block.timestamp);
emit Promoted(coin, tokenId);
}
// ================================================================ the ledger
/// circ: the coin's circulating supply at this trade (the curve's sold count, or circulatingOf in the pool).
function _accrue(address coin, Book memory b, uint256 fee, uint256 circ) private {
unchecked {
uint256 creatorCut = (fee * 25) / 100;
uint256 vaultCut = (fee * 15) / 100;
uint256 sirenCut = fee / 10;
uint256 ownerCut = fee - creatorCut - vaultCut - sirenCut;
b.creatorOwed += uint88(creatorCut);
uint256 held;
Yield storage y = yields[coin];
uint256 vaulted = y.vaulted;
if (vaulted >= MIN_VAULTED) {
y.acc += uint128((vaultCut * ACC) / circ);
// reserved rounded up and capped at the cut, so the vault can never be owed more than is held
held = (vaultCut * vaulted + circ - 1) / circ;
if (held > vaultCut) held = vaultCut;
}
Owed memory o = owed;
o.owner += uint96(ownerCut);
o.siren += uint80(sirenCut + vaultCut - held);
o.yieldHeld += uint80(held);
owed = o;
}
}
function payOwner() external exclusive {
uint256 a = owed.owner;
owed.owner = 0;
emit Paid(0, OWNER, a);
_send(OWNER, a);
}
function payCreatorShare(address coin) external exclusive {
uint256 a = books[coin].creatorOwed;
books[coin].creatorOwed = 0;
address to = payeeOf[coin];
emit Paid(1, to, a);
_send(to, a);
}
/// The creator's payee, and only it, may hand the creator share of `coin` to another address.
function movePayee(address coin, address to) external {
if (msg.sender != payeeOf[coin] || to == address(0)) revert NotPayee();
payeeOf[coin] = to;
emit PayeeMoved(coin, msg.sender, to);
}
function feedSiren() external exclusive {
uint256 a = owed.siren;
owed.siren = 0;
emit Paid(2, SIREN, a);
_send(SIREN, a);
}
// ---------------------------------------------------------------- what the vault may change
function noteVault(address coin, int256 delta) external returns (uint256 acc) {
if (msg.sender != VAULT) revert NotVault();
Yield storage y = yields[coin];
acc = y.acc;
if (delta >= 0) y.vaulted += uint128(uint256(delta));
else y.vaulted -= uint128(uint256(-delta));
}
function releaseYield(uint256 amount) external {
if (msg.sender != VAULT) revert NotVault();
owed.yieldHeld -= uint80(amount);
_send(VAULT, amount);
}
// ================================================================ the hook (address flags 0x22CC)
function beforeInitialize(address sender, PoolKey calldata, uint160) external view returns (bytes4) {
if (msg.sender != POOL_MANAGER) revert NotPoolManager();
if (sender != address(this)) revert BadLaunch();
return this.beforeInitialize.selector;
}
function beforeRemoveLiquidity(address, PoolKey calldata, ModifyLiquidityParams calldata, bytes calldata)
external
view
returns (bytes4)
{
if (msg.sender != POOL_MANAGER) revert NotPoolManager();
if (ALARM.code.length != 0) revert QDayFrozen();
return this.beforeRemoveLiquidity.selector;
}
function beforeSwap(address, PoolKey calldata key, SwapParams calldata p, bytes calldata)
external
returns (bytes4, int256, uint24)
{
if (msg.sender != POOL_MANAGER) revert NotPoolManager();
int256 hookDelta;
if (p.zeroForOne == (p.amountSpecified < 0)) {
// ETH is the specified side: an exact-input buy or an exact-output sell
uint256 amt = p.amountSpecified < 0 ? uint256(-p.amountSpecified) : uint256(p.amountSpecified);
uint256 fee = p.zeroForOne ? _onePercent(amt) : _ceilDiv(amt, 99);
IPoolManager(POOL_MANAGER).take(address(0), address(this), fee);
address coin = key.currency1;
Book memory b = books[coin];
_accrue(coin, b, fee, _poolCirculating(coin));
books[coin] = b;
emit HookFee(coin, fee);
assembly {
tstore(1, fee)
}
hookDelta = int256(fee) << 128;
}
return (this.beforeSwap.selector, hookDelta, 0);
}
function afterSwap(address, PoolKey calldata key, SwapParams calldata p, int256 delta, bytes calldata)
external
returns (bytes4, int128)
{
if (msg.sender != POOL_MANAGER) revert NotPoolManager();
int256 eth = delta >> 128;
int128 hookDelta;
if (p.zeroForOne == (p.amountSpecified < 0)) {
uint256 taken;
assembly {
taken := tload(1)
tstore(1, 0)
}
// the fee was charged on the whole amount, so the whole amount has to have swapped
if (p.zeroForOne ? uint256(-eth) + taken != uint256(-p.amountSpecified) : uint256(eth) != uint256(p.amountSpecified) + taken) {
revert PartialFill();
}
} else {
// ETH is the unspecified side: an exact-output buy pays 1% of its gross on top, an exact-input sell
// hands over 1% of what it receives
uint256 fee = p.zeroForOne ? _ceilDiv(uint256(-eth), 99) : _onePercent(uint256(eth));
IPoolManager(POOL_MANAGER).take(address(0), address(this), fee);
address coin = key.currency1;
Book memory b = books[coin];
_accrue(coin, b, fee, _poolCirculating(coin));
books[coin] = b;
emit HookFee(coin, fee);
hookDelta = int128(int256(fee));
}
return (this.afterSwap.selector, hookDelta);
}
/// After promotion: every coin not held by the PoolManager. The hook runs before the swap settles, so this is the
/// supply as it stood when the trade began.
function _poolCirculating(address coin) private view returns (uint256) {
return SUPPLY - QdayCoin(coin).balanceOf(POOL_MANAGER);
}
// ================================================================ views
function poolFor(address coin) public view returns (PoolKey memory) {
return PoolKey(address(0), coin, 0, SPACING, address(this));
}
function promoted(address coin) external view returns (bool) {
return books[coin].phase == 2;
}
function coinPhase(address coin) external view returns (uint8) {
return books[coin].phase;
}
function accOf(address coin) external view returns (uint256) {
return yields[coin].acc;
}
/// The supply the vault share is divided by: sold by the curve, or after promotion everything outside the pool.
function circulatingOf(address coin) external view returns (uint256) {
uint8 phase = books[coin].phase;
if (phase == 1) return books[coin].sold;
if (phase == 2) return _poolCirculating(coin);
return 0;
}
/// Coins out, fee and refund for `gross` wei on the curve, as buy() computes them.
function previewBuy(address coin, uint256 gross) external view returns (uint256 out, uint256 fee, uint256 refund) {
Book memory b = books[coin];
if (b.phase != 1) revert CurveClosed();
fee = _onePercent(gross);
uint256 v = V0 + b.raised;
out = (T0 - b.sold) - _ceilDiv(K, v + gross - fee);
if (b.sold + out >= CURVE) {
out = CURVE - b.sold;
uint256 need = V0 + RAISE - v;
uint256 g = _grossFor(need);
refund = gross - g;
fee = g - need;
}
}
/// ETH to the seller and fee for `amount` coins on the curve, as sell() computes them.
function previewSell(address coin, uint256 amount) external view returns (uint256 net, uint256 fee) {
Book memory b = books[coin];
if (b.phase != 1) revert CurveClosed();
uint256 gross = V0 + b.raised - _ceilDiv(K, T0 - b.sold + amount);
fee = _onePercent(gross);
net = gross - fee;
}
// ================================================================ arithmetic
function _onePercent(uint256 gross) private pure returns (uint256) {
return (gross * FEE_BPS + 9_999) / 10_000;
}
/// The smallest gross whose net after the fee is at least `need`.
function _grossFor(uint256 need) private pure returns (uint256 g) {
g = _ceilDiv(need * 10_000, 10_000 - FEE_BPS);
while (g - _onePercent(g) < need) ++g;
while (g != 0 && g - 1 - _onePercent(g - 1) >= need) --g;
}
function _ceilDiv(uint256 a, uint256 b) private pure returns (uint256) {
return (a + b - 1) / b;
}
function _send(address to, uint256 amount) private {
if (amount == 0) return;
(bool ok,) = to.call{value: amount}("");
if (!ok) revert PayFailed();
}
}
QdayCoin.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
// QDAY PREPRINT 1 . A coin that stops at Q-Day
// web https://quantumday.fun . x https://x.com/qday_fun
//
// Result. Each coin the QDAY pad launches: 1,000,000,000 units with 18 decimals, created in one piece and
// handed to the pad, which sells three quarters on its curve and puts the rest in the coin's pool.
// No owner. No minting after birth, no pause, no tax, no blacklist.
// Construction. An ordinary ERC-20, with one shortcut: the pad and the vault may draw coins from the account that
// is calling them, so a curve sale or a vault deposit needs no separate approval. They draw only
// from their own caller; that rule lives in QdayPad and QdayVault, which are fixed code.
// After Q-Day. The siren's secp256k1 prize, once claimed, leaves code at the alarm address given to this coin at
// birth. From that block on, a coin moves only between the pad, the vault and the Uniswap v4
// PoolManager. Coins in ordinary wallets stay where they are, because whoever can forge their
// signatures could otherwise take them. Coins in the vault keep trading under hash-based keys.
// Measured. The alarm costs one EXTCODESIZE (2,600 gas cold) on a transfer with a wallet on either side, and
// nothing on a transfer between the pad, the vault and the PoolManager.
contract QdayCoin {
uint256 public constant totalSupply = 1_000_000_000e18;
uint8 public constant decimals = 18;
address internal constant POOL_MANAGER = 0x000000000004444c5dc75cB358380D2e3dE08A90;
address public immutable pad;
address public immutable vault;
address public immutable alarm;
string public name;
string public symbol;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
error QDayFrozen();
error NotQday();
constructor(string memory name_, string memory symbol_, address vault_, address alarm_) {
pad = msg.sender;
vault = vault_;
alarm = alarm_;
name = name_;
symbol = symbol_;
balanceOf[msg.sender] = totalSupply;
emit Transfer(address(0), msg.sender, totalSupply);
}
function transfer(address to, uint256 amount) external returns (bool) {
_move(msg.sender, to, amount);
return true;
}
function transferFrom(address from, address to, uint256 amount) external returns (bool) {
uint256 room = allowance[from][msg.sender];
if (room != type(uint256).max) allowance[from][msg.sender] = room - amount;
_move(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;
}
/// Only the pad and the vault, and they pass their own caller as `from`.
function drawFrom(address from, uint256 amount) external {
if (msg.sender != pad && msg.sender != vault) revert NotQday();
_move(from, msg.sender, amount);
}
/// True once the siren's secp256k1 prize has been claimed.
function qdayMode() external view returns (bool) {
return alarm.code.length != 0;
}
function _move(address from, address to, uint256 amount) private {
if (!(_ours(from) && _ours(to)) && alarm.code.length != 0) revert QDayFrozen();
balanceOf[from] -= amount;
unchecked {
balanceOf[to] += amount;
}
emit Transfer(from, to, amount);
}
function _ours(address a) private view returns (bool) {
return a == pad || a == vault || a == POOL_MANAGER;
}
}
QdayTypes.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
// QDAY PREPRINT 0 . Interfaces
// web https://quantumday.fun . x https://x.com/qday_fun
//
// Result. The few Uniswap v4 shapes the pad, the vault and the siren use, declared here so that no QDAY
// source imports anything from outside this folder.
// Construction. Field order and types follow v4-core, so the ABI encodings match the deployed PoolManager,
// PositionManager and Permit2 exactly; only the ABI matters to them, not these names.
struct PoolKey {
address currency0;
address currency1;
uint24 fee;
int24 tickSpacing;
address hooks;
}
struct SwapParams {
bool zeroForOne;
int256 amountSpecified;
uint160 sqrtPriceLimitX96;
}
struct ModifyLiquidityParams {
int24 tickLower;
int24 tickUpper;
int256 liquidityDelta;
bytes32 salt;
}
interface IPoolManager {
function initialize(PoolKey memory key, uint160 sqrtPriceX96) external returns (int24 tick);
function unlock(bytes calldata data) external returns (bytes memory);
function swap(PoolKey memory key, SwapParams memory params, bytes calldata hookData) external returns (int256 delta);
function sync(address currency) external;
function settle() external payable returns (uint256 paid);
function take(address currency, address to, uint256 amount) external;
}
interface IUnlockCallback {
function unlockCallback(bytes calldata data) external returns (bytes memory);
}
interface IPositionManager {
function modifyLiquidities(bytes calldata unlockData, uint256 deadline) external payable;
function nextTokenId() external view returns (uint256);
}
interface IAllowanceTransfer {
function approve(address token, address spender, uint160 amount, uint48 expiration) external;
}
/// The pad, as the coins and the vault reach it.
interface IQdayPad {
function buy(address coin, uint256 minOut, address to) external payable returns (uint256 out);
function sell(address coin, uint256 amount, uint256 minEth, address to) external returns (uint256 net);
function promoted(address coin) external view returns (bool);
function coinPhase(address coin) external view returns (uint8);
function noteVault(address coin, int256 delta) external returns (uint256 acc);
function accOf(address coin) external view returns (uint256);
function releaseYield(uint256 amount) external;
}
interface IQdayCoin {
function drawFrom(address from, uint256 amount) external;
function transfer(address to, uint256 amount) external returns (bool);
function approve(address spender, uint256 amount) external returns (bool);
function balanceOf(address who) external view returns (uint256);
}
Compiler settingsShow
{
"viaIR": true,
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "cancun"
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.