Skip to content
Verified contractExactMIT

UnspentDeposits

Address0xcda54d26b2b79d471399e31ae80a5bd48eb950be
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,164,591
Creation transaction
0x26d4417afd…0a5560e55e

contracts/contracts/UnspentDepositDesk.sol

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

import {UnspentSignatureProbe} from "./UnspentInterfaces.sol";

/*
+-----------------------------------------------------------------------------+
|  DEPOSIT ATTESTATION DESK                                                   |
|                                                                             |
|  Where a locked API key becomes a number the chain can trust.               |
|                                                                             |
|  The relay reads the key, signs what it found, and the desk checks that     |
|  signature before it lets the ledger mint a single note against it.         |
+-----------------------------------------------------------------------------+
|  house rules, in the order the desk applies them                            |
|                                                                             |
|    [1]  the key balance must have been readable by the relay                |
|    [2]  the key must carry inference rights and nothing else                |
|    [3]  the expiry must sit at least seven days past the attestation        |
|    [4]  the balance must cover at least one whole note                      |
|    [5]  once minted against, a deposit cannot be minted against again       |
|    [6]  a balance that falls for any reason except redemptions freezes it   |
|                                                                             |
|  A call that breaks a rule returns false and leaves a Declined event with   |
|  the number of the rule. It never reverts, so a batching relay can keep     |
|  going when one entry in the batch is bad.                                  |
+-----------------------------------------------------------------------------+
|  project   https://unspentllm.com                                           |
+-----------------------------------------------------------------------------+
*/
contract UnspentDeposits {
    uint64 public constant MIN_LEAD = 7 days;
    uint256 public constant NOTE_MICRO = 1_000_000;
    uint256 public constant TOLERANCE_MICRO = 10_000;
    uint8 public constant CLASS_COUNT = 3; // 0 frontier, 1 fast, 2 embedding

    bytes32 private constant DEPOSIT_TAG = keccak256("unspent.desk.deposit.v1");
    bytes32 private constant REPORT_TAG = keccak256("unspent.desk.report.v1");

    struct Deposit {
        address depositor;
        uint8 cls;
        uint64 expiry;
        bool frozen;
        bool minted;
        bool lapsed;
        uint256 balanceMicro;
        uint256 servedMicro;
        uint256 lastReport;
    }

    address public immutable token;
    address public immutable binder;
    address public relay;
    address public ledger;
    address public house;
    bool public bound;

    mapping(bytes32 => Deposit) private deposits;
    mapping(bytes32 => bool) public known;

    uint256 public depositCount;
    uint256 public frozenCount;
    uint256 public attestedMicro;

    event Attested(bytes32 indexed depositId, address indexed depositor, uint8 cls, uint64 expiry, uint256 balanceMicro);
    event Declined(bytes32 indexed depositId, uint8 rule);
    event Frozen(bytes32 indexed depositId, uint256 expectedMicro, uint256 reportedMicro);
    event Retracted(bytes32 indexed depositId);
    event Lapsed(bytes32 indexed depositId);
    event RelayChanged(address indexed relay);

    constructor(address relay_, address token_) {
        relay = relay_;
        token = token_;
        binder = msg.sender;
    }

    // [A] wiring. Called once by the token while it is being built.
    function bindNeighbours(address ledger_, address house_) external returns (bool) {
        if (msg.sender != binder || bound) return false;
        if (ledger_ == address(0) || house_ == address(0)) return false;
        ledger = ledger_;
        house = house_;
        bound = true;
        return true;
    }

    // [B] the relay's attestation of a freshly locked key.
    // Every argument below is covered by the signature, so none can be edited in transit.
    function attestDeposit(
        bytes32 depositId,
        address depositor,
        uint8 cls,
        uint256 balanceMicro,
        uint64 expiry,
        bool inferenceOnly,
        bool readable,
        bytes calldata sig
    ) external returns (bool) {
        if (known[depositId] || depositor == address(0) || cls >= CLASS_COUNT) {
            emit Declined(depositId, 0);
            return false;
        }
        if (!readable) {
            emit Declined(depositId, 1);
            return false;
        }
        if (!inferenceOnly) {
            emit Declined(depositId, 2);
            return false;
        }
        if (expiry < block.timestamp + MIN_LEAD) {
            emit Declined(depositId, 3);
            return false;
        }
        if (balanceMicro < NOTE_MICRO) {
            emit Declined(depositId, 4);
            return false;
        }
        bytes32 digest = keccak256(
            abi.encode(DEPOSIT_TAG, address(this), block.chainid, depositId, depositor, cls, balanceMicro, expiry, inferenceOnly, readable)
        );
        if (!UnspentSignatureProbe.accepts(relay, digest, sig)) {
            emit Declined(depositId, 7);
            return false;
        }
        Deposit storage d = deposits[depositId];
        d.depositor = depositor;
        d.cls = cls;
        d.expiry = expiry;
        d.balanceMicro = balanceMicro;
        known[depositId] = true;
        depositCount += 1;
        attestedMicro += balanceMicro;
        emit Attested(depositId, depositor, cls, expiry, balanceMicro);
        return true;
    }

    // [C] a later reading of the same key. The relay passes a rising nonce so an old
    // reading cannot be replayed to undo a freeze.
    function reportBalance(bytes32 depositId, uint256 balanceMicro, uint256 nonce, bytes calldata sig)
        external
        returns (bool)
    {
        Deposit storage d = deposits[depositId];
        if (!known[depositId] || d.lapsed || nonce <= d.lastReport) return false;
        bytes32 digest = keccak256(abi.encode(REPORT_TAG, address(this), block.chainid, depositId, balanceMicro, nonce));
        if (!UnspentSignatureProbe.accepts(relay, digest, sig)) return false;
        d.lastReport = nonce;
        uint256 expected = d.balanceMicro > d.servedMicro ? d.balanceMicro - d.servedMicro : 0;
        if (!d.frozen && balanceMicro + TOLERANCE_MICRO < expected) {
            d.frozen = true;
            frozenCount += 1;
            emit Frozen(depositId, expected, balanceMicro);
        }
        return true;
    }

    // [D] a depositor who changed their mind before anything was minted
    function retractUnminted(bytes32 depositId) external returns (bool) {
        Deposit storage d = deposits[depositId];
        if (!known[depositId] || d.minted || d.lapsed || d.depositor != msg.sender) return false;
        d.lapsed = true;
        emit Retracted(depositId);
        return true;
    }

    // [E] hooks used by the ledger and the clearing house, and nobody else
    function markMinted(bytes32 depositId) external returns (bool) {
        Deposit storage d = deposits[depositId];
        if (msg.sender != ledger || !known[depositId] || d.minted || d.frozen || d.lapsed) return false;
        d.minted = true;
        return true;
    }

    function recordServed(bytes32 depositId, uint256 micro) external returns (bool) {
        if (msg.sender != house || !known[depositId]) return false;
        deposits[depositId].servedMicro += micro;
        return true;
    }

    // [F] relay housekeeping
    function rotateRelay(address next) external returns (bool) {
        if (msg.sender != relay || next == address(0)) return false;
        relay = next;
        emit RelayChanged(next);
        return true;
    }

    // execute: sweeps one stale deposit. If its expiry has passed and it never minted,
    // the deposit is closed for good. Anyone may call it.
    function execute(uint256 depositNumber) external returns (bool) {
        bytes32 depositId = bytes32(depositNumber);
        Deposit storage d = deposits[depositId];
        if (!known[depositId] || d.lapsed || d.minted || d.expiry > block.timestamp) return false;
        d.lapsed = true;
        emit Lapsed(depositId);
        return true;
    }

    // [G] reads
    function depositInfo(bytes32 depositId)
        external
        view
        returns (address depositor, uint8 cls, uint64 expiry, uint256 balanceMicro, bool frozen, bool minted)
    {
        Deposit storage d = deposits[depositId];
        bool closed = d.lapsed;
        return (closed ? address(0) : d.depositor, d.cls, d.expiry, d.balanceMicro, d.frozen, d.minted);
    }

    function servedMicroOf(bytes32 depositId) external view returns (uint256) {
        return deposits[depositId].servedMicro;
    }

    function previewNotes(bytes32 depositId) external view returns (uint256) {
        return deposits[depositId].balanceMicro / NOTE_MICRO;
    }
}

