Working tree is now the canonical repo. Off-chain play-money + trustless on-chain (FPMM AMM + CLOB via UMA settlement), AI market factory, creator/social/distribution, mobile shell + push, one-command demo + deploy-kit. Secrets excluded (see ENV-AND-KEYS.md). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
7.3 KiB
Lifebuoy
Class that allows for rescue of ETH, ERC20, ERC721 tokens.
This contract is created to mitigate the following disasters:
- Careless user sends tokens to the wrong chain or wrong contract.
- Careless dev deploys a contract without a withdraw function in attempt to rescue careless user's tokens, due to deployment nonce mismatch caused by script misfire / misconfiguration.
- Careless dev forgets to add a withdraw function to a NFT sale contract.
Note:
if you are deploying via a untrusted tx.origin,
you MUST override _lifebuoyDefaultDeployer to return a trusted address.
For best safety:
- For non-escrow contracts, inherit Lifebuoy as much as possible, and leave it unlocked.
- For escrow contracts, lock access as tight as possible, as soon as possible. Or simply don't inherit Lifebuoy. Escrow: Your contract is designed to hold ETH, ERC20s, ERC721s (e.g. liquidity pools).
All rescue and rescue authorization functions require either:
- Caller is the deployer
AND the contract is not a proxy
AND
rescueLocked() & _LIFEBUOY_DEPLOYER_ACCESS_LOCK == 0. - Caller is
owner()ANDrescueLocked() & _LIFEBUOY_OWNER_ACCESS_LOCK == 0.
The choice of using bit flags to represent locked statuses is for efficiency, flexibility, convenience.
This contract is optimized with a priority on minimal bytecode size, as the methods are not intended to be called often.
Custom Errors
RescueUnauthorizedOrLocked()
error RescueUnauthorizedOrLocked()
The caller is not authorized to rescue or lock the rescue function.
RescueTransferFailed()
error RescueTransferFailed()
The rescue operation has failed due to a failed transfer.
Lock Flags Constants
These flags are kept internal to avoid bloating up the function dispatch.
You can just copy paste this into your own code.
_LIFEBUOY_DEPLOYER_ACCESS_LOCK
uint256 internal constant _LIFEBUOY_DEPLOYER_ACCESS_LOCK = 1 << 0
Flag to denote that the deployer's access is locked. (1)
_LIFEBUOY_OWNER_ACCESS_LOCK
uint256 internal constant _LIFEBUOY_OWNER_ACCESS_LOCK = 1 << 1
Flag to denote that the owner()'s access is locked. (2)
_LIFEBUOY_LOCK_RESCUE_LOCK
uint256 internal constant _LIFEBUOY_LOCK_RESCUE_LOCK = 1 << 2
Flag to denote that the lockRescue function is locked. (4)
_LIFEBUOY_RESCUE_ETH_LOCK
uint256 internal constant _LIFEBUOY_RESCUE_ETH_LOCK = 1 << 3
Flag to denote that the rescueETH function is locked. (8)
_LIFEBUOY_RESCUE_ERC20_LOCK
uint256 internal constant _LIFEBUOY_RESCUE_ERC20_LOCK = 1 << 4
Flag to denote that the rescueERC20 function is locked. (16)
_LIFEBUOY_RESCUE_ERC721_LOCK
uint256 internal constant _LIFEBUOY_RESCUE_ERC721_LOCK = 1 << 5
Flag to denote that the rescueERC721 function is locked. (32)
_LIFEBUOY_RESCUE_ERC1155_LOCK
uint256 internal constant _LIFEBUOY_RESCUE_ERC1155_LOCK = 1 << 6
Flag to denote that the rescueERC1155 function is locked. (64)
_LIFEBUOY_RESCUE_ERC6909_LOCK
uint256 internal constant _LIFEBUOY_RESCUE_ERC6909_LOCK = 1 << 7
Flag to denote that the rescueERC6909 function is locked. (128)
Immutables
_lifebuoyDeployerHash
bytes32 internal immutable _lifebuoyDeployerHash
For checking that the caller is the deployer and
that the context is not a delegatecall
(so that the implementation deployer cannot drain proxies).
Storage
_RESCUE_LOCKED_FLAGS_SLOT
bytes32 internal constant _RESCUE_LOCKED_FLAGS_SLOT =
0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffb8e2915b
The rescue locked flags slot is given by:
bytes32(~uint256(uint32(bytes4(keccak256("_RESCUE_LOCKED_FLAGS_SLOT_NOT"))))).
It is intentionally chosen to be a high value
to avoid collision with lower slots.
The choice of manual storage layout is to enable compatibility
with both regular and upgradeable contracts.
Constructor
_lifebuoyDefaultDeployer()
function _lifebuoyDefaultDeployer()
internal
view
virtual
returns (address)
Returns tx.origin by default. Override to return another address if needed.
Note: If you are deploying via a untrusted tx.origin (e.g. ERC4337 bundler)
you MUST override this function to return a trusted address.
Rescue Operations
rescueETH(address,uint256)
function rescueETH(address to, uint256 amount)
public
payable
virtual
onlyRescuer(_LIFEBUOY_RESCUE_ETH_LOCK)
Sends amount (in wei) ETH from the current contract to to.
Reverts upon failure.
rescueERC20(address,address,uint256)
function rescueERC20(address token, address to, uint256 amount)
public
payable
virtual
onlyRescuer(_LIFEBUOY_RESCUE_ERC20_LOCK)
Sends amount of ERC20 token from the current contract to to.
Does not check for existence of token or return data. Reverts upon failure.
rescueERC721(address,address,uint256)
function rescueERC721(address token, address to, uint256 id)
public
payable
virtual
onlyRescuer(_LIFEBUOY_RESCUE_ERC721_LOCK)
Sends id of ERC721 token from the current contract to to.
Does not check for existence of token or return data. Reverts upon failure.
rescueERC1155(address,address,uint256,uint256,bytes)
function rescueERC1155(
address token,
address to,
uint256 id,
uint256 amount,
bytes calldata data
) public payable virtual onlyRescuer(_LIFEBUOY_RESCUE_ERC1155_LOCK)
Sends amount of id of ERC1155 token from the current contract to to.
Does not check for existence of token or return data. Reverts upon failure.
rescueERC6909(address,address,uint256,uint256)
function rescueERC6909(
address token,
address to,
uint256 id,
uint256 amount
) public payable virtual onlyRescuer(_LIFEBUOY_RESCUE_ERC6909_LOCK)
Sends amount of id of ERC6909 token from the current contract to to.
Does not check for existence of token or return data. Reverts upon failure.
Rescue Authorization Operations
rescueLocked()
function rescueLocked() public view virtual returns (uint256 locks)
Returns the flags denoting whether access to rescue functions
(including lockRescue) is locked.
lockRescue(uint256)
function lockRescue(uint256 locksToSet)
public
payable
virtual
onlyRescuer(_LIFEBUOY_LOCK_RESCUE_LOCK)
Locks (i.e. permanently removes) access to rescue functions (including lockRescue).
_lockRescue(uint256)
function _lockRescue(uint256 locksToSet) internal virtual
Internal function to set the lock flags without going through access control.
_checkRescuer(uint256)
function _checkRescuer(uint256 modeLock) internal view virtual
Requires that the rescue function being guarded is:
- Not locked, AND
- Called by either:
(a) Theowner(), OR
(b) The deployer (if not via a delegate call and deployer is an EOA).
onlyRescuer(uint256)
modifier onlyRescuer(uint256 modeLock) virtual
Modifier that calls _checkRescuer() at the start of the function.