Verified contractExactMIT
TimeCapsuleRegistryV4
Address0x20522aa43ea36902e67e9c043dbd39d08311a8bd
- 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
- No
- EVM version
- cancun
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,366,995
- Creation transaction
- 0xdb9fc9d91c…91b010fa41
src/TimeCapsuleRegistryV4.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
interface INativeFeeOracle {
struct Price {
int64 price;
uint64 conf;
int32 expo;
uint256 publishTime;
}
function getUpdateFee(bytes[] calldata updateData) external view returns (uint256);
function updatePriceFeeds(bytes[] calldata updateData) external payable;
function getPriceNoOlderThan(bytes32 id, uint256 age) external view returns (Price memory);
}
/// @notice Immutable capsule registry: USD pricing, native-coin settlement, one owner-signed transaction.
/// @dev All supported chains use 18-decimal native coins. Oracle and feed are pinned at deployment.
contract TimeCapsuleRegistryV4 {
uint256 public constant MAX_PUBLIC_TITLE_BYTES = 2048;
uint256 public constant MAX_PUBLIC_TITLE_CHARACTERS = 512;
uint256 public constant MAX_PROTOCOL_FEE_USD_MICROS = 1_000_000_000_000;
uint16 public constant REGISTRY_VERSION = 4;
enum CapsuleMode {
Public,
PrivatePassword
}
struct CapsuleMeta {
address owner;
uint64 createdAt;
uint64 unlockAt;
CapsuleMode mode;
uint16 formatVersion;
bytes32 payloadHash;
}
struct FeeReview {
uint256 maxProtocolFee;
uint256 maxOracleFee;
uint64 expectedPolicyRevision;
uint64 validUntil;
}
address public immutable admin;
INativeFeeOracle public immutable nativeFeeOracle;
bytes32 public immutable nativeUsdFeedId;
uint32 public immutable maxPriceAge;
uint16 public immutable maxConfidenceBps;
address payable public feeRecipient;
/// @notice USD millionths: 10 = $0.00001; 1_000_000 = $1; 0 = free.
uint256 public protocolFee;
uint64 public feePolicyRevision = 1;
uint32 public maxPayloadSize;
uint32 public minLockDuration;
uint256 public totalCapsulesCount;
mapping(uint256 => CapsuleMeta) public capsules;
mapping(uint256 => string) public publicTitles;
mapping(address => uint256[]) private _ownerCapsules;
uint256 private _status = 1;
event CapsuleSealed(
uint256 indexed capsuleId,
address indexed owner,
uint64 unlockAt,
CapsuleMode mode,
uint16 formatVersion,
bytes32 payloadHash,
bytes payload
);
event PublicTitleRecorded(uint256 indexed capsuleId, address indexed owner, string title);
event CapsuleFeePaid(uint256 indexed capsuleId, address indexed token, uint256 amount);
event NativeFeeSettled(
uint256 indexed capsuleId,
address indexed recipient,
uint256 feeUsdMicros,
uint256 nativeFee,
uint256 oracleFee,
uint256 refund,
uint64 policyRevision
);
event ProtocolFeeUpdated(uint256 oldFee, uint256 newFee);
event FeeRecipientUpdated(address indexed oldRecipient, address indexed newRecipient);
event FeePolicyRevisionUpdated(uint64 revision);
event MaxPayloadSizeUpdated(uint32 oldSize, uint32 newSize);
event MinLockDurationUpdated(uint32 oldDuration, uint32 newDuration);
event FeesWithdrawn(address indexed recipient, uint256 amount);
error Unauthorized();
error ZeroAddress();
error UnsupportedChain();
error InvalidConfig();
error InvalidOraclePrice();
error StaleOraclePrice();
error UnsupportedOracleExponent(int32 exponent);
error FeePolicyChanged();
error FeeQuoteExpired();
error FeeLimitExceeded();
error InsufficientFee(uint256 required, uint256 provided);
error UnexpectedNativeValue();
error NativePaymentRequired();
error ReentrancyGuardReentrantCall();
error TransferFailed();
error InvalidUnlockTime(uint64 unlockAt, uint256 currentTimestamp);
error InvalidPayloadLength(uint256 length, uint32 maxAllowed);
error PayloadHashMismatch(bytes32 providedHash, bytes32 computedHash);
error CapsuleNotFound(uint256 capsuleId);
error PublicTitleRequired();
error PrivateTitleMustBeEmpty();
error PublicTitleBlank();
error PublicTitleTooLong(uint256 length, uint256 maxAllowed);
error PublicTitleTooManyCharacters(uint256 length, uint256 maxAllowed);
error InvalidPublicTitleEncoding();
modifier onlyAdmin() {
if (msg.sender != admin) revert Unauthorized();
_;
}
modifier nonReentrant() {
if (_status == 2) revert ReentrancyGuardReentrantCall();
_status = 2;
_;
_status = 1;
}
constructor(
address payable recipient,
uint256 initialFeeUsdMicros,
uint32 payloadLimit,
uint32 lockDuration,
address oracle,
bytes32 feedId,
uint32 priceAge,
uint16 confidenceBps
) {
uint256 id = block.chainid;
if (!(id == 143 || id == 1 || id == 56 || id == 42161 || id == 8453 || id == 10 || id == 137 || id == 10143
|| id == 11155111 || id == 97 || id == 421614 || id == 84532 || id == 11155420 || id == 80002)) {
revert UnsupportedChain();
}
if (
recipient == address(0) || recipient == address(this) || oracle.code.length == 0 || feedId == bytes32(0)
|| initialFeeUsdMicros > MAX_PROTOCOL_FEE_USD_MICROS || payloadLimit == 0 || payloadLimit > 65536
|| priceAge == 0 || priceAge > 3600 || confidenceBps == 0 || confidenceBps > 1000
) revert InvalidConfig();
admin = msg.sender;
feeRecipient = recipient;
protocolFee = initialFeeUsdMicros;
maxPayloadSize = payloadLimit;
minLockDuration = lockDuration;
nativeFeeOracle = INativeFeeOracle(oracle);
nativeUsdFeedId = feedId;
maxPriceAge = priceAge;
maxConfidenceBps = confidenceBps;
}
/// @dev Simulate with eth_call. This is never a separate user-paid transaction.
function quoteNativeFee(bytes[] calldata priceUpdate)
external
payable
nonReentrant
returns (uint256 nativeFee, uint256 oracleFee)
{
if (protocolFee == 0) {
if (msg.value != 0) revert UnexpectedNativeValue();
return (0, 0);
}
oracleFee = nativeFeeOracle.getUpdateFee(priceUpdate);
if (msg.value != oracleFee) revert InsufficientFee(oracleFee, msg.value);
nativeFeeOracle.updatePriceFeeds{value: oracleFee}(priceUpdate);
nativeFee = _nativeFeeForUsd(protocolFee);
}
function sealCapsuleWithNative(
bytes[] calldata priceUpdate,
uint64 unlockAt,
CapsuleMode mode,
uint16 formatVersion,
bytes32 payloadHash,
bytes calldata payload,
string calldata publicTitle,
FeeReview calldata review
) external payable nonReentrant returns (uint256 capsuleId) {
(uint256 nativeFee, uint256 oracleFee) = _collectFee(priceUpdate, review);
capsuleId = _processSeal(unlockAt, mode, formatVersion, payloadHash, payload, publicTitle);
_settle(capsuleId, nativeFee, oracleFee);
}
/// @notice Compatibility entry for free capsules. Cannot bypass a nonzero protocol fee.
function sealCapsule(
uint64 unlockAt,
CapsuleMode mode,
uint16 formatVersion,
bytes32 payloadHash,
bytes calldata payload,
string calldata publicTitle
) external payable nonReentrant returns (uint256 capsuleId) {
if (protocolFee != 0) revert NativePaymentRequired();
if (msg.value != 0) revert UnexpectedNativeValue();
capsuleId = _processSeal(unlockAt, mode, formatVersion, payloadHash, payload, publicTitle);
_settle(capsuleId, 0, 0);
}
function _collectFee(bytes[] calldata priceUpdate, FeeReview calldata review)
private
returns (uint256 nativeFee, uint256 oracleFee)
{
if (review.expectedPolicyRevision != feePolicyRevision) revert FeePolicyChanged();
if (block.timestamp > review.validUntil) revert FeeQuoteExpired();
if (protocolFee != 0) {
oracleFee = nativeFeeOracle.getUpdateFee(priceUpdate);
if (oracleFee > review.maxOracleFee) revert FeeLimitExceeded();
if (msg.value < oracleFee) revert InsufficientFee(oracleFee, msg.value);
nativeFeeOracle.updatePriceFeeds{value: oracleFee}(priceUpdate);
nativeFee = _nativeFeeForUsd(protocolFee);
}
if (nativeFee > review.maxProtocolFee) revert FeeLimitExceeded();
if (msg.value < nativeFee + oracleFee) revert InsufficientFee(nativeFee + oracleFee, msg.value);
}
function _nativeFeeForUsd(uint256 usdMicros) private view returns (uint256) {
INativeFeeOracle.Price memory price = nativeFeeOracle.getPriceNoOlderThan(nativeUsdFeedId, maxPriceAge);
if (price.price <= 0 || uint256(price.conf) * 10000 > uint256(uint64(price.price)) * maxConfidenceBps) {
revert InvalidOraclePrice();
}
if (
price.publishTime == 0 || price.publishTime > block.timestamp
|| block.timestamp - price.publishTime > maxPriceAge
) {
revert StaleOraclePrice();
}
if (price.expo < -18 || price.expo > 0) revert UnsupportedOracleExponent(price.expo);
uint256 numerator = usdMicros * 10 ** uint256(int256(18) - int256(price.expo));
uint256 denominator = uint256(uint64(price.price)) * 1_000_000;
return numerator / denominator + (numerator % denominator == 0 ? 0 : 1);
}
function _settle(uint256 capsuleId, uint256 nativeFee, uint256 oracleFee) private {
uint256 refund = msg.value - nativeFee - oracleFee;
if (nativeFee != 0) {
(bool sent,) = feeRecipient.call{value: nativeFee}("");
if (!sent) revert TransferFailed();
}
if (refund != 0) {
(bool refunded,) = payable(msg.sender).call{value: refund}("");
if (!refunded) revert TransferFailed();
}
emit CapsuleFeePaid(capsuleId, address(0), nativeFee);
emit NativeFeeSettled(capsuleId, feeRecipient, protocolFee, nativeFee, oracleFee, refund, feePolicyRevision);
}
function _processSeal(
uint64 unlockAt,
CapsuleMode mode,
uint16 formatVersion,
bytes32 payloadHash,
bytes calldata payload,
string calldata publicTitle
) internal returns (uint256 capsuleId) {
if (unlockAt <= block.timestamp + minLockDuration) {
revert InvalidUnlockTime(unlockAt, block.timestamp);
}
if (payload.length == 0 || payload.length > maxPayloadSize) {
revert InvalidPayloadLength(payload.length, maxPayloadSize);
}
_validateTitle(mode, publicTitle);
bytes32 computedHash = sha256(payload);
if (payloadHash != computedHash) {
revert PayloadHashMismatch(payloadHash, computedHash);
}
totalCapsulesCount++;
capsuleId = totalCapsulesCount;
capsules[capsuleId] = CapsuleMeta({
owner: msg.sender,
createdAt: uint64(block.timestamp),
unlockAt: unlockAt,
mode: mode,
formatVersion: formatVersion,
payloadHash: payloadHash
});
if (mode == CapsuleMode.Public) {
publicTitles[capsuleId] = publicTitle;
}
_ownerCapsules[msg.sender].push(capsuleId);
emit CapsuleSealed(capsuleId, msg.sender, unlockAt, mode, formatVersion, payloadHash, payload);
if (mode == CapsuleMode.Public) {
emit PublicTitleRecorded(capsuleId, msg.sender, publicTitle);
}
}
function _validateTitle(CapsuleMode mode, string calldata publicTitle) internal pure {
bytes calldata titleBytes = bytes(publicTitle);
if (mode == CapsuleMode.PrivatePassword) {
if (titleBytes.length != 0) revert PrivateTitleMustBeEmpty();
return;
}
if (titleBytes.length == 0) revert PublicTitleRequired();
if (titleBytes.length > MAX_PUBLIC_TITLE_BYTES) {
revert PublicTitleTooLong(titleBytes.length, MAX_PUBLIC_TITLE_BYTES);
}
uint256 titleCharacters = _countUtf8Characters(titleBytes);
if (titleCharacters > MAX_PUBLIC_TITLE_CHARACTERS) {
revert PublicTitleTooManyCharacters(titleCharacters, MAX_PUBLIC_TITLE_CHARACTERS);
}
bool onlyAsciiWhitespace = true;
for (uint256 i = 0; i < titleBytes.length; i++) {
bytes1 character = titleBytes[i];
if (
character != 0x20 // space
&& character != 0x09 // horizontal tab
&& character != 0x0a // line feed
&& character != 0x0b // vertical tab
&& character != 0x0c // form feed
&& character != 0x0d // carriage return
) {
onlyAsciiWhitespace = false;
break;
}
}
if (onlyAsciiWhitespace) revert PublicTitleBlank();
}
function _countUtf8Characters(bytes calldata value) internal pure returns (uint256 characters) {
uint256 index;
while (index < value.length) {
index = _nextUtf8CharacterIndex(value, index);
characters++;
}
}
function _nextUtf8CharacterIndex(bytes calldata value, uint256 index) private pure returns (uint256) {
uint8 first = uint8(value[index]);
if (first <= 0x7f) return index + 1;
if (first >= 0xc2 && first <= 0xdf) {
if (index + 1 >= value.length || !_isUtf8Continuation(value[index + 1])) {
revert InvalidPublicTitleEncoding();
}
return index + 2;
}
if (first >= 0xe0 && first <= 0xef) {
if (index + 2 >= value.length) revert InvalidPublicTitleEncoding();
uint8 second = uint8(value[index + 1]);
if (
!_isUtf8Continuation(value[index + 1]) || !_isUtf8Continuation(value[index + 2])
|| (first == 0xe0 && second < 0xa0) || (first == 0xed && second >= 0xa0)
) revert InvalidPublicTitleEncoding();
return index + 3;
}
if (first >= 0xf0 && first <= 0xf4) {
if (index + 3 >= value.length) revert InvalidPublicTitleEncoding();
uint8 second = uint8(value[index + 1]);
if (
!_isUtf8Continuation(value[index + 1]) || !_isUtf8Continuation(value[index + 2])
|| !_isUtf8Continuation(value[index + 3]) || (first == 0xf0 && second < 0x90)
|| (first == 0xf4 && second > 0x8f)
) revert InvalidPublicTitleEncoding();
return index + 4;
}
revert InvalidPublicTitleEncoding();
}
function _isUtf8Continuation(bytes1 value) private pure returns (bool) {
uint8 byteValue = uint8(value);
return byteValue >= 0x80 && byteValue <= 0xbf;
}
// --- Views ---
function getCapsuleMeta(uint256 capsuleId) external view returns (CapsuleMeta memory) {
CapsuleMeta memory meta = capsules[capsuleId];
if (meta.owner == address(0)) revert CapsuleNotFound(capsuleId);
return meta;
}
function getOwnerCapsuleCount(address capsuleOwner) external view returns (uint256) {
return _ownerCapsules[capsuleOwner].length;
}
function getOwnerCapsuleIds(address capsuleOwner, uint256 offset, uint256 limit)
external
view
returns (uint256[] memory)
{
uint256[] storage allIds = _ownerCapsules[capsuleOwner];
uint256 total = allIds.length;
if (offset >= total || limit == 0) {
return new uint256[](0);
}
uint256 remaining = total - offset;
uint256 resultLength = limit < remaining ? limit : remaining;
uint256[] memory result = new uint256[](resultLength);
for (uint256 i = 0; i < resultLength; i++) {
result[i] = allIds[offset + i];
}
return result;
}
function totalCapsules() external view returns (uint256) {
return totalCapsulesCount;
}
function setProtocolFee(uint256 newFeeUsdMicros) external nonReentrant onlyAdmin {
if (newFeeUsdMicros > MAX_PROTOCOL_FEE_USD_MICROS) revert InvalidConfig();
emit ProtocolFeeUpdated(protocolFee, newFeeUsdMicros);
protocolFee = newFeeUsdMicros;
emit FeePolicyRevisionUpdated(++feePolicyRevision);
}
function setFeeRecipient(address payable recipient) external nonReentrant onlyAdmin {
if (recipient == address(0) || recipient == address(this)) revert ZeroAddress();
emit FeeRecipientUpdated(feeRecipient, recipient);
feeRecipient = recipient;
emit FeePolicyRevisionUpdated(++feePolicyRevision);
}
function setMaxPayloadSize(uint32 newSize) external nonReentrant onlyAdmin {
if (newSize == 0 || newSize > 65536) revert InvalidConfig();
emit MaxPayloadSizeUpdated(maxPayloadSize, newSize);
maxPayloadSize = newSize;
}
function setMinLockDuration(uint32 duration) external nonReentrant onlyAdmin {
emit MinLockDurationUpdated(minLockDuration, duration);
minLockDuration = duration;
}
/// @notice Only forced/accidental balance; ordinary protocol fees are forwarded during sealing.
function withdrawFees() external nonReentrant {
uint256 balance = address(this).balance;
if (balance == 0) return;
emit FeesWithdrawn(feeRecipient, balance);
(bool sent,) = feeRecipient.call{value: balance}("");
if (!sent) revert TransferFailed();
}
}
Compiler settingsShow
{
"viaIR": false,
"metadata": {
"appendCBOR": true,
"bytecodeHash": "ipfs",
"useLiteralContent": false
},
"libraries": {},
"optimizer": {
"runs": 200,
"enabled": false
},
"evmVersion": "cancun",
"remappings": [
"forge-std/=lib/forge-std/src/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.