contracts/contracts/UnspentInterfaces.sol

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

/*
  margin notes
  ~~~~~~~~~~~~
  Everything the Unspent contracts say to each other is declared here, so that each
  contract can be read without opening the other five. Nothing in this file holds state.

  Units used across the suite
    note        one credit note, worth one US dollar of inference inside a model class
    micro       one millionth of a dollar, the unit the relay reports key balances in
    bps         basis points, so 50 means half of one percent

  The project lives at https://unspentllm.com
*/

interface IUnspentToken {
    function balanceOf(address who) external view returns (uint256);
    function allowance(address owner, address spender) external view returns (uint256);
    function transfer(address to, uint256 amount) external returns (bool);
    function transferFrom(address from, address to, uint256 amount) external returns (bool);
    function burn(uint256 amount) external;
}

interface IUnspentDepositDesk {
    function relay() external view returns (address);
    function depositInfo(bytes32 depositId)
        external
        view
        returns (address depositor, uint8 cls, uint64 expiry, uint256 balanceMicro, bool frozen, bool minted);
    function markMinted(bytes32 depositId) external returns (bool);
    function recordServed(bytes32 depositId, uint256 micro) external returns (bool);
}

interface IUnspentNoteLedger {
    function balanceOf(address who, uint256 id) external view returns (uint256);
    function isLive(uint256 id) external view returns (bool);
    function isKnown(uint256 id) external view returns (bool);
    function burnForRedemption(address holder, uint256 id, uint256 amount) external returns (bool);
    function setApprovalForAll(address operator, bool approved) external;
    function isApprovedForAll(address owner, address operator) external view returns (bool);
    function applyParam(uint8 key, uint256 value) external returns (bool);
}

