Skip to content
Verified contractExactMIT

EmberSeatRegistry

Address0xd29ee55842eebc01435d2d682337d0642e010d05
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.24

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.24+commit.e11b9ed9
Language
solidity
Optimization
Yes, 200 runs
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,121,336
Creation transaction
0x8b52372409…c2fcb89f57

ember/EmberSeatRegistry.sol

// SPDX-License-Identifier: MIT
// <<< ember world | EmberSeatRegistry >>>
pragma solidity 0.8.24;

/*
    Every Seat NFT in Ember World is a house on the canal. A house shows lit windows
    when its agent is online, and this contract lets anybody keep the hearth behind those
    windows burning. Logs thrown on the fire raise a seat's heat, and heat cools by
    one point every hour, so a hearth that nobody visits goes dark by itself.

    IMD Ember        : https://imdember.com/
    Agent portraits  : https://api.imd.fun/agents/by-token/{id}.svg
    Swarm feed       : https://api.imd.fun/swarm

*/
contract EmberSeatRegistry {
    address public constant SEATS = 0x0000eC93127BAA929E58E97dd0095A2BFb38ec1D;

    uint256 public constant MAX_HEAT = 240;
    uint256 public constant COOL_STEP = 1 hours;
    uint256 public constant MAX_BATCH = 25;
    uint256 public constant MAX_SIGN = 64;
    uint256 public constant LIGHT_LOGS = 6;
    uint256 public constant STOKE_CAP = 24;

    struct Hearth {
        uint64 stamp;
        uint32 heat;
        uint32 lifetime;
        address keeper;
    }

    mapping(uint256 => Hearth) private _hearths;
    mapping(uint256 => string) private _signs;
    mapping(uint256 => address) private _signers;
    mapping(address => uint256) private _lightsBy;
    mapping(address => uint256) private _logsBy;

    uint256[] private _trail;
    uint256 public totalLights;
    uint256 public totalLogs;
    uint256 public hottestSeat;

    event HearthStoked(uint256 indexed seatId, address indexed keeper, uint256 logs, uint256 heat);
    event SignHung(uint256 indexed seatId, address indexed signer, string text);

    // ---------------------------------------------------------------- writes

    function lightHearth(uint256 seatId) external returns (bool) {
        return _stoke(seatId, LIGHT_LOGS, msg.sender);
    }

    function stokeHearth(uint256 seatId, uint256 logs) external returns (bool) {
        if (logs > STOKE_CAP) logs = STOKE_CAP;
        return _stoke(seatId, logs, msg.sender);
    }

    function lightMany(uint256[] calldata seatIds) external returns (uint256 lit) {
        uint256 n = seatIds.length;
        if (n > MAX_BATCH) n = MAX_BATCH;
        for (uint256 i = 0; i < n; i++) {
            if (_stoke(seatIds[i], LIGHT_LOGS, msg.sender)) lit++;
        }
    }

    function lightRange(uint256 fromSeat, uint256 count) external returns (uint256 lit) {
        if (count > MAX_BATCH) count = MAX_BATCH;
        if (fromSeat > type(uint128).max) return 0;
        for (uint256 i = 0; i < count; i++) {
            if (_stoke(fromSeat + i, LIGHT_LOGS, msg.sender)) lit++;
        }
    }

    function hangSign(uint256 seatId, string calldata text) external returns (bool) {
        uint256 len = bytes(text).length;
        if (len == 0 || len > MAX_SIGN) return false;
        if (!_exists(seatId)) return false;
        _signs[seatId] = text;
        _signers[seatId] = msg.sender;
        emit SignHung(seatId, msg.sender, text);
        return true;
    }

    // ----------------------------------------------------------------- views

    function heatOf(uint256 seatId) public view returns (uint256) {
        Hearth memory h = _hearths[seatId];
        if (h.heat == 0) return 0;
        uint256 cooled = (block.timestamp - h.stamp) / COOL_STEP;
        return h.heat > cooled ? h.heat - cooled : 0;
    }

    function isLit(uint256 seatId) external view returns (bool) {
        return heatOf(seatId) > 0;
    }

    function hoursLeft(uint256 seatId) external view returns (uint256) {
        return heatOf(seatId);
    }

    function keeperOf(uint256 seatId) external view returns (address) {
        return _hearths[seatId].keeper;
    }

    function lastStokedAt(uint256 seatId) external view returns (uint256) {
        return _hearths[seatId].stamp;
    }

    function logsEver(uint256 seatId) external view returns (uint256) {
        return _hearths[seatId].lifetime;
    }

    function signOf(uint256 seatId) external view returns (string memory) {
        return _signs[seatId];
    }

    function signerOf(uint256 seatId) external view returns (address) {
        return _signers[seatId];
    }

    function houseExists(uint256 seatId) external view returns (bool) {
        return _exists(seatId);
    }

    function houseOwner(uint256 seatId) external view returns (address owner_) {
        (bool ok, bytes memory ret) = _ownerCall(seatId);
        if (ok && ret.length >= 32) {
            uint256 w = abi.decode(ret, (uint256));
            if (w <= type(uint160).max) owner_ = address(uint160(w));
        }
    }

    function houseImage(uint256 seatId) external pure returns (string memory) {
        return string.concat("https://api.imd.fun/agents/by-token/", _str(seatId), ".svg");
    }

    function lightsBy(address who) external view returns (uint256) {
        return _lightsBy[who];
    }

    function logsBy(address who) external view returns (uint256) {
        return _logsBy[who];
    }

    function litAmong(uint256[] calldata seatIds) external view returns (uint256 lit) {
        uint256 n = seatIds.length;
        if (n > 200) n = 200;
        for (uint256 i = 0; i < n; i++) {
            if (heatOf(seatIds[i]) > 0) lit++;
        }
    }

    function litInRange(uint256 fromSeat, uint256 count) external view returns (uint256 lit) {
        if (count > 200) count = 200;
        if (fromSeat > type(uint128).max) return 0;
        for (uint256 i = 0; i < count; i++) {
            if (heatOf(fromSeat + i) > 0) lit++;
        }
    }

    function hottestHeat() external view returns (uint256) {
        return heatOf(hottestSeat);
    }

    function trailLength() external view returns (uint256) {
        return _trail.length;
    }

    function trailAt(uint256 index) external view returns (uint256) {
        if (index >= _trail.length) return 0;
        return _trail[index];
    }

    function latestTrail(uint256 n) external view returns (uint256[] memory out) {
        if (n > 50) n = 50;
        uint256 len = _trail.length;
        if (n > len) n = len;
        out = new uint256[](n);
        for (uint256 i = 0; i < n; i++) {
            out[i] = _trail[len - 1 - i];
        }
    }

    // -------------------------------------------------------------- internal

    function _stoke(uint256 seatId, uint256 logs, address who) internal returns (bool) {
        if (logs == 0) return false;
        if (!_exists(seatId)) return false;
        Hearth storage h = _hearths[seatId];
        uint256 now_ = heatOf(seatId) + logs;
        if (now_ > MAX_HEAT) now_ = MAX_HEAT;
        h.heat = uint32(now_);
        h.stamp = uint64(block.timestamp);
        h.keeper = who;
        h.lifetime += uint32(logs);
        _lightsBy[who] += 1;
        _logsBy[who] += logs;
        totalLights += 1;
        totalLogs += logs;
        _trail.push(seatId);
        if (seatId != hottestSeat && now_ >= heatOf(hottestSeat)) hottestSeat = seatId;
        emit HearthStoked(seatId, who, logs, now_);
        return true;
    }

    function _ownerCall(uint256 seatId) internal view returns (bool ok, bytes memory ret) {
        if (SEATS.code.length == 0) return (false, "");
        (ok, ret) = SEATS.staticcall{gas: 80_000}(abi.encodeWithSelector(0x6352211e, seatId));
    }

    function _exists(uint256 seatId) internal view returns (bool) {
        (bool ok, bytes memory ret) = _ownerCall(seatId);
        if (!ok || ret.length < 32) return false;
        uint256 w = abi.decode(ret, (uint256));
        return w != 0 && w <= type(uint160).max;
    }

    function _str(uint256 v) internal pure returns (string memory) {
        if (v == 0) return "0";
        uint256 t = v;
        uint256 d;
        while (t != 0) { d++; t /= 10; }
        bytes memory b = new bytes(d);
        while (v != 0) { d--; b[d] = bytes1(uint8(48 + v % 10)); v /= 10; }
        return string(b);
    }
}
Compiler settingsShow
{
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "remappings": []
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.