Skip to content
Verified contractPartialMIT

DefibCreditVault

Address0xb26ed9e2a05e31d036f14df69a364e11a2cc9f08
Balance

0 ETH

 

Transactions

1

 

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
Via IR
yes
License
MIT
Match
Partialcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,139,981
Creation transaction
0xbd7b3a9388…4f1910e1fa

src/DefibCreditVault.sol

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

/*  DefibCreditVault — the ETH side of Defib AI (an OpenAI-compatible API paid in ETH).

    Two kinds of ETH live here, kept apart:

    1. FEE INFLOW (the "pool"). Plain ETH transfers — e.g. the $DEFIB coin's fee stream pointed at this contract —
       land in the pool. `holderBps` of every inflow (default 50%, set by the owner, on-chain) is earmarked for
       $DEFIB holder AI credits; the platform hands those out hourly as API credits (product access, not ETH, not
       withdrawable). The pool pays the AI provider's bills; the owner draws it with `withdrawPool`.

    2. USER CREDIT. `deposit()` puts ETH on your own balance. API usage is metered off-chain and the platform's
       operator settles it on-chain (`settle`), moving only what you actually used to the treasury — never more than
       your balance. You can always get the rest back: `requestWithdraw`, wait one hour (the window lets the operator
       settle usage you already made), then `withdraw`. No one else can move your balance.  */