interface IUnspentFeeConduit {
    function routeFee(uint256 id, uint256 notes) external returns (bool);
    function sweepExpired(uint256 id) external returns (bool);
    function applyParam(uint8 key, uint256 value) external returns (bool);
}

interface IUnspentClearingHouse {
    function totalServed() external view returns (uint256);
    function notifyRefund(uint256 amount) external returns (bool);
    function applyParam(uint8 key, uint256 value) external returns (bool);
}

interface IUnspentBondPool {
    function totalActive() external view returns (uint256);
    function notifyReward(uint256 amount) external returns (bool);
}

/* a swap venue for fee notes, chosen by a staker vote and replaceable by another one */
interface IUnspentBuybackAdapter {
    function swapNotes(uint256 id, uint256 notes, address to) external returns (uint256 out);
}

/*
  Signature check used by the desk and the clearing house. A signer with code is asked
  through the ERC-1271 call so that a multisig can act as the relay or as a holder; a
  plain address is checked with ecrecover. Nothing in here ever reverts.
*/
library UnspentSignatureProbe {
    uint256 private constant HALF_ORDER = 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0;

    function accepts(address signer, bytes32 digest, bytes memory sig) internal view returns (bool) {
        if (signer == address(0)) return false;
        if (signer.code.length > 0) {
            (bool called, bytes memory ret) =
                signer.staticcall(abi.encodeWithSelector(0x1626ba7e, digest, sig));
            if (!called || ret.length < 32) return false;
            return bytes4(abi.decode(ret, (bytes32))) == bytes4(0x1626ba7e);
        }
        if (sig.length != 65) return false;
        bytes32 r;
        bytes32 s;
        uint8 v;
        assembly {
            r := mload(add(sig, 32))
            s := mload(add(sig, 64))
            v := byte(0, mload(add(sig, 96)))
        }
        if (v < 27) v += 27;
        if (v != 27 && v != 28) return false;
        if (uint256(s) > HALF_ORDER) return false;
        return ecrecover(digest, v, r, s) == signer;
    }
}
Compiler settingsShow
{
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "remappings": []
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.