Verified contractPartialMIT
FWA
Address0xb276f62db0ce8ca2ca5bc522695be604521eac1c
- Balance
99.336695 ETH
- Transactions
561,474
- Compiler
solc 0.8.30
- Same code
Only this contract
Details
- Compiler
- solc 0.8.30+commit.73712a01
- Language
- solidity
- Optimization
- Yes, 1 runs
- EVM version
- prague
- Via IR
- yes
- License
- MIT
- Match
- Partialcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 25,546,793
- Creation transaction
- 0xa711a902fd…7112fd3c78
9 files
- src/FWA.solmain122.86 KB
- lib/chainlink-evm/contracts/src/v0.8/vrf/dev/interfaces/IVRFCoordinatorV2Plus.sol2.14 KB
- lib/chainlink-evm/contracts/src/v0.8/vrf/dev/interfaces/IVRFSubscriptionV2Plus.sol3.95 KB
- lib/chainlink-evm/contracts/src/v0.8/vrf/dev/libraries/VRFV2PlusClient.sol650 B
- lib/solady/src/auth/Ownable.sol12.55 KB
- lib/solady/src/tokens/ERC721.sol40 KB
- lib/solady/src/utils/ReentrancyGuard.sol2.33 KB
- lib/solady/src/utils/SafeTransferLib.sol32.8 KB
- src/FWAConfigKeys.sol2.97 KB
src/FWA.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
// Solady
import {ERC721} from "solady/src/tokens/ERC721.sol";
import {Ownable} from "solady/src/auth/Ownable.sol";
import {ReentrancyGuard} from "solady/src/utils/ReentrancyGuard.sol";
import {SafeTransferLib} from "solady/src/utils/SafeTransferLib.sol";
// Chainlink VRF v2.5 — SUBSCRIPTION model. We do NOT inherit `VRFConsumerBaseV2Plus`: it declares a
// non-virtual `rawFulfillRandomWords` (so its callback can't be re-authorized) and pulls in Chainlink's
// `ConfirmedOwner`, which would clash with Solady's `Ownable`. Instead FWA hand-rolls the minimal consumer
// glue below (coordinator storage + a coordinator-only `rawFulfillRandomWords`), keeping Solady ownership
// and a single VRF path. Subscription is used over direct funding because its observed fulfillment
// latency is materially shorter and more predictable, reducing expirations at the fixed word deadline.
import {IVRFCoordinatorV2Plus} from "@chainlink/contracts/src/v0.8/vrf/dev/interfaces/IVRFCoordinatorV2Plus.sol";
import {VRFV2PlusClient} from "@chainlink/contracts/src/v0.8/vrf/dev/libraries/VRFV2PlusClient.sol";
import {FWAConfigKeys} from "./FWAConfigKeys.sol";
interface IFWARewards {
function token() external view returns (address);
function fwa() external view returns (address);
function onListingActivated(uint256 listingId, address depositor, uint256 backing) external;
function onListingRepriced(uint256 listingId, uint256 newBacking) external;
function onListingRemoved(uint256 listingId) external;
function registerAcquisition(uint256 requestId, address purchaser, uint256 fee, uint256 surchargeBps)
external
returns (uint256 tokenSlice, uint64 rewardEpoch);
function settleAcquisition(uint256 requestId) external payable;
function refundAcquisition(uint256 requestId) external;
function startEmission() external;
function buyFor(address recipient, uint256 minOut) external payable returns (uint256 tokenOut);
}
interface IFWAVRFService {
function requestFee() external view returns (uint256);
function prepareRequests(uint256 requestCount) external payable;
}
/// @title FWA
/// @notice A randomized NFT acquisition pool with depositor-funded standing bids. A depositor lists
/// an ERC721 together with a chosen amount of ETH backing. That backing does two things: it
/// sets the listing's selection weight INVERSELY (weight is `NUM / backing`, so a
/// lightly-backed listing is selected more often and a richly-backed one less often), and it
/// funds an irrevocable standing bid from the depositor to reacquire the NFT. A purchaser
/// pays an expected-value-priced acquisition fee for a Chainlink VRF draw that allocates one
/// randomly selected listing in proportion to its weight. The purchaser then either keeps
/// the NFT or accepts the depositor's standing bid — selling the NFT back to its depositor
/// for the escrowed ETH. The two outcomes are mutually exclusive: a purchaser can never
/// retain both the NFT and the ETH.
/// @dev Economic design:
/// - Each listing's backing ETH is escrowed per listing and only ever settles that listing's
/// standing bid, so the contract is always solvent and any un-allocated listing is fully
/// withdrawable by its depositor.
/// - Acquisitions are priced at the pool's expected value times a protocol surcharge:
/// `EV = Σ(weightᵢ·backingᵢ) / Σweightᵢ`. With inverse weights (`weightᵢ·backingᵢ ≈ NUM`)
/// this reduces to the harmonic mean of the backings — so a pool of richer listings prices
/// *cheaper*, reflecting that an acquisition is far more likely to land a lightly-backed one.
/// - Acquisition fees are shared among depositors EQUALLY per active listing via a dividend
/// accumulator (each listing's fee share is a flat 1), so a lightly-backed listing earns the
/// same per-acquisition fee as a richly-backed one. This keeps acquisitions cheap and small
/// deposits attractive. Fees accrue as a withdrawable credit.
/// - The size incentive lives in a separate, marketable TOP-BACKED LISTING: a single tracked
/// top listing earns a `topListingShareBps` share off every acquisition into a visible,
/// growing pot that settles to the top holder's credit when their listing is allocated,
/// withdrawn, or taken over. The top slot tracks committed backing — a deposit (or a raise)
/// that clears the current top by `topThresholdBps` auto-seizes it, a vacant top is taken by
/// the next deposit, and an already-active listing seizes it via `claimTopSpot` once it
/// clears the bar. Reducing your own backing — a withdraw or a lower re-price — forfeits it.
/// Richly-backed listings also have low (inverse) weight, so they are allocated rarely and
/// hold the top slot longer, compounding the incentive for the highest-committed backers.
/// Selection runs in O(TREE_DEPTH) via a sparse segment tree over active listing slots.
/// Uses Chainlink VRF v2.5 SUBSCRIPTION (native payment): an external `FWAVRFService` receives each
/// purchaser's service fee, pre-funds callback coverage, and may use only proven surplus to sponsor
/// canonical processing — so an acquisition never draws down escrowed backing or depositor fees.
contract FWA is Ownable, ReentrancyGuard {
/*//////////////////////////////////////////////////////////////
TYPES
//////////////////////////////////////////////////////////////*/
enum ListingStatus {
None,
Active,
Allocated,
Withdrawn,
Settled,
// A deposit made while an acquisition is unresolved: escrowed but NOT yet in the selection
// pool. A later request may reserve it before asking VRF; otherwise it activates once the
// sequencer is idle. Appended last so pre-existing numeric listing states remain stable.
Staged
}
enum AcquisitionStatus {
None,
Pending, // waiting for an on-time VRF word
Fulfilled,
Expired, // skipped in sequence after its word deadline; escrowed fee credited for withdrawal
Refunded, // no listing / slippage refund; escrowed fee credited for withdrawal
Ready, // an on-time word is cached and must be settled in request order
TimedOut // a callback arrived after its deadline; the request is skipped when it reaches the head
}
struct Listing {
address collection;
address depositor;
address purchaser;
uint256 tokenId;
uint256 weight; // selection weight; derived as NUM / backing (weight ∝ 1 / backing)
uint256 value; // backing ETH escrowed for this listing (funds the depositor's standing bid)
uint256 feeShare; // fee-distribution share key, √backing, fixed at deposit
uint256 feeDebt; // dividend-accumulator checkpoint (scaled by SCALE)
uint256 slot;
uint64 allocatedAt; // timestamp the listing was allocated; gates the purchaser settlement window
ListingStatus status;
}
struct Acquisition {
address purchaser;
uint256 requestBlock;
uint256 priceEscrowed; // acquisition fee held until the acquisition settles (distributed) or is refunded
uint256 listingId;
AcquisitionStatus status;
}
/// @dev Sequencing fields are separate so the existing public `acquisitions(requestId)` getter ABI
/// remains stable for indexers. Every value is fixed at request time except `randomWord`, which
/// the authenticated VRF callback writes exactly once.
struct AcquisitionMeta {
uint64 sequence;
uint64 wordDeadlineBlock;
uint64 rewardEpoch;
uint16 maxPositiveSlippageBps;
uint16 maxNegativeSlippageBps;
uint256 randomWord;
}
/*//////////////////////////////////////////////////////////////
CONSTANTS
//////////////////////////////////////////////////////////////*/
/*
Supports 2^32 simultaneously active listings.
This does NOT allocate 2^32 storage slots. The tree is a mapping,
so only nodes that are actually touched consume storage.
Each insertion/removal/selection takes roughly TREE_DEPTH steps.
*/
uint256 internal constant TREE_DEPTH = 32;
uint256 internal constant CAPACITY = 1 << TREE_DEPTH;
/*
Segment tree layout:
node 1
/ \
node 2 node 3
/ ... ... \
Leaves begin at index CAPACITY.
Listing slot 1 => tree node CAPACITY
Listing slot 2 => tree node CAPACITY + 1
Listing slot CAPACITY => tree node (2 * CAPACITY) - 1
*/
uint256 internal constant LEAF_BASE = CAPACITY;
/// @notice Denominator for basis-point math.
uint256 public constant BPS = 10_000;
/// @notice Fixed-point numerator for the inverse selection weight. A listing's acquisition weight is
/// `INVERSE_WEIGHT_NUMERATOR / value`, so selection weight are INVERSELY proportional to backing.
/// As a shared constant it cancels in the relative selection weight between any two listings and
/// only sets the absolute precision of the weights; sized far above any realistic
/// backing so `NUM / value` never rounds to zero for a sane listing.
uint256 internal constant INVERSE_WEIGHT_NUMERATOR = 1e36;
/// @notice Fixed-point scale for the fee dividend accumulator. Fees are shared by the
/// per-listing key `√value` (summed into `feeShareTotal`); SCALE keeps
/// `fee·SCALE / feeShareTotal` from rounding to zero and silently dropping fees.
/// The product `feeShare·accFeePerEV` is bounded by `Σfees·SCALE` (≈1e60 for any
/// realistic fee history), comfortably within uint256.
uint256 internal constant SCALE = 1e36;
/*//////////////////////////////////////////////////////////////
VRF CONFIG
//////////////////////////////////////////////////////////////*/
/// @notice Chainlink VRF v2.5 coordinator. FWA requests randomness against `vrfSubId` and the
/// coordinator calls `rawFulfillRandomWords` back (the only authorized caller). Owner-settable
/// (`setAddr(VRF_COORDINATOR)`) so a Chainlink coordinator migration can be followed without redeploy.
IVRFCoordinatorV2Plus internal s_vrfCoordinator;
/// @notice The VRF subscription that pays fulfillment gas. Owned by an EOA/multisig (which runs
/// `createSubscription`/`addConsumer` and can cancel/recover); the immutable service reads
/// this id and funds it permissionlessly from purchaser fees before FWA issues requests.
uint256 internal vrfSubId;
/// @notice External fee, subscription-funding, and optional processor-sponsorship module.
/// Immutable so purchaser fees cannot be redirected after deployment.
IFWAVRFService public immutable vrfService;
/// @notice VRF gas lane (key hash) selecting the price ceiling for requests. It can be changed only
/// through the immutable service together with the callback coverage underwriting it.
bytes32 internal vrfKeyHash;
/// @notice Number of random words requested per acquisition.
uint32 internal constant NUM_WORDS = 1;
/// @notice Gas limit for the cache-first VRF callback. With at least
/// `MIN_FAST_PATH_CALLBACK_GAS_LIMIT`, an in-order word whose request owns no reserved
/// listings gets one failure-isolated settlement attempt; every other word remains cached
/// for the public processor. Adjustable only through the immutable service while paused.
uint32 public callbackGasLimit;
uint32 public constant MIN_CALLBACK_GAS_LIMIT = 150_000;
uint32 public constant MAX_CALLBACK_GAS_LIMIT = 2_500_000;
/// @dev The fast path forwards a fixed stipend into the existing guarded one-item processor and
/// retains enough parent gas to return successfully even if the subcall consumes its stipend.
uint32 internal constant MIN_FAST_PATH_CALLBACK_GAS_LIMIT = 900_000;
uint256 internal constant CALLBACK_FAST_PATH_GAS = 700_000;
uint256 internal constant CALLBACK_RETURN_GAS_RESERVE = 50_000;
/// @notice Block confirmations the coordinator waits before responding. Constrained to Chainlink's
/// supported 3..200 range, with at least `MIN_CALLBACK_SLACK_BLOCKS` before the word deadline.
uint16 internal requestConfirmations = 3;
/// @notice Floor on `requestConfirmations`. It is the security axiom — no VRF word can exist before
/// this many blocks — so the setters never let it drop below the Chainlink minimum of 3.
uint256 internal constant MIN_REQUEST_CONFIRMATIONS = 3;
uint256 internal constant MAX_REQUEST_CONFIRMATIONS = 200;
uint256 internal constant MIN_CALLBACK_SLACK_BLOCKS = 2;
/*//////////////////////////////////////////////////////////////
ACQUISITION SEQUENCER
//////////////////////////////////////////////////////////////*/
/// @notice Maximum FIFO staged listings committed to each acquisition before its VRF request.
/// The callback never activates them; the canonical processor activates exactly this
/// request-time batch immediately before settling that sequence.
uint256 internal maxActivationsPerAcquisition = 6;
uint256 internal constant MAX_ACTIVATIONS_PER_ACQUISITION = 16;
/// @notice Number of requests still waiting for a callback. A cached word reduces this counter but
/// remains unresolved until canonical processing advances its sequence.
uint256 public pendingAcquisitionCount;
/// @notice Number of issued VRF requests for which the authenticated coordinator callback has not
/// yet been observed. Unlike `pendingAcquisitionCount`, local expiry does not reduce this:
/// Chainlink may still fulfill and bill the subscription later.
uint256 public unfulfilledVrfCount;
/// @dev First-callback marker keeps `unfulfilledVrfCount` duplicate-safe, including after local expiry.
mapping(uint256 requestId => bool observed) internal callbackObserved;
/// @notice Number of issued acquisitions not yet terminally processed. This is the pool-mutation
/// lock: it remains non-zero after words are cached and only falls as the sequence advances.
uint256 public unsettledAcquisitionCount;
/// @notice Absolute per-request word deadline offset. A callback at `requestBlock + timeout` is
/// accepted; a callback after it is permanently late. Snapshotted into each acquisition.
uint256 public selectionTimeoutBlocks = 30;
/// @notice Upper bound on the configurable word deadline.
uint256 internal constant MAX_WINDOW_BLOCKS = 7_200;
/// @notice Max acquisitions a single `acquireBatch` batch may fire, bounding the request-tx loop.
/// Owner-adjustable.
uint256 internal maxAcquisitionsPerTx = 5;
/// @notice Monotonic request sequence. VRF callbacks may arrive in any order, but only
/// `nextSequenceToProcess` may mutate the pool.
uint64 public lastIssuedSequence;
uint64 public nextSequenceToProcess = 1;
/// @notice Gates acquisitions. Starts `false` so the pool deploys in a "loading" phase: deposits
/// and withdrawals work, but no one can acquisition until the owner opens the game. Owner
/// flips it on with `setBool(ACQUISITIONS_ENABLED, true)`.
bool internal acquisitionsEnabled;
/// @notice Emergency switch. When on, new acquisitions and deposits are halted. Already-issued
/// sequences remain cacheable and processable; once the ordered prefix is terminal,
/// active-listing exits unlock normally. Owner-toggled, independent of `acquisitionsEnabled`.
bool internal withdrawOnly;
/// @notice When true, only collections in `collectionWhitelisted` may be deposited as NEW
/// listings; flip it off to allow any ERC721. Affects deposits only — listings already in
/// the pool, plus acquisitions and claims, are never gated by the whitelist. Owner-toggled.
bool internal whitelistEnabled = true;
/// @notice Collections allowed for NEW listing deposits while `whitelistEnabled` is true.
mapping(address => bool) public collectionWhitelisted;
/// @dev Owner-appointed address (typically a curation contract) also allowed to update the
/// collection whitelist. Zero (the default) means owner-only. Set via
/// `setAddr(WHITELIST_MANAGER, ...)`; indexers track it through `ConfigSet`.
address internal whitelistManager;
/*//////////////////////////////////////////////////////////////
ECONOMIC CONFIG
//////////////////////////////////////////////////////////////*/
/// @notice Protocol surcharge added on top of expected value when pricing an acquisition, in bps.
uint256 internal surchargeBps = 1_000; // 10%
/// @notice Protocol maximum positive price drift accepted at ordered settlement. Snapshotted per
/// request. Purchasers may separately choose their maximum negative drift, up to 100%.
uint256 public selectionSlippageBps = 1_000; // 10%
/// @notice Fraction of a listing's backing a purchaser receives if they take ETH, in bps.
/// The retained remainder is the settlement penalty, routed per `retainedToProtocol`.
uint256 public settlementDiscountBps = 8_500; // purchaser gets 85% on an ETH settlement
/// @notice How long the purchaser has the exclusive right to choose NFT-or-ETH. After it lapses
/// the depositor may resolve the listing themselves (`depositorReclaimBacking` /
/// `depositorReclaimNFT`); the purchaser can still claim too. Must be <= `finalizeWindow`.
uint256 public settlementWindow = 24 hours;
/// @notice After this elapses from the acquisition with no resolution, `finalizeUnsettled` becomes
/// permissionless (default: NFT to purchaser, backing to depositor), so assets never
/// lock even if both parties walk away. Must be >= `settlementWindow`.
uint256 public finalizeWindow = 7 days;
/// @notice Minimum backing ETH a deposit must escrow. `0` disables the floor. Guards against dust
/// listings that would distort the pool's pricing and selection weight, and — because the
/// acquisition-fee split is a flat share per active listing — raises the capital cost of
/// sybil fee-farming with many tiny listings. Defaults to a non-zero floor for that reason;
/// owner-adjustable (including back to `0`).
uint256 internal minBacking = 0.01 ether;
/// @notice Immutable bounds on the purchaser's ETH settlement payout. These keep the configurable
/// payout near its intended 85% while limiting the retained protocol share to at most 20%.
uint256 internal constant MIN_SETTLEMENT_DISCOUNT_BPS = 8_000;
uint256 internal constant MAX_SETTLEMENT_DISCOUNT_BPS = 9_500;
/// @notice Protocol fee on each acquisition, in bps of the pool acquisition fee, routed to the owner.
/// Carved out of the depositor surcharge — the purchaser pays the same total. `0` disables.
uint256 public ownerAcquisitionFeeBps = 100; // 1%
/// @notice Protocol fee on an NFT-outcome resolution (`keepNFT` / `depositorReclaimBacking` /
/// `finalizeUnsettled`), in bps of the listing value, taken from the depositor's backing
/// return. ETH settlements are NOT charged this — there the retained settlementDiscount is the
/// protocol's whole take (see `retainedToProtocol`), so the purchaser keeps the full
/// `settlementDiscountBps`. Hard-capped at 5% of backing. `0` disables.
uint256 public ownerSettlementFeeBps = 100; // 1% of listing value
/// @notice Immutable maximum protocol cut from backing on an NFT-outcome resolution.
uint256 internal constant MAX_OWNER_SETTLEMENT_FEE_BPS = 500;
/// @notice Destination of the retained settlementDiscount on an ETH claim — the `1 - settlementDiscountBps` slice of
/// backing a purchaser forgoes by accepting the bid instead of taking the NFT. When true (the
/// default), that settlement penalty accrues to the protocol; when false it is shared among
/// active depositors (the original behaviour). Owner-toggled; does not change what the
/// purchaser receives, only who keeps the forgone remainder.
bool public retainedToProtocol = true;
/// @notice Slice of each acquisition's distributable fee routed to the top-listing pot, in bps.
/// Carved before the equal per-listing distribution and credited to the top holder; the
/// purchaser pays the same total. `0` disables (topListingShare folds back into the equal split).
uint256 public topListingShareBps = 500; // 5% of the distributable acquisition fee
/// @notice Hysteresis for seizing the top: a challenger's backing must be at least the current
/// top's value × (1 + `topThresholdBps`/BPS) to take it via `claimTopSpot`.
/// `0` means ties (>=) acquire. Stops the top from flipping on a dust-sized overtake.
uint256 public topThresholdBps = 1_000; // must exceed the top by 10%
/*//////////////////////////////////////////////////////////////
LISTING STORAGE
//////////////////////////////////////////////////////////////*/
uint256 public nextListingId = 1;
mapping(uint256 listingId => Listing) public listings;
/// @notice Recipient entitled to pull a resolved listing's NFT whose delivery reverted at
/// resolution (a pausable/non-standard/misbehaving collection). `0` if none pending.
/// The ETH side of that resolution already settled; only the NFT is outstanding.
mapping(uint256 listingId => address recipient) public stuckNFTRecipient;
/*//////////////////////////////////////////////////////////////
STAGING QUEUE (FIFO)
//////////////////////////////////////////////////////////////*/
/*
Deposits made while an acquisition is unresolved are parked here — escrowed but excluded from
every pool total and from the selection tree. Each later acquisition detaches up to
`maxActivationsPerAcquisition` entries from the FIFO into its immutable reserved batch before
requesting VRF. The canonical processor activates exactly that batch at the acquisition's turn.
*/
uint256 internal stagingHead; // first (oldest) staged listing; 0 when empty
uint256 internal stagingTail; // last (newest) staged listing; 0 when empty
mapping(uint256 listingId => uint256) internal stagingNext; // toward the tail; 0 = none
mapping(uint256 listingId => uint256) internal stagingPrev; // toward the head; 0 = none
/// @notice Unreserved staging-queue length, kept in lockstep with the FIFO.
uint256 internal stagedCount;
/// @notice Optional cap on the unreserved staging FIFO (`0` = unlimited, the default). This is
/// an operational throttle only; request-time reservation, not queue length, provides the
/// anti-steering boundary. Owner-set via `setUint(MAX_STAGED_LISTINGS, ...)`.
uint256 internal maxStagedListings;
/// @notice Sequence that owns a detached staged listing; zero means it remains unreserved.
mapping(uint256 listingId => uint64 sequence) public reservedForSequence;
uint256 public reservedStagedCount;
/// @dev Exact request-time activation batch. Its length is bounded by
/// `maxActivationsPerAcquisition`, and its entries are removed from the public staging FIFO.
mapping(uint64 sequence => uint256[]) internal reservedListingsBySequence;
/*
Active slot => permanent listing ID.
A listing ID is never reused.
A tree slot can be reused after its listing leaves the active pool.
*/
mapping(uint256 slot => uint256 listingId) public slotToListing;
uint256 public activeListingCount;
uint256 public totalWeight;
/// @notice Σ(weightᵢ · valueᵢ) over active listings. Drives the expected-value acquisition price:
/// with inverse weights each term `weightᵢ·valueᵢ ≈ NUM`, so this tracks
/// ≈ `activeListingCount · NUM` and `weightedBackingTotal / totalWeight` works out to
/// the harmonic mean of the values. (Fee distribution uses `feeShareTotal`, not this.)
uint256 public weightedBackingTotal;
/// @notice Σ feeShareᵢ over active listings — the denominator for the fee dividend accumulator.
/// Each listing's share is a flat 1 (equal-per-listing distribution), so this tracks
/// `activeListingCount`. Kept as its own running sum so the fee basis stays independent of
/// the EV-based acquisition-price basis (`weightedBackingTotal`). The size incentive that the old
/// √value weighting provided now lives in the separate top-listing topListingShare.
uint256 public feeShareTotal;
/*//////////////////////////////////////////////////////////////
FEE DISTRIBUTION
//////////////////////////////////////////////////////////////*/
/// @dev Accumulated fee per unit of fee share (a flat 1 per listing), scaled by SCALE.
/// A listing's pending fees are `feeShare * accFeePerEV / SCALE - feeDebt`.
uint256 internal accFeePerEV;
/// @notice Withdrawable earnings (settled acquisition-fee share) per depositor.
mapping(address depositor => uint256 amount) public feeCredit;
/// @notice Protocol fees accrued — the acquisition-fee cut, the settlementDiscount cut, and any distribution
/// that had no active pool to receive it — held in-contract until paid out via the
/// permissionless `payoutFees`.
uint256 public accruedOwnerFees;
/// @notice Destination for protocol fees. Owner-configurable; defaults to the deployer.
/// Anyone may call `payoutFees` to push the accrued balance here.
address public payoutAddress;
/*//////////////////////////////////////////////////////////////
TOP LISTING
//////////////////////////////////////////////////////////////*/
/// @notice The active listing currently holding the top; `0` when vacant. The top earns a
/// topListingShare off every acquisition (`topListingShareBps`). A deposit or claim that clears the bar
/// auto-seizes it, but we store only this id: on vacate we never scan for or backfill to
/// the next-largest listing — the top simply opens until the next deposit or claim takes it.
uint256 public topListingId;
/// @notice TopListingShare accrued for the CURRENT top holder but not yet settled — a visible, growing
/// pot. It is credited to the holder's `feeCredit` the moment the top is vacated (its
/// listing is allocated, withdrawn, or taken over) and then resets to 0 for the next holder, so
/// each holder banks exactly what accrued during their tenure.
uint256 public topListingPot;
/*//////////////////////////////////////////////////////////////
FWAToken REWARDS MODULE
//////////////////////////////////////////////////////////////*/
/// @notice FWAToken receives the protocol-fee buyback portion. Participant rewards and all swaps
/// live in the separately deployed `FWARewards` module.
address public token;
IFWARewards public rewards;
/// @notice Whether a purchaser may accept the bid as FWAToken via `acceptBidAsTokens`. Owner-toggled; `acceptDepositorBid` (the
/// plain ETH settlement) is always available regardless.
bool internal acceptBidAsTokensEnabled = true;
/// @notice ETH slice of an acquisition's surcharge earmarked to buy FWAToken for the purchaser on success.
mapping(uint256 requestId => uint256) public acquisitionTokenSlice;
// --- Protocol-fee -> FWAToken buy pressure (Phase-2 sink) ---
/// @notice Fraction of accrued protocol fees (the in-game cuts: acquisition cut + NFT-settlementDiscount + retained
/// settlementDiscount) routed into FWAToken buy pressure on `payoutFees`; the rest still pays out to the
/// owner. `0` = off (default) — flip on for Phase 2. The LP 1% fee is never included.
/// The buy/burn/redistribute then happens in the FWAToken token's TWAP `buyback`; the sink mode
/// (burn vs redistribute) lives there.
uint256 internal protocolFeeToTokenBps;
/*//////////////////////////////////////////////////////////////
SPARSE SEGMENT TREE
//////////////////////////////////////////////////////////////*/
/*
tree[node] contains the total weight beneath that node.
This replaces a giant array. Unused nodes simply return zero.
*/
mapping(uint256 node => uint256 weight) internal tree;
/*//////////////////////////////////////////////////////////////
SLOT FREE LIST
//////////////////////////////////////////////////////////////*/
/*
Slots are initially allocated sequentially.
When a listing is allocated or withdrawn, its slot is placed into
a linked-list free pool and can be assigned to another listing.
*/
uint256 internal nextUnusedSlot = 1;
uint256 internal freeSlotHead;
mapping(uint256 slot => uint256 nextFreeSlot) internal nextFreeSlot;
/*//////////////////////////////////////////////////////////////
ACQUISITION STORAGE
//////////////////////////////////////////////////////////////*/
mapping(uint256 requestId => Acquisition) public acquisitions;
mapping(uint256 requestId => AcquisitionMeta) public acquisitionMeta;
mapping(uint64 sequence => uint256 requestId) public requestIdAtSequence;
/// @notice Pull-based acquisition refunds. Ordered processing never calls an untrusted purchaser.
mapping(address purchaser => uint256 amount) public acquisitionRefundCredit;
uint256 public acquisitionRefundCreditTotal;
uint256 public acquisitionEscrowTotal;
/*//////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
event NFTListed(
uint256 indexed listingId,
uint256 indexed slot,
address indexed depositor,
address collection,
uint256 tokenId,
uint256 weight,
uint256 value
);
/// @param value The listing's backing/weight, so an indexer can debit the pool's active value
/// from this event alone (no join back to `NFTListed`).
event ListingWithdrawn(uint256 indexed listingId, address indexed depositor, uint256 value);
/// @dev A deposit landed while an acquisition was unresolved (or behind an older FIFO entry), so
/// it was parked outside the selection tree. `stagedAtBlock` is informational; a later request
/// may reserve the listing before VRF, otherwise it activates after the sequencer becomes idle.
event ListingStaged(
uint256 indexed listingId,
address indexed depositor,
address collection,
uint256 tokenId,
uint256 value,
uint256 stagedAtBlock
);
/// @dev A depositor re-priced an active listing's backing. `newWeight` lets an indexer update
/// the active pool weight/value from this event alone.
event BackingUpdated(
uint256 indexed listingId, address indexed depositor, uint256 oldBacking, uint256 newBacking, uint256 newWeight
);
event AcquisitionRequested(
uint256 indexed requestId, address indexed purchaser, uint256 acquisitionFee, uint256 totalWeight
);
/// @dev Local sequence and immutable timeout assigned to a VRF request. `reservedCount` is the
/// exact FIFO activation batch committed before randomness was requested.
event AcquisitionSequenced(
uint256 indexed requestId, uint64 indexed sequence, uint64 wordDeadlineBlock, uint256 reservedCount
);
event RandomnessCached(uint256 indexed requestId, uint64 indexed sequence, uint256 randomWord);
event RandomnessTimedOut(
uint256 indexed requestId, uint64 indexed sequence, uint64 wordDeadlineBlock, uint256 callbackBlock
);
event FulfillmentIgnored(uint256 indexed requestId, AcquisitionStatus status);
event UnfulfilledVrfReconciled(uint256 count);
/// @dev `processor` is `address(this)` when the failure-isolated callback self-call succeeds;
/// otherwise it is the address that called the public processor.
event AcquisitionProcessed(
uint256 indexed requestId, uint64 indexed sequence, AcquisitionStatus status, address indexed processor
);
event AcquisitionRefundWithdrawn(address indexed purchaser, uint256 amount);
event AcquisitionExpired(
uint256 indexed requestId, uint64 indexed sequence, address indexed purchaser, uint256 refund
);
/// @dev Ordered processing reached an empty pool, so no listing could be allocated and the
/// escrowed acquisition fee became a purchaser pull credit.
event AcquisitionRefundedNoListing(uint256 indexed requestId, address indexed purchaser, uint256 refund);
/// @dev Ordered processing found price drift beyond the request's positive / negative tolerances,
/// so the acquisition was refunded instead of paid out. Distinct from
/// `AcquisitionRefundedNoListing` so the two refund causes are distinguishable off-chain.
event AcquisitionRefundedSlippage(
uint256 indexed requestId, address indexed purchaser, uint256 refund, uint256 escrowedFee, uint256 liveFee
);
/// @param depositor The listing's depositor, and `value` its backing/weight, so an indexer can
/// debit the pool's active value and attribute the deposit duration from this event
/// alone (no join back to `NFTListed`).
event NFTAllocated(
uint256 indexed requestId,
uint256 indexed listingId,
address indexed purchaser,
address depositor,
uint256 value,
uint256 randomWord
);
event NFTKept(uint256 indexed listingId, address indexed purchaser, address indexed depositor, uint256 backing);
/// @dev Old listing settled keepNFT-style but the NFT stayed in custody, re-listed as
/// `newListingId` with the purchaser as depositor. Purchaser/depositor/new backing are
/// recoverable from `NFTAllocated` plus the new listing's `NFTListed`/`ListingStaged`,
/// which fire separately.
event NFTRelisted(uint256 indexed listingId, uint256 indexed newListingId, uint256 toDepositor);
event DepositorBidAccepted(
uint256 indexed listingId,
address indexed purchaser,
address indexed depositor,
uint256 payout,
uint256 retained
);
/// @dev Purchaser cashed out via `acceptBidAsTokens`: `ethPayout` of backing bought `tokenOut` FWAToken for them.
event DepositorBidAcceptedAsTokens(
uint256 indexed listingId,
address indexed purchaser,
address indexed depositor,
uint256 ethPayout,
uint256 retained,
uint256 tokenOut
);
event UnsettledFinalized(uint256 indexed listingId, address indexed purchaser, address indexed depositor);
/// @dev A resolved listing's NFT could not be delivered (the collection reverted); `recipient`
/// may pull it later via `recoverStuckNFT`. The resolution's ETH legs still settled.
event NFTDeliveryFailed(uint256 indexed listingId, address indexed recipient, address collection, uint256 tokenId);
event StuckNFTRecovered(uint256 indexed listingId, address indexed recipient);
/// @dev Acquisition-fee share settled into a depositor's withdrawable `feeCredit`, keyed by the
/// listing whose accrued fees were settled. (Distributions with no active pool to receive
/// them are routed to protocol fees and emit `OwnerFeesAccrued` instead.) An indexer can
/// track each address's claimable balance as `Σ EarningsAccrued − Σ EarningsWithdrawn`.
event EarningsAccrued(address indexed depositor, uint256 indexed listingId, uint256 amount);
event EarningsWithdrawn(address indexed depositor, uint256 amount);
/// @dev A single-value owner-config change (`setUint`/`setBool`/`setAddr`). `key` is a
/// `FWAConfigKeys` constant (globally unique across the three dispatchers); bools encode
/// as 0/1, addresses as `uint256(uint160(a))`, the VRF key hash as `uint256(keyHash)`.
event ConfigSet(uint256 indexed key, uint256 value);
/// @dev A purchaser paid `amount` wei into the immutable external VRF service.
event VrfServiceFeePaid(address indexed purchaser, uint256 amount);
event CollectionWhitelistSet(address indexed collection, bool allowed);
/// @dev Protocol fee accrued (from an acquisition, a claim, or an unallocated distribution).
event OwnerFeesAccrued(uint256 amount);
/// @dev Accrued protocol fees paid out to the payout address.
event FeesPaidOut(address indexed to, uint256 amount);
/// @dev The top moved to `listingId` (`0` = vacated); `depositor` is the new holder (or `0`).
event TopListingSet(uint256 indexed listingId, address indexed depositor);
/// @dev An acquisition's topListingShare was added to the live pot for the current top holder.
event TopListingFunded(uint256 indexed listingId, uint256 amount, uint256 newPot);
/// @dev The top was vacated; `amount` settled into `depositor`'s withdrawable `feeCredit`.
event TopListingSettled(uint256 indexed listingId, address indexed depositor, uint256 amount);
event RewardsConfigured(address indexed rewards, address indexed token);
/// @dev `amount` ETH of protocol fees was routed to the FWAToken token's buyback reserve on payout.
event ProtocolFeesToToken(uint256 amount);
/*//////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/
error ZeroBacking();
error BelowMinBacking();
error BackingTooHigh();
error InvalidCollection();
error CollectionNotWhitelisted();
error ListingNotActive();
error ListingNotAllocated();
error NotDepositor();
error NotPurchaser();
error NoActiveListings();
error WithdrawLocked();
error InvalidTreeResult();
error ActiveListingCapacityReached();
error StagingQueueFull();
error NFTTransferFailed();
error InsufficientPayment();
error AcquisitionFeeTooHigh();
error InvalidAcquisitionCount();
error PoolValueTooLow();
error SettlementWindowNotElapsed();
error InvalidConfig();
error NoEarnings();
error NotStuckRecipient();
error WithdrawOnlyActive();
error AcquisitionsNotEnabled();
error NoOwnerFees();
error TopNotBeaten();
error TokenNotConfigured();
error OnlyCoordinator();
error AcceptBidAsTokensDisabled();
error SequenceInvariantBroken();
error NoAcquisitionRefund();
error AcquisitionStateLocked();
error OnlyVrfService();
error VrfRequestsPending();
error DuplicateRequestId();
// `Unauthorized()` is inherited from Solady's Ownable.
/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
/// @param coordinator Chainlink VRF v2.5 coordinator for the target chain.
/// @param subId The VRF subscription that pays fulfillment gas (owned by an EOA/multisig; the
/// owner must `addConsumer(subId, address(this))` before acquisitions can settle).
/// @param keyHash The VRF gas lane (price ceiling) for requests.
/// @param callbackGasLimit_ Gas made available to the VRF consumer callback.
/// @param service The separately deployed FWAVRFService that receives purchaser service fees.
constructor(address coordinator, uint256 subId, bytes32 keyHash, uint32 callbackGasLimit_, address service) {
if (
coordinator == address(0) || subId == 0 || keyHash == bytes32(0)
|| callbackGasLimit_ < MIN_CALLBACK_GAS_LIMIT || callbackGasLimit_ > MAX_CALLBACK_GAS_LIMIT
|| service == address(0) || service.code.length == 0
) {
revert InvalidConfig();
}
s_vrfCoordinator = IVRFCoordinatorV2Plus(coordinator);
vrfSubId = subId;
vrfKeyHash = keyHash;
callbackGasLimit = callbackGasLimit_;
vrfService = IFWAVRFService(service);
_initializeOwner(msg.sender);
payoutAddress = msg.sender; // default the fee destination to the deployer
// Emit the initial config so an indexer can reconstruct full state from logs alone,
// without a contract read at the deploy block. (Solady's _initializeOwner already
// logged OwnershipTransferred for the owner.) Constructor code is initcode-only, so
// these emits cost no runtime bytecode.
emit ConfigSet(FWAConfigKeys.VRF_COORDINATOR, uint256(uint160(coordinator)));
emit ConfigSet(FWAConfigKeys.VRF_SUB_ID, subId);
emit ConfigSet(FWAConfigKeys.VRF_KEY_HASH, uint256(keyHash));
emit ConfigSet(FWAConfigKeys.VRF_SERVICE, uint256(uint160(service)));
emit ConfigSet(FWAConfigKeys.SURCHARGE_BPS, surchargeBps);
emit ConfigSet(FWAConfigKeys.SETTLEMENT_DISCOUNT_BPS, settlementDiscountBps);
emit ConfigSet(FWAConfigKeys.OWNER_ACQUISITION_FEE_BPS, ownerAcquisitionFeeBps);
emit ConfigSet(FWAConfigKeys.OWNER_SETTLEMENT_FEE_BPS, ownerSettlementFeeBps);
emit ConfigSet(FWAConfigKeys.SETTLEMENT_WINDOW, settlementWindow);
emit ConfigSet(FWAConfigKeys.FINALIZE_WINDOW, finalizeWindow);
emit ConfigSet(FWAConfigKeys.MIN_BACKING, minBacking);
emit ConfigSet(FWAConfigKeys.CALLBACK_GAS_LIMIT, callbackGasLimit);
emit ConfigSet(FWAConfigKeys.REQUEST_CONFIRMATIONS, requestConfirmations);
emit ConfigSet(FWAConfigKeys.MAX_ACTIVATIONS_PER_ACQUISITION, maxActivationsPerAcquisition);
emit ConfigSet(FWAConfigKeys.SELECTION_TIMEOUT_BLOCKS, selectionTimeoutBlocks);
emit ConfigSet(FWAConfigKeys.ACQUISITIONS_ENABLED, 0); // deploys in the loading phase
emit ConfigSet(FWAConfigKeys.WHITELIST_ENABLED, 1); // only whitelisted collections may deposit
emit ConfigSet(FWAConfigKeys.PAYOUT_ADDRESS, uint256(uint160(payoutAddress)));
emit ConfigSet(FWAConfigKeys.TOP_LISTING_SHARE_BPS, topListingShareBps);
emit ConfigSet(FWAConfigKeys.TOP_THRESHOLD_BPS, topThresholdBps);
emit ConfigSet(FWAConfigKeys.RETAINED_TO_PROTOCOL, retainedToProtocol ? 1 : 0);
}
/*//////////////////////////////////////////////////////////////
DEPOSIT AN ERC721
//////////////////////////////////////////////////////////////*/
/// @notice Escrow an ERC721 plus backing ETH as a listing. `msg.value` funds the depositor's
/// standing bid and derives inverse selection weight (`NUM / backing`).
/// @dev Payable: the backing is held until the listing is allocated or withdrawn. The
/// depositor must approve this contract for the token first.
function listNFT(address collection, uint256 tokenId) external payable nonReentrant returns (uint256 listingId) {
listingId = _createListing(collection, tokenId, msg.sender);
}
/// @dev Shared listing constructor for `listNFT` (`pullFrom = msg.sender`) and `relistNFT`
/// (`pullFrom = address(this)`: the NFT is already in custody from the won listing, so the
/// pull is skipped). Applies the full new-deposit gauntlet and the staging-queue
/// anti-steering branch identically for both entry points. `msg.sender` is the new
/// depositor and `msg.value` the backing. NOTE: `pullFrom` is deliberately a runtime
/// address (not a bool flag) so via-IR's function specializer doesn't clone this body
/// per call site — the bytecode budget can't afford two copies.
function _createListing(address collection, uint256 tokenId, address pullFrom)
internal
returns (uint256 listingId)
{
if (withdrawOnly) revert WithdrawOnlyActive();
// Deposits remain allowed with acquisitions in flight, but they stage outside the tree and
// cannot affect an already-issued request's committed selection input.
if (msg.value == 0) revert ZeroBacking();
if (msg.value < minBacking) revert BelowMinBacking();
if (collection.code.length == 0) revert InvalidCollection();
if (whitelistEnabled && !collectionWhitelisted[collection]) revert CollectionNotWhitelisted();
uint256 value = msg.value;
// Acquisition selection weight are INVERSELY proportional to the backing: weight = NUM / value, so a
// cheaper listing is more likely to be allocated and a richer one less likely.
uint256 weight = INVERSE_WEIGHT_NUMERATOR / value;
if (weight == 0) revert BackingTooHigh();
if (pullFrom != address(this)) {
/*
Using transferFrom deliberately instead of safeTransferFrom:
1. The destination is this known contract.
2. We do not need an ERC721 receiver callback.
3. Random safe transfers sent directly to this contract revert
instead of becoming untracked listings.
User must approve this contract first.
*/
ERC721(collection).transferFrom(pullFrom, address(this), tokenId);
// Optional defensive check for unusual NFT implementations.
if (ERC721(collection).ownerOf(tokenId) != address(this)) {
revert NFTTransferFailed();
}
}
listingId = nextListingId++;
// Fee-distribution share key: a flat 1, so acquisition fees split equally per active listing. The
// size incentive the old √value weighting carried now lives in the separate top-listing
// topListingShare. Fixed here so it never has to be recomputed (a re-price keeps the same share).
uint256 feeShare = 1;
// Record the listing's immutable economic fields now. Pool and rewards checkpoints are taken
// only at activation, so a staged listing earns nothing before it joins the selectable pool.
listings[listingId] = Listing({
collection: collection,
depositor: msg.sender,
purchaser: address(0),
tokenId: tokenId,
weight: weight,
value: value,
feeShare: feeShare,
feeDebt: 0,
slot: 0,
allocatedAt: 0,
status: ListingStatus.Active // provisional — set precisely in the branch below
});
// With no unresolved acquisition and no older staging backlog, activate immediately. Otherwise
// append to staging. A request issued later may reserve it, but every already-issued request has
// already committed its exact batch and can never observe this new input.
if (unsettledAcquisitionCount == 0 && stagingHead == 0) {
_activateListing(listingId);
} else {
if (maxStagedListings != 0 && stagedCount >= maxStagedListings) revert StagingQueueFull();
listings[listingId].status = ListingStatus.Staged;
_stagingPush(listingId);
emit ListingStaged(listingId, msg.sender, collection, tokenId, value, block.number);
// Preserve FIFO if an old unreserved tail survived the last acquisition window: a fresh
// listing never leapfrogs it merely because the pool is currently idle.
if (unsettledAcquisitionCount == 0) _drainStaging(maxActivationsPerAcquisition);
}
}
/*//////////////////////////////////////////////////////////////
STAGING QUEUE
//////////////////////////////////////////////////////////////*/
/// @notice Permissionlessly activate up to `maxCount` unreserved FIFO listings while no acquisition
/// remains unresolved. During a window, only the ordered processor may activate the exact
/// request-time batches.
function activateListings(uint256 maxCount) external nonReentrant {
if (unsettledAcquisitionCount != 0) return;
_drainStaging(maxCount);
}
/// @dev Move a listing — fresh, idle-drained, or from a request's reserved batch — into the active
/// selection pool: take its fee/reward checkpoints against the
/// CURRENT indices (ceil, so it never over-credits), assign a tree slot, add its weight and pool
/// totals, and run the top-spot take/seize. Emits `NFTListed` at go-live (the point its `slot`
/// exists), mirroring the original single-step deposit.
function _activateListing(uint256 listingId) internal {
Listing storage listing = listings[listingId];
uint256 value = listing.value;
uint256 weight = listing.weight;
uint256 feeShare = listing.feeShare;
// Ceil the fee checkpoint so this listing can never receive fees distributed before activation.
listing.feeDebt = (feeShare * accFeePerEV + SCALE - 1) / SCALE;
uint256 slot = _allocateSlot();
listing.slot = slot;
slotToListing[slot] = listingId;
_addTreeWeight(slot, weight);
totalWeight += weight;
weightedBackingTotal += weight * value;
feeShareTotal += feeShare;
activeListingCount++;
listing.status = ListingStatus.Active;
IFWARewards r = rewards;
if (address(r) != address(0)) r.onListingActivated(listingId, listing.depositor, value);
// Take a vacant top with no threshold (so the pool always has a top listing once non-empty and
// the topListingShare never strands); otherwise auto-seize when this listing clears the standing
// top by `topThresholdBps`, settling the outgoing holder's pot. Same bar as `claimTopSpot`.
uint256 currentTop = topListingId;
if (currentTop == 0) {
_setTop(listingId);
} else if (currentTop != listingId && _beatsTop(value, currentTop)) {
_vacateTop();
_setTop(listingId);
}
emit NFTListed(listingId, slot, listing.depositor, listing.collection, listing.tokenId, weight, value);
}
/// @dev Activate an unreserved FIFO prefix. Only callable through paths that have established there
/// is no unresolved acquisition, so block maturity is unnecessary: sequence commitment is the
/// anti-steering boundary.
function _drainStaging(uint256 maxCount) internal {
uint256 drained = 0;
while (drained < maxCount) {
uint256 head = stagingHead;
if (head == 0) return;
_stagingUnlink(head);
_activateListing(head);
unchecked {
drained++;
}
}
}
/// @dev Detach the exact next FIFO batch before requesting randomness. Listings remain `Staged`
/// but no public queue operation can reach them; only their owning sequence can activate them.
function _reserveStagedBatch(uint64 sequence) internal returns (uint256 count) {
uint256 maxCount = maxActivationsPerAcquisition;
while (count < maxCount) {
uint256 head = stagingHead;
if (head == 0) break;
_stagingUnlink(head);
reservedForSequence[head] = sequence;
reservedListingsBySequence[sequence].push(head);
unchecked {
++reservedStagedCount;
++count;
}
}
}
/// @dev Activate exactly the batch committed by `sequence`. Called on success and every refund /
/// expiry path so timeout changes only the missing random removal, never the staged prefix.
function _activateReservedBatch(uint64 sequence) internal {
uint256[] storage batch = reservedListingsBySequence[sequence];
uint256 length = batch.length;
for (uint256 i = 0; i < length; i++) {
uint256 listingId = batch[i];
if (reservedForSequence[listingId] != sequence || listings[listingId].status != ListingStatus.Staged) {
revert SequenceInvariantBroken();
}
delete reservedForSequence[listingId];
unchecked {
--reservedStagedCount;
}
_activateListing(listingId);
}
delete reservedListingsBySequence[sequence];
}
function reservedListingCount(uint64 sequence) external view returns (uint256) {
return reservedListingsBySequence[sequence].length;
}
function reservedListingAt(uint64 sequence, uint256 index) external view returns (uint256) {
return reservedListingsBySequence[sequence][index];
}
/// @dev Push a listing onto the tail of the staging FIFO (doubly-linked).
function _stagingPush(uint256 listingId) internal {
uint256 tail = stagingTail;
stagingPrev[listingId] = tail;
stagingNext[listingId] = 0;
if (tail == 0) {
stagingHead = listingId;
} else {
stagingNext[tail] = listingId;
}
stagingTail = listingId;
unchecked {
++stagedCount;
}
}
/// @dev Unlink a listing from the staging FIFO in O(1), so reservation / idle activation leaves
/// no tombstone for a later prefix walk to scan.
function _stagingUnlink(uint256 listingId) internal {
uint256 prev = stagingPrev[listingId];
uint256 next = stagingNext[listingId];
if (prev == 0) {
stagingHead = next;
} else {
stagingNext[prev] = next;
}
if (next == 0) {
stagingTail = prev;
} else {
stagingPrev[next] = prev;
}
delete stagingPrev[listingId];
delete stagingNext[listingId];
unchecked {
--stagedCount;
}
}
/*//////////////////////////////////////////////////////////////
WITHDRAW ACTIVE LISTING
//////////////////////////////////////////////////////////////*/
/// @dev Withdrawing, repricing, moving the top, or publicly activating listings could change an
/// already-issued draw. Keep them locked until every issued sequence is terminal, including
/// after its word has been cached but before the ordered processor reaches it.
function _requireExitUnlocked() internal view {
if (unsettledAcquisitionCount != 0) revert WithdrawLocked();
}
/// @notice Withdraw an active listing you deposited: returns the NFT, its backing ETH, and
/// settles any acquisition-fee share it accrued into your withdrawable earnings.
/// @dev Load `listingId` and require it Active and deposited by `msg.sender`. Shared guard
/// prologue of every depositor-facing action on a live listing.
function _activeOwned(uint256 listingId) internal view returns (Listing storage listing) {
listing = listings[listingId];
if (listing.status != ListingStatus.Active) revert ListingNotActive();
if (listing.depositor != msg.sender) revert NotDepositor();
}
function withdrawListing(uint256 listingId) external nonReentrant {
// Exit gate: a withdrawal removes weight from the tree, which could steer a live acquisition, so
// it is blocked while an honorable VRF word could still exist (see `_requireExitUnlocked`).
_requireExitUnlocked();
Listing storage listing = _activeOwned(listingId);
uint256 value = listing.value;
address collection = listing.collection;
uint256 tokenId = listing.tokenId;
/*
Effects before interaction. Settle the listing's accrued fees to the depositor's
credit, then remove its weight/EV from the pool and free its slot.
*/
_settleAndRemove(listingId);
// If this listing held the top, settle its top-listing pot to the depositor and vacate.
// The depositor banks both their normal fee share (above) and the accrued pot.
if (topListingId == listingId) _vacateTop();
listing.status = ListingStatus.Withdrawn;
SafeTransferLib.forceSafeTransferETH(msg.sender, value);
ERC721(collection).safeTransferFrom(address(this), msg.sender, tokenId);
emit ListingWithdrawn(listingId, msg.sender, value);
}
/*//////////////////////////////////////////////////////////////
UPDATE LISTING BACKING
//////////////////////////////////////////////////////////////*/
/// @notice Change the backing ETH on one of your active listings, which also re-prices its inverse
/// selection weight (`weight = NUM / value`). Its equal-per-listing fee share stays one. Send the shortfall as
/// `msg.value` to increase; any overpayment and any freed backing on a decrease are
/// refunded in the same call.
/// @dev A value change alters the selection weight and pool value a pending purchaser paid against —
/// exactly like a withdrawal — so it respects the same exit gate (`_requireExitUnlocked`), in
/// BOTH directions (an increase steers just as a decrease does). Increases add capital, so they
/// are additionally halted in `withdrawOnly`; decreases return capital and are allowed once the
/// gate clears. Accrued fees are settled at the OLD share before the share changes, then the fee
/// checkpoint is reset against the new share, so the dividend accounting stays exact.
/// @param listingId The active listing to re-price.
/// @param newBacking The new backing ETH for the listing.
function updateBacking(uint256 listingId, uint256 newBacking) external payable nonReentrant {
Listing storage listing = _activeOwned(listingId);
if (newBacking < minBacking) revert BelowMinBacking();
if (newBacking == 0) revert ZeroBacking();
uint256 oldBacking = listing.value;
// Increases add capital (deposit-like): halted in emergency withdraw-only mode.
if (newBacking > oldBacking && withdrawOnly) revert WithdrawOnlyActive();
// Exit gate: a re-price in EITHER direction moves this listing's weight and the pool value a
// pending purchaser paid against, so — like a withdrawal — it is blocked while an honorable VRF
// word could still exist.
_requireExitUnlocked();
uint256 newWeight = INVERSE_WEIGHT_NUMERATOR / newBacking;
if (newWeight == 0) revert BackingTooHigh();
// Net ETH the depositor must cover is `newBacking - oldBacking`; everything else returns.
// refund = msg.value - (newBacking - oldBacking), kept non-negative by the check below.
if (msg.value + oldBacking < newBacking) revert InsufficientPayment();
uint256 refund = msg.value + oldBacking - newBacking;
// Settle accrued fees at the OLD share before it changes, so the dividend math is exact;
// mirrors `_settleAndRemove`. The depositor withdraws the credit via `withdrawEarnings`.
uint256 pending = _pendingFees(listing);
if (pending != 0) {
feeCredit[msg.sender] += pending;
emit EarningsAccrued(msg.sender, listingId, pending);
}
IFWARewards r = rewards;
if (address(r) != address(0)) r.onListingRepriced(listingId, newBacking);
// Re-weight the tree and pool totals by the deltas (exact: we use the stored old fields).
uint256 oldWeight = listing.weight;
uint256 slot = listing.slot;
if (newWeight > oldWeight) {
_addTreeWeight(slot, newWeight - oldWeight);
} else if (newWeight < oldWeight) {
_removeTreeWeight(slot, oldWeight - newWeight);
}
totalWeight = totalWeight - oldWeight + newWeight;
weightedBackingTotal = weightedBackingTotal - oldWeight * oldBacking + newWeight * newBacking;
// Equal weighting: a listing's share is a flat 1 regardless of value, so a re-price leaves the
// share (and `feeShareTotal`) unchanged. Kept in delta form to stay robust if the share
// basis ever changes again. (Top bookkeeping for the re-price runs after the writes below.)
uint256 newFeeShare = 1;
feeShareTotal = feeShareTotal - listing.feeShare + newFeeShare;
// Write the new fields and re-checkpoint the fee debt against the new share (ceil, as in
// `listNFT`, so the listing is never credited more than its true future share).
listing.value = newBacking;
listing.weight = newWeight;
listing.feeShare = newFeeShare;
listing.feeDebt = (newFeeShare * accFeePerEV + SCALE - 1) / SCALE;
// Top bookkeeping on a re-price:
// - This listing already holds the top: a strict decrease forfeits it (vacate, settling the
// pot back to the holder) — reducing your committed backing gives up the title, mirroring
// a withdraw. A raise (or no change) keeps the top and its in-progress pot intact.
// - Someone else holds it: a raise clearing the bar by `topThresholdBps` auto-seizes it,
// settling the outgoing holder's pot — mirroring `listNFT`.
// - Vacant top: left for deposits / `claimTopSpot` to fill.
uint256 currentTop = topListingId;
if (currentTop == listingId) {
if (newBacking < oldBacking) _vacateTop();
} else if (currentTop != 0 && _beatsTop(newBacking, currentTop)) {
_vacateTop();
_setTop(listingId);
}
emit BackingUpdated(listingId, msg.sender, oldBacking, newBacking, newWeight);
if (refund != 0) SafeTransferLib.forceSafeTransferETH(msg.sender, refund);
}
/*//////////////////////////////////////////////////////////////
CLAIM THE TOP
//////////////////////////////////////////////////////////////*/
/// @notice Claim the top-listing top for one of your active listings. If the top is vacant the
/// first active recipient takes it with no threshold; otherwise your listing's backing must be
/// at least the current top's value × (1 + `topThresholdBps`/BPS). On a takeover the
/// outgoing holder's accrued pot settles to their `feeCredit` and the pot resets for you.
/// @dev This is how an already-active larger listing — or one that raised its value via
/// `updateBacking` — seizes the top. Effects-only (credits `feeCredit`, no ETH transfer),
/// but kept `nonReentrant` for consistency with the other state-changing externals.
/// @param listingId The active listing of yours to top.
function claimTopSpot(uint256 listingId) external nonReentrant {
Listing storage listing = _activeOwned(listingId);
uint256 currentId = topListingId;
// Already holding the top: no-op. This guard is load-bearing — without it the vacate below
// would settle (and zero) this listing's own in-progress pot and then re-top it, wiping the
// holder's accrued topListingShare.
if (currentId == listingId) return;
if (currentId != 0) {
if (!_beatsTop(listing.value, currentId)) revert TopNotBeaten();
_vacateTop(); // settle the outgoing holder's pot, clear the top
}
_setTop(listingId);
}
/*//////////////////////////////////////////////////////////////
REQUEST A ACQUISITION
//////////////////////////////////////////////////////////////*/
/// @notice The external VRF service's purchaser charge for one request.
/// @dev Relies on `tx.gasprice`; quote via an `eth_call` using the intended transaction gas price.
function vrfServiceFee() public view returns (uint256) {
return vrfService.requestFee();
}
/// @notice The expected-value-based fee an acquisition costs the depositor pool right now. With
/// inverse selection weight the EV is the harmonic mean of the pool's values, so a pool of
/// richer listings prices cheaper — you're far more likely to acquisition a low-value listing.
/// @return acquisitionFee The pool fee, `EV · (1 + surcharge)`, shared among depositors.
function acquisitionFee() public view returns (uint256) {
if (totalWeight == 0) return 0;
uint256 ev = weightedBackingTotal / totalWeight;
return ev * (BPS + surchargeBps) / BPS;
}
/// @notice Quote the full native cost of an acquisition: the depositor pool fee plus the VRF service fee.
/// @return fee The depositor pool fee.
/// @return vrf The VRF service fee (recoups the subscription's fulfillment gas).
/// @return total `fee + vrf`, the minimum `msg.value` for `acquire`.
function quoteAcquisitionPrice() external view returns (uint256 fee, uint256 vrf, uint256 total) {
fee = acquisitionFee();
vrf = vrfServiceFee();
total = fee + vrf;
}
/// @notice Pay for and request a VRF acquisition against the current listing pool. Send at least
/// `quoteAcquisitionPrice().total`; any excess is refunded. Requests are fully concurrent,
/// while their cached words settle strictly in the local sequence assigned on-chain.
/// @dev The acquisition fee remains escrowed until ordered processing distributes it or converts it
/// to a pull refund. The separate VRF fee is forwarded before requesting randomness, allowing
/// the service to fund callback coverage without touching backing or depositor fees.
/// @param maxAcquisitionFee Slippage guard: revert if the pool fee exceeds this. `0` disables
/// the check. Protects the purchaser from a deposit front-running the request and
/// raising the price under them.
/// @param minWeightedValue Slippage guard: revert if `weightedBackingTotal` is below this.
/// `0` disables the check. Protects the purchaser from a withdrawal front-running the
/// request and pulling listing value out of the pool they paid to acquisition against.
function acquire(uint256 maxAcquisitionFee, uint256 minWeightedValue)
external
payable
nonReentrant
returns (uint256 requestId)
{
requestId = _acquire(maxAcquisitionFee, minWeightedValue, selectionSlippageBps);
}
/// @notice Request an acquisition while choosing the maximum negative settlement drift you accept.
/// Positive drift remains capped by the protocol's snapshotted `selectionSlippageBps`.
function acquire(uint256 maxAcquisitionFee, uint256 minWeightedValue, uint256 maxNegativeSlippageBps)
external
payable
nonReentrant
returns (uint256 requestId)
{
requestId = _acquire(maxAcquisitionFee, minWeightedValue, maxNegativeSlippageBps);
}
function _acquire(uint256 maxAcquisitionFee, uint256 minWeightedValue, uint256 maxNegativeSlippageBps)
internal
returns (uint256 requestId)
{
if (maxNegativeSlippageBps > BPS) revert InvalidConfig();
if (unsettledAcquisitionCount == 0) _drainStaging(maxActivationsPerAcquisition);
uint256 fee = _validateAcquisition(maxAcquisitionFee, minWeightedValue);
uint256 vrf = vrfServiceFee();
uint256 cost = fee + vrf;
if (msg.value < cost) revert InsufficientPayment();
// Effects in the service (including any subscription top-up) roll back if the later VRF request fails.
vrfService.prepareRequests{value: vrf}(1);
emit VrfServiceFeePaid(msg.sender, vrf);
requestId = _openAcquisition(fee, maxNegativeSlippageBps);
// Refund any overpayment above the pool fee + VRF service fee.
_refund(msg.value - cost);
}
/// @notice Acquisition `count` times in one call: fires `count` independent VRF requests (one callback
/// each), every one priced, escrowed, settled and slippage-checked exactly like a single
/// `acquire`. You pay `count * (acquisitionFee + vrfFee)`; overpayment is refunded. The pool
/// is static during this request, so the slippage guards are checked once for all `count`;
/// each acquisition re-checks drift at its own ordered processing turn. Note: only the first acquisition of a batch
/// earns the cold-gap FWAToken surcharge slice — the rest are "hot" (gap 0), so batching can't farm
/// that bonus.
function acquireBatch(uint256 count, uint256 maxAcquisitionFee, uint256 minWeightedValue)
external
payable
nonReentrant
returns (uint256[] memory requestIds)
{
requestIds = _acquireBatch(count, maxAcquisitionFee, minWeightedValue, selectionSlippageBps);
}
function acquireBatch(
uint256 count,
uint256 maxAcquisitionFee,
uint256 minWeightedValue,
uint256 maxNegativeSlippageBps
) external payable nonReentrant returns (uint256[] memory requestIds) {
requestIds = _acquireBatch(count, maxAcquisitionFee, minWeightedValue, maxNegativeSlippageBps);
}
function _acquireBatch(
uint256 count,
uint256 maxAcquisitionFee,
uint256 minWeightedValue,
uint256 maxNegativeSlippageBps
) internal returns (uint256[] memory requestIds) {
if (count == 0 || count > maxAcquisitionsPerTx) revert InvalidAcquisitionCount();
if (maxNegativeSlippageBps > BPS) revert InvalidConfig();
if (unsettledAcquisitionCount == 0) _drainStaging(count * maxActivationsPerAcquisition);
uint256 fee = _validateAcquisition(maxAcquisitionFee, minWeightedValue);
uint256 vrf = vrfServiceFee();
uint256 cost = count * (fee + vrf);
if (msg.value < cost) revert InsufficientPayment();
uint256 vrfTotal = count * vrf;
// Reserve all batch callbacks before request #1. The whole transaction is atomic if any request fails.
vrfService.prepareRequests{value: vrfTotal}(count);
emit VrfServiceFeePaid(msg.sender, vrfTotal);
requestIds = new uint256[](count);
for (uint256 i = 0; i < count; i++) {
requestIds[i] = _openAcquisition(fee, maxNegativeSlippageBps);
}
_refund(msg.value - cost);
}
/// @dev Shared acquisition precondition + slippage checks. Returns the current `acquisitionFee()`. Reverts if
/// acquisitions are disabled, the pool is empty, the pool value drifted below `minWeightedValue`, or
/// the fee exceeds `maxAcquisitionFee`.
function _validateAcquisition(uint256 maxAcquisitionFee, uint256 minWeightedValue)
internal
view
returns (uint256 fee)
{
if (withdrawOnly) revert WithdrawOnlyActive();
if (!acquisitionsEnabled) revert AcquisitionsNotEnabled(); // still in the loading phase
if (totalWeight == 0) revert NoActiveListings();
// Slippage guards: assert the pool the purchaser saw hasn't shifted under them before
// this request is mined.
if (minWeightedValue != 0 && weightedBackingTotal < minWeightedValue) revert PoolValueTooLow();
fee = acquisitionFee();
if (maxAcquisitionFee != 0 && fee > maxAcquisitionFee) revert AcquisitionFeeTooHigh();
}
/// @dev Open one VRF-backed acquisition, commit its local sequence/batch, and register any optional
/// token reward in the external rewards module.
function _openAcquisition(uint256 fee, uint256 maxNegativeSlippageBps) internal returns (uint256 requestId) {
uint64 sequence = ++lastIssuedSequence;
uint256 reservedCount = _reserveStagedBatch(sequence);
uint64 deadline = uint64(block.number + selectionTimeoutBlocks);
// The sequence and exact staged batch are committed before the external VRF request. If the
// coordinator reverts, the entire transaction rolls both commitments back.
requestId = s_vrfCoordinator.requestRandomWords(
VRFV2PlusClient.RandomWordsRequest({
keyHash: vrfKeyHash,
subId: vrfSubId,
requestConfirmations: requestConfirmations,
callbackGasLimit: callbackGasLimit,
numWords: NUM_WORDS,
extraArgs: VRFV2PlusClient._argsToBytes(VRFV2PlusClient.ExtraArgsV1({nativePayment: true}))
})
);
if (requestId == 0 || acquisitions[requestId].status != AcquisitionStatus.None) {
revert DuplicateRequestId();
}
acquisitions[requestId] = Acquisition({
purchaser: msg.sender,
requestBlock: block.number,
priceEscrowed: fee,
listingId: 0,
status: AcquisitionStatus.Pending
});
uint256 surchargeSlice;
uint64 rewardEpoch;
IFWARewards r = rewards;
if (address(r) != address(0)) {
(surchargeSlice, rewardEpoch) = r.registerAcquisition(requestId, msg.sender, fee, surchargeBps);
if (surchargeSlice > fee) revert InvalidConfig();
acquisitionTokenSlice[requestId] = surchargeSlice;
}
acquisitionMeta[requestId] = AcquisitionMeta({
sequence: sequence,
wordDeadlineBlock: deadline,
rewardEpoch: rewardEpoch,
maxPositiveSlippageBps: uint16(selectionSlippageBps),
maxNegativeSlippageBps: uint16(maxNegativeSlippageBps),
randomWord: 0
});
requestIdAtSequence[sequence] = requestId;
pendingAcquisitionCount += 1;
unfulfilledVrfCount += 1;
unsettledAcquisitionCount += 1;
acquisitionEscrowTotal += fee;
emit AcquisitionRequested(requestId, msg.sender, fee, totalWeight);
emit AcquisitionSequenced(requestId, sequence, deadline, reservedCount);
}
/// @dev Refund overpayment above the total acquisition cost, if any.
function _refund(uint256 amount) internal {
if (amount != 0) SafeTransferLib.forceSafeTransferETH(msg.sender, amount);
}
/// @notice Single source of truth used by FWAVRFService for subscription balance reads and top-ups.
function vrfCoordinatorAndSubId() external view returns (address coordinator, uint256 subId) {
coordinator = address(s_vrfCoordinator);
subId = vrfSubId;
}
/// @notice Current gas lane and callback budget, used by the immutable service to bind request
/// coverage to the exact tuple FWA will send to Chainlink.
function vrfRequestConfig() external view returns (bytes32 keyHash, uint32 gasLimit) {
keyHash = vrfKeyHash;
gasLimit = callbackGasLimit;
}
/// @notice Change the Chainlink request tuple only through FWAVRFService's atomic coverage update.
function configureVrfRequest(bytes32 keyHash, uint32 gasLimit) external {
if (msg.sender != address(vrfService)) revert OnlyVrfService();
if (acquisitionsEnabled || unsettledAcquisitionCount != 0 || unfulfilledVrfCount != 0) {
revert AcquisitionStateLocked();
}
if (keyHash == bytes32(0) || gasLimit < MIN_CALLBACK_GAS_LIMIT || gasLimit > MAX_CALLBACK_GAS_LIMIT) {
revert InvalidConfig();
}
vrfKeyHash = keyHash;
callbackGasLimit = gasLimit;
emit ConfigSet(FWAConfigKeys.VRF_KEY_HASH, uint256(keyHash));
emit ConfigSet(FWAConfigKeys.CALLBACK_GAS_LIMIT, gasLimit);
}
/// @notice Clear callback liabilities that the coordinator has already completed (and potentially
/// billed) without a successful consumer callback. Safe only for FWA's dedicated subscription.
/// @dev Permissionless and fail-closed: the coordinator must report that the subscription has no
/// remaining request commitment. Acquisition expiry/processing remains a separate ordered step.
function reconcileUnfulfilledVrfCount() external returns (uint256 reconciled) {
reconciled = unfulfilledVrfCount;
if (reconciled == 0) return 0;
if (s_vrfCoordinator.pendingRequestExists(vrfSubId)) revert VrfRequestsPending();
unfulfilledVrfCount = 0;
emit UnfulfilledVrfReconciled(reconciled);
}
/*//////////////////////////////////////////////////////////////
CHAINLINK CALLBACK
//////////////////////////////////////////////////////////////*/
/// @notice VRF v2.5 fulfillment entrypoint. The coordinator is the ONLY authorized caller — this is
/// the hand-rolled equivalent of the Chainlink base's `rawFulfillRandomWords` (we don't
/// inherit the base; see the contract header). It caches or rejects the delivered word first,
/// then may make one failure-isolated attempt to process the canonical head.
function rawFulfillRandomWords(uint256 requestId, uint256[] calldata randomWords) external {
if (msg.sender != address(s_vrfCoordinator)) revert OnlyCoordinator();
fulfillRandomWords(requestId, randomWords);
}
/// @dev Coordinator callback: cache an on-time word or permanently classify it late. Unknown,
/// duplicate, terminal and malformed fulfillments return without reverting. Fast-path settlement
/// is best effort only; its child failure cannot roll back the cached word.
function fulfillRandomWords(uint256 requestId, uint256[] calldata randomWords) internal {
Acquisition storage acquisition = acquisitions[requestId];
// Chainlink bills when the authenticated callback is attempted, even if this acquisition already
// expired locally or the word array is malformed. Release its external coverage exactly once only
// for a request this FWA actually issued. Unknown and duplicate callbacks remain harmless.
if (acquisition.status != AcquisitionStatus.None && !callbackObserved[requestId]) {
callbackObserved[requestId] = true;
uint256 count = unfulfilledVrfCount;
if (count != 0) unfulfilledVrfCount = count - 1;
}
if (acquisition.status != AcquisitionStatus.Pending) {
emit FulfillmentIgnored(requestId, acquisition.status);
return;
}
AcquisitionMeta storage meta = acquisitionMeta[requestId];
if (block.number > meta.wordDeadlineBlock) {
acquisition.status = AcquisitionStatus.TimedOut;
pendingAcquisitionCount -= 1;
emit RandomnessTimedOut(requestId, meta.sequence, meta.wordDeadlineBlock, block.number);
return;
}
if (randomWords.length == 0) {
emit FulfillmentIgnored(requestId, acquisition.status);
return;
}
meta.randomWord = randomWords[0];
acquisition.status = AcquisitionStatus.Ready;
pendingAcquisitionCount -= 1;
emit RandomnessCached(requestId, meta.sequence, randomWords[0]);
_tryProcessCallbackHead(meta.sequence);
}
/// @dev Best-effort fast path. The word and callback-liability effects are committed in the parent
/// frame before this function runs. The child call reuses the exact permissionless processor,
/// including its reentrancy guard; any OOG, invariant failure, or rewards-module revert rolls
/// back only the child frame and leaves this acquisition `Ready` for a later processor.
/// Return data is deliberately not copied, preventing a reverting callee from consuming the
/// parent's return reserve with an oversized revert payload.
function _tryProcessCallbackHead(uint64 sequence) internal {
if (
callbackGasLimit < MIN_FAST_PATH_CALLBACK_GAS_LIMIT || sequence != nextSequenceToProcess
|| reservedListingsBySequence[sequence].length != 0
) return;
if (gasleft() < CALLBACK_FAST_PATH_GAS + CALLBACK_RETURN_GAS_RESERVE) {
return;
}
bytes memory callData = abi.encodeCall(this.processAcquisitions, (1));
uint256 gasStipend = CALLBACK_FAST_PATH_GAS;
assembly ("memory-safe") {
pop(call(gasStipend, address(), 0, add(callData, 0x20), mload(callData), 0, 0))
}
}
/// @notice Process the canonical ready/expired acquisition prefix. The caller chooses only a gas
/// batching bound; request IDs and ordering are fixed by `nextSequenceToProcess`.
function processAcquisitions(uint256 maxCount) external nonReentrant returns (uint256 processed) {
while (processed < maxCount && nextSequenceToProcess <= lastIssuedSequence) {
uint64 sequence = nextSequenceToProcess;
uint256 requestId = requestIdAtSequence[sequence];
Acquisition storage acquisition = acquisitions[requestId];
AcquisitionMeta storage meta = acquisitionMeta[requestId];
if (requestId == 0 || meta.sequence != sequence) revert SequenceInvariantBroken();
AcquisitionStatus status = acquisition.status;
if (status == AcquisitionStatus.Pending) {
if (block.number <= meta.wordDeadlineBlock) break;
pendingAcquisitionCount -= 1;
_activateReservedBatch(sequence);
uint256 refund = _creditAcquisitionRefund(acquisition, AcquisitionStatus.Expired);
emit AcquisitionExpired(requestId, sequence, acquisition.purchaser, refund);
} else if (status == AcquisitionStatus.TimedOut) {
_activateReservedBatch(sequence);
uint256 refund = _creditAcquisitionRefund(acquisition, AcquisitionStatus.Expired);
emit AcquisitionExpired(requestId, sequence, acquisition.purchaser, refund);
} else if (status == AcquisitionStatus.Ready) {
_activateReservedBatch(sequence);
_settleReadyAcquisition(requestId, acquisition, meta);
} else {
revert SequenceInvariantBroken();
}
_finishAcquisition(requestId, acquisition, meta, msg.sender);
unchecked {
++nextSequenceToProcess;
++processed;
}
}
}
function _creditAcquisitionRefund(Acquisition storage acquisition, AcquisitionStatus status)
internal
returns (uint256 refund)
{
refund = acquisition.priceEscrowed;
acquisition.status = status;
acquisitionRefundCredit[acquisition.purchaser] += refund;
acquisitionRefundCreditTotal += refund;
}
function _finishAcquisition(
uint256 requestId,
Acquisition storage acquisition,
AcquisitionMeta storage meta,
address processor
) internal {
IFWARewards r = rewards;
uint256 slice = acquisitionTokenSlice[requestId];
delete acquisitionTokenSlice[requestId];
acquisitionEscrowTotal -= acquisition.priceEscrowed;
unsettledAcquisitionCount -= 1;
if (address(r) != address(0)) {
if (acquisition.status == AcquisitionStatus.Fulfilled) {
r.settleAcquisition{value: slice}(requestId);
} else {
r.refundAcquisition(requestId);
}
}
emit AcquisitionProcessed(requestId, meta.sequence, acquisition.status, processor);
}
/// @dev Settle a cached word against the pool after its immutable activation batch is applied.
function _settleReadyAcquisition(uint256 requestId, Acquisition storage acquisition, AcquisitionMeta storage meta)
internal
{
/*
Empty pool: earlier canonical sequences removed every listing, so nothing can be selected.
Convert the purchaser's escrowed acquisition fee into a pull credit without any external call.
*/
if (totalWeight == 0) {
uint256 refund = _creditAcquisitionRefund(acquisition, AcquisitionStatus.Refunded);
emit AcquisitionRefundedNoListing(requestId, acquisition.purchaser, refund);
return;
}
/*
Ordered-settlement slippage guard: re-price against the canonical live pool after this
sequence's immutable reserved batch is activated. Earlier sequences may have removed listings
and shifted the fee. Drift outside the request's snapshotted tolerances becomes a pull refund;
the would-be selected listing remains active.
*/
{
uint256 liveFee = acquisitionFee();
uint256 escrowed = acquisition.priceEscrowed;
uint256 positiveTolerance = escrowed * meta.maxPositiveSlippageBps / BPS;
uint256 negativeTolerance = escrowed * meta.maxNegativeSlippageBps / BPS;
if (
(liveFee > escrowed && liveFee - escrowed > positiveTolerance)
|| (liveFee < escrowed && escrowed - liveFee > negativeTolerance)
) {
_creditAcquisitionRefund(acquisition, AcquisitionStatus.Refunded);
emit AcquisitionRefundedSlippage(requestId, acquisition.purchaser, escrowed, escrowed, liveFee);
return;
}
}
/*
Selection resolves against the canonical live pool produced by every earlier sequence plus
this request's reserved activation batch. Find the slot whose cumulative weight range contains
the target in TREE_DEPTH descents.
*/
uint256 randomWord = meta.randomWord;
uint256 target = randomWord % totalWeight;
uint256 slot = _selectSlot(target);
uint256 listingId = slotToListing[slot];
if (listingId == 0) revert InvalidTreeResult();
Listing storage listing = listings[listingId];
if (listing.status != ListingStatus.Active) revert InvalidTreeResult();
// The rewards module registered this immutable surcharge slice at request time. It receives the
// ETH in `_finishAcquisition`; the remaining fee is distributed here.
uint256 acquisitionFeePaid = acquisition.priceEscrowed;
uint256 slice = acquisitionTokenSlice[requestId];
// Skim the owner's protocol cut from the DEPOSITOR portion (post-slice), then route the rest
// BEFORE removing the allocated listing, so the listing being allocated shares in the acquisition it bore selection weight for
// (settled into its depositor's credit as it leaves the pool). The purchaser paid the same
// fee either way.
uint256 distributable = acquisitionFeePaid - slice;
uint256 ownerCut = distributable * ownerAcquisitionFeeBps / BPS;
_accrueOwnerFee(ownerCut);
distributable -= ownerCut;
// Cache whether the allocated listing is the top BEFORE any removal, so the ordering below is
// robust even if `_settleAndRemove` ever changes.
bool allocatedWasTop = topListingId == listingId;
// Carve the top-listing topListingShare off the top and add it to the live pot (even when the top is
// the listing being allocated — it earns this acquisition's topListingShare before being settled out). The rest is
// split equally across active listings. With no top the topListingShare folds back into that split.
if (topListingId != 0) {
uint256 topListingShare = distributable * topListingShareBps / BPS;
topListingPot += topListingShare;
emit TopListingFunded(topListingId, topListingShare, topListingPot);
_distribute(distributable - topListingShare);
} else {
_distribute(distributable);
}
// Settle the allocated listing's equal fee share to its depositor, then remove it from the
// active pool (weight, EV, slot). Backing ETH stays escrowed for the claim choice.
_settleAndRemove(listingId);
// If the listing just allocated held the top, settle the full pot (incl. this acquisition's topListingShare) to
// its depositor and vacate. A fresh top is taken on the next deposit / claimTopSpot.
if (allocatedWasTop) _vacateTop();
listing.purchaser = acquisition.purchaser;
listing.allocatedAt = uint64(block.timestamp);
listing.status = ListingStatus.Allocated;
acquisition.listingId = listingId;
acquisition.status = AcquisitionStatus.Fulfilled;
emit NFTAllocated(requestId, listingId, acquisition.purchaser, listing.depositor, listing.value, randomWord);
}
/// @notice Pull acquisition fees credited by ordered expiry, empty-pool, or slippage refunds.
function withdrawAcquisitionRefund() external nonReentrant returns (uint256 amount) {
amount = acquisitionRefundCredit[msg.sender];
if (amount == 0) revert NoAcquisitionRefund();
acquisitionRefundCredit[msg.sender] = 0;
acquisitionRefundCreditTotal -= amount;
SafeTransferLib.forceSafeTransferETH(msg.sender, amount);
emit AcquisitionRefundWithdrawn(msg.sender, amount);
}
/*//////////////////////////////////////////////////////////////
PURCHASER RESOLUTION
//////////////////////////////////////////////////////////////*/
/// @dev Load `listingId` and require it Allocated with `msg.sender` as its purchaser. Shared
/// guard prologue of the purchaser's resolution choices.
function _allocatedByPurchaser(uint256 listingId) internal view returns (Listing storage listing) {
listing = listings[listingId];
if (listing.status != ListingStatus.Allocated) revert ListingNotAllocated();
if (listing.purchaser != msg.sender) revert NotPurchaser();
}
/// @dev Load `listingId` and require it Allocated, deposited by `msg.sender`, and past the
/// purchaser's exclusive `settlementWindow`. Shared guard prologue of the depositor's
/// resolution choices.
function _allocatedByDepositorAfterWindow(uint256 listingId) internal view returns (Listing storage listing) {
listing = listings[listingId];
if (listing.status != ListingStatus.Allocated) revert ListingNotAllocated();
if (listing.depositor != msg.sender) revert NotDepositor();
if (block.timestamp < uint256(listing.allocatedAt) + settlementWindow) revert SettlementWindowNotElapsed();
}
/// @notice Purchaser takes the NFT. The listing's backing ETH returns to its depositor, less the
/// owner's protocol cut. Available the whole time the listing is allocated.
/// @dev Purchaser-initiated, so the NFT transfer is strict: if it fails, the whole call reverts
/// and the purchaser can fall back to `acceptDepositorBid` rather than be committed to an
/// undeliverable NFT.
function keepNFT(uint256 listingId) external nonReentrant {
Listing storage listing = _allocatedByPurchaser(listingId);
(address depositor, address purchaser, uint256 toDepositor) = _settleNFTToPurchaser(listingId, listing, true);
emit NFTKept(listingId, purchaser, depositor, toDepositor);
}
/// @notice Purchaser takes the NFT and sells into the ETH bid: receives `settlementDiscountBps`% of the backing. The retained
/// remainder is routed per `retainedToProtocol`, and the NFT is purchased by
/// the depositor. Available the whole time the listing is allocated.
function acceptDepositorBid(uint256 listingId) external nonReentrant {
Listing storage listing = _allocatedByPurchaser(listingId);
(address depositor, address purchaser, uint256 payout, uint256 retained) =
_settleBackingToPurchaser(listingId, listing);
SafeTransferLib.forceSafeTransferETH(purchaser, payout);
emit DepositorBidAccepted(listingId, purchaser, depositor, payout, retained);
}
/// @notice Purchaser accepts the bid, but takes the settlementDiscount as FWAToken instead of ETH: the `settlementDiscountBps` ETH
/// that would have been paid out is used to BUY FWAToken from the pool and delivered to the
/// purchaser. The NFT returns to the depositor and the retained penalty is routed as usual —
/// identical to `acceptDepositorBid` except the purchaser's payout arrives as FWAToken.
/// @param minOut Slippage guard on the FWAToken received.
function acceptBidAsTokens(uint256 listingId, uint256 minOut) external nonReentrant returns (uint256 tokenOut) {
IFWARewards r = rewards;
if (address(r) == address(0)) revert TokenNotConfigured();
if (!acceptBidAsTokensEnabled) revert AcceptBidAsTokensDisabled();
Listing storage listing = _allocatedByPurchaser(listingId);
(address depositor, address purchaser, uint256 payout, uint256 retained) =
_settleBackingToPurchaser(listingId, listing);
tokenOut = r.buyFor{value: payout}(purchaser, minOut);
emit DepositorBidAcceptedAsTokens(listingId, purchaser, depositor, payout, retained, tokenOut);
}
/// @notice Purchaser keeps the NFT economically but immediately re-lists it as a NEW listing
/// with themselves as depositor at price = `msg.value` (fresh backing). The old
/// depositor is paid exactly as in `keepNFT` (backing less the owner cut) and the NFT
/// never leaves custody. The new listing runs the full deposit gauntlet and the same
/// staging branch as `listNFT`, so it cannot be word-informed into a live selection.
function relistNFT(uint256 listingId) external payable nonReentrant {
Listing storage listing = _allocatedByPurchaser(listingId);
(,, uint256 toDepositor) = _payoutBackingToDepositor(listing);
uint256 newListingId = _createListing(listing.collection, listing.tokenId, address(this));
emit NFTRelisted(listingId, newListingId, toDepositor);
}
/// @notice Once the purchaser's `settlementWindow` lapses without a choice, the depositor may resolve
/// the listing themselves and KEEP THE ETH: they receive the backing (less the owner
/// cut) and the NFT goes to the purchaser — identical economics to `keepNFT`.
function depositorReclaimBacking(uint256 listingId) external nonReentrant {
Listing storage listing = _allocatedByDepositorAfterWindow(listingId);
(address depositor, address purchaser, uint256 toDepositor) = _settleNFTToPurchaser(listingId, listing, false);
emit NFTKept(listingId, purchaser, depositor, toDepositor);
}
/// @notice Once the purchaser's `settlementWindow` lapses without a choice, the depositor may resolve
/// the listing themselves and RECLAIM THE NFT: the purchaser gets the `settlementDiscountBps`% ETH
/// payout (retained remainder routed per `retainedToProtocol`) and the NFT returns to
/// the depositor — identical economics to `acceptDepositorBid`.
function depositorReclaimNFT(uint256 listingId) external nonReentrant {
Listing storage listing = _allocatedByDepositorAfterWindow(listingId);
(address depositor, address purchaser, uint256 payout, uint256 retained) =
_settleBackingToPurchaser(listingId, listing);
SafeTransferLib.forceSafeTransferETH(purchaser, payout);
emit DepositorBidAccepted(listingId, purchaser, depositor, payout, retained);
}
/// @notice After `finalizeWindow` lapses with neither the purchaser nor the depositor resolving,
/// anyone may finalize the default: the NFT goes to the purchaser and the backing ETH
/// returns to the depositor (less the owner's protocol cut), so neither asset locks.
function finalizeUnsettled(uint256 listingId) external nonReentrant {
Listing storage listing = listings[listingId];
if (listing.status != ListingStatus.Allocated) revert ListingNotAllocated();
if (block.timestamp < uint256(listing.allocatedAt) + finalizeWindow) revert SettlementWindowNotElapsed();
(address depositor, address purchaser,) = _settleNFTToPurchaser(listingId, listing, false);
emit UnsettledFinalized(listingId, purchaser, depositor);
}
/// @notice Pull an NFT whose delivery failed during resolution (e.g. a pausable, reverting, or
/// otherwise misbehaving collection). Only the recipient the NFT was owed to may
/// claim it; every ETH leg already settled when the listing resolved. Reverts if the
/// collection is still failing — the recipient can simply retry once it recovers.
function recoverStuckNFT(uint256 listingId) external nonReentrant {
if (stuckNFTRecipient[listingId] != msg.sender) revert NotStuckRecipient();
// Cleared before the transfer; if the transfer reverts, the revert restores it and the
// recipient stays entitled to retry.
delete stuckNFTRecipient[listingId];
Listing storage listing = listings[listingId];
ERC721(listing.collection).transferFrom(address(this), msg.sender, listing.tokenId);
emit StuckNFTRecovered(listingId, msg.sender);
}
/// @dev NFT-to-purchaser resolution shared by `keepNFT`, `depositorReclaimBacking`, and
/// `finalizeUnsettled`. Marks the listing Settled, returns the backing to the depositor
/// less the owner's protocol cut, and sends the NFT to the purchaser. `strict` makes the NFT
/// transfer revert on failure (purchaser-initiated `keepNFT`, which has a `acceptDepositorBid`
/// fallback); otherwise it is delivered best-effort so a misbehaving NFT can neither
/// block the depositor's ETH return nor lock the listing. The caller emits.
function _settleNFTToPurchaser(uint256 listingId, Listing storage listing, bool strict)
internal
returns (address depositor, address purchaser, uint256 toDepositor)
{
(depositor, purchaser, toDepositor) = _payoutBackingToDepositor(listing);
if (strict) {
ERC721(listing.collection).safeTransferFrom(address(this), purchaser, listing.tokenId);
} else {
_deliverNFT(listingId, listing.collection, purchaser, listing.tokenId);
}
}
/// @dev Settlement ETH leg shared by `_settleNFTToPurchaser` and `relistNFT`: marks the listing
/// Settled, accrues the owner cut, and returns the backing (less that cut) to the depositor.
/// No NFT movement.
function _payoutBackingToDepositor(Listing storage listing)
internal
returns (address depositor, address purchaser, uint256 toDepositor)
{
listing.status = ListingStatus.Settled;
depositor = listing.depositor;
purchaser = listing.purchaser;
uint256 value = listing.value;
// The purchaser takes no ETH on this outcome, so the owner's cut comes out of the
// depositor's backing return. The immutable 5% cap keeps at least 95% for the depositor.
uint256 ownerCut = value * ownerSettlementFeeBps / BPS;
toDepositor = value - ownerCut;
_accrueOwnerFee(ownerCut);
SafeTransferLib.forceSafeTransferETH(depositor, toDepositor);
}
/// @dev ETH-settlement resolution shared by `acceptDepositorBid`, `depositorReclaimNFT`, and `acceptBidAsTokens`. Marks
/// the listing Settled, computes the purchaser's `settlementDiscountBps` payout, routes the retained settlement
/// penalty per `retainedToProtocol`, and returns the NFT to the depositor best-effort. It does
/// NOT deliver the `payout` — the caller delivers it as ETH or as FWAToken bought from the pool, so
/// the purchaser can accept the bid either way. Caller emits.
function _settleBackingToPurchaser(uint256 listingId, Listing storage listing)
internal
returns (address depositor, address purchaser, uint256 payout, uint256 retained)
{
listing.status = ListingStatus.Settled;
depositor = listing.depositor;
purchaser = listing.purchaser;
uint256 value = listing.value;
payout = value * settlementDiscountBps / BPS; // purchaser's settlement, e.g. 85%
retained = value - payout; // the settlement penalty, e.g. 15%
// The retained penalty is the protocol's ONLY take on a settlement — no separate owner
// resolution cut is charged here, so the purchaser receives the full settlementDiscount. By default
// (`retainedToProtocol`) the penalty is protocol revenue; otherwise it is shared among
// active depositors, falling back to the protocol when no active EV remains to receive it.
if (retained != 0) {
if (retainedToProtocol) _accrueOwnerFee(retained);
else _distribute(retained);
}
_deliverNFT(listingId, listing.collection, depositor, listing.tokenId);
}
/// @dev Best-effort NFT delivery for a resolved listing. On any failure — a pausable, reverting,
/// or non-standard collection — the NFT is recorded as claimable by `to` via
/// `recoverStuckNFT`, so resolution's ETH legs always settle and the listing never locks.
function _deliverNFT(uint256 listingId, address collection, address to, uint256 tokenId) internal {
try ERC721(collection).transferFrom(address(this), to, tokenId) {
// delivered
} catch {
stuckNFTRecipient[listingId] = to;
emit NFTDeliveryFailed(listingId, to, collection, tokenId);
}
}
/*//////////////////////////////////////////////////////////////
WITHDRAW EARNINGS
//////////////////////////////////////////////////////////////*/
/// @notice Withdraw your accrued share of acquisition earnings. No delay: these are
/// already-collected fees credited to you.
function withdrawEarnings() external nonReentrant returns (uint256 amount) {
amount = feeCredit[msg.sender];
if (amount == 0) revert NoEarnings();
feeCredit[msg.sender] = 0;
SafeTransferLib.forceSafeTransferETH(msg.sender, amount);
emit EarningsWithdrawn(msg.sender, amount);
}
/// @notice Claim the acquisition-fee share your still-active listings have collected so far, without
/// withdrawing them. The listings stay in the pool and keep earning — this just sweeps
/// their accrued fees to you and pays out in one call.
/// @dev Harvest pattern: for each listing, credit its pending fees, then advance its dividend
/// checkpoint (`feeDebt`) to the current (floored) accumulator so the same fees can
/// never be claimed twice. Rounding down preserves `Σ credited ≤ Σ collected`, the same
/// solvency guarantee `_settleAndRemove` relies on. Touches neither `weight`/`value`
/// nor the pool totals, so a pending purchaser's pool is unaffected (no withdraw lock).
/// @param listingIds Active listings to harvest. Every one must be yours and still active.
/// @return total The ETH paid out across all the harvested listings.
function claimListingFees(uint256[] calldata listingIds) external nonReentrant returns (uint256 total) {
for (uint256 i = 0; i < listingIds.length; i++) {
uint256 listingId = listingIds[i];
Listing storage listing = _activeOwned(listingId);
uint256 pending = _pendingFees(listing);
if (pending == 0) continue;
listing.feeDebt = listing.feeShare * accFeePerEV / SCALE;
total += pending;
emit EarningsAccrued(msg.sender, listingId, pending);
}
if (total == 0) revert NoEarnings();
SafeTransferLib.forceSafeTransferETH(msg.sender, total);
emit EarningsWithdrawn(msg.sender, total);
}
/// @notice Push accrued protocol fees out. A configurable `protocolFeeToTokenBps` slice is sent to
/// the FWAToken token's buyback reserve (where its TWAP `buyback` buys & burns/redistributes);
/// the remainder goes to the owner payout address. Permissionless. Returns the owner slice.
function payoutFees() external nonReentrant returns (uint256 amount) {
uint256 accrued = accruedOwnerFees;
if (accrued == 0) revert NoOwnerFees();
accruedOwnerFees = 0;
uint256 tokenPortion = token == address(0) ? 0 : accrued * protocolFeeToTokenBps / BPS;
amount = accrued - tokenPortion;
address to = payoutAddress;
if (amount != 0) SafeTransferLib.forceSafeTransferETH(to, amount);
if (tokenPortion != 0) {
SafeTransferLib.forceSafeTransferETH(token, tokenPortion); // FWAToken buyback reserve
emit ProtocolFeesToToken(tokenPortion);
}
emit FeesPaidOut(to, amount);
}
/// @notice Unsettled acquisition-fee share an active listing has accrued but not yet credited.
/// Returns 0 for listings that are no longer active (their fees are already
/// settled into the depositor's `feeCredit`).
function pendingFees(uint256 listingId) external view returns (uint256) {
Listing storage listing = listings[listingId];
if (listing.status != ListingStatus.Active) return 0;
return _pendingFees(listing);
}
/*//////////////////////////////////////////////////////////////
FEE / EV ACCOUNTING
//////////////////////////////////////////////////////////////*/
/// @dev A listing's EV contribution (weight · value). Sums into `weightedBackingTotal`, which
/// drives only the acquisition price — NOT fee distribution (that uses `feeShare`, √value).
/// With inverse weights this is ≈ NUM for every listing.
function _evOf(Listing storage listing) internal view returns (uint256) {
return listing.weight * listing.value;
}
/// @dev Fees accrued to a listing since its last checkpoint, by its √value share. The current
/// (floored) value can sit just below the (ceiled) `feeDebt` when no fees have accrued,
/// so the subtraction saturates at zero instead of underflowing — and rounds the listing's
/// credit down, so the contract never over-credits.
function _pendingFees(Listing storage listing) internal view returns (uint256) {
uint256 acc = listing.feeShare * accFeePerEV / SCALE;
uint256 debt = listing.feeDebt;
return acc > debt ? acc - debt : 0;
}
/// @dev Accrue a protocol fee to the owner's withdrawable balance.
function _accrueOwnerFee(uint256 amount) internal {
if (amount == 0) return;
accruedOwnerFees += amount;
emit OwnerFeesAccrued(amount);
}
/// @dev Distribute `amount` wei to active depositors equally per listing (flat fee share). If no
/// active share remains to receive it, the amount accrues to protocol fees so it is
/// never stranded.
function _distribute(uint256 amount) internal {
if (feeShareTotal > 0) {
accFeePerEV += amount * SCALE / feeShareTotal;
} else {
_accrueOwnerFee(amount);
}
}
/// @dev Settle the current top's accrued pot into its holder's `feeCredit` and vacate the
/// top. Idempotent (no-op when vacant) and resets `topListingPot` to 0 in the same write
/// as clearing `topListingId`, so the pot — additive ETH that was already collected via the
/// acquisition topListingShare — can be settled at most once per tenure and is never double-paid or stranded.
/// Effect-only (credits `feeCredit`, no ETH transfer), so it is reentrancy-safe to call
/// before interactions, mirroring `_settleAndRemove`.
function _vacateTop() internal {
uint256 id = topListingId;
if (id == 0) return;
uint256 pot = topListingPot;
topListingId = 0;
topListingPot = 0;
if (pot != 0) {
address depositor = listings[id].depositor;
feeCredit[depositor] += pot;
emit TopListingSettled(id, depositor, pot);
}
emit TopListingSet(0, address(0));
}
/// @dev Top an active listing. The caller must have vacated any prior top first, so the pot
/// starts fresh at 0 for the new holder.
function _setTop(uint256 listingId) internal {
topListingId = listingId;
emit TopListingSet(listingId, listings[listingId].depositor);
}
/// @dev True when `value` clears the standing top (`currentId`, must be non-zero) by
/// `topThresholdBps`. Cross-multiplied so no intermediate division rounds the bar down in
/// the challenger's favor; safe from overflow since value <= 1e36 (weight != 0) and the bps
/// factor <= 2e4, so the products stay far inside uint256.
function _beatsTop(uint256 value, uint256 currentId) internal view returns (bool) {
return value * BPS >= listings[currentId].value * (BPS + topThresholdBps);
}
/// @dev Settle a listing's accrued fees into its depositor's credit, then remove it from
/// the active pool (weight, EV, fee share, slot, mapping). Settlement MUST precede the
/// share removal so the pending amount is computed against the listing's live share.
function _settleAndRemove(uint256 listingId) internal {
Listing storage listing = listings[listingId];
uint256 pending = _pendingFees(listing);
if (pending != 0) {
feeCredit[listing.depositor] += pending;
emit EarningsAccrued(listing.depositor, listingId, pending);
}
IFWARewards r = rewards;
if (address(r) != address(0)) r.onListingRemoved(listingId);
uint256 slot = listing.slot;
_removeTreeWeight(slot, listing.weight);
totalWeight -= listing.weight;
weightedBackingTotal -= _evOf(listing);
feeShareTotal -= listing.feeShare;
activeListingCount--;
delete slotToListing[slot];
_releaseSlot(slot);
listing.slot = 0;
}
/*//////////////////////////////////////////////////////////////
SEGMENT TREE: ADD WEIGHT
//////////////////////////////////////////////////////////////*/
function _addTreeWeight(uint256 slot, uint256 amount) internal {
uint256 node = LEAF_BASE + slot - 1;
/*
Update leaf, then every ancestor through the root.
*/
while (true) {
tree[node] += amount;
if (node == 1) break;
node >>= 1;
}
}
/*//////////////////////////////////////////////////////////////
SEGMENT TREE: REMOVE WEIGHT
//////////////////////////////////////////////////////////////*/
function _removeTreeWeight(uint256 slot, uint256 amount) internal {
uint256 node = LEAF_BASE + slot - 1;
while (true) {
tree[node] -= amount;
if (node == 1) break;
node >>= 1;
}
}
/*//////////////////////////////////////////////////////////////
SEGMENT TREE: SELECT LISTING
//////////////////////////////////////////////////////////////*/
function _selectSlot(uint256 target) internal view returns (uint256 slot) {
/*
target must be in:
0 <= target < tree[1]
At each branch:
- If target is inside the left subtree, descend left.
- Otherwise subtract the left subtree's range and descend right.
*/
uint256 node = 1;
while (node < LEAF_BASE) {
uint256 leftChild = node << 1;
uint256 leftWeight = tree[leftChild];
if (target < leftWeight) {
node = leftChild;
} else {
target -= leftWeight;
node = leftChild | 1;
}
}
slot = node - LEAF_BASE + 1;
}
/*//////////////////////////////////////////////////////////////
SLOT ALLOCATION
//////////////////////////////////////////////////////////////*/
function _allocateSlot() internal returns (uint256 slot) {
/*
Reuse a previously released slot when possible.
*/
if (freeSlotHead != 0) {
slot = freeSlotHead;
freeSlotHead = nextFreeSlot[slot];
delete nextFreeSlot[slot];
return slot;
}
/*
Otherwise expand into the next unused logical slot.
No array is expanded and no storage is preallocated.
*/
slot = nextUnusedSlot++;
if (slot > CAPACITY) revert ActiveListingCapacityReached();
}
function _releaseSlot(uint256 slot) internal {
nextFreeSlot[slot] = freeSlotHead;
freeSlotHead = slot;
}
/*//////////////////////////////////////////////////////////////
OWNER CONFIG
//////////////////////////////////////////////////////////////*/
/// @dev Revert unless `v` is a valid bps fraction (<= BPS).
function _bpsCapped(uint256 v) internal pure returns (uint256) {
if (v > BPS) revert InvalidConfig();
return v;
}
/// @notice Set a uint-valued config knob, keyed by a `FWAConfigKeys` constant. Consolidates the
/// former per-knob setters into one dispatcher to keep the runtime bytecode under the
/// EIP-170 limit; each branch preserves its old setter's validation exactly (bounds,
/// window cross-invariants, narrow-type ranges). The VRF key hash is passed as
/// `uint256(keyHash)`. Unknown keys revert `InvalidConfig`.
function setUint(uint256 key, uint256 value) external onlyOwner {
if (unsettledAcquisitionCount != 0) revert AcquisitionStateLocked();
if (key == FWAConfigKeys.VRF_SUB_ID) {
if (value == 0) revert InvalidConfig();
if (unfulfilledVrfCount != 0) revert AcquisitionStateLocked();
vrfSubId = value;
} else if (key == FWAConfigKeys.REQUEST_CONFIRMATIONS) {
if (value < MIN_REQUEST_CONFIRMATIONS || value > MAX_REQUEST_CONFIRMATIONS) revert InvalidConfig();
if (selectionTimeoutBlocks < value + MIN_CALLBACK_SLACK_BLOCKS) revert InvalidConfig();
requestConfirmations = uint16(value);
} else if (key == FWAConfigKeys.MAX_ACTIVATIONS_PER_ACQUISITION) {
if (value == 0 || value > MAX_ACTIVATIONS_PER_ACQUISITION) revert InvalidConfig();
maxActivationsPerAcquisition = value;
} else if (key == FWAConfigKeys.SELECTION_TIMEOUT_BLOCKS) {
if (value < uint256(requestConfirmations) + MIN_CALLBACK_SLACK_BLOCKS || value > MAX_WINDOW_BLOCKS) {
revert InvalidConfig();
}
selectionTimeoutBlocks = value;
} else if (key == FWAConfigKeys.MAX_ACQUISITIONS_PER_TX) {
if (value == 0) revert InvalidConfig();
maxAcquisitionsPerTx = value;
} else if (key == FWAConfigKeys.SURCHARGE_BPS) {
surchargeBps = value; // deliberately uncapped: an EV surcharge above 100% is meaningful
} else if (key == FWAConfigKeys.SELECTION_SLIPPAGE_BPS) {
selectionSlippageBps = _bpsCapped(value);
} else if (key == FWAConfigKeys.TOP_LISTING_SHARE_BPS) {
topListingShareBps = _bpsCapped(value);
} else if (key == FWAConfigKeys.TOP_THRESHOLD_BPS) {
topThresholdBps = _bpsCapped(value);
} else if (key == FWAConfigKeys.SETTLEMENT_DISCOUNT_BPS) {
if (value < MIN_SETTLEMENT_DISCOUNT_BPS || value > MAX_SETTLEMENT_DISCOUNT_BPS) {
revert InvalidConfig();
}
// Keep the purchaser's payout non-negative: the owner's cut can't exceed the discount.
if (value < ownerSettlementFeeBps) revert InvalidConfig();
settlementDiscountBps = value;
} else if (key == FWAConfigKeys.OWNER_ACQUISITION_FEE_BPS) {
ownerAcquisitionFeeBps = _bpsCapped(value);
} else if (key == FWAConfigKeys.OWNER_SETTLEMENT_FEE_BPS) {
if (value > MAX_OWNER_SETTLEMENT_FEE_BPS || value > settlementDiscountBps) revert InvalidConfig();
ownerSettlementFeeBps = value;
} else if (key == FWAConfigKeys.SETTLEMENT_WINDOW) {
if (value > finalizeWindow) revert InvalidConfig();
settlementWindow = value;
} else if (key == FWAConfigKeys.FINALIZE_WINDOW) {
if (value < settlementWindow) revert InvalidConfig();
finalizeWindow = value;
} else if (key == FWAConfigKeys.MIN_BACKING) {
minBacking = value;
} else if (key == FWAConfigKeys.PROTOCOL_FEE_TO_TOKEN_BPS) {
protocolFeeToTokenBps = _bpsCapped(value);
} else if (key == FWAConfigKeys.MAX_STAGED_LISTINGS) {
maxStagedListings = value; // 0 = unlimited
} else {
revert InvalidConfig();
}
emit ConfigSet(key, value);
}
/// @notice Set a bool-valued config knob. Opening acquisitions also starts the optional rewards
/// module's emission clock (idempotently).
function setBool(uint256 key, bool value) external onlyOwner {
if (key == FWAConfigKeys.RETAINED_TO_PROTOCOL) {
retainedToProtocol = value;
} else if (key == FWAConfigKeys.ACQUISITIONS_ENABLED) {
acquisitionsEnabled = value;
IFWARewards r = rewards;
if (value && address(r) != address(0)) r.startEmission();
} else if (key == FWAConfigKeys.WITHDRAW_ONLY) {
withdrawOnly = value;
} else if (key == FWAConfigKeys.WHITELIST_ENABLED) {
whitelistEnabled = value;
} else if (key == FWAConfigKeys.ACCEPT_BID_AS_TOKENS_ENABLED) {
acceptBidAsTokensEnabled = value;
} else {
revert InvalidConfig();
}
emit ConfigSet(key, value ? 1 : 0);
}
/// @notice Set an address-valued config knob, keyed by a `FWAConfigKeys` constant. Zero address
/// rejected for every key except `WHITELIST_MANAGER` (zero revokes the manager). A new
/// VRF coordinator's subscription must already list this contract as a consumer. Unknown
/// keys revert `InvalidConfig`.
function setAddr(uint256 key, address value) external onlyOwner {
if (key == FWAConfigKeys.WHITELIST_MANAGER) {
whitelistManager = value; // zero allowed: revokes the manager (msg.sender is never zero)
} else if (value == address(0)) {
revert InvalidConfig();
} else if (key == FWAConfigKeys.VRF_COORDINATOR) {
if (unsettledAcquisitionCount != 0 || unfulfilledVrfCount != 0) revert AcquisitionStateLocked();
s_vrfCoordinator = IVRFCoordinatorV2Plus(value);
} else if (key == FWAConfigKeys.PAYOUT_ADDRESS) {
payoutAddress = value;
} else {
revert InvalidConfig();
}
emit ConfigSet(key, uint256(uint160(value)));
}
/// @notice Add (`allowed = true`) or remove (`false`) collections from the deposit whitelist.
/// All entries get the same `allowed` value. Only gates NEW deposits while
/// `whitelistEnabled` is true; listings already in the pool are unaffected. Callable by
/// the owner or the owner-appointed `whitelistManager` (a curation contract, say).
function setCollectionsWhitelisted(address[] calldata collections, bool allowed) external {
if (msg.sender != owner() && msg.sender != whitelistManager) revert Unauthorized();
for (uint256 i = 0; i < collections.length; i++) {
collectionWhitelisted[collections[i]] = allowed;
emit CollectionWhitelistSet(collections[i], allowed);
}
}
/// @notice One-time wire of the separately deployed FWAToken rewards/swap module.
function setRewards(address module) external onlyOwner {
if (module == address(0) || address(rewards) != address(0)) revert InvalidConfig();
if (unsettledAcquisitionCount != 0) revert AcquisitionStateLocked();
if (activeListingCount != 0 || stagedCount != 0 || reservedStagedCount != 0) revert InvalidConfig();
IFWARewards r = IFWARewards(module);
address rewardToken = r.token();
if (rewardToken == address(0) || r.fwa() != address(this)) revert InvalidConfig();
rewards = r;
token = rewardToken;
if (acquisitionsEnabled) r.startEmission();
emit RewardsConfigured(module, rewardToken);
}
/// @notice Migration guard consumed by the rewards module before an owner token rescue.
function canRescueRewards() external view returns (bool) {
return withdrawOnly && unsettledAcquisitionCount == 0;
}
/*//////////////////////////////////////////////////////////////
TREE VIEWS
//////////////////////////////////////////////////////////////*/
function treeRootWeight() external view returns (uint256) {
return tree[1];
}
}
lib/chainlink-evm/contracts/src/v0.8/vrf/dev/interfaces/IVRFCoordinatorV2Plus.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {VRFV2PlusClient} from "../libraries/VRFV2PlusClient.sol";
import {IVRFSubscriptionV2Plus} from "./IVRFSubscriptionV2Plus.sol";
// Interface that enables consumers of VRFCoordinatorV2Plus to be future-proof for upgrades
// This interface is supported by subsequent versions of VRFCoordinatorV2Plus
interface IVRFCoordinatorV2Plus is IVRFSubscriptionV2Plus {
/**
* @notice Request a set of random words.
* @param req - a struct containing following fields for randomness request:
* keyHash - Corresponds to a particular oracle job which uses
* that key for generating the VRF proof. Different keyHash's have different gas price
* ceilings, so you can select a specific one to bound your maximum per request cost.
* subId - The ID of the VRF subscription. Must be funded
* with the minimum subscription balance required for the selected keyHash.
* requestConfirmations - How many blocks you'd like the
* oracle to wait before responding to the request. See SECURITY CONSIDERATIONS
* for why you may want to request more. The acceptable range is
* [minimumRequestBlockConfirmations, 200].
* callbackGasLimit - How much gas you'd like to receive in your
* fulfillRandomWords callback. Note that gasleft() inside fulfillRandomWords
* may be slightly less than this amount because of gas used calling the function
* (argument decoding etc.), so you may need to request slightly more than you expect
* to have inside fulfillRandomWords. The acceptable range is
* [0, maxGasLimit]
* numWords - The number of uint256 random values you'd like to receive
* in your fulfillRandomWords callback. Note these numbers are expanded in a
* secure way by the VRFCoordinator from a single random value supplied by the oracle.
* extraArgs - abi-encoded extra args
* @return requestId - A unique identifier of the request. Can be used to match
* a request to a response in fulfillRandomWords.
*/
function requestRandomWords(
VRFV2PlusClient.RandomWordsRequest calldata req
) external returns (uint256 requestId);
}
lib/chainlink-evm/contracts/src/v0.8/vrf/dev/interfaces/IVRFSubscriptionV2Plus.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/// @notice The IVRFSubscriptionV2Plus interface defines the subscription
/// @notice related methods implemented by the V2Plus coordinator.
interface IVRFSubscriptionV2Plus {
/**
* @notice Add a consumer to a VRF subscription.
* @param subId - ID of the subscription
* @param consumer - New consumer which can use the subscription
*/
function addConsumer(uint256 subId, address consumer) external;
/**
* @notice Remove a consumer from a VRF subscription.
* @param subId - ID of the subscription
* @param consumer - Consumer to remove from the subscription
*/
function removeConsumer(uint256 subId, address consumer) external;
/**
* @notice Cancel a subscription
* @param subId - ID of the subscription
* @param to - Where to send the remaining LINK to
*/
function cancelSubscription(uint256 subId, address to) external;
/**
* @notice Accept subscription owner transfer.
* @param subId - ID of the subscription
* @dev will revert if original owner of subId has
* not requested that msg.sender become the new owner.
*/
function acceptSubscriptionOwnerTransfer(
uint256 subId
) external;
/**
* @notice Request subscription owner transfer.
* @param subId - ID of the subscription
* @param newOwner - proposed new owner of the subscription
*/
function requestSubscriptionOwnerTransfer(uint256 subId, address newOwner) external;
/**
* @notice Create a VRF subscription.
* @return subId - A unique subscription id.
* @dev You can manage the consumer set dynamically with addConsumer/removeConsumer.
* @dev Note to fund the subscription with LINK, use transferAndCall. For example
* @dev LINKTOKEN.transferAndCall(
* @dev address(COORDINATOR),
* @dev amount,
* @dev abi.encode(subId));
* @dev Note to fund the subscription with Native, use fundSubscriptionWithNative. Be sure
* @dev to send Native with the call, for example:
* @dev COORDINATOR.fundSubscriptionWithNative{value: amount}(subId);
*/
function createSubscription() external returns (uint256 subId);
/**
* @notice Get a VRF subscription.
* @param subId - ID of the subscription
* @return balance - LINK balance of the subscription in juels.
* @return nativeBalance - native balance of the subscription in wei.
* @return reqCount - Requests count of subscription.
* @return owner - owner of the subscription.
* @return consumers - list of consumer address which are able to use this subscription.
*/
function getSubscription(
uint256 subId
)
external
view
returns (uint96 balance, uint96 nativeBalance, uint64 reqCount, address owner, address[] memory consumers);
/*
* @notice Check to see if there exists a request commitment consumers
* for all consumers and keyhashes for a given sub.
* @param subId - ID of the subscription
* @return true if there exists at least one unfulfilled request for the subscription, false
* otherwise.
*/
function pendingRequestExists(
uint256 subId
) external view returns (bool);
/**
* @notice Paginate through all active VRF subscriptions.
* @param startIndex index of the subscription to start from
* @param maxCount maximum number of subscriptions to return, 0 to return all
* @dev the order of IDs in the list is **not guaranteed**, therefore, if making successive calls, one
* @dev should consider keeping the blockheight constant to ensure a holistic picture of the contract state
*/
function getActiveSubscriptionIds(uint256 startIndex, uint256 maxCount) external view returns (uint256[] memory);
/**
* @notice Fund a subscription with native.
* @param subId - ID of the subscription
* @notice This method expects msg.value to be greater than or equal to 0.
*/
function fundSubscriptionWithNative(
uint256 subId
) external payable;
}
lib/chainlink-evm/contracts/src/v0.8/vrf/dev/libraries/VRFV2PlusClient.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
// End consumer library.
library VRFV2PlusClient {
// extraArgs will evolve to support new features
bytes4 public constant EXTRA_ARGS_V1_TAG = bytes4(keccak256("VRF ExtraArgsV1"));
struct ExtraArgsV1 {
bool nativePayment;
}
struct RandomWordsRequest {
bytes32 keyHash;
uint256 subId;
uint16 requestConfirmations;
uint32 callbackGasLimit;
uint32 numWords;
bytes extraArgs;
}
function _argsToBytes(
ExtraArgsV1 memory extraArgs
) internal pure returns (bytes memory bts) {
return abi.encodeWithSelector(EXTRA_ARGS_V1_TAG, extraArgs);
}
}
lib/solady/src/auth/Ownable.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/// @notice Simple single owner authorization mixin.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/auth/Ownable.sol)
///
/// @dev Note:
/// This implementation does NOT auto-initialize the owner to `msg.sender`.
/// You MUST call the `_initializeOwner` in the constructor / initializer.
///
/// While the ownable portion follows
/// [EIP-173](https://eips.ethereum.org/EIPS/eip-173) for compatibility,
/// the nomenclature for the 2-step ownership handover may be unique to this codebase.
abstract contract Ownable {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CUSTOM ERRORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The caller is not authorized to call the function.
error Unauthorized();
/// @dev The `newOwner` cannot be the zero address.
error NewOwnerIsZeroAddress();
/// @dev The `pendingOwner` does not have a valid handover request.
error NoHandoverRequest();
/// @dev Cannot double-initialize.
error AlreadyInitialized();
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* EVENTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The ownership is transferred from `oldOwner` to `newOwner`.
/// This event is intentionally kept the same as OpenZeppelin's Ownable to be
/// compatible with indexers and [EIP-173](https://eips.ethereum.org/EIPS/eip-173),
/// despite it not being as lightweight as a single argument event.
event OwnershipTransferred(address indexed oldOwner, address indexed newOwner);
/// @dev An ownership handover to `pendingOwner` has been requested.
event OwnershipHandoverRequested(address indexed pendingOwner);
/// @dev The ownership handover to `pendingOwner` has been canceled.
event OwnershipHandoverCanceled(address indexed pendingOwner);
/// @dev `keccak256(bytes("OwnershipTransferred(address,address)"))`.
uint256 private constant _OWNERSHIP_TRANSFERRED_EVENT_SIGNATURE =
0x8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0;
/// @dev `keccak256(bytes("OwnershipHandoverRequested(address)"))`.
uint256 private constant _OWNERSHIP_HANDOVER_REQUESTED_EVENT_SIGNATURE =
0xdbf36a107da19e49527a7176a1babf963b4b0ff8cde35ee35d6cd8f1f9ac7e1d;
/// @dev `keccak256(bytes("OwnershipHandoverCanceled(address)"))`.
uint256 private constant _OWNERSHIP_HANDOVER_CANCELED_EVENT_SIGNATURE =
0xfa7b8eab7da67f412cc9575ed43464468f9bfbae89d1675917346ca6d8fe3c92;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* STORAGE */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The owner slot is given by:
/// `bytes32(~uint256(uint32(bytes4(keccak256("_OWNER_SLOT_NOT")))))`.
/// It is intentionally chosen to be a high value
/// to avoid collision with lower slots.
/// The choice of manual storage layout is to enable compatibility
/// with both regular and upgradeable contracts.
bytes32 internal constant _OWNER_SLOT =
0xffffffffffffffffffffffffffffffffffffffffffffffffffffffff74873927;
/// The ownership handover slot of `newOwner` is given by:
/// ```
/// mstore(0x00, or(shl(96, user), _HANDOVER_SLOT_SEED))
/// let handoverSlot := keccak256(0x00, 0x20)
/// ```
/// It stores the expiry timestamp of the two-step ownership handover.
uint256 private constant _HANDOVER_SLOT_SEED = 0x389a75e1;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INTERNAL FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Override to return true to make `_initializeOwner` prevent double-initialization.
function _guardInitializeOwner() internal pure virtual returns (bool guard) {}
/// @dev Initializes the owner directly without authorization guard.
/// This function must be called upon initialization,
/// regardless of whether the contract is upgradeable or not.
/// This is to enable generalization to both regular and upgradeable contracts,
/// and to save gas in case the initial owner is not the caller.
/// For performance reasons, this function will not check if there
/// is an existing owner.
function _initializeOwner(address newOwner) internal virtual {
if (_guardInitializeOwner()) {
/// @solidity memory-safe-assembly
assembly {
let ownerSlot := _OWNER_SLOT
if sload(ownerSlot) {
mstore(0x00, 0x0dc149f0) // `AlreadyInitialized()`.
revert(0x1c, 0x04)
}
// Clean the upper 96 bits.
newOwner := shr(96, shl(96, newOwner))
// Store the new value.
sstore(ownerSlot, or(newOwner, shl(255, iszero(newOwner))))
// Emit the {OwnershipTransferred} event.
log3(0, 0, _OWNERSHIP_TRANSFERRED_EVENT_SIGNATURE, 0, newOwner)
}
} else {
/// @solidity memory-safe-assembly
assembly {
// Clean the upper 96 bits.
newOwner := shr(96, shl(96, newOwner))
// Store the new value.
sstore(_OWNER_SLOT, newOwner)
// Emit the {OwnershipTransferred} event.
log3(0, 0, _OWNERSHIP_TRANSFERRED_EVENT_SIGNATURE, 0, newOwner)
}
}
}
/// @dev Sets the owner directly without authorization guard.
function _setOwner(address newOwner) internal virtual {
if (_guardInitializeOwner()) {
/// @solidity memory-safe-assembly
assembly {
let ownerSlot := _OWNER_SLOT
// Clean the upper 96 bits.
newOwner := shr(96, shl(96, newOwner))
// Emit the {OwnershipTransferred} event.
log3(0, 0, _OWNERSHIP_TRANSFERRED_EVENT_SIGNATURE, sload(ownerSlot), newOwner)
// Store the new value.
sstore(ownerSlot, or(newOwner, shl(255, iszero(newOwner))))
}
} else {
/// @solidity memory-safe-assembly
assembly {
let ownerSlot := _OWNER_SLOT
// Clean the upper 96 bits.
newOwner := shr(96, shl(96, newOwner))
// Emit the {OwnershipTransferred} event.
log3(0, 0, _OWNERSHIP_TRANSFERRED_EVENT_SIGNATURE, sload(ownerSlot), newOwner)
// Store the new value.
sstore(ownerSlot, newOwner)
}
}
}
/// @dev Throws if the sender is not the owner.
function _checkOwner() internal view virtual {
/// @solidity memory-safe-assembly
assembly {
// If the caller is not the stored owner, revert.
if iszero(eq(caller(), sload(_OWNER_SLOT))) {
mstore(0x00, 0x82b42900) // `Unauthorized()`.
revert(0x1c, 0x04)
}
}
}
/// @dev Returns how long a two-step ownership handover is valid for in seconds.
/// Override to return a different value if needed.
/// Made internal to conserve bytecode. Wrap it in a public function if needed.
function _ownershipHandoverValidFor() internal view virtual returns (uint64) {
return 48 * 3600;
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* PUBLIC UPDATE FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Allows the owner to transfer the ownership to `newOwner`.
function transferOwnership(address newOwner) public payable virtual onlyOwner {
/// @solidity memory-safe-assembly
assembly {
if iszero(shl(96, newOwner)) {
mstore(0x00, 0x7448fbae) // `NewOwnerIsZeroAddress()`.
revert(0x1c, 0x04)
}
}
_setOwner(newOwner);
}
/// @dev Allows the owner to renounce their ownership.
function renounceOwnership() public payable virtual onlyOwner {
_setOwner(address(0));
}
/// @dev Request a two-step ownership handover to the caller.
/// The request will automatically expire in 48 hours (172800 seconds) by default.
function requestOwnershipHandover() public payable virtual {
unchecked {
uint256 expires = block.timestamp + _ownershipHandoverValidFor();
/// @solidity memory-safe-assembly
assembly {
// Compute and set the handover slot to `expires`.
mstore(0x0c, _HANDOVER_SLOT_SEED)
mstore(0x00, caller())
sstore(keccak256(0x0c, 0x20), expires)
// Emit the {OwnershipHandoverRequested} event.
log2(0, 0, _OWNERSHIP_HANDOVER_REQUESTED_EVENT_SIGNATURE, caller())
}
}
}
/// @dev Cancels the two-step ownership handover to the caller, if any.
function cancelOwnershipHandover() public payable virtual {
/// @solidity memory-safe-assembly
assembly {
// Compute and set the handover slot to 0.
mstore(0x0c, _HANDOVER_SLOT_SEED)
mstore(0x00, caller())
sstore(keccak256(0x0c, 0x20), 0)
// Emit the {OwnershipHandoverCanceled} event.
log2(0, 0, _OWNERSHIP_HANDOVER_CANCELED_EVENT_SIGNATURE, caller())
}
}
/// @dev Allows the owner to complete the two-step ownership handover to `pendingOwner`.
/// Reverts if there is no existing ownership handover requested by `pendingOwner`.
function completeOwnershipHandover(address pendingOwner) public payable virtual onlyOwner {
/// @solidity memory-safe-assembly
assembly {
// Compute and set the handover slot to 0.
mstore(0x0c, _HANDOVER_SLOT_SEED)
mstore(0x00, pendingOwner)
let handoverSlot := keccak256(0x0c, 0x20)
// If the handover does not exist, or has expired.
if gt(timestamp(), sload(handoverSlot)) {
mstore(0x00, 0x6f5e8818) // `NoHandoverRequest()`.
revert(0x1c, 0x04)
}
// Set the handover slot to 0.
sstore(handoverSlot, 0)
}
_setOwner(pendingOwner);
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* PUBLIC READ FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Returns the owner of the contract.
function owner() public view virtual returns (address result) {
/// @solidity memory-safe-assembly
assembly {
result := sload(_OWNER_SLOT)
}
}
/// @dev Returns the expiry timestamp for the two-step ownership handover to `pendingOwner`.
function ownershipHandoverExpiresAt(address pendingOwner)
public
view
virtual
returns (uint256 result)
{
/// @solidity memory-safe-assembly
assembly {
// Compute the handover slot.
mstore(0x0c, _HANDOVER_SLOT_SEED)
mstore(0x00, pendingOwner)
// Load the handover slot.
result := sload(keccak256(0x0c, 0x20))
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* MODIFIERS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Marks a function as only callable by the owner.
modifier onlyOwner() virtual {
_checkOwner();
_;
}
}
lib/solady/src/tokens/ERC721.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/// @notice Simple ERC721 implementation with storage hitchhiking.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/tokens/ERC721.sol)
/// @author Modified from Solmate (https://github.com/transmissions11/solmate/blob/main/src/tokens/ERC721.sol)
/// @author Modified from OpenZeppelin (https://github.com/OpenZeppelin/openzeppelin-contracts/tree/master/contracts/token/ERC721/ERC721.sol)
///
/// @dev Note:
/// - The ERC721 standard allows for self-approvals.
/// For performance, this implementation WILL NOT revert for such actions.
/// Please add any checks with overrides if desired.
/// - For performance, methods are made payable where permitted by the ERC721 standard.
/// - The `safeTransfer` functions use the identity precompile (0x4)
/// to copy memory internally.
///
/// If you are overriding:
/// - NEVER violate the ERC721 invariant:
/// the balance of an owner MUST always be equal to their number of ownership slots.
/// The transfer functions do not have an underflow guard for user token balances.
/// - Make sure all variables written to storage are properly cleaned
/// (e.g. the bool value for `isApprovedForAll` MUST be either 1 or 0 under the hood).
/// - Check that the overridden function is actually used in the function you want to
/// change the behavior of. Much of the code has been manually inlined for performance.
abstract contract ERC721 {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CONSTANTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev An account can hold up to 4294967295 tokens.
uint256 internal constant _MAX_ACCOUNT_BALANCE = 0xffffffff;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CUSTOM ERRORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Only the token owner or an approved account can manage the token.
error NotOwnerNorApproved();
/// @dev The token does not exist.
error TokenDoesNotExist();
/// @dev The token already exists.
error TokenAlreadyExists();
/// @dev Cannot query the balance for the zero address.
error BalanceQueryForZeroAddress();
/// @dev Cannot mint or transfer to the zero address.
error TransferToZeroAddress();
/// @dev The token must be owned by `from`.
error TransferFromIncorrectOwner();
/// @dev The recipient's balance has overflowed.
error AccountBalanceOverflow();
/// @dev Cannot safely transfer to a contract that does not implement
/// the ERC721Receiver interface.
error TransferToNonERC721ReceiverImplementer();
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* EVENTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Emitted when token `id` is transferred from `from` to `to`.
event Transfer(address indexed from, address indexed to, uint256 indexed id);
/// @dev Emitted when `owner` enables `account` to manage the `id` token.
event Approval(address indexed owner, address indexed account, uint256 indexed id);
/// @dev Emitted when `owner` enables or disables `operator` to manage all of their tokens.
event ApprovalForAll(address indexed owner, address indexed operator, bool isApproved);
/// @dev `keccak256(bytes("Transfer(address,address,uint256)"))`.
uint256 private constant _TRANSFER_EVENT_SIGNATURE =
0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef;
/// @dev `keccak256(bytes("Approval(address,address,uint256)"))`.
uint256 private constant _APPROVAL_EVENT_SIGNATURE =
0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925;
/// @dev `keccak256(bytes("ApprovalForAll(address,address,bool)"))`.
uint256 private constant _APPROVAL_FOR_ALL_EVENT_SIGNATURE =
0x17307eab39ab6107e8899845ad3d59bd9653f200f220920489ca2b5937696c31;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* STORAGE */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The ownership data slot of `id` is given by:
/// ```
/// mstore(0x00, id)
/// mstore(0x1c, _ERC721_MASTER_SLOT_SEED)
/// let ownershipSlot := add(id, add(id, keccak256(0x00, 0x20)))
/// ```
/// Bits Layout:
/// - [0..159] `addr`
/// - [160..255] `extraData`
///
/// The approved address slot is given by: `add(1, ownershipSlot)`.
///
/// See: https://notes.ethereum.org/%40vbuterin/verkle_tree_eip
///
/// The balance slot of `owner` is given by:
/// ```
/// mstore(0x1c, _ERC721_MASTER_SLOT_SEED)
/// mstore(0x00, owner)
/// let balanceSlot := keccak256(0x0c, 0x1c)
/// ```
/// Bits Layout:
/// - [0..31] `balance`
/// - [32..255] `aux`
///
/// The `operator` approval slot of `owner` is given by:
/// ```
/// mstore(0x1c, or(_ERC721_MASTER_SLOT_SEED, operator))
/// mstore(0x00, owner)
/// let operatorApprovalSlot := keccak256(0x0c, 0x30)
/// ```
uint256 private constant _ERC721_MASTER_SLOT_SEED = 0x7d8825530a5a2e7a << 192;
/// @dev Pre-shifted and pre-masked constant.
uint256 private constant _ERC721_MASTER_SLOT_SEED_MASKED = 0x0a5a2e7a00000000;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* ERC721 METADATA */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Returns the token collection name.
function name() public view virtual returns (string memory);
/// @dev Returns the token collection symbol.
function symbol() public view virtual returns (string memory);
/// @dev Returns the Uniform Resource Identifier (URI) for token `id`.
function tokenURI(uint256 id) public view virtual returns (string memory);
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* ERC721 */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Returns the owner of token `id`.
///
/// Requirements:
/// - Token `id` must exist.
function ownerOf(uint256 id) public view virtual returns (address result) {
result = _ownerOf(id);
/// @solidity memory-safe-assembly
assembly {
if iszero(result) {
mstore(0x00, 0xceea21b6) // `TokenDoesNotExist()`.
revert(0x1c, 0x04)
}
}
}
/// @dev Returns the number of tokens owned by `owner`.
///
/// Requirements:
/// - `owner` must not be the zero address.
function balanceOf(address owner) public view virtual returns (uint256 result) {
/// @solidity memory-safe-assembly
assembly {
// Revert if the `owner` is the zero address.
if iszero(owner) {
mstore(0x00, 0x8f4eb604) // `BalanceQueryForZeroAddress()`.
revert(0x1c, 0x04)
}
mstore(0x1c, _ERC721_MASTER_SLOT_SEED)
mstore(0x00, owner)
result := and(sload(keccak256(0x0c, 0x1c)), _MAX_ACCOUNT_BALANCE)
}
}
/// @dev Returns the account approved to manage token `id`.
///
/// Requirements:
/// - Token `id` must exist.
function getApproved(uint256 id) public view virtual returns (address result) {
/// @solidity memory-safe-assembly
assembly {
mstore(0x00, id)
mstore(0x1c, _ERC721_MASTER_SLOT_SEED)
let ownershipSlot := add(id, add(id, keccak256(0x00, 0x20)))
if iszero(shl(96, sload(ownershipSlot))) {
mstore(0x00, 0xceea21b6) // `TokenDoesNotExist()`.
revert(0x1c, 0x04)
}
result := sload(add(1, ownershipSlot))
}
}
/// @dev Sets `account` as the approved account to manage token `id`.
///
/// Requirements:
/// - Token `id` must exist.
/// - The caller must be the owner of the token,
/// or an approved operator for the token owner.
///
/// Emits an {Approval} event.
function approve(address account, uint256 id) public payable virtual {
_approve(msg.sender, account, id);
}
/// @dev Returns whether `operator` is approved to manage the tokens of `owner`.
function isApprovedForAll(address owner, address operator)
public
view
virtual
returns (bool result)
{
/// @solidity memory-safe-assembly
assembly {
mstore(0x1c, operator)
mstore(0x08, _ERC721_MASTER_SLOT_SEED_MASKED)
mstore(0x00, owner)
result := sload(keccak256(0x0c, 0x30))
}
}
/// @dev Sets whether `operator` is approved to manage the tokens of the caller.
///
/// Emits an {ApprovalForAll} event.
function setApprovalForAll(address operator, bool isApproved) public virtual {
/// @solidity memory-safe-assembly
assembly {
// Convert to 0 or 1.
isApproved := iszero(iszero(isApproved))
// Update the `isApproved` for (`msg.sender`, `operator`).
mstore(0x1c, operator)
mstore(0x08, _ERC721_MASTER_SLOT_SEED_MASKED)
mstore(0x00, caller())
sstore(keccak256(0x0c, 0x30), isApproved)
// Emit the {ApprovalForAll} event.
mstore(0x00, isApproved)
// forgefmt: disable-next-item
log3(0x00, 0x20, _APPROVAL_FOR_ALL_EVENT_SIGNATURE, caller(), shr(96, shl(96, operator)))
}
}
/// @dev Transfers token `id` from `from` to `to`.
///
/// Requirements:
///
/// - Token `id` must exist.
/// - `from` must be the owner of the token.
/// - `to` cannot be the zero address.
/// - The caller must be the owner of the token, or be approved to manage the token.
///
/// Emits a {Transfer} event.
function transferFrom(address from, address to, uint256 id) public payable virtual {
_beforeTokenTransfer(from, to, id);
/// @solidity memory-safe-assembly
assembly {
// Clear the upper 96 bits.
let bitmaskAddress := shr(96, not(0))
from := and(bitmaskAddress, from)
to := and(bitmaskAddress, to)
// Load the ownership data.
mstore(0x00, id)
mstore(0x1c, or(_ERC721_MASTER_SLOT_SEED, caller()))
let ownershipSlot := add(id, add(id, keccak256(0x00, 0x20)))
let ownershipPacked := sload(ownershipSlot)
let owner := and(bitmaskAddress, ownershipPacked)
// Revert if the token does not exist, or if `from` is not the owner.
if iszero(mul(owner, eq(owner, from))) {
// `TokenDoesNotExist()`, `TransferFromIncorrectOwner()`.
mstore(shl(2, iszero(owner)), 0xceea21b6a1148100)
revert(0x1c, 0x04)
}
// Load, check, and update the token approval.
{
mstore(0x00, from)
let approvedAddress := sload(add(1, ownershipSlot))
// Revert if the caller is not the owner, nor approved.
if iszero(or(eq(caller(), from), eq(caller(), approvedAddress))) {
if iszero(sload(keccak256(0x0c, 0x30))) {
mstore(0x00, 0x4b6e7f18) // `NotOwnerNorApproved()`.
revert(0x1c, 0x04)
}
}
// Delete the approved address if any.
if approvedAddress { sstore(add(1, ownershipSlot), 0) }
}
// Update with the new owner.
sstore(ownershipSlot, xor(ownershipPacked, xor(from, to)))
// Decrement the balance of `from`.
{
let fromBalanceSlot := keccak256(0x0c, 0x1c)
sstore(fromBalanceSlot, sub(sload(fromBalanceSlot), 1))
}
// Increment the balance of `to`.
{
mstore(0x00, to)
let toBalanceSlot := keccak256(0x0c, 0x1c)
let toBalanceSlotPacked := add(sload(toBalanceSlot), 1)
// Revert if `to` is the zero address, or if the account balance overflows.
if iszero(mul(to, and(toBalanceSlotPacked, _MAX_ACCOUNT_BALANCE))) {
// `TransferToZeroAddress()`, `AccountBalanceOverflow()`.
mstore(shl(2, iszero(to)), 0xea553b3401336cea)
revert(0x1c, 0x04)
}
sstore(toBalanceSlot, toBalanceSlotPacked)
}
// Emit the {Transfer} event.
log4(codesize(), 0x00, _TRANSFER_EVENT_SIGNATURE, from, to, id)
}
_afterTokenTransfer(from, to, id);
}
/// @dev Equivalent to `safeTransferFrom(from, to, id, "")`.
function safeTransferFrom(address from, address to, uint256 id) public payable virtual {
transferFrom(from, to, id);
if (_hasCode(to)) _checkOnERC721Received(from, to, id, "");
}
/// @dev Transfers token `id` from `from` to `to`.
///
/// Requirements:
///
/// - Token `id` must exist.
/// - `from` must be the owner of the token.
/// - `to` cannot be the zero address.
/// - The caller must be the owner of the token, or be approved to manage the token.
/// - If `to` refers to a smart contract, it must implement
/// {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
///
/// Emits a {Transfer} event.
function safeTransferFrom(address from, address to, uint256 id, bytes calldata data)
public
payable
virtual
{
transferFrom(from, to, id);
if (_hasCode(to)) _checkOnERC721Received(from, to, id, data);
}
/// @dev Returns true if this contract implements the interface defined by `interfaceId`.
/// See: https://eips.ethereum.org/EIPS/eip-165
/// This function call must use less than 30000 gas.
function supportsInterface(bytes4 interfaceId) public view virtual returns (bool result) {
/// @solidity memory-safe-assembly
assembly {
let s := shr(224, interfaceId)
// ERC165: 0x01ffc9a7, ERC721: 0x80ac58cd, ERC721Metadata: 0x5b5e139f.
result := or(or(eq(s, 0x01ffc9a7), eq(s, 0x80ac58cd)), eq(s, 0x5b5e139f))
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INTERNAL QUERY FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Returns if token `id` exists.
function _exists(uint256 id) internal view virtual returns (bool result) {
/// @solidity memory-safe-assembly
assembly {
mstore(0x00, id)
mstore(0x1c, _ERC721_MASTER_SLOT_SEED)
result := iszero(iszero(shl(96, sload(add(id, add(id, keccak256(0x00, 0x20)))))))
}
}
/// @dev Returns the owner of token `id`.
/// Returns the zero address instead of reverting if the token does not exist.
function _ownerOf(uint256 id) internal view virtual returns (address result) {
/// @solidity memory-safe-assembly
assembly {
mstore(0x00, id)
mstore(0x1c, _ERC721_MASTER_SLOT_SEED)
result := shr(96, shl(96, sload(add(id, add(id, keccak256(0x00, 0x20))))))
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INTERNAL DATA HITCHHIKING FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
// For performance, no events are emitted for the hitchhiking setters.
// Please emit your own events if required.
/// @dev Returns the auxiliary data for `owner`.
/// Minting, transferring, burning the tokens of `owner` will not change the auxiliary data.
/// Auxiliary data can be set for any address, even if it does not have any tokens.
function _getAux(address owner) internal view virtual returns (uint224 result) {
/// @solidity memory-safe-assembly
assembly {
mstore(0x1c, _ERC721_MASTER_SLOT_SEED)
mstore(0x00, owner)
result := shr(32, sload(keccak256(0x0c, 0x1c)))
}
}
/// @dev Set the auxiliary data for `owner` to `value`.
/// Minting, transferring, burning the tokens of `owner` will not change the auxiliary data.
/// Auxiliary data can be set for any address, even if it does not have any tokens.
function _setAux(address owner, uint224 value) internal virtual {
/// @solidity memory-safe-assembly
assembly {
mstore(0x1c, _ERC721_MASTER_SLOT_SEED)
mstore(0x00, owner)
let balanceSlot := keccak256(0x0c, 0x1c)
let packed := sload(balanceSlot)
sstore(balanceSlot, xor(packed, shl(32, xor(value, shr(32, packed)))))
}
}
/// @dev Returns the extra data for token `id`.
/// Minting, transferring, burning a token will not change the extra data.
/// The extra data can be set on a non-existent token.
function _getExtraData(uint256 id) internal view virtual returns (uint96 result) {
/// @solidity memory-safe-assembly
assembly {
mstore(0x00, id)
mstore(0x1c, _ERC721_MASTER_SLOT_SEED)
result := shr(160, sload(add(id, add(id, keccak256(0x00, 0x20)))))
}
}
/// @dev Sets the extra data for token `id` to `value`.
/// Minting, transferring, burning a token will not change the extra data.
/// The extra data can be set on a non-existent token.
function _setExtraData(uint256 id, uint96 value) internal virtual {
/// @solidity memory-safe-assembly
assembly {
mstore(0x00, id)
mstore(0x1c, _ERC721_MASTER_SLOT_SEED)
let ownershipSlot := add(id, add(id, keccak256(0x00, 0x20)))
let packed := sload(ownershipSlot)
sstore(ownershipSlot, xor(packed, shl(160, xor(value, shr(160, packed)))))
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INTERNAL MINT FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Mints token `id` to `to`.
///
/// Requirements:
///
/// - Token `id` must not exist.
/// - `to` cannot be the zero address.
///
/// Emits a {Transfer} event.
function _mint(address to, uint256 id) internal virtual {
_beforeTokenTransfer(address(0), to, id);
/// @solidity memory-safe-assembly
assembly {
// Clear the upper 96 bits.
to := shr(96, shl(96, to))
// Load the ownership data.
mstore(0x00, id)
mstore(0x1c, _ERC721_MASTER_SLOT_SEED)
let ownershipSlot := add(id, add(id, keccak256(0x00, 0x20)))
let ownershipPacked := sload(ownershipSlot)
// Revert if the token already exists.
if shl(96, ownershipPacked) {
mstore(0x00, 0xc991cbb1) // `TokenAlreadyExists()`.
revert(0x1c, 0x04)
}
// Update with the owner.
sstore(ownershipSlot, or(ownershipPacked, to))
// Increment the balance of the owner.
{
mstore(0x00, to)
let balanceSlot := keccak256(0x0c, 0x1c)
let balanceSlotPacked := add(sload(balanceSlot), 1)
// Revert if `to` is the zero address, or if the account balance overflows.
if iszero(mul(to, and(balanceSlotPacked, _MAX_ACCOUNT_BALANCE))) {
// `TransferToZeroAddress()`, `AccountBalanceOverflow()`.
mstore(shl(2, iszero(to)), 0xea553b3401336cea)
revert(0x1c, 0x04)
}
sstore(balanceSlot, balanceSlotPacked)
}
// Emit the {Transfer} event.
log4(codesize(), 0x00, _TRANSFER_EVENT_SIGNATURE, 0, to, id)
}
_afterTokenTransfer(address(0), to, id);
}
/// @dev Mints token `id` to `to`, and updates the extra data for token `id` to `value`.
/// Does NOT check if token `id` already exists (assumes `id` is auto-incrementing).
///
/// Requirements:
///
/// - `to` cannot be the zero address.
///
/// Emits a {Transfer} event.
function _mintAndSetExtraDataUnchecked(address to, uint256 id, uint96 value) internal virtual {
_beforeTokenTransfer(address(0), to, id);
/// @solidity memory-safe-assembly
assembly {
// Clear the upper 96 bits.
to := shr(96, shl(96, to))
// Update with the owner and extra data.
mstore(0x00, id)
mstore(0x1c, _ERC721_MASTER_SLOT_SEED)
sstore(add(id, add(id, keccak256(0x00, 0x20))), or(shl(160, value), to))
// Increment the balance of the owner.
{
mstore(0x00, to)
let balanceSlot := keccak256(0x0c, 0x1c)
let balanceSlotPacked := add(sload(balanceSlot), 1)
// Revert if `to` is the zero address, or if the account balance overflows.
if iszero(mul(to, and(balanceSlotPacked, _MAX_ACCOUNT_BALANCE))) {
// `TransferToZeroAddress()`, `AccountBalanceOverflow()`.
mstore(shl(2, iszero(to)), 0xea553b3401336cea)
revert(0x1c, 0x04)
}
sstore(balanceSlot, balanceSlotPacked)
}
// Emit the {Transfer} event.
log4(codesize(), 0x00, _TRANSFER_EVENT_SIGNATURE, 0, to, id)
}
_afterTokenTransfer(address(0), to, id);
}
/// @dev Equivalent to `_safeMint(to, id, "")`.
function _safeMint(address to, uint256 id) internal virtual {
_safeMint(to, id, "");
}
/// @dev Mints token `id` to `to`.
///
/// Requirements:
///
/// - Token `id` must not exist.
/// - `to` cannot be the zero address.
/// - If `to` refers to a smart contract, it must implement
/// {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
///
/// Emits a {Transfer} event.
function _safeMint(address to, uint256 id, bytes memory data) internal virtual {
_mint(to, id);
if (_hasCode(to)) _checkOnERC721Received(address(0), to, id, data);
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INTERNAL BURN FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Equivalent to `_burn(address(0), id)`.
function _burn(uint256 id) internal virtual {
_burn(address(0), id);
}
/// @dev Destroys token `id`, using `by`.
///
/// Requirements:
///
/// - Token `id` must exist.
/// - If `by` is not the zero address,
/// it must be the owner of the token, or be approved to manage the token.
///
/// Emits a {Transfer} event.
function _burn(address by, uint256 id) internal virtual {
address owner = ownerOf(id);
_beforeTokenTransfer(owner, address(0), id);
/// @solidity memory-safe-assembly
assembly {
// Clear the upper 96 bits.
by := shr(96, shl(96, by))
// Load the ownership data.
mstore(0x00, id)
mstore(0x1c, or(_ERC721_MASTER_SLOT_SEED, by))
let ownershipSlot := add(id, add(id, keccak256(0x00, 0x20)))
let ownershipPacked := sload(ownershipSlot)
// Reload the owner in case it is changed in `_beforeTokenTransfer`.
owner := shr(96, shl(96, ownershipPacked))
// Revert if the token does not exist.
if iszero(owner) {
mstore(0x00, 0xceea21b6) // `TokenDoesNotExist()`.
revert(0x1c, 0x04)
}
// Load and check the token approval.
{
mstore(0x00, owner)
let approvedAddress := sload(add(1, ownershipSlot))
// If `by` is not the zero address, do the authorization check.
// Revert if the `by` is not the owner, nor approved.
if iszero(or(iszero(by), or(eq(by, owner), eq(by, approvedAddress)))) {
if iszero(sload(keccak256(0x0c, 0x30))) {
mstore(0x00, 0x4b6e7f18) // `NotOwnerNorApproved()`.
revert(0x1c, 0x04)
}
}
// Delete the approved address if any.
if approvedAddress { sstore(add(1, ownershipSlot), 0) }
}
// Clear the owner.
sstore(ownershipSlot, xor(ownershipPacked, owner))
// Decrement the balance of `owner`.
{
let balanceSlot := keccak256(0x0c, 0x1c)
sstore(balanceSlot, sub(sload(balanceSlot), 1))
}
// Emit the {Transfer} event.
log4(codesize(), 0x00, _TRANSFER_EVENT_SIGNATURE, owner, 0, id)
}
_afterTokenTransfer(owner, address(0), id);
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INTERNAL APPROVAL FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Returns whether `account` is the owner of token `id`, or is approved to manage it.
///
/// Requirements:
/// - Token `id` must exist.
function _isApprovedOrOwner(address account, uint256 id)
internal
view
virtual
returns (bool result)
{
/// @solidity memory-safe-assembly
assembly {
result := 1
// Clear the upper 96 bits.
account := shr(96, shl(96, account))
// Load the ownership data.
mstore(0x00, id)
mstore(0x1c, or(_ERC721_MASTER_SLOT_SEED, account))
let ownershipSlot := add(id, add(id, keccak256(0x00, 0x20)))
let owner := shr(96, shl(96, sload(ownershipSlot)))
// Revert if the token does not exist.
if iszero(owner) {
mstore(0x00, 0xceea21b6) // `TokenDoesNotExist()`.
revert(0x1c, 0x04)
}
// Check if `account` is the `owner`.
if iszero(eq(account, owner)) {
mstore(0x00, owner)
// Check if `account` is approved to manage the token.
if iszero(sload(keccak256(0x0c, 0x30))) {
result := eq(account, sload(add(1, ownershipSlot)))
}
}
}
}
/// @dev Returns the account approved to manage token `id`.
/// Returns the zero address instead of reverting if the token does not exist.
function _getApproved(uint256 id) internal view virtual returns (address result) {
/// @solidity memory-safe-assembly
assembly {
mstore(0x00, id)
mstore(0x1c, _ERC721_MASTER_SLOT_SEED)
result := sload(add(1, add(id, add(id, keccak256(0x00, 0x20)))))
}
}
/// @dev Equivalent to `_approve(address(0), account, id)`.
function _approve(address account, uint256 id) internal virtual {
_approve(address(0), account, id);
}
/// @dev Sets `account` as the approved account to manage token `id`, using `by`.
///
/// Requirements:
/// - Token `id` must exist.
/// - If `by` is not the zero address, `by` must be the owner
/// or an approved operator for the token owner.
///
/// Emits a {Approval} event.
function _approve(address by, address account, uint256 id) internal virtual {
/// @solidity memory-safe-assembly
assembly {
// Clear the upper 96 bits.
let bitmaskAddress := shr(96, not(0))
account := and(bitmaskAddress, account)
by := and(bitmaskAddress, by)
// Load the owner of the token.
mstore(0x00, id)
mstore(0x1c, or(_ERC721_MASTER_SLOT_SEED, by))
let ownershipSlot := add(id, add(id, keccak256(0x00, 0x20)))
let owner := and(bitmaskAddress, sload(ownershipSlot))
// Revert if the token does not exist.
if iszero(owner) {
mstore(0x00, 0xceea21b6) // `TokenDoesNotExist()`.
revert(0x1c, 0x04)
}
// If `by` is not the zero address, do the authorization check.
// Revert if `by` is not the owner, nor approved.
if iszero(or(iszero(by), eq(by, owner))) {
mstore(0x00, owner)
if iszero(sload(keccak256(0x0c, 0x30))) {
mstore(0x00, 0x4b6e7f18) // `NotOwnerNorApproved()`.
revert(0x1c, 0x04)
}
}
// Sets `account` as the approved account to manage `id`.
sstore(add(1, ownershipSlot), account)
// Emit the {Approval} event.
log4(codesize(), 0x00, _APPROVAL_EVENT_SIGNATURE, owner, account, id)
}
}
/// @dev Approve or remove the `operator` as an operator for `by`,
/// without authorization checks.
///
/// Emits an {ApprovalForAll} event.
function _setApprovalForAll(address by, address operator, bool isApproved) internal virtual {
/// @solidity memory-safe-assembly
assembly {
// Clear the upper 96 bits.
by := shr(96, shl(96, by))
operator := shr(96, shl(96, operator))
// Convert to 0 or 1.
isApproved := iszero(iszero(isApproved))
// Update the `isApproved` for (`by`, `operator`).
mstore(0x1c, or(_ERC721_MASTER_SLOT_SEED, operator))
mstore(0x00, by)
sstore(keccak256(0x0c, 0x30), isApproved)
// Emit the {ApprovalForAll} event.
mstore(0x00, isApproved)
log3(0x00, 0x20, _APPROVAL_FOR_ALL_EVENT_SIGNATURE, by, operator)
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INTERNAL TRANSFER FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Equivalent to `_transfer(address(0), from, to, id)`.
function _transfer(address from, address to, uint256 id) internal virtual {
_transfer(address(0), from, to, id);
}
/// @dev Transfers token `id` from `from` to `to`.
///
/// Requirements:
///
/// - Token `id` must exist.
/// - `from` must be the owner of the token.
/// - `to` cannot be the zero address.
/// - If `by` is not the zero address,
/// it must be the owner of the token, or be approved to manage the token.
///
/// Emits a {Transfer} event.
function _transfer(address by, address from, address to, uint256 id) internal virtual {
_beforeTokenTransfer(from, to, id);
/// @solidity memory-safe-assembly
assembly {
// Clear the upper 96 bits.
let bitmaskAddress := shr(96, not(0))
from := and(bitmaskAddress, from)
to := and(bitmaskAddress, to)
by := and(bitmaskAddress, by)
// Load the ownership data.
mstore(0x00, id)
mstore(0x1c, or(_ERC721_MASTER_SLOT_SEED, by))
let ownershipSlot := add(id, add(id, keccak256(0x00, 0x20)))
let ownershipPacked := sload(ownershipSlot)
let owner := and(bitmaskAddress, ownershipPacked)
// Revert if the token does not exist, or if `from` is not the owner.
if iszero(mul(owner, eq(owner, from))) {
// `TokenDoesNotExist()`, `TransferFromIncorrectOwner()`.
mstore(shl(2, iszero(owner)), 0xceea21b6a1148100)
revert(0x1c, 0x04)
}
// Load, check, and update the token approval.
{
mstore(0x00, from)
let approvedAddress := sload(add(1, ownershipSlot))
// If `by` is not the zero address, do the authorization check.
// Revert if the `by` is not the owner, nor approved.
if iszero(or(iszero(by), or(eq(by, from), eq(by, approvedAddress)))) {
if iszero(sload(keccak256(0x0c, 0x30))) {
mstore(0x00, 0x4b6e7f18) // `NotOwnerNorApproved()`.
revert(0x1c, 0x04)
}
}
// Delete the approved address if any.
if approvedAddress { sstore(add(1, ownershipSlot), 0) }
}
// Update with the new owner.
sstore(ownershipSlot, xor(ownershipPacked, xor(from, to)))
// Decrement the balance of `from`.
{
let fromBalanceSlot := keccak256(0x0c, 0x1c)
sstore(fromBalanceSlot, sub(sload(fromBalanceSlot), 1))
}
// Increment the balance of `to`.
{
mstore(0x00, to)
let toBalanceSlot := keccak256(0x0c, 0x1c)
let toBalanceSlotPacked := add(sload(toBalanceSlot), 1)
// Revert if `to` is the zero address, or if the account balance overflows.
if iszero(mul(to, and(toBalanceSlotPacked, _MAX_ACCOUNT_BALANCE))) {
// `TransferToZeroAddress()`, `AccountBalanceOverflow()`.
mstore(shl(2, iszero(to)), 0xea553b3401336cea)
revert(0x1c, 0x04)
}
sstore(toBalanceSlot, toBalanceSlotPacked)
}
// Emit the {Transfer} event.
log4(codesize(), 0x00, _TRANSFER_EVENT_SIGNATURE, from, to, id)
}
_afterTokenTransfer(from, to, id);
}
/// @dev Equivalent to `_safeTransfer(from, to, id, "")`.
function _safeTransfer(address from, address to, uint256 id) internal virtual {
_safeTransfer(from, to, id, "");
}
/// @dev Transfers token `id` from `from` to `to`.
///
/// Requirements:
///
/// - Token `id` must exist.
/// - `from` must be the owner of the token.
/// - `to` cannot be the zero address.
/// - The caller must be the owner of the token, or be approved to manage the token.
/// - If `to` refers to a smart contract, it must implement
/// {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
///
/// Emits a {Transfer} event.
function _safeTransfer(address from, address to, uint256 id, bytes memory data)
internal
virtual
{
_transfer(address(0), from, to, id);
if (_hasCode(to)) _checkOnERC721Received(from, to, id, data);
}
/// @dev Equivalent to `_safeTransfer(by, from, to, id, "")`.
function _safeTransfer(address by, address from, address to, uint256 id) internal virtual {
_safeTransfer(by, from, to, id, "");
}
/// @dev Transfers token `id` from `from` to `to`.
///
/// Requirements:
///
/// - Token `id` must exist.
/// - `from` must be the owner of the token.
/// - `to` cannot be the zero address.
/// - If `by` is not the zero address,
/// it must be the owner of the token, or be approved to manage the token.
/// - If `to` refers to a smart contract, it must implement
/// {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
///
/// Emits a {Transfer} event.
function _safeTransfer(address by, address from, address to, uint256 id, bytes memory data)
internal
virtual
{
_transfer(by, from, to, id);
if (_hasCode(to)) _checkOnERC721Received(from, to, id, data);
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* HOOKS FOR OVERRIDING */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Hook that is called before any token transfers, including minting and burning.
function _beforeTokenTransfer(address from, address to, uint256 id) internal virtual {}
/// @dev Hook that is called after any token transfers, including minting and burning.
function _afterTokenTransfer(address from, address to, uint256 id) internal virtual {}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* PRIVATE HELPERS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Returns if `a` has bytecode of non-zero length.
function _hasCode(address a) private view returns (bool result) {
/// @solidity memory-safe-assembly
assembly {
result := extcodesize(a) // Can handle dirty upper bits.
}
}
/// @dev Perform a call to invoke {IERC721Receiver-onERC721Received} on `to`.
/// Reverts if the target does not support the function correctly.
function _checkOnERC721Received(address from, address to, uint256 id, bytes memory data)
private
{
/// @solidity memory-safe-assembly
assembly {
// Prepare the calldata.
let m := mload(0x40)
let onERC721ReceivedSelector := 0x150b7a02
mstore(m, onERC721ReceivedSelector)
mstore(add(m, 0x20), caller()) // The `operator`, which is always `msg.sender`.
mstore(add(m, 0x40), shr(96, shl(96, from)))
mstore(add(m, 0x60), id)
mstore(add(m, 0x80), 0x80)
let n := mload(data)
mstore(add(m, 0xa0), n)
if n { pop(staticcall(gas(), 4, add(data, 0x20), n, add(m, 0xc0), n)) }
// Revert if the call reverts.
if iszero(call(gas(), to, 0, add(m, 0x1c), add(n, 0xa4), m, 0x20)) {
if returndatasize() {
// Bubble up the revert if the call reverts.
returndatacopy(m, 0x00, returndatasize())
revert(m, returndatasize())
}
}
// Load the returndata and compare it.
if iszero(eq(mload(m), shl(224, onERC721ReceivedSelector))) {
mstore(0x00, 0xd1a57ed6) // `TransferToNonERC721ReceiverImplementer()`.
revert(0x1c, 0x04)
}
}
}
}
lib/solady/src/utils/ReentrancyGuard.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/// @notice Reentrancy guard mixin.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/ReentrancyGuard.sol)
abstract contract ReentrancyGuard {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CUSTOM ERRORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Unauthorized reentrant call.
error Reentrancy();
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* STORAGE */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Equivalent to: `uint72(bytes9(keccak256("_REENTRANCY_GUARD_SLOT")))`.
/// 9 bytes is large enough to avoid collisions with lower slots,
/// but not too large to result in excessive bytecode bloat.
uint256 private constant _REENTRANCY_GUARD_SLOT = 0x929eee149b4bd21268;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* REENTRANCY GUARD */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Guards a function from reentrancy.
modifier nonReentrant() virtual {
/// @solidity memory-safe-assembly
assembly {
if eq(sload(_REENTRANCY_GUARD_SLOT), address()) {
mstore(0x00, 0xab143c06) // `Reentrancy()`.
revert(0x1c, 0x04)
}
sstore(_REENTRANCY_GUARD_SLOT, address())
}
_;
/// @solidity memory-safe-assembly
assembly {
sstore(_REENTRANCY_GUARD_SLOT, codesize())
}
}
/// @dev Guards a view function from read-only reentrancy.
modifier nonReadReentrant() virtual {
/// @solidity memory-safe-assembly
assembly {
if eq(sload(_REENTRANCY_GUARD_SLOT), address()) {
mstore(0x00, 0xab143c06) // `Reentrancy()`.
revert(0x1c, 0x04)
}
}
_;
}
}
lib/solady/src/utils/SafeTransferLib.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/// @notice Safe ETH and ERC20 transfer library that gracefully handles missing return values.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/SafeTransferLib.sol)
/// @author Modified from Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/SafeTransferLib.sol)
/// @author Permit2 operations from (https://github.com/Uniswap/permit2/blob/main/src/libraries/Permit2Lib.sol)
///
/// @dev Note:
/// - For ETH transfers, please use `forceSafeTransferETH` for DoS protection.
library SafeTransferLib {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CUSTOM ERRORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The ETH transfer has failed.
error ETHTransferFailed();
/// @dev The ERC20 `transferFrom` has failed.
error TransferFromFailed();
/// @dev The ERC20 `transfer` has failed.
error TransferFailed();
/// @dev The ERC20 `approve` has failed.
error ApproveFailed();
/// @dev The ERC20 `totalSupply` query has failed.
error TotalSupplyQueryFailed();
/// @dev The Permit2 operation has failed.
error Permit2Failed();
/// @dev The Permit2 amount must be less than `2**160 - 1`.
error Permit2AmountOverflow();
/// @dev The Permit2 approve operation has failed.
error Permit2ApproveFailed();
/// @dev The Permit2 lockdown operation has failed.
error Permit2LockdownFailed();
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CONSTANTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Suggested gas stipend for contract receiving ETH that disallows any storage writes.
uint256 internal constant GAS_STIPEND_NO_STORAGE_WRITES = 2300;
/// @dev Suggested gas stipend for contract receiving ETH to perform a few
/// storage reads and writes, but low enough to prevent griefing.
uint256 internal constant GAS_STIPEND_NO_GRIEF = 100000;
/// @dev The unique EIP-712 domain separator for the DAI token contract.
bytes32 internal constant DAI_DOMAIN_SEPARATOR =
0xdbb8cf42e1ecb028be3f3dbc922e1d878b963f411dc388ced501601c60f7c6f7;
/// @dev The address for the WETH9 contract on Ethereum mainnet.
address internal constant WETH9 = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
/// @dev The canonical Permit2 address.
/// [Github](https://github.com/Uniswap/permit2)
/// [Etherscan](https://etherscan.io/address/0x000000000022D473030F116dDEE9F6B43aC78BA3)
address internal constant PERMIT2 = 0x000000000022D473030F116dDEE9F6B43aC78BA3;
/// @dev The canonical address of the `SELFDESTRUCT` ETH mover.
/// See: https://gist.github.com/Vectorized/1cb8ad4cf393b1378e08f23f79bd99fa
/// [Etherscan](https://etherscan.io/address/0x00000000000073c48c8055bD43D1A53799176f0D)
address internal constant ETH_MOVER = 0x00000000000073c48c8055bD43D1A53799176f0D;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* ETH OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
// If the ETH transfer MUST succeed with a reasonable gas budget, use the force variants.
//
// The regular variants:
// - Forwards all remaining gas to the target.
// - Reverts if the target reverts.
// - Reverts if the current contract has insufficient balance.
//
// The force variants:
// - Forwards with an optional gas stipend
// (defaults to `GAS_STIPEND_NO_GRIEF`, which is sufficient for most cases).
// - If the target reverts, or if the gas stipend is exhausted,
// creates a temporary contract to force send the ETH via `SELFDESTRUCT`.
// Future compatible with `SENDALL`: https://eips.ethereum.org/EIPS/eip-4758.
// - Reverts if the current contract has insufficient balance.
//
// The try variants:
// - Forwards with a mandatory gas stipend.
// - Instead of reverting, returns whether the transfer succeeded.
/// @dev Sends `amount` (in wei) ETH to `to`.
function safeTransferETH(address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
if iszero(call(gas(), to, amount, codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.
revert(0x1c, 0x04)
}
}
}
/// @dev Sends all the ETH in the current contract to `to`.
function safeTransferAllETH(address to) internal {
/// @solidity memory-safe-assembly
assembly {
// Transfer all the ETH and check if it succeeded or not.
if iszero(call(gas(), to, selfbalance(), codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.
revert(0x1c, 0x04)
}
}
}
/// @dev Force sends `amount` (in wei) ETH to `to`, with a `gasStipend`.
function forceSafeTransferETH(address to, uint256 amount, uint256 gasStipend) internal {
/// @solidity memory-safe-assembly
assembly {
if lt(selfbalance(), amount) {
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.
revert(0x1c, 0x04)
}
if iszero(call(gasStipend, to, amount, codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, to) // Store the address in scratch space.
mstore8(0x0b, 0x73) // Opcode `PUSH20`.
mstore8(0x20, 0xff) // Opcode `SELFDESTRUCT`.
if iszero(create(amount, 0x0b, 0x16)) { revert(codesize(), codesize()) } // For gas estimation.
}
}
}
/// @dev Force sends all the ETH in the current contract to `to`, with a `gasStipend`.
function forceSafeTransferAllETH(address to, uint256 gasStipend) internal {
/// @solidity memory-safe-assembly
assembly {
if iszero(call(gasStipend, to, selfbalance(), codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, to) // Store the address in scratch space.
mstore8(0x0b, 0x73) // Opcode `PUSH20`.
mstore8(0x20, 0xff) // Opcode `SELFDESTRUCT`.
if iszero(create(selfbalance(), 0x0b, 0x16)) {
revert(codesize(), codesize())
} // For gas estimation.
}
}
}
/// @dev Force sends `amount` (in wei) ETH to `to`, with `GAS_STIPEND_NO_GRIEF`.
function forceSafeTransferETH(address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
if lt(selfbalance(), amount) {
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.
revert(0x1c, 0x04)
}
if iszero(call(GAS_STIPEND_NO_GRIEF, to, amount, codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, to) // Store the address in scratch space.
mstore8(0x0b, 0x73) // Opcode `PUSH20`.
mstore8(0x20, 0xff) // Opcode `SELFDESTRUCT`.
if iszero(create(amount, 0x0b, 0x16)) { revert(codesize(), codesize()) } // For gas estimation.
}
}
}
/// @dev Force sends all the ETH in the current contract to `to`, with `GAS_STIPEND_NO_GRIEF`.
function forceSafeTransferAllETH(address to) internal {
/// @solidity memory-safe-assembly
assembly {
// forgefmt: disable-next-item
if iszero(call(GAS_STIPEND_NO_GRIEF, to, selfbalance(), codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, to) // Store the address in scratch space.
mstore8(0x0b, 0x73) // Opcode `PUSH20`.
mstore8(0x20, 0xff) // Opcode `SELFDESTRUCT`.
if iszero(create(selfbalance(), 0x0b, 0x16)) { revert(codesize(), codesize()) } // For gas estimation.
}
}
}
/// @dev Sends `amount` (in wei) ETH to `to`, with a `gasStipend`.
function trySafeTransferETH(address to, uint256 amount, uint256 gasStipend)
internal
returns (bool success)
{
/// @solidity memory-safe-assembly
assembly {
success := call(gasStipend, to, amount, codesize(), 0x00, codesize(), 0x00)
}
}
/// @dev Sends all the ETH in the current contract to `to`, with a `gasStipend`.
function trySafeTransferAllETH(address to, uint256 gasStipend) internal returns (bool success) {
/// @solidity memory-safe-assembly
assembly {
success := call(gasStipend, to, selfbalance(), codesize(), 0x00, codesize(), 0x00)
}
}
/// @dev Force transfers ETH to `to`, without triggering the fallback (if any).
/// This method attempts to use a separate contract to send via `SELFDESTRUCT`,
/// and upon failure, deploys a minimal vault to accrue the ETH.
function safeMoveETH(address to, uint256 amount) internal returns (address vault) {
/// @solidity memory-safe-assembly
assembly {
to := shr(96, shl(96, to)) // Clean upper 96 bits.
for { let mover := ETH_MOVER } iszero(eq(to, address())) {} {
let selfBalanceBefore := selfbalance()
if or(lt(selfBalanceBefore, amount), eq(to, mover)) {
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.
revert(0x1c, 0x04)
}
if extcodesize(mover) {
let balanceBefore := balance(to) // Check via delta, in case `SELFDESTRUCT` is bricked.
mstore(0x00, to)
pop(call(gas(), mover, amount, 0x00, 0x20, codesize(), 0x00))
// If `address(to).balance >= amount + balanceBefore`, skip vault workflow.
if iszero(lt(balance(to), add(amount, balanceBefore))) { break }
// Just in case `SELFDESTRUCT` is changed to not revert and do nothing.
if lt(selfBalanceBefore, selfbalance()) { invalid() }
}
let m := mload(0x40)
// If the mover is missing or bricked, deploy a minimal vault
// that withdraws all ETH to `to` when being called only by `to`.
// forgefmt: disable-next-item
mstore(add(m, 0x20), 0x33146025575b600160005260206000f35b3d3d3d3d47335af1601a5760003dfd)
mstore(m, or(to, shl(160, 0x6035600b3d3960353df3fe73)))
// Compute and store the bytecode hash.
mstore8(0x00, 0xff) // Write the prefix.
mstore(0x35, keccak256(m, 0x40))
mstore(0x01, shl(96, address())) // Deployer.
mstore(0x15, 0) // Salt.
vault := keccak256(0x00, 0x55)
pop(call(gas(), vault, amount, codesize(), 0x00, codesize(), 0x00))
// The vault returns a single word on success. Failure reverts with empty data.
if iszero(returndatasize()) {
if iszero(create2(0, m, 0x40, 0)) { revert(codesize(), codesize()) } // For gas estimation.
}
mstore(0x40, m) // Restore the free memory pointer.
break
}
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* ERC20 OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Sends `amount` of ERC20 `token` from `from` to `to`.
/// Reverts upon failure.
///
/// The `from` account must have at least `amount` approved for
/// the current contract to manage.
function safeTransferFrom(address token, address from, address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40) // Cache the free memory pointer.
mstore(0x60, amount) // Store the `amount` argument.
mstore(0x40, to) // Store the `to` argument.
mstore(0x2c, shl(96, from)) // Store the `from` argument.
mstore(0x0c, 0x23b872dd000000000000000000000000) // `transferFrom(address,address,uint256)`.
let success := call(gas(), token, 0, 0x1c, 0x64, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {
mstore(0x00, 0x7939f424) // `TransferFromFailed()`.
revert(0x1c, 0x04)
}
}
mstore(0x60, 0) // Restore the zero slot to zero.
mstore(0x40, m) // Restore the free memory pointer.
}
}
/// @dev Sends `amount` of ERC20 `token` from `from` to `to`.
///
/// The `from` account must have at least `amount` approved for the current contract to manage.
function trySafeTransferFrom(address token, address from, address to, uint256 amount)
internal
returns (bool success)
{
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40) // Cache the free memory pointer.
mstore(0x60, amount) // Store the `amount` argument.
mstore(0x40, to) // Store the `to` argument.
mstore(0x2c, shl(96, from)) // Store the `from` argument.
mstore(0x0c, 0x23b872dd000000000000000000000000) // `transferFrom(address,address,uint256)`.
success := call(gas(), token, 0, 0x1c, 0x64, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
success := lt(or(iszero(extcodesize(token)), returndatasize()), success)
}
mstore(0x60, 0) // Restore the zero slot to zero.
mstore(0x40, m) // Restore the free memory pointer.
}
}
/// @dev Sends all of ERC20 `token` from `from` to `to`.
/// Reverts upon failure.
///
/// The `from` account must have their entire balance approved for the current contract to manage.
function safeTransferAllFrom(address token, address from, address to)
internal
returns (uint256 amount)
{
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40) // Cache the free memory pointer.
mstore(0x40, to) // Store the `to` argument.
mstore(0x2c, shl(96, from)) // Store the `from` argument.
mstore(0x0c, 0x70a08231000000000000000000000000) // `balanceOf(address)`.
// Read the balance, reverting upon failure.
if iszero(
and( // The arguments of `and` are evaluated from right to left.
gt(returndatasize(), 0x1f), // At least 32 bytes returned.
staticcall(gas(), token, 0x1c, 0x24, 0x60, 0x20)
)
) {
mstore(0x00, 0x7939f424) // `TransferFromFailed()`.
revert(0x1c, 0x04)
}
mstore(0x00, 0x23b872dd) // `transferFrom(address,address,uint256)`.
amount := mload(0x60) // The `amount` is already at 0x60. We'll need to return it.
// Perform the transfer, reverting upon failure.
let success := call(gas(), token, 0, 0x1c, 0x64, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {
mstore(0x00, 0x7939f424) // `TransferFromFailed()`.
revert(0x1c, 0x04)
}
}
mstore(0x60, 0) // Restore the zero slot to zero.
mstore(0x40, m) // Restore the free memory pointer.
}
}
/// @dev Sends `amount` of ERC20 `token` from the current contract to `to`.
/// Reverts upon failure.
function safeTransfer(address token, address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
mstore(0x14, to) // Store the `to` argument.
mstore(0x34, amount) // Store the `amount` argument.
mstore(0x00, 0xa9059cbb000000000000000000000000) // `transfer(address,uint256)`.
// Perform the transfer, reverting upon failure.
let success := call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {
mstore(0x00, 0x90b8ec18) // `TransferFailed()`.
revert(0x1c, 0x04)
}
}
mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.
}
}
/// @dev Sends all of ERC20 `token` from the current contract to `to`.
/// Reverts upon failure.
function safeTransferAll(address token, address to) internal returns (uint256 amount) {
/// @solidity memory-safe-assembly
assembly {
mstore(0x00, 0x70a08231) // Store the function selector of `balanceOf(address)`.
mstore(0x20, address()) // Store the address of the current contract.
// Read the balance, reverting upon failure.
if iszero(
and( // The arguments of `and` are evaluated from right to left.
gt(returndatasize(), 0x1f), // At least 32 bytes returned.
staticcall(gas(), token, 0x1c, 0x24, 0x34, 0x20)
)
) {
mstore(0x00, 0x90b8ec18) // `TransferFailed()`.
revert(0x1c, 0x04)
}
mstore(0x14, to) // Store the `to` argument.
amount := mload(0x34) // The `amount` is already at 0x34. We'll need to return it.
mstore(0x00, 0xa9059cbb000000000000000000000000) // `transfer(address,uint256)`.
// Perform the transfer, reverting upon failure.
let success := call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {
mstore(0x00, 0x90b8ec18) // `TransferFailed()`.
revert(0x1c, 0x04)
}
}
mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.
}
}
/// @dev Sets `amount` of ERC20 `token` for `to` to manage on behalf of the current contract.
/// Reverts upon failure.
function safeApprove(address token, address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
mstore(0x14, to) // Store the `to` argument.
mstore(0x34, amount) // Store the `amount` argument.
mstore(0x00, 0x095ea7b3000000000000000000000000) // `approve(address,uint256)`.
let success := call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {
mstore(0x00, 0x3e3f8f73) // `ApproveFailed()`.
revert(0x1c, 0x04)
}
}
mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.
}
}
/// @dev Sets `amount` of ERC20 `token` for `to` to manage on behalf of the current contract.
/// If the initial attempt to approve fails, attempts to reset the approved amount to zero,
/// then retries the approval again (some tokens, e.g. USDT, requires this).
/// Reverts upon failure.
function safeApproveWithRetry(address token, address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
mstore(0x14, to) // Store the `to` argument.
mstore(0x34, amount) // Store the `amount` argument.
mstore(0x00, 0x095ea7b3000000000000000000000000) // `approve(address,uint256)`.
// Perform the approval, retrying upon failure.
let success := call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {
mstore(0x34, 0) // Store 0 for the `amount`.
mstore(0x00, 0x095ea7b3000000000000000000000000) // `approve(address,uint256)`.
pop(call(gas(), token, 0, 0x10, 0x44, codesize(), 0x00)) // Reset the approval.
mstore(0x34, amount) // Store back the original `amount`.
// Retry the approval, reverting upon failure.
success := call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
// Check the `extcodesize` again just in case the token selfdestructs lol.
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {
mstore(0x00, 0x3e3f8f73) // `ApproveFailed()`.
revert(0x1c, 0x04)
}
}
}
}
mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.
}
}
/// @dev Returns the amount of ERC20 `token` owned by `account`.
/// Returns zero if the `token` does not exist.
function balanceOf(address token, address account) internal view returns (uint256 amount) {
/// @solidity memory-safe-assembly
assembly {
mstore(0x14, account) // Store the `account` argument.
mstore(0x00, 0x70a08231000000000000000000000000) // `balanceOf(address)`.
amount := mul( // The arguments of `mul` are evaluated from right to left.
mload(0x20),
and( // The arguments of `and` are evaluated from right to left.
gt(returndatasize(), 0x1f), // At least 32 bytes returned.
staticcall(gas(), token, 0x10, 0x24, 0x20, 0x20)
)
)
}
}
/// @dev Performs a `token.balanceOf(account)` check.
/// `implemented` denotes whether the `token` implements `balanceOf`.
/// `amount` is zero if the `token` does not implement `balanceOf`.
function checkBalanceOf(address token, address account)
internal
view
returns (bool implemented, uint256 amount)
{
/// @solidity memory-safe-assembly
assembly {
mstore(0x14, account) // Store the `account` argument.
mstore(0x00, 0x70a08231000000000000000000000000) // `balanceOf(address)`.
implemented := and( // The arguments of `and` are evaluated from right to left.
gt(returndatasize(), 0x1f), // At least 32 bytes returned.
staticcall(gas(), token, 0x10, 0x24, 0x20, 0x20)
)
amount := mul(mload(0x20), implemented)
}
}
/// @dev Returns the total supply of the `token`.
/// Reverts if the token does not exist or does not implement `totalSupply()`.
function totalSupply(address token) internal view returns (uint256 result) {
/// @solidity memory-safe-assembly
assembly {
mstore(0x00, 0x18160ddd) // `totalSupply()`.
if iszero(
and(gt(returndatasize(), 0x1f), staticcall(gas(), token, 0x1c, 0x04, 0x00, 0x20))
) {
mstore(0x00, 0x54cd9435) // `TotalSupplyQueryFailed()`.
revert(0x1c, 0x04)
}
result := mload(0x00)
}
}
/// @dev Sends `amount` of ERC20 `token` from `from` to `to`.
/// If the initial attempt fails, try to use Permit2 to transfer the token.
/// Reverts upon failure.
///
/// The `from` account must have at least `amount` approved for the current contract to manage.
function safeTransferFrom2(address token, address from, address to, uint256 amount) internal {
if (!trySafeTransferFrom(token, from, to, amount)) {
permit2TransferFrom(token, from, to, amount);
}
}
/// @dev Sends `amount` of ERC20 `token` from `from` to `to` via Permit2.
/// Reverts upon failure.
function permit2TransferFrom(address token, address from, address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40)
mstore(add(m, 0x74), shr(96, shl(96, token)))
mstore(add(m, 0x54), amount)
mstore(add(m, 0x34), to)
mstore(add(m, 0x20), shl(96, from))
// `transferFrom(address,address,uint160,address)`.
mstore(m, 0x36c78516000000000000000000000000)
let p := PERMIT2
let exists := eq(chainid(), 1)
if iszero(exists) { exists := iszero(iszero(extcodesize(p))) }
if iszero(
and(
call(gas(), p, 0, add(m, 0x10), 0x84, codesize(), 0x00),
lt(iszero(extcodesize(token)), exists) // Token has code and Permit2 exists.
)
) {
mstore(0x00, 0x7939f4248757f0fd) // `TransferFromFailed()` or `Permit2AmountOverflow()`.
revert(add(0x18, shl(2, iszero(iszero(shr(160, amount))))), 0x04)
}
}
}
/// @dev Permit a user to spend a given amount of
/// another user's tokens via native EIP-2612 permit if possible, falling
/// back to Permit2 if native permit fails or is not implemented on the token.
function permit2(
address token,
address owner,
address spender,
uint256 amount,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) internal {
bool success;
/// @solidity memory-safe-assembly
assembly {
for {} shl(96, xor(token, WETH9)) {} {
mstore(0x00, 0x3644e515) // `DOMAIN_SEPARATOR()`.
if iszero(
and( // The arguments of `and` are evaluated from right to left.
lt(iszero(mload(0x00)), eq(returndatasize(), 0x20)), // Returns 1 non-zero word.
// Gas stipend to limit gas burn for tokens that don't refund gas when
// an non-existing function is called. 5K should be enough for a SLOAD.
staticcall(5000, token, 0x1c, 0x04, 0x00, 0x20)
)
) { break }
// After here, we can be sure that token is a contract.
let m := mload(0x40)
mstore(add(m, 0x34), spender)
mstore(add(m, 0x20), shl(96, owner))
mstore(add(m, 0x74), deadline)
if eq(mload(0x00), DAI_DOMAIN_SEPARATOR) {
mstore(0x14, owner)
mstore(0x00, 0x7ecebe00000000000000000000000000) // `nonces(address)`.
mstore(
add(m, 0x94),
lt(iszero(amount), staticcall(gas(), token, 0x10, 0x24, add(m, 0x54), 0x20))
)
mstore(m, 0x8fcbaf0c000000000000000000000000) // `IDAIPermit.permit`.
// `nonces` is already at `add(m, 0x54)`.
// `amount != 0` is already stored at `add(m, 0x94)`.
mstore(add(m, 0xb4), and(0xff, v))
mstore(add(m, 0xd4), r)
mstore(add(m, 0xf4), s)
success := call(gas(), token, 0, add(m, 0x10), 0x104, codesize(), 0x00)
break
}
mstore(m, 0xd505accf000000000000000000000000) // `IERC20Permit.permit`.
mstore(add(m, 0x54), amount)
mstore(add(m, 0x94), and(0xff, v))
mstore(add(m, 0xb4), r)
mstore(add(m, 0xd4), s)
success := call(gas(), token, 0, add(m, 0x10), 0xe4, codesize(), 0x00)
break
}
}
if (!success) simplePermit2(token, owner, spender, amount, deadline, v, r, s);
}
/// @dev Simple permit on the Permit2 contract.
function simplePermit2(
address token,
address owner,
address spender,
uint256 amount,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) internal {
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40)
mstore(m, 0x927da105) // `allowance(address,address,address)`.
{
let addressMask := shr(96, not(0))
mstore(add(m, 0x20), and(addressMask, owner))
mstore(add(m, 0x40), and(addressMask, token))
mstore(add(m, 0x60), and(addressMask, spender))
mstore(add(m, 0xc0), and(addressMask, spender))
}
let p := mul(PERMIT2, iszero(shr(160, amount)))
if iszero(
and( // The arguments of `and` are evaluated from right to left.
gt(returndatasize(), 0x5f), // Returns 3 words: `amount`, `expiration`, `nonce`.
staticcall(gas(), p, add(m, 0x1c), 0x64, add(m, 0x60), 0x60)
)
) {
mstore(0x00, 0x6b836e6b8757f0fd) // `Permit2Failed()` or `Permit2AmountOverflow()`.
revert(add(0x18, shl(2, iszero(p))), 0x04)
}
mstore(m, 0x2b67b570) // `Permit2.permit` (PermitSingle variant).
// `owner` is already `add(m, 0x20)`.
// `token` is already at `add(m, 0x40)`.
mstore(add(m, 0x60), amount)
mstore(add(m, 0x80), 0xffffffffffff) // `expiration = type(uint48).max`.
// `nonce` is already at `add(m, 0xa0)`.
// `spender` is already at `add(m, 0xc0)`.
mstore(add(m, 0xe0), deadline)
mstore(add(m, 0x100), 0x100) // `signature` offset.
mstore(add(m, 0x120), 0x41) // `signature` length.
mstore(add(m, 0x140), r)
mstore(add(m, 0x160), s)
mstore(add(m, 0x180), shl(248, v))
if iszero( // Revert if token does not have code, or if the call fails.
mul(extcodesize(token), call(gas(), p, 0, add(m, 0x1c), 0x184, codesize(), 0x00))
) {
mstore(0x00, 0x6b836e6b) // `Permit2Failed()`.
revert(0x1c, 0x04)
}
}
}
/// @dev Approves `spender` to spend `amount` of `token` for `address(this)`.
function permit2Approve(address token, address spender, uint160 amount, uint48 expiration)
internal
{
/// @solidity memory-safe-assembly
assembly {
let addressMask := shr(96, not(0))
let m := mload(0x40)
mstore(m, 0x87517c45) // `approve(address,address,uint160,uint48)`.
mstore(add(m, 0x20), and(addressMask, token))
mstore(add(m, 0x40), and(addressMask, spender))
mstore(add(m, 0x60), and(addressMask, amount))
mstore(add(m, 0x80), and(0xffffffffffff, expiration))
if iszero(call(gas(), PERMIT2, 0, add(m, 0x1c), 0x84, codesize(), 0x00)) {
mstore(0x00, 0x324f14ae) // `Permit2ApproveFailed()`.
revert(0x1c, 0x04)
}
}
}
/// @dev Revokes an approval for `token` and `spender` for `address(this)`.
function permit2Lockdown(address token, address spender) internal {
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40)
mstore(m, 0xcc53287f) // `Permit2.lockdown`.
mstore(add(m, 0x20), 0x20) // Offset of the `approvals`.
mstore(add(m, 0x40), 1) // `approvals.length`.
mstore(add(m, 0x60), shr(96, shl(96, token)))
mstore(add(m, 0x80), shr(96, shl(96, spender)))
if iszero(call(gas(), PERMIT2, 0, add(m, 0x1c), 0xa0, codesize(), 0x00)) {
mstore(0x00, 0x96b3de23) // `Permit2LockdownFailed()`.
revert(0x1c, 0x04)
}
}
}
}
src/FWAConfigKeys.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
/// @title FWAConfigKeys
/// @notice Keys for FWA's consolidated owner-config setters (`setUint`/`setBool`/`setAddr`). The former
/// per-knob setters were merged into three generic dispatchers to keep FWA's runtime
/// bytecode under the 24,576-byte EIP-170 limit; these named constants keep call sites readable
/// (`pool.setUint(FWAConfigKeys.SURCHARGE_BPS, 1200)`). Constants live in a library so they inline
/// at zero bytecode cost in both FWA and its callers. Values are globally unique across all three
/// dispatchers so the emitted `ConfigSet(key, value)` event is unambiguous for indexers.
library FWAConfigKeys {
// --- constructor / FWAVRFService request-tuple events (not accepted by setUint) ---
uint256 internal constant CALLBACK_GAS_LIMIT = 1;
uint256 internal constant VRF_KEY_HASH = 24;
// --- setUint (uint256 value) ---
uint256 internal constant VRF_SUB_ID = 2;
// Values 3..6 are intentionally unused. VRF service-fee pricing, callback coverage and subscription
// funding moved into the external FWAVRFService before deployment.
uint256 internal constant REQUEST_CONFIRMATIONS = 7;
// Values 8 and 9 are intentionally unused. Block maturity and the separate callback window were
// replaced before deployment by request-time reservation plus `SELECTION_TIMEOUT_BLOCKS`.
uint256 internal constant MAX_ACTIVATIONS_PER_ACQUISITION = 10;
uint256 internal constant SELECTION_TIMEOUT_BLOCKS = 11;
uint256 internal constant MAX_ACQUISITIONS_PER_TX = 12;
uint256 internal constant SURCHARGE_BPS = 13;
uint256 internal constant SELECTION_SLIPPAGE_BPS = 14;
uint256 internal constant TOP_LISTING_SHARE_BPS = 15;
uint256 internal constant TOP_THRESHOLD_BPS = 16;
uint256 internal constant SETTLEMENT_DISCOUNT_BPS = 17;
uint256 internal constant OWNER_ACQUISITION_FEE_BPS = 18;
uint256 internal constant OWNER_SETTLEMENT_FEE_BPS = 19;
uint256 internal constant SETTLEMENT_WINDOW = 20;
uint256 internal constant FINALIZE_WINDOW = 21;
uint256 internal constant MIN_BACKING = 22;
uint256 internal constant PROTOCOL_FEE_TO_TOKEN_BPS = 23;
uint256 internal constant MAX_STAGED_LISTINGS = 25;
// --- setBool (bool value) ---
uint256 internal constant RETAINED_TO_PROTOCOL = 40;
uint256 internal constant ACQUISITIONS_ENABLED = 41;
uint256 internal constant WITHDRAW_ONLY = 42;
uint256 internal constant WHITELIST_ENABLED = 43;
uint256 internal constant ACCEPT_BID_AS_TOKENS_ENABLED = 44;
// --- setAddr (address value) ---
uint256 internal constant VRF_COORDINATOR = 60;
uint256 internal constant PAYOUT_ADDRESS = 61;
uint256 internal constant WHITELIST_MANAGER = 62;
/// @dev Constructor-only immutable address, emitted through ConfigSet but not accepted by setAddr.
uint256 internal constant VRF_SERVICE = 63;
}
Compiler settingsShow
{
"viaIR": true,
"metadata": {
"appendCBOR": false,
"bytecodeHash": "none",
"useLiteralContent": false
},
"optimizer": {
"runs": 1,
"enabled": true
},
"evmVersion": "prague",
"remappings": [
"solady/=lib/solady/",
"forge-std/=lib/forge-std/src/",
"@chainlink/contracts/=lib/chainlink-evm/contracts/",
"@uniswap/v4-core/=lib/v4-core/",
"@uniswap/v4-periphery/=lib/v4-periphery/",
"project/=/",
"@ensdomains/=lib/v4-core/node_modules/@ensdomains/",
"@openzeppelin/=lib/v4-core/lib/openzeppelin-contracts/",
"chainlink-evm/=lib/chainlink-evm/",
"ds-test/=lib/v4-core/lib/forge-std/lib/ds-test/src/",
"erc4626-tests/=lib/v4-core/lib/openzeppelin-contracts/lib/erc4626-tests/",
"forge-gas-snapshot/=lib/v4-periphery/lib/permit2/lib/forge-gas-snapshot/src/",
"hardhat/=lib/v4-core/node_modules/hardhat/",
"openzeppelin-contracts/=lib/v4-core/lib/openzeppelin-contracts/",
"permit2/=lib/v4-periphery/lib/permit2/",
"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.