contract DefibCreditVault {
  address public owner;
  address public operator;                 // settles metered usage
  address payable public treasury;         // receives settled usage
  uint16 public holderBps = 5000;          // share of fee inflow earmarked for $DEFIB holder credits
  uint64 public constant WITHDRAW_DELAY = 1 hours;

  // packed in one slot so a fee push stays far under a 40k-gas stipend (lifetime inflow / holders' share / still here)
  uint80 public poolIn; uint80 public holderEarmarked; uint80 public poolBalance;
  mapping(address => uint256) public balanceOf;      // user credit (ETH)
  uint256 public totalUserBalance;
  struct Pending { uint192 amount; uint64 unlockAt; }
  mapping(address => Pending) public pendingWithdraw;
  mapping(address => uint256) public totalSettled;
  bool private _lock;

  event FeeIn(address indexed from, uint256 amount, uint256 toHolders);
  event Deposited(address indexed user, uint256 amount);
  event WithdrawRequested(address indexed user, uint256 amount, uint64 unlockAt);
  event Withdrawn(address indexed user, uint256 amount);
  event Settled(address indexed user, uint256 amount, bytes32 ref);
  event PoolWithdrawn(address indexed to, uint256 amount);
  event HolderBpsSet(uint16 bps);

  modifier onlyOwner() { require(msg.sender == owner, "owner"); _; }
  modifier nonReentrant() { require(!_lock, "reentry"); _lock = true; _; _lock = false; }

  constructor(address _owner, address _operator, address payable _treasury) {
    require(_owner != address(0) && _treasury != address(0), "args");
    owner = _owner; operator = _operator; treasury = _treasury;
  }

  /// fee inflow: a plain transfer (kept cheap so a hook's 40k-gas push always succeeds)
  receive() external payable {
    uint256 h = msg.value * holderBps / 10000;
    poolIn += uint80(msg.value); holderEarmarked += uint80(h); poolBalance += uint80(msg.value);
    emit FeeIn(msg.sender, msg.value, h);
  }

  /// if a fee push ever fell back to "owed" in the Defib hook, anyone can pull it in here (it arrives as fee inflow)
  function pullOwed(address hook) external {
    (bool ok, ) = hook.call(abi.encodeWithSignature("claim(address)", address(0)));
    require(ok, "nothing owed");
  }

  /* ---------------- users ---------------- */

  function deposit() external payable { _deposit(msg.sender); }
  function depositFor(address user) external payable { _deposit(user); }
  function _deposit(address user) internal {
    require(msg.value > 0 && user != address(0), "zero");
    balanceOf[user] += msg.value; totalUserBalance += msg.value;
    emit Deposited(user, msg.value);
  }

  /// start a withdrawal; replaces any pending one. Usage made before the hour is up can still be settled.
  function requestWithdraw(uint256 amount) external {
    require(amount > 0 && amount <= balanceOf[msg.sender], "amount");
    uint64 t = uint64(block.timestamp) + WITHDRAW_DELAY;
    pendingWithdraw[msg.sender] = Pending(uint192(amount), t);
    emit WithdrawRequested(msg.sender, amount, t);
  }
  function cancelWithdraw() external { delete pendingWithdraw[msg.sender]; emit WithdrawRequested(msg.sender, 0, 0); }

  function withdraw() external nonReentrant returns (uint256 amount) {
    Pending memory p = pendingWithdraw[msg.sender];
    require(p.amount > 0, "no request"); require(block.timestamp >= p.unlockAt, "wait");
    amount = p.amount; uint256 b = balanceOf[msg.sender]; if (amount > b) amount = b;    // settled usage comes off first
    require(amount > 0, "nothing");
    delete pendingWithdraw[msg.sender];
    balanceOf[msg.sender] = b - amount; totalUserBalance -= amount;
    (bool ok, ) = msg.sender.call{ value: amount }(""); require(ok, "send");
    emit Withdrawn(msg.sender, amount);
  }

  /* ---------------- operator ---------------- */

  /// settle metered usage: each amount is capped at the user's balance. `ref` = the off-chain batch id.
  function settle(address[] calldata users, uint256[] calldata amounts, bytes32 ref) external nonReentrant returns (uint256 total) {
    require(msg.sender == operator || msg.sender == owner, "operator");
    require(users.length == amounts.length, "len");
    for (uint256 i = 0; i < users.length; i++) {
      uint256 b = balanceOf[users[i]]; uint256 a = amounts[i] > b ? b : amounts[i];
      if (a == 0) continue;
      balanceOf[users[i]] = b - a; totalSettled[users[i]] += a; total += a;
      emit Settled(users[i], a, ref);
    }
    totalUserBalance -= total;
    if (total > 0) { (bool ok, ) = treasury.call{ value: total }(""); require(ok, "treasury"); }
  }

  /* ---------------- owner ---------------- */

  /// fee ETH only — user balances can never be touched by this
  function withdrawPool(address payable to, uint256 amount) external onlyOwner nonReentrant {
    require(amount <= poolBalance, "pool");
    poolBalance -= uint80(amount);
    (bool ok, ) = to.call{ value: amount }(""); require(ok, "send");
    emit PoolWithdrawn(to, amount);
  }
  function setHolderBps(uint16 bps) external onlyOwner { require(bps <= 10000, "bps"); holderBps = bps; emit HolderBpsSet(bps); }
  function setOperator(address o) external onlyOwner { operator = o; }
  function setTreasury(address payable t) external onlyOwner { require(t != address(0), "zero"); treasury = t; }
  function setOwner(address o) external onlyOwner { require(o != address(0), "zero"); owner = o; }

  /// ETH the vault holds = user balances + pool (anything else was force-sent)
  function solvent() external view returns (bool) { return address(this).balance >= totalUserBalance + poolBalance; }
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "none",
    "useLiteralContent": false
  },
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": [
    "@v4/=lib/v4-core/src/",
    "forge-std/=lib/forge-std/src/",
    "@uniswap/v4-core/=lib/v4-core/",
    "@ensdomains/=lib/v4-core/node_modules/@ensdomains/",
    "@openzeppelin/=lib/v4-core/lib/openzeppelin-contracts/",
    "ds-test/=lib/v4-core/lib/forge-std/lib/ds-test/src/",
    "hardhat/=lib/v4-core/node_modules/hardhat/",
    "openzeppelin-contracts/=lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/",
    "solmate/=lib/v4-core/lib/solmate/",
    "v4-core/=lib/v4-core/src/",
    "v4-periphery/=lib/v4-periphery/src/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.