Skip to content
Verified contractExactUNLICENSED

CharterAuction

Address0x70dc689eba0fa1019bbce9a2ad7e4c12d194983e
Balance

0 ETH

 

Transactions

4

 

Compiler

solc 0.8.26

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.26+commit.8a97fa7a
Language
solidity
Optimization
Yes, 200 runs
EVM version
cancun
License
UNLICENSED
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,143,738
Creation transaction
0x39755f5f36…2ec64fd441

src/ethereum/v3/CharterAuction.sol

// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.26;

import {AuctionBase, IAuctionBank} from "./AuctionBase.sol";

contract CharterAuction is AuctionBase {
    error NoReservePrice();
    error InvalidCount();
    error InsufficientPayment();
    error NoPolManager();
    error EthTransferFailed();
    event ReservePriceSet(uint256 price);
    event ChartersPerDaySet(uint256 count);
    event CharterPurchased(uint256 indexed charterId, address indexed buyer, uint256 indexed day, uint256 price);

    uint256 public constant CHARTERS_PER_DAY_MAX = 100;
    uint256 public chartersPerDay;
    uint256 public charterReservePrice;

    constructor(address bank_, address registry_, address initialOwner)
        AuctionBase(bank_, registry_, initialOwner, 3)
    {}

    function setReservePrice(uint256 price) external onlyOwner onlyBeforeLaunch {
        if (price == 0) revert NoReservePrice();
        charterReservePrice = price;
        emit ReservePriceSet(price);
    }

    function setChartersPerDay(uint256 count) external onlyBeforeLaunch {
        if (msg.sender != owner && msg.sender != supplyController()) revert NotSupplyAuthority();
        if (count == 0 || count > CHARTERS_PER_DAY_MAX) revert OutOfBounds();
        chartersPerDay = count;
        emit ChartersPerDaySet(count);
    }

    function buyCharter(uint256 maxPrice) external payable nonReentrant returns (uint256 charterId) {
        return _buyCharters(1, maxPrice)[0];
    }

    function buyCharterBatch(uint256 quantity, uint256 maximumUnitPrice) external payable nonReentrant returns (uint256[] memory charterIds) {
        return _buyCharters(quantity, maximumUnitPrice);
    }

    function _buyCharters(uint256 quantity, uint256 maximumUnitPrice) private returns (uint256[] memory charterIds) {
        if (paused()) revert ContractPaused();
        if (quantity == 0 || quantity > CHARTERS_PER_DAY_MAX) revert InvalidCount();
        _roll();
        if (quantity > dayCap - soldToday) revert DailyCapExceeded();
        uint256 price = currentPrice();
        if (price > maximumUnitPrice) revert PriceAboveMax();
        uint256 total = price * quantity;
        if (msg.value < total) revert InsufficientPayment();
        _recordSale(price, quantity);
        charterIds = new uint256[](quantity);
        for (uint256 i; i < quantity; ++i) {
            uint256 id = IAuctionBank(bank).createCharter(msg.sender);
            charterIds[i] = id;
            emit CharterPurchased(id, msg.sender, currentDay, price);
        }
        address pol = _get(bytes32("POL_MANAGER"));
        if (pol.code.length == 0) revert NoPolManager();
        (bool sent,) = pol.call{value: total}("");
        if (!sent) revert EthTransferFailed();
        uint256 refund = msg.value - total;
        if (refund != 0) {
            (bool refunded,) = msg.sender.call{value: refund}("");
            if (!refunded) revert EthTransferFailed();
        }
    }

    function _floorPrice() internal view override returns (uint256) {
        return charterReservePrice == 0 ? 1 : charterReservePrice;
    }

    function _perDay() internal view override returns (uint256) {
        return chartersPerDay;
    }

    function configurationReady() public view override returns (bool) {
        return initialPriceConfigured && !paused() && charterReservePrice != 0 && chartersPerDay != 0;
    }
}

src/ethereum/v3/AuctionBase.sol

// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.26;

import {AuctionFixedPointMath as Math} from "./math/AuctionFixedPointMath.sol";
import {IAddressRegistry} from "./IAddressRegistry.sol";

interface IAuctionBank {
    function registry() external view returns (address);
    function emissionsStarted() external view returns (bool);
    function administrationFrozen() external view returns (bool);
    function charterNFT() external view returns (address);
    function totalBranches() external view returns (uint256);
    function multiplierWad() external view returns (uint256);
    function baseIssuancePerDay() external view returns (uint256);
    function rollover() external;
    function openBranches(uint256 charterId, uint256 count, uint256 cost) external;
    function createCharter(address to) external returns (uint256);
}

