Verified contractExactMIT
CapVaultV5
Address0x7aa82b86e0e4d9722acb8ec46decc71b4c67c15f
- Balance
0 ETH
- Transactions
4
- Compiler
solc 0.8.38
- Same code
Only this contract
Details
- Compiler
- solc 0.8.38+commit.1835d247
- Language
- solidity
- Optimization
- Yes, 200 runs
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,428,566
- Creation transaction
- 0x3b4aadae10…c334c1ea40
CapVaultV5.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.38;
/// @notice Minimal ERC-20 surface used by the vault.
/// @dev `transfer` is declared returning bool per the standard, but the vault invokes it through a
/// low-level call, so tokens that return nothing (USDT-style) still work.
interface IERC20 {
function transfer(address to, uint256 amount) external returns (bool);
function balanceOf(address account) external view returns (uint256);
}
/// @title CapVaultV5
contract CapVaultV5 {
struct PendingSummary {
uint256 timestamp;
uint256 count;
bool isLocked;
uint256 approvalNonce;
bool payPending;
bool unlockPending;
bool rescuePending;
bool escapePending;
bool approvalKeyChangePending;
}
struct ChangePreApproval {
address newKey;
uint256 deadline;
uint8 count;
uint8 bitmap;
}
uint256 private constant WEEK = 7 days;
/// @dev Unix epoch (1970-01-01) was a Thursday, so Monday 00:00 UTC is day 4 of a week.
uint256 private constant MONDAY_OFFSET = 4 days;
IERC20 public immutable token;
address public immutable safe;
address public immutable payee;
address public approvalKey;
address[5] public owners;
uint256 public immutable start;
uint256 public immutable weeklyCap;
uint256 public week;
uint256 public spent;
bool public locked;
uint256 public unlockAt;
bool public unlockByPassword;
address public rescueTo;
uint256 public rescueAt;
bool public rescueByPassword;
uint256 public approvalNonce;
uint256 public proposalId;
uint256 public nextProposalId;
address public pendingApprovalKey;
uint256 public approvalKeyChangeAt;
/// @dev Pre-approvals for a key change stay valid for this long after they are cast.
uint256 public constant APPROVAL_WINDOW = 2 days;
uint256 public approvalDeadline;
uint8 public approvalCount;
uint8 public approvalBitmap;
bool public approvalReady;
uint8 public cancelApprovalCount;
uint8 public cancelApprovalBitmap;
mapping(uint256 => ChangePreApproval) private _changePreApprovals;
uint256 public payAt;
uint256 public payAmount;
address public escapeTarget;
uint256 public escapeValue;
bytes public escapeData;
uint256 public escapeAt;
bool public escapeByPassword;
bytes32 public immutable DOMAIN_SEPARATOR;
bool private _entered;
uint256 public constant UNLOCK_DELAY_OWNER = 2 weeks;
uint256 public constant UNLOCK_DELAY_PASSWORD = 1 weeks;
uint256 public constant RESCUE_DELAY_OWNER = 60 days;
uint256 public constant RESCUE_DELAY_PASSWORD = 30 days;
// Escape timelocks are 1.5x the matching rescue timelock.
uint256 public constant ESCAPE_DELAY_OWNER = 90 days; // 1.5 * 60 days
uint256 public constant ESCAPE_DELAY_PASSWORD = 45 days; // 1.5 * 30 days
uint256 public constant PAY_DELAY = 1 days;
/// @dev Queued key change becomes executable after this delay. Must stay below
/// APPROVAL_WINDOW so a ripe request still has a live execution window.
uint256 public constant APPROVAL_KEY_CHANGE_DELAY = 1 days;
bytes32 private constant UNLOCK_TYPEHASH =
keccak256("Unlock(uint256 nonce,uint256 deadline)");
bytes32 private constant RESCUE_TYPEHASH =
keccak256("Rescue(address to,uint256 nonce,uint256 deadline)");
bytes32 private constant CANCEL_UNLOCK_TYPEHASH =
keccak256("CancelUnlock(uint256 nonce,uint256 deadline)");
bytes32 private constant CANCEL_RESCUE_TYPEHASH =
keccak256("CancelRescue(uint256 nonce,uint256 deadline)");
bytes32 private constant ESCAPE_TYPEHASH =
keccak256("Escape(address target,uint256 value,bytes32 dataHash,uint256 nonce,uint256 deadline)");
bytes32 private constant CANCEL_ESCAPE_TYPEHASH =
keccak256("CancelEscape(bytes32 callHash,uint256 nonce,uint256 deadline)");
bytes32 private constant CHANGE_APPROVAL_KEY_TYPEHASH =
keccak256("ChangeApprovalKey(uint256 proposalId,address newApprovalKey,uint256 deadline)");
bytes32 private constant CANCEL_CHANGE_APPROVAL_KEY_TYPEHASH =
keccak256("CancelChangeApprovalKey(uint256 proposalId,address newApprovalKey,uint256 deadline)");
event Paid(uint256 requested, uint256 actual);
event Locked(bool state);
event UnlockRequested(uint256 executeAt);
event UnlockCancelled();
event Unlocked();
event RescueRequested(address indexed to, uint256 executeAt);
event RescueCancelled();
event Rescued(address indexed to, uint256 requested, uint256 actual);
event EscapeRequested(address indexed target, uint256 value, bytes32 indexed dataHash, uint256 executeAt);
event EscapeCancelled();
event EscapeExecuted(address indexed target, uint256 value, bytes32 indexed dataHash);
event PayRequested(uint256 amount, uint256 executeAt);
event PayCancelled();
event ApprovalKeyChangeProposalCreated(
uint256 indexed proposalId,
address indexed newApprovalKey,
address indexed owner
);
event ApprovalKeyChangeRequested(uint256 indexed proposalId, address indexed newApprovalKey, uint256 executeAt);
event ApprovalKeyChangeApproved(
uint256 indexed proposalId,
address indexed newApprovalKey,
address indexed owner,
uint256 approvalCount
);
event ApprovalKeyChangeCancelApproved(uint256 indexed proposalId, address indexed owner, uint256 approvalCount);
event ApprovalKeyChangeCancelled(uint256 indexed proposalId);
event ApprovalKeyChanged(uint256 indexed proposalId, address indexed oldApprovalKey, address indexed newApprovalKey);
constructor(
IERC20 _token,
address _safe,
address _payee,
address _approvalKey,
address[5] memory _owners,
uint256 _start,
uint256 _weeklyCap
) {
require(address(_token).code.length > 0, "token not contract");
require(_safe != address(0), "zero safe");
require(_safe != address(this), "safe is vault");
require(_payee != address(0), "zero payee");
require(_payee != address(this), "payee is vault");
require(_approvalKey != address(0), "zero approval key");
require(_weeklyCap > 0, "zero cap");
require(_start % WEEK == MONDAY_OFFSET, "start not monday");
for (uint256 i = 0; i < 5; i++) {
require(_owners[i] != address(0), "zero owner");
}
token = _token;
safe = _safe;
payee = _payee;
approvalKey = _approvalKey;
owners = _owners;
start = _start;
weeklyCap = _weeklyCap;
DOMAIN_SEPARATOR = keccak256(
abi.encode(
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
keccak256("CapVault"),
keccak256("1"),
block.chainid,
address(this)
)
);
}
modifier onlySafe() {
require(msg.sender == safe, "not safe");
_;
}
/// @dev The Safe itself or any one of the five owners. This is deliberately not a threshold.
modifier onlyOwner() {
require(msg.sender == safe || _isOwner(msg.sender), "not owner");
_;
}
modifier onlyOwnerEOA() {
require(msg.sender.code.length == 0, "not owner EOA");
require(_isOwner(msg.sender), "not owner");
_;
}
modifier noEscapePending() {
require(escapeAt == 0, "escape pending");
_;
}
modifier noRescuePending() {
require(rescueAt == 0, "rescue pending");
_;
}
modifier nonReentrant() {
require(!_entered, "reentrant");
_entered = true;
_;
_entered = false;
}
/// @notice Bucket index for the current timestamp. Returns 0 before `start`.
function currentWeek() public view returns (uint256) {
if (block.timestamp < start) return 0;
return (block.timestamp - start) / WEEK;
}
/// @notice Budget still available in the current bucket.
function remainingThisWeek() public view returns (uint256) {
return currentWeek() == week ? weeklyCap - spent : weeklyCap;
}
/// @notice Number of pending actions (pay, unlock, rescue, escape, approval key change).
function pendingCount() public view returns (uint256 count) {
if (payAt != 0) count++;
if (unlockAt != 0) count++;
if (rescueAt != 0) count++;
if (escapeAt != 0) count++;
if (approvalKeyChangeAt != 0) count++;
}
/// @notice Compact pending summary. Use the per-action getters for details.
function getPendingSummary() external view returns (PendingSummary memory s) {
s.timestamp = block.timestamp;
s.count = pendingCount();
s.isLocked = locked;
s.approvalNonce = approvalNonce;
s.payPending = payAt != 0;
s.unlockPending = unlockAt != 0;
s.rescuePending = rescueAt != 0;
s.escapePending = escapeAt != 0;
s.approvalKeyChangePending = approvalKeyChangeAt != 0;
}
/// @notice Pay request details.
function getPayState()
external
view
returns (bool pending, bool ripe, uint256 executeAt, uint256 amount)
{
pending = payAt != 0;
executeAt = payAt;
amount = payAmount;
ripe = pending && block.timestamp >= payAt;
}
/// @notice Unlock request details.
function getUnlockState()
external
view
returns (bool pending, bool ripe, bool byPassword, uint256 executeAt)
{
pending = unlockAt != 0;
executeAt = unlockAt;
byPassword = unlockByPassword;
ripe = pending && block.timestamp >= unlockAt;
}
/// @notice Rescue request details.
function getRescueState()
external
view
returns (
bool pending,
bool ripe,
bool byPassword,
address to,
uint256 executeAt
)
{
pending = rescueAt != 0;
executeAt = rescueAt;
byPassword = rescueByPassword;
to = rescueTo;
ripe = pending && block.timestamp >= rescueAt;
}
/// @notice Escape request details. Full calldata remains available via `escapeData()`.
function getEscapeState()
external
view
returns (
bool pending,
bool ripe,
bool byPassword,
address target,
uint256 value,
uint256 executeAt,
bytes32 callHash,
uint256 dataLength
)
{
pending = escapeAt != 0;
executeAt = escapeAt;
byPassword = escapeByPassword;
target = escapeTarget;
value = escapeValue;
callHash = escapeHash();
dataLength = escapeData.length;
ripe = pending && block.timestamp >= escapeAt;
}
/// @notice Pending approval key-change details. `id` remains the last used proposal ID.
function getApprovalKeyState()
external
view
returns (bool pending, bool ripe, uint256 id, address newApprovalKey, uint256 executeAt)
{
pending = approvalKeyChangeAt != 0;
executeAt = approvalKeyChangeAt;
id = proposalId;
newApprovalKey = pendingApprovalKey;
ripe = pending && block.timestamp >= approvalKeyChangeAt;
}
/// @notice Pre-approval state for a future key change.
function getChangePreApproval(uint256 id)
external
view
returns (address newKey, uint256 deadline, uint8 count, uint8 bitmap)
{
ChangePreApproval storage p = _changePreApprovals[id];
newKey = p.newKey;
deadline = p.deadline;
count = p.count;
bitmap = p.bitmap;
}
/// @notice Queue a payment to the fixed payee. Only the Safe can request.
function requestPay(uint256 amount) external onlySafe {
require(!locked, "locked");
require(block.timestamp >= start, "not started");
require(amount > 0, "zero amount");
uint256 remaining = currentWeek() == week ? weeklyCap - spent : weeklyCap;
require(amount <= remaining, "over cap");
require(token.balanceOf(address(this)) >= amount, "insufficient");
payAmount = amount;
payAt = block.timestamp + PAY_DELAY;
emit PayRequested(amount, payAt);
}
/// @notice Execute a queued payment after the 1-day delay. Safe or any owner can execute.
function executePay() external onlyOwner nonReentrant {
require(payAt != 0 && block.timestamp >= payAt, "not ripe");
uint256 amount = payAmount;
_clearPay();
_pay(amount);
}
/// @notice Any owner or the Safe can cancel a queued payment.
function cancelPay() external onlyOwner {
require(payAt != 0, "no pay");
_clearPay();
emit PayCancelled();
}
function _pay(uint256 amount) private {
require(!locked, "locked");
require(block.timestamp >= start, "not started");
require(amount > 0, "zero amount");
uint256 w = (block.timestamp - start) / WEEK;
if (w > week) {
week = w;
spent = 0;
}
uint256 remaining = weeklyCap - spent;
require(amount <= remaining, "over cap");
require(token.balanceOf(address(this)) >= amount, "insufficient");
uint256 actual = _transfer(payee, amount);
// Charge at least the requested amount. A token that debits less than it credits
// must not let the payee receive more than `weeklyCap` over several payments.
uint256 charged = actual > amount ? actual : amount;
require(charged <= remaining, "over cap");
spent += charged;
emit Paid(amount, actual);
}
/// @notice Any owner or the Safe can brake. Immediate. A no-password lockdown cancels a
/// no-password unlock, but cannot undo a password-authorized unlock; it never
/// cancels a rescue or an escape.
function lockdown() external onlyOwner {
locked = true;
if (!(unlockAt != 0 && unlockByPassword)) {
unlockAt = 0;
unlockByPassword = false;
}
emit Locked(true);
}
/// @notice Owner/Safe-only unlock request without a password signature. 2-week timelock.
function requestUnlockByOwner() external onlyOwner noEscapePending {
require(locked, "not locked");
require(rescueAt == 0, "rescue pending");
require(!(unlockAt != 0 && unlockByPassword), "password required");
unlockAt = block.timestamp + UNLOCK_DELAY_OWNER;
unlockByPassword = false;
emit UnlockRequested(unlockAt);
}
/// @notice Owner/Safe-only unlock request with a approval-key signature. 1-week timelock.
function requestUnlock(uint256 deadline, uint256 nonce, bytes calldata sig)
external
onlyOwner
noEscapePending
{
require(locked, "not locked");
require(rescueAt == 0, "rescue pending");
require(block.timestamp <= deadline, "expired");
require(nonce == approvalNonce, "bad nonce");
bytes32 digest = _digest(keccak256(abi.encode(UNLOCK_TYPEHASH, nonce, deadline)));
require(_recover(digest, sig) == approvalKey, "bad sig");
approvalNonce = nonce + 1;
unlockAt = block.timestamp + UNLOCK_DELAY_PASSWORD;
unlockByPassword = true;
emit UnlockRequested(unlockAt);
}
/// @notice Owner/Safe-only immediate cancel of a no-password unlock.
function cancelUnlock() external onlyOwner {
require(unlockAt != 0, "no unlock");
require(!unlockByPassword, "password required");
unlockAt = 0;
unlockByPassword = false;
emit UnlockCancelled();
}
/// @notice Owner/Safe-only immediate cancel of any pending unlock, including a
/// password-authorized one. Requires a approval-key signature.
function cancelUnlockWithPassword(uint256 deadline, uint256 nonce, bytes calldata sig)
external
onlyOwner
{
require(unlockAt != 0, "no unlock");
require(block.timestamp <= deadline, "expired");
require(nonce == approvalNonce, "bad nonce");
bytes32 digest = _digest(keccak256(abi.encode(CANCEL_UNLOCK_TYPEHASH, nonce, deadline)));
require(_recover(digest, sig) == approvalKey, "bad sig");
approvalNonce = nonce + 1;
unlockAt = 0;
unlockByPassword = false;
emit UnlockCancelled();
}
/// @notice Owner/Safe-only execution once the unlock delay has passed.
function executeUnlock() external onlyOwner noEscapePending {
require(unlockAt != 0 && block.timestamp >= unlockAt, "not ripe");
unlockAt = 0;
unlockByPassword = false;
locked = false;
emit Unlocked();
}
/// @notice Owner/Safe-only rescue request without a password signature. 60-day timelock.
function requestRescueByOwner(address to) external onlyOwner {
require(to != address(0), "zero to");
require(to != address(this), "to is vault");
require(!(rescueAt != 0 && rescueByPassword), "password required");
require(!(unlockAt != 0 && unlockByPassword), "password required");
rescueTo = to;
rescueAt = block.timestamp + RESCUE_DELAY_OWNER;
rescueByPassword = false;
locked = true;
unlockAt = 0;
unlockByPassword = false;
emit RescueRequested(to, rescueAt);
}
/// @notice Owner/Safe-only rescue request with a approval-key signature. 30-day timelock.
function requestRescue(address to, uint256 deadline, uint256 nonce, bytes calldata sig)
external
onlyOwner
{
require(to != address(0), "zero to");
require(to != address(this), "to is vault");
require(block.timestamp <= deadline, "expired");
require(nonce == approvalNonce, "bad nonce");
bytes32 digest = _digest(keccak256(abi.encode(RESCUE_TYPEHASH, to, nonce, deadline)));
require(_recover(digest, sig) == approvalKey, "bad sig");
approvalNonce = nonce + 1;
rescueTo = to;
rescueAt = block.timestamp + RESCUE_DELAY_PASSWORD;
rescueByPassword = true;
locked = true;
unlockAt = 0;
unlockByPassword = false;
emit RescueRequested(to, rescueAt);
}
/// @notice Owner/Safe-only immediate cancel of a no-password rescue. The vault stays locked.
function cancelRescue() external onlyOwner {
require(rescueAt != 0, "no rescue");
require(!rescueByPassword, "password required");
_clearRescue();
emit RescueCancelled();
}
/// @notice Owner/Safe-only immediate cancel of any pending rescue, including a
/// password-authorized one. Requires a approval-key signature.
function cancelRescueWithPassword(uint256 deadline, uint256 nonce, bytes calldata sig)
external
onlyOwner
{
require(rescueAt != 0, "no rescue");
require(block.timestamp <= deadline, "expired");
require(nonce == approvalNonce, "bad nonce");
bytes32 digest = _digest(keccak256(abi.encode(CANCEL_RESCUE_TYPEHASH, nonce, deadline)));
require(_recover(digest, sig) == approvalKey, "bad sig");
approvalNonce = nonce + 1;
_clearRescue();
emit RescueCancelled();
}
/// @notice Owner/Safe-only execution once the rescue delay has passed. Sends the whole balance.
function executeRescue() external onlyOwner nonReentrant {
_executeRescue(token.balanceOf(address(this)));
}
/// @notice Owner/Safe-only partial rescue. Useful when a full-balance transfer would fail,
/// or when only part of the balance should leave.
function executeRescue(uint256 amount) external onlyOwner nonReentrant {
_executeRescue(amount);
}
function _executeRescue(uint256 amount) private {
require(rescueAt != 0 && block.timestamp >= rescueAt, "not ripe");
require(amount > 0, "zero amount");
require(amount <= token.balanceOf(address(this)), "insufficient");
address to = rescueTo;
_clearRescue();
uint256 actual = _transfer(to, amount);
emit Rescued(to, amount, actual);
}
/// @notice Queue an approval-key change after five owners have pre-approved its proposal
/// ID and new key. Requires the current and new key signatures. The proposal ID is
/// the one-time guard; this path does not consume `approvalNonce`.
function requestChangeApprovalKey(
uint256 newProposalId,
address newApprovalKey,
uint256 deadline,
bytes calldata sig,
bytes calldata newApprovalKeySig
) external onlyOwner {
require(newProposalId != 0, "bad proposal id");
require(newProposalId > proposalId, "bad proposal id");
require(newApprovalKey != address(0), "zero approval key");
require(newApprovalKey != approvalKey, "same approval key");
ChangePreApproval storage p = _changePreApprovals[newProposalId];
require(p.count == 5, "not approved");
require(p.newKey == newApprovalKey, "bad new key");
require(block.timestamp <= p.deadline, "approval expired");
require(block.timestamp <= deadline, "expired");
bytes32 digest = _digest(
keccak256(
abi.encode(
CHANGE_APPROVAL_KEY_TYPEHASH, newProposalId, newApprovalKey, deadline
)
)
);
require(_recover(digest, sig) == approvalKey, "bad sig");
require(_recover(digest, newApprovalKeySig) == newApprovalKey, "bad new sig");
proposalId = newProposalId;
pendingApprovalKey = newApprovalKey;
approvalKeyChangeAt = block.timestamp + APPROVAL_KEY_CHANGE_DELAY;
approvalDeadline = block.timestamp + APPROVAL_WINDOW;
approvalCount = 5;
approvalBitmap = 31;
approvalReady = true;
_clearCancelApprovalState();
delete _changePreApprovals[newProposalId];
emit ApprovalKeyChangeRequested(newProposalId, newApprovalKey, approvalKeyChangeAt);
}
/// @notice Pre-approve a future approval-key change. Pass proposalId 0 to create a new
/// proposal with a system-assigned ID, then use that ID for the other approvals.
function approveChangeApprovalKey(uint256 approveProposalId, address newApprovalKey)
external
onlyOwnerEOA
returns (uint256 assignedProposalId)
{
require(newApprovalKey != address(0), "zero approval key");
require(newApprovalKey != approvalKey, "same approval key");
ChangePreApproval storage p;
if (approveProposalId == 0) {
assignedProposalId = ++nextProposalId;
p = _changePreApprovals[assignedProposalId];
p.newKey = newApprovalKey;
p.deadline = block.timestamp + APPROVAL_WINDOW;
emit ApprovalKeyChangeProposalCreated(
assignedProposalId, newApprovalKey, msg.sender
);
} else {
assignedProposalId = approveProposalId;
require(assignedProposalId > proposalId, "bad proposal id");
p = _changePreApprovals[assignedProposalId];
require(p.deadline != 0, "unknown proposal");
require(p.newKey == newApprovalKey, "bad new key");
require(block.timestamp <= p.deadline, "approval expired");
}
uint256 bit = _ownerBit(msg.sender);
require(bit != 0, "not owner");
require(p.bitmap & bit == 0, "already approved");
p.bitmap |= uint8(bit);
p.count += 1;
emit ApprovalKeyChangeApproved(
assignedProposalId, newApprovalKey, msg.sender, p.count
);
}
/// @notice Cancel the current approval-key change after five owner EOAs have approved the
/// cancellation. Requires the current approval-key signature. This path does not
/// consume `approvalNonce`; the pending-change state and proposal ID guard replay.
function cancelChangeApprovalKey(
uint256 cancelProposalId,
uint256 deadline,
bytes calldata sig
) external onlyOwner {
require(approvalKeyChangeAt != 0, "no approval key change");
require(cancelProposalId == proposalId, "bad proposal id");
require(cancelApprovalCount == 5, "not approved");
require(block.timestamp <= deadline, "expired");
bytes32 digest = _digest(
keccak256(
abi.encode(
CANCEL_CHANGE_APPROVAL_KEY_TYPEHASH,
cancelProposalId,
pendingApprovalKey,
deadline
)
)
);
require(_recover(digest, sig) == approvalKey, "bad sig");
uint256 cancelledProposalId = proposalId;
_clearApprovalKeyChange();
_clearApprovalState();
_clearCancelApprovalState();
emit ApprovalKeyChangeCancelled(cancelledProposalId);
}
/// @notice Approve cancellation of a pending approval-key change once as an owner EOA.
/// Five approvals are required before `cancelChangeApprovalKey` can execute.
function approveCancelChangeApprovalKey(uint256 cancelProposalId) external onlyOwnerEOA {
require(pendingApprovalKey != address(0), "no approval key change");
require(cancelProposalId == proposalId, "bad proposal id");
uint256 bit = _ownerBit(msg.sender);
require(bit != 0, "not owner");
require(cancelApprovalBitmap & bit == 0, "already approved");
cancelApprovalBitmap |= uint8(bit);
cancelApprovalCount += 1;
emit ApprovalKeyChangeCancelApproved(cancelProposalId, msg.sender, cancelApprovalCount);
}
/// @notice Execute a queued approval-key change after the timelock, using the five
/// pre-approvals, before the one-day execution deadline. Safe or any owner.
function executeChangeApprovalKey() external onlyOwner {
require(approvalKeyChangeAt != 0 && block.timestamp >= approvalKeyChangeAt, "not ripe");
require(approvalReady, "not approved");
require(block.timestamp <= approvalDeadline, "approval expired");
address oldApprovalKey = approvalKey;
address newApprovalKey = pendingApprovalKey;
uint256 changedProposalId = proposalId;
_clearApprovalKeyChange();
_clearApprovalState();
_clearCancelApprovalState();
_clearAllPending();
approvalKey = newApprovalKey;
emit ApprovalKeyChanged(changedProposalId, oldApprovalKey, newApprovalKey);
}
/// @notice Hash binding an escape request to its target, value and calldata.
function escapeHash() public view returns (bytes32) {
return keccak256(abi.encode(escapeTarget, escapeValue, keccak256(escapeData)));
}
/// @notice Queue an arbitrary call without a approval-key signature. 90-day timelock.
function requestEscapeByOwner(address target, uint256 value, bytes calldata data)
external
onlyOwner
{
require(target != address(0), "zero target");
require(target != address(this), "target is vault");
require(!(escapeAt != 0 && escapeByPassword), "password required");
escapeTarget = target;
escapeValue = value;
escapeData = data;
escapeAt = block.timestamp + ESCAPE_DELAY_OWNER;
escapeByPassword = false;
emit EscapeRequested(target, value, keccak256(data), escapeAt);
}
/// @notice Queue an arbitrary call with a approval-key signature. 45-day timelock.
function requestEscape(
address target,
uint256 value,
bytes calldata data,
uint256 deadline,
uint256 nonce,
bytes calldata sig
) external onlyOwner {
require(target != address(0), "zero target");
require(target != address(this), "target is vault");
require(block.timestamp <= deadline, "expired");
require(nonce == approvalNonce, "bad nonce");
bytes32 dataHash = keccak256(data);
bytes32 digest = _digest(
keccak256(abi.encode(ESCAPE_TYPEHASH, target, value, dataHash, nonce, deadline))
);
require(_recover(digest, sig) == approvalKey, "bad sig");
approvalNonce = nonce + 1;
escapeTarget = target;
escapeValue = value;
escapeData = data;
escapeAt = block.timestamp + ESCAPE_DELAY_PASSWORD;
escapeByPassword = true;
emit EscapeRequested(target, value, dataHash, escapeAt);
}
/// @notice Cancel a no-password escape. The vault remains locked if it was locked.
function cancelEscape() external onlyOwner {
require(escapeAt != 0, "no escape");
require(!escapeByPassword, "password required");
_clearEscape();
emit EscapeCancelled();
}
/// @notice Cancel any pending escape, including a password-authorized one.
function cancelEscapeWithPassword(uint256 deadline, uint256 nonce, bytes calldata sig)
external
onlyOwner
{
require(escapeAt != 0, "no escape");
require(block.timestamp <= deadline, "expired");
require(nonce == approvalNonce, "bad nonce");
bytes32 callHash = escapeHash();
bytes32 digest = _digest(
keccak256(abi.encode(CANCEL_ESCAPE_TYPEHASH, callHash, nonce, deadline))
);
require(_recover(digest, sig) == approvalKey, "bad sig");
approvalNonce = nonce + 1;
_clearEscape();
emit EscapeCancelled();
}
/// @notice Execute a queued escape after its timelock. The target call can do anything.
function executeEscape() external onlyOwner nonReentrant {
require(escapeAt != 0 && block.timestamp >= escapeAt, "not ripe");
address target = escapeTarget;
uint256 value = escapeValue;
bytes memory data = escapeData;
bytes32 dataHash = keccak256(data);
_clearEscape();
(bool ok, ) = target.call{value: value}(data);
require(ok, "escape failed");
emit EscapeExecuted(target, value, dataHash);
}
function balance() external view returns (uint256) {
return token.balanceOf(address(this));
}
function isOwner(address who) external view returns (bool) {
return _isOwner(who);
}
function _isOwner(address who) internal view returns (bool) {
for (uint256 i = 0; i < 5; i++) {
if (owners[i] == who) return true;
}
return false;
}
function _ownerBit(address who) internal view returns (uint256) {
for (uint256 i = 0; i < 5; i++) {
if (owners[i] == who) return 1 << i;
}
return 0;
}
function _clearRescue() internal {
rescueTo = address(0);
rescueAt = 0;
rescueByPassword = false;
}
function _clearEscape() internal {
escapeTarget = address(0);
escapeValue = 0;
delete escapeData;
escapeAt = 0;
escapeByPassword = false;
}
function _clearPay() internal {
payAt = 0;
payAmount = 0;
}
function _clearApprovalKeyChange() internal {
pendingApprovalKey = address(0);
approvalKeyChangeAt = 0;
}
function _clearApprovalState() internal {
approvalDeadline = 0;
approvalCount = 0;
approvalBitmap = 0;
approvalReady = false;
}
function _clearCancelApprovalState() internal {
cancelApprovalCount = 0;
cancelApprovalBitmap = 0;
}
function _clearAllPending() internal {
if (payAt != 0) {
_clearPay();
emit PayCancelled();
}
if (rescueAt != 0) {
_clearRescue();
emit RescueCancelled();
}
if (escapeAt != 0) {
_clearEscape();
emit EscapeCancelled();
}
if (unlockAt != 0) {
unlockAt = 0;
unlockByPassword = false;
emit UnlockCancelled();
}
}
function _digest(bytes32 structHash) internal view returns (bytes32) {
return keccak256(abi.encodePacked("\x19\x01", DOMAIN_SEPARATOR, structHash));
}
/// @dev SafeERC20-style transfer that accounts for the ACTUAL balance decrease.
/// - Tolerates tokens that return no data.
/// - Rejects a 32-byte `false` return.
/// - Ignores other non-standard return data; the balance delta is authoritative.
/// - Rejects a transfer that does not reduce the vault balance.
/// - Returns the actual amount debited, which may differ from `amount` for
/// fee-on-transfer or rebasing tokens.
function _transfer(address to, uint256 amount) private returns (uint256 actual) {
IERC20 t = token;
uint256 balanceBefore = t.balanceOf(address(this));
require(balanceBefore >= amount, "insufficient");
(bool ok, bytes memory ret) = address(t).call(abi.encodeCall(IERC20.transfer, (to, amount)));
require(ok, "transfer failed");
if (ret.length == 32) {
require(abi.decode(ret, (bool)), "transfer failed");
}
uint256 balanceAfter = t.balanceOf(address(this));
require(balanceAfter <= balanceBefore, "balance increased");
actual = balanceBefore - balanceAfter;
require(actual > 0, "no movement");
}
function _recover(bytes32 digest, bytes calldata sig) internal pure returns (address) {
if (sig.length != 65) return address(0);
bytes32 r;
bytes32 s;
uint8 v;
assembly {
r := calldataload(sig.offset)
s := calldataload(add(sig.offset, 32))
v := byte(0, calldataload(add(sig.offset, 64)))
}
if (uint256(s) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) {
return address(0);
}
if (v != 27 && v != 28) return address(0);
return ecrecover(digest, v, r, s);
}
}
Compiler settingsShow
{
"optimizer": {
"runs": 200,
"enabled": true
},
"remappings": []
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.