// SPDX-License-Identifier: MIT pragma solidity ^0.8.24; import {IERC721Min, IPriceFeed, ICardDollar} from "./Interfaces.sol"; contract CardVault { uint256 public constant MAX_AGE = 3 days; uint256 public constant AUCTION_START_BPS = 12000; struct Vault { address owner; address collection; uint256 tokenId; uint256 debt; uint256 lastAccrual; uint256 auctionStart; bool open; } IPriceFeed public immutable feed; ICardDollar public immutable dollar; address public feeRecipient; uint256 public immutable maxLtvBps; uint256 public immutable liqBps; uint256 public immutable feeBpsPerYear; uint256 public immutable auctionDuration; address public admin; uint256 public badDebt; uint256 public totalFees; mapping(address => bool) public allowedCollections; mapping(uint256 => Vault) public vaults; mapping(uint256 => uint256) public interestOwed; mapping(uint256 => uint256) public auctionStartPrice; uint256 internal count; bool internal locked; event Opened(uint256 indexed vaultId, address indexed owner, address collection, uint256 tokenId, uint256 debt); event Minted(uint256 indexed vaultId, uint256 amount); event Repaid(uint256 indexed vaultId, uint256 amount, uint256 interestPart); event Closed(uint256 indexed vaultId); event AuctionStarted(uint256 indexed vaultId, uint256 startPrice); event AuctionCancelled(uint256 indexed vaultId); event AuctionBought(uint256 indexed vaultId, address indexed buyer, uint256 paid, uint256 burned, uint256 surplus, uint256 shortfall); event CollectionAllowed(address indexed collection, bool ok); modifier nonReentrant() { require(!locked, "reentrant"); locked = true; _; locked = false; } constructor( address feed_, address dollar_, address feeRecipient_, uint256 maxLtvBps_, uint256 liqBps_, uint256 feeBpsPerYear_, uint256 auctionDuration_, address admin_ ) { require(maxLtvBps_ > 0 && maxLtvBps_ <= liqBps_ && liqBps_ <= 10000, "bad ratios"); require(auctionDuration_ > 0, "bad duration"); require(feeRecipient_ != address(0), "zero fee recipient"); feed = IPriceFeed(feed_); dollar = ICardDollar(dollar_); feeRecipient = feeRecipient_; maxLtvBps = maxLtvBps_; liqBps = liqBps_; feeBpsPerYear = feeBpsPerYear_; auctionDuration = auctionDuration_; admin = admin_; } function allowCollection(address c, bool ok) external { require(msg.sender == admin, "not admin"); allowedCollections[c] = ok; emit CollectionAllowed(c, ok); } function vaultCount() external view returns (uint256) { return count; } function _pending(Vault storage v) internal view returns (uint256) { if (v.debt == 0) return 0; uint256 dt = block.timestamp - v.lastAccrual; return v.debt * feeBpsPerYear * dt / (10000 * 365 days); } function _accrue(uint256 id) internal { Vault storage v = vaults[id]; uint256 p = _pending(v); if (p > 0) { v.debt += p; interestOwed[id] += p; } v.lastAccrual = block.timestamp; } function debtOf(uint256 vaultId) public view returns (uint256) { Vault storage v = vaults[vaultId]; return v.debt + _pending(v); } function _freshPriceView(address c, uint256 id) internal view returns (uint256 p, bool fresh) { uint256 at; (p, at) = feed.priceOf(c, id); fresh = p > 0 && block.timestamp - at <= MAX_AGE; } function open(address collection, uint256 tokenId, uint256 mintAmount) external nonReentrant returns (uint256 vaultId) { require(allowedCollections[collection], "collection not allowed"); uint256 price = feed.freshPrice(collection, tokenId); require(mintAmount * 10000 <= price * maxLtvBps, "over max ltv"); IERC721Min(collection).transferFrom(msg.sender, address(this), tokenId); vaultId = ++count; vaults[vaultId] = Vault(msg.sender, collection, tokenId, mintAmount, block.timestamp, 0, true); if (mintAmount > 0) dollar.mint(msg.sender, mintAmount); emit Opened(vaultId, msg.sender, collection, tokenId, mintAmount); } function mintMore(uint256 vaultId, uint256 amount) external nonReentrant { Vault storage v = vaults[vaultId]; require(v.open, "not open"); require(v.owner == msg.sender, "not owner"); require(v.auctionStart == 0, "in auction"); _accrue(vaultId); uint256 price = feed.freshPrice(v.collection, v.tokenId); require((v.debt + amount) * 10000 <= price * maxLtvBps, "over max ltv"); v.debt += amount; dollar.mint(msg.sender, amount); emit Minted(vaultId, amount); } function repay(uint256 vaultId, uint256 amount) external nonReentrant { Vault storage v = vaults[vaultId]; require(v.open, "not open"); _accrue(vaultId); if (amount > v.debt) amount = v.debt; require(amount > 0, "nothing to repay"); uint256 interestPart = interestOwed[vaultId]; if (interestPart > amount) interestPart = amount; interestOwed[vaultId] -= interestPart; v.debt -= amount; dollar.burn(msg.sender, amount); if (interestPart > 0) { totalFees += interestPart; dollar.mint(feeRecipient, interestPart); } emit Repaid(vaultId, amount, interestPart); if (v.auctionStart != 0 && v.debt == 0) { v.auctionStart = 0; auctionStartPrice[vaultId] = 0; emit AuctionCancelled(vaultId); } } function close(uint256 vaultId) external nonReentrant { Vault storage v = vaults[vaultId]; require(v.open, "not open"); require(v.owner == msg.sender, "not owner"); _accrue(vaultId); require(v.debt == 0, "debt outstanding"); v.open = false; v.auctionStart = 0; IERC721Min(v.collection).transferFrom(address(this), v.owner, v.tokenId); emit Closed(vaultId); } function isLiquidatable(uint256 vaultId) public view returns (bool) { Vault storage v = vaults[vaultId]; if (!v.open) return false; (uint256 p, bool fresh) = _freshPriceView(v.collection, v.tokenId); if (!fresh) return false; return debtOf(vaultId) * 10000 > p * liqBps; } function startAuction(uint256 vaultId) external nonReentrant { Vault storage v = vaults[vaultId]; require(v.auctionStart == 0, "auction running"); require(isLiquidatable(vaultId), "not liquidatable"); _accrue(vaultId); uint256 p = feed.freshPrice(v.collection, v.tokenId); v.auctionStart = block.timestamp; auctionStartPrice[vaultId] = p * AUCTION_START_BPS / 10000; emit AuctionStarted(vaultId, auctionStartPrice[vaultId]); } function auctionPrice(uint256 vaultId) public view returns (uint256) { Vault storage v = vaults[vaultId]; if (!v.open || v.auctionStart == 0) return 0; uint256 elapsed = block.timestamp - v.auctionStart; if (elapsed >= auctionDuration) return 0; return auctionStartPrice[vaultId] * (auctionDuration - elapsed) / auctionDuration; } function buy(uint256 vaultId) external nonReentrant { Vault storage v = vaults[vaultId]; require(v.open, "not open"); require(v.auctionStart != 0, "no auction"); _accrue(vaultId); uint256 paid = auctionPrice(vaultId); uint256 debt = v.debt; uint256 burned = paid < debt ? paid : debt; uint256 surplus = paid - burned; uint256 shortfall = debt - burned; uint256 interestPart = interestOwed[vaultId]; if (interestPart > burned) interestPart = burned; v.debt = 0; interestOwed[vaultId] = 0; v.open = false; v.auctionStart = 0; auctionStartPrice[vaultId] = 0; badDebt += shortfall; if (paid > 0) dollar.burn(msg.sender, paid); if (interestPart > 0) { totalFees += interestPart; dollar.mint(feeRecipient, interestPart); } if (surplus > 0) dollar.mint(v.owner, surplus); IERC721Min(v.collection).transferFrom(address(this), msg.sender, v.tokenId); emit AuctionBought(vaultId, msg.sender, paid, burned, surplus, shortfall); } }