abstract contract AuctionBase {
    error Unauthorized();
    error InvalidOwner();
    error AlreadyStarted();
    error NotStarted();
    error EmissionsNotStarted();
    error AdministrationFrozen();
    error AuctionNotConfigured();
    error ContractPaused();
    error NotGuardian();
    error NotPaused();
    error NotSupplyAuthority();
    error OutOfBounds();
    error InvalidHalfLife();
    error DailyCapExceeded();
    error PriceAboveMax();
    error Reentrancy();

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
    event OwnershipTransferStarted(address indexed previousOwner, address indexed newOwner);
    event AuctionConfigured(uint256 initialPrice);
    event AuctionStarted(uint256 indexed day, uint256 startPrice);
    event DayRolled(uint256 indexed day, uint256 startPrice, uint256 floorPrice, uint256 cap);
    event DecayHalfLifeSet(uint256 halfLife);
    event SupplyControllerSet(address controller);
    event GuardianPaused(address indexed guardian);
    event GuardianUnpaused();

    uint256 public constant AUCTION_DAY = 1 days;

    function DECAY_HALF_LIFE_MAX() public pure virtual returns (uint256) { return 1 days; }
    address public immutable bank;
    address public immutable registry;
    uint256 public immutable startMultiplier;

    address public owner;
    address public pendingOwner;
    uint256 public auctionAnchor;
    uint256 public decayHalfLife;
    bool public started;
    uint256 public currentDay;
    uint256 public dayStartPrice;
    uint256 public dayFloorPrice;
    uint256 public dayHalfLife;
    uint256 public dayCap;
    uint256 public soldToday;
    uint256 public lastSalePrice;
    uint256 public lastSaleDay;
    uint256 public configuredInitialPrice;
    bool public initialPriceConfigured;
    uint256 private _guardStatus = 1;
    bool private _pausedFlag;
    address private _supplyController;

    constructor(address bank_, address registry_, address initialOwner, uint256 multiplier_) {
        if (initialOwner == address(0)) revert InvalidOwner();
        bank = bank_;
        registry = registry_;
        startMultiplier = multiplier_;
        owner = initialOwner;
        emit OwnershipTransferred(address(0), initialOwner);
    }

    modifier onlyOwner() { if (msg.sender != owner) revert Unauthorized(); _; }
    modifier onlyBeforeLaunch() {
        if (IAuctionBank(bank).administrationFrozen()) revert AdministrationFrozen();
        _;
    }
    modifier nonReentrant() {
        if (_guardStatus == 2) revert Reentrancy();
        _guardStatus = 2;
        _;
        _guardStatus = 1;
    }

    function transferOwnership(address next) external onlyOwner onlyBeforeLaunch {
        if (next == address(0)) revert InvalidOwner();
        pendingOwner = next;
        emit OwnershipTransferStarted(owner, next);
    }
    function acceptOwnership() external onlyBeforeLaunch {
        if (msg.sender != pendingOwner) revert Unauthorized();
        emit OwnershipTransferred(owner, msg.sender);
        owner = msg.sender;
        pendingOwner = address(0);
    }
    function renounceOwnership() external onlyOwner onlyBeforeLaunch {
        emit OwnershipTransferred(owner, address(0));
        owner = address(0);
        pendingOwner = address(0);
    }
    function pause() external onlyBeforeLaunch {
        if (msg.sender != _get("GUARDIAN")) revert NotGuardian();
        if (_pausedFlag) revert ContractPaused();
        _pausedFlag = true;
        emit GuardianPaused(msg.sender);
    }
    function unpause() external onlyOwner onlyBeforeLaunch {
        if (!_pausedFlag) revert NotPaused();
        _pausedFlag = false;
        emit GuardianUnpaused();
    }
    function setSupplyController(address controller) external onlyOwner onlyBeforeLaunch {
        _supplyController = controller;
        emit SupplyControllerSet(controller);
    }
    function setDecayHalfLife(uint256 halfLife) public virtual onlyOwner onlyBeforeLaunch {
        _setHalfLife(halfLife);
    }
    function configureInitialPrice(uint256 initialPrice) external onlyOwner onlyBeforeLaunch {
        if (started) revert AlreadyStarted();
        configuredInitialPrice = initialPrice;
        initialPriceConfigured = true;
        emit AuctionConfigured(initialPrice);
    }
    function activate() external nonReentrant {
        if (started) revert AlreadyStarted();
        if (!IAuctionBank(bank).emissionsStarted()) revert EmissionsNotStarted();
        if (!configurationReady()) revert AuctionNotConfigured();
        _start(configuredInitialPrice);
    }
    function paused() public view returns (bool) { return _pausedFlag; }
    function supplyController() public view returns (address) { return _supplyController; }
    function administrationFrozen() public view returns (bool) {
        return IAuctionBank(bank).administrationFrozen();
    }
    function remainingToday() external view virtual returns (uint256) {
        if (!started) return 0;
        if (_day() != currentDay) return _perDay();
        return dayCap - soldToday;
    }
    function currentPrice() public view virtual returns (uint256) {
        return _priceForDuration(AUCTION_DAY);
    }

    function _priceForDuration(uint256 duration) internal view returns (uint256) {
        if (!started) return 0;
        uint256 today = _day();
        uint256 start;
        uint256 floor;
        uint256 halfLife;
        if (today == currentDay) {
            start = dayStartPrice;
            floor = dayFloorPrice;
            halfLife = dayHalfLife;
        } else {
            (start, floor) = _nextDayPrices(today);
            halfLife = decayHalfLife;
        }
        if (start <= floor) return floor;
        uint256 elapsed = (block.timestamp - auctionAnchor) % duration;
        if (halfLife != 0) {
            uint256 halfLifeDecay = uint256(Math.powWad(int256(0.5 ether), int256(elapsed * 1 ether / halfLife)));
            return floor + (start - floor) * halfLifeDecay / 1 ether;
        }
        int256 ratio = int256(floor * 1 ether / start);
        if (ratio <= 0) return floor;
        uint256 decay = uint256(Math.powWad(ratio, int256(elapsed * 1 ether / duration)));
        uint256 price = start * decay / 1 ether;
        return price < floor ? floor : price;
    }

    function _setHalfLife(uint256 halfLife) internal {
        if (halfLife > DECAY_HALF_LIFE_MAX()) revert InvalidHalfLife();
        _rollIfStarted();
        decayHalfLife = halfLife;
        emit DecayHalfLifeSet(halfLife);
    }
    function _start(uint256 initialPrice) internal {
        auctionAnchor = block.timestamp;
        started = true;
        currentDay = 0;
        dayFloorPrice = _floorPrice();
        dayStartPrice = initialPrice > dayFloorPrice ? initialPrice : dayFloorPrice;
        dayHalfLife = decayHalfLife;
        dayCap = _perDay();
        emit AuctionStarted(0, dayStartPrice);
    }
    function _rollIfStarted() internal { if (started) _roll(); }
    function _roll() internal {
        if (!started) revert NotStarted();
        uint256 today = _day();
        if (today == currentDay) return;
        (uint256 start, uint256 floor) = _nextDayPrices(today);
        currentDay = today;
        dayStartPrice = start;
        dayFloorPrice = floor;
        dayHalfLife = decayHalfLife;
        dayCap = _perDay();
        soldToday = 0;
        emit DayRolled(today, start, floor, dayCap);
    }
    function _recordSale(uint256 price, uint256 count) internal {
        soldToday += count;
        lastSalePrice = price;
        lastSaleDay = currentDay;
    }
    function _nextDayPrices(uint256 day) internal view returns (uint256 start, uint256 floor) {
        floor = _floorPrice();
        bool previousDaySale = lastSalePrice != 0 && day == lastSaleDay + 1;
        start = (previousDaySale ? lastSalePrice : floor) * startMultiplier;
        if (start < floor) start = floor;
    }
    function _day() internal view returns (uint256) { return (block.timestamp - auctionAnchor) / AUCTION_DAY; }
    function _get(bytes32 key) internal view returns (address) {
        return IAddressRegistry(registry).get(key);
    }

    function configurationReady() public view virtual returns (bool);
    function _perDay() internal view virtual returns (uint256);
    function _floorPrice() internal view virtual returns (uint256);
}

src/ethereum/v3/IAddressRegistry.sol

// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.26;

interface IAddressRegistry {
    error OwnableUnauthorizedAccount(address account);
    error OwnableInvalidOwner(address owner);
    error KeyIsLocked(bytes32 key);
    error KeyNotSet(bytes32 key);
    error KeyIsSet(bytes32 key);
    error AlreadyOpen();

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
    event OwnershipTransferStarted(address indexed previousOwner, address indexed newOwner);
    event AddressSet(bytes32 indexed key, address indexed value);
    event KeyLocked(bytes32 indexed key, address value);
    event ExecutionOpened();

    function owner() external view returns (address);
    function pendingOwner() external view returns (address);
    function transferOwnership(address newOwner) external;
    function acceptOwnership() external;
    function renounceOwnership() external;
    function get(bytes32 key) external view returns (address);
    function set(bytes32 key, address value) external;
    function locked(bytes32 key) external view returns (bool);
    function lock(bytes32 key) external;
    function lockDisabled(bytes32 key) external;
    function executionPermissionless() external view returns (bool);
    function openExecution() external;
}

src/ethereum/v3/math/AuctionFixedPointMath.sol

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

/// @notice Solady fixed-point primitives selected to reproduce deployed auction arithmetic.
/// @dev Authored integration; these are upstream library functions, not recovered protocol Solidity.
/// @author Solady / Vectorized; exp/ln credit Remco Bloemen (MIT).
// Upstream: https://raw.githubusercontent.com/Vectorized/solady/9fe23ffdcd395c4169396064226ed27206b222c3/src/utils/FixedPointMathLib.sol
// Upstream file SHA256: d322e2a0b1f43dbd80a3cf26a40d1ca1cc19cf03737f1e8513485e266b47e9c3
library AuctionFixedPointMath {
    uint256 internal constant WAD = 1e18;

    function powWad(int256 x, int256 y) internal pure returns (int256) {
        // Using `ln(x)` means `x` must be greater than 0.
        return expWad((lnWad(x) * y) / int256(WAD));
    }

    function expWad(int256 x) internal pure returns (int256 r) {
        unchecked {
            // When the result is less than 0.5 we return zero.
            // This happens when `x <= (log(1e-18) * 1e18) ~ -4.15e19`.
            if (x <= -41446531673892822313) return r;

            /// @solidity memory-safe-assembly
            assembly {
                // When the result is greater than `(2**255 - 1) / 1e18` we can not represent it as
                // an int. This happens when `x >= floor(log((2**255 - 1) / 1e18) * 1e18) ≈ 135`.
                if iszero(slt(x, 135305999368893231589)) {
                    mstore(0x00, 0xa37bfec9) // `ExpOverflow()`.
                    revert(0x1c, 0x04)
                }
            }

            // `x` is now in the range `(-42, 136) * 1e18`. Convert to `(-42, 136) * 2**96`
            // for more intermediate precision and a binary basis. This base conversion
            // is a multiplication by 1e18 / 2**96 = 5**18 / 2**78.
            x = (x << 78) / 5 ** 18;

            // Reduce range of x to (-½ ln 2, ½ ln 2) * 2**96 by factoring out powers
            // of two such that exp(x) = exp(x') * 2**k, where k is an integer.
            // Solving this gives k = round(x / log(2)) and x' = x - k * log(2).
            int256 k = ((x << 96) / 54916777467707473351141471128 + 2 ** 95) >> 96;
            x = x - k * 54916777467707473351141471128;

            // `k` is in the range `[-61, 195]`.

            // Evaluate using a (6, 7)-term rational approximation.
            // `p` is made monic, we'll multiply by a scale factor later.
            int256 y = x + 1346386616545796478920950773328;
            y = ((y * x) >> 96) + 57155421227552351082224309758442;
            int256 p = y + x - 94201549194550492254356042504812;
            p = ((p * y) >> 96) + 28719021644029726153956944680412240;
            p = p * x + (4385272521454847904659076985693276 << 96);

            // We leave `p` in `2**192` basis so we don't need to scale it back up for the division.
            int256 q = x - 2855989394907223263936484059900;
            q = ((q * x) >> 96) + 50020603652535783019961831881945;
            q = ((q * x) >> 96) - 533845033583426703283633433725380;
            q = ((q * x) >> 96) + 3604857256930695427073651918091429;
            q = ((q * x) >> 96) - 14423608567350463180887372962807573;
            q = ((q * x) >> 96) + 26449188498355588339934803723976023;

            /// @solidity memory-safe-assembly
            assembly {
                // Div in assembly because solidity adds a zero check despite the unchecked.
                // The q polynomial won't have zeros in the domain as all its roots are complex.
                // No scaling is necessary because p is already `2**96` too large.
                r := sdiv(p, q)
            }

            // r should be in the range `(0.09, 0.25) * 2**96`.

            // We now need to multiply r by:
            // - The scale factor `s ≈ 6.031367120`.
            // - The `2**k` factor from the range reduction.
            // - The `1e18 / 2**96` factor for base conversion.
            // We do this all at once, with an intermediate result in `2**213`
            // basis, so the final right shift is always by a positive amount.
            r = int256((uint256(r) * 3822833074963236453042738258902158003155416615667) >> uint256(195 - k));
        }
    }

    function lnWad(int256 x) internal pure returns (int256 r) {
        /// @solidity memory-safe-assembly
        assembly {
            // We want to convert `x` from `10**18` fixed point to `2**96` fixed point.
            // We do this by multiplying by `2**96 / 10**18`. But since
            // `ln(x * C) = ln(x) + ln(C)`, we can simply do nothing here
            // and add `ln(2**96 / 10**18)` at the end.

            // Compute `k = log2(x) - 96`, `r = 159 - k = 255 - log2(x) = 255 ^ log2(x)`.
            r := shl(7, lt(0xffffffffffffffffffffffffffffffff, x))
            r := or(r, shl(6, lt(0xffffffffffffffff, shr(r, x))))
            r := or(r, shl(5, lt(0xffffffff, shr(r, x))))
            r := or(r, shl(4, lt(0xffff, shr(r, x))))
            r := or(r, shl(3, lt(0xff, shr(r, x))))
            // We place the check here for more optimal stack operations.
            if iszero(sgt(x, 0)) {
                mstore(0x00, 0x1615e638) // `LnWadUndefined()`.
                revert(0x1c, 0x04)
            }
            // forgefmt: disable-next-item
            r := xor(r, byte(and(0x1f, shr(shr(r, x), 0x8421084210842108cc6318c6db6d54be)),
                0xf8f9f9faf9fdfafbf9fdfcfdfafbfcfef9fafdfafcfcfbfefafafcfbffffffff))

            // Reduce range of x to (1, 2) * 2**96
            // ln(2^k * x) = k * ln(2) + ln(x)
            x := shr(159, shl(r, x))

            // Evaluate using a (8, 8)-term rational approximation.
            // `p` is made monic, we will multiply by a scale factor later.
            // forgefmt: disable-next-item
            let p := sub( // This heavily nested expression is to avoid stack-too-deep for via-ir.
                sar(96, mul(add(43456485725739037958740375743393,
                sar(96, mul(add(24828157081833163892658089445524,
                sar(96, mul(add(3273285459638523848632254066296,
                    x), x))), x))), x)), 11111509109440967052023855526967)
            p := sub(sar(96, mul(p, x)), 45023709667254063763336534515857)
            p := sub(sar(96, mul(p, x)), 14706773417378608786704636184526)
            p := sub(mul(p, x), shl(96, 795164235651350426258249787498))
            // We leave `p` in `2**192` basis so we don't need to scale it back up for the division.

            // `q` is monic by convention.
            let q := add(5573035233440673466300451813936, x)
            q := add(71694874799317883764090561454958, sar(96, mul(x, q)))
            q := add(283447036172924575727196451306956, sar(96, mul(x, q)))
            q := add(401686690394027663651624208769553, sar(96, mul(x, q)))
            q := add(204048457590392012362485061816622, sar(96, mul(x, q)))
            q := add(31853899698501571402653359427138, sar(96, mul(x, q)))
            q := add(909429971244387300277376558375, sar(96, mul(x, q)))

            // `p / q` is in the range `(0, 0.125) * 2**96`.

            // Finalization, we need to:
            // - Multiply by the scale factor `s = 5.549…`.
            // - Add `ln(2**96 / 10**18)`.
            // - Add `k * ln(2)`.
            // - Multiply by `10**18 / 2**96 = 5**18 >> 78`.

            // The q polynomial is known not to have zeros in the domain.
            // No scaling required because p is already `2**96` too large.
            p := sdiv(p, q)
            // Multiply by the scaling factor: `s * 5**18 * 2**96`, base is now `5**18 * 2**192`.
            p := mul(1677202110996718588342820967067443963516166, p)
            // Add `ln(2) * k * 5**18 * 2**192`.
            // forgefmt: disable-next-item
            p := add(mul(16597577552685614221487285958193947469193820559219878177908093499208371, sub(159, r)), p)
            // Add `ln(2**96 / 10**18) * 5**18 * 2**192`.
            p := add(600920179829731861736702779321621459595472258049074101567377883020018308, p)
            // Base conversion: mul `2**18 / 2**192`.
            r := sar(174, p)
        }
    }
}
Compiler settingsShow
{
  "metadata": {
    "bytecodeHash": "ipfs"
  },
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": []
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.