ERC-4546 Unified deposit contract (Wrapped Deposits)
Stagnant · Vaults & DeFi · created 2021-12-11
A single shared contract handles ETH, ERC-20, and NFT deposits on behalf of apps, so users only need to approve token spending once to deposit into multiple apps.
Why it is needed
To deposit ERC-20 tokens into an app, a user must first approve the token for spending. Sending a separate approval transaction for the exact amount on every deposit is cumbersome and incurs repeated fees, while approving an unlimited amount at once is convenient but creates a security risk, since that app could take the user's entire balance. In addition, the concept of approval is hard to explain to new or non-expert users, and this process must be repeated identically in every individual app that supports ERC-20 deposits.
What to implement
depositERC20(address to, address token, uint amount) external;: Deposits the amount of ERC-20 tokens held by msg.sender into the to contract.depositERC721(address to, address token, uint tokenId) external;: Deposits an ERC-721 NFT into the to contract.safeDepositERC721(address to, address token, uint tokenId, bytes memory data) external;: Safely deposits an ERC-721 NFT into the to contract, along with additional data (data).safeDepositERC1155(address to, address token, uint tokenId, uint value, bytes calldata data) external;: Deposits a quantity (value) of a single ERC-1155 token into the to contract.batchDepositERC1155(address to, address token, uint[] calldata tokenIds, uint[] calldata values, bytes calldata data) external;: Batch deposits multiple kinds of ERC-1155 tokens at once.depositEther(address to) external payable;: Deposits an amount of ETH equal to msg.value into the to contract.function acceptERC20Deposit(address depositor, address token, uint amount) external returns (bool);: Determines whether the receiving contract will accept the ERC-20 deposit. It must return true for the deposit to actually proceed.function acceptERC721Deposit(address depositor, address token, uint tokenId) external returns (bool);: Determines whether the receiving contract will accept the ERC-721 NFT deposit.function acceptERC1155Deposit(address depositor, address token, uint tokenId, uint value, bytes calldata data) external returns (bool);: Determines whether the receiving contract will accept a single ERC-1155 deposit.function acceptERC1155BatchDeposit(address depositor, address token, uint[] calldata tokenIds, uint[] calldata values, bytes calldata data) external returns (bool);: Determines whether the receiving contract will accept a batch ERC-1155 deposit.function acceptEtherDeposit(address depositor, uint amount) external returns (bool);: Determines whether the receiving contract will accept the ETH deposit.
In depth
Singleton structure and immutability
The unified deposit contract SHOULD be deployed at an identifiable address (e.g. 0x1111119a9e30bceadf9f939390293ffacef93fe9) and MUST be non-upgradeable with unchangeable state variables. This is a requirement so that all users and apps can share a single trusted point.
Common rules for deposit functions
The depositERC20, depositERC721, safeDepositERC721, safeDepositERC1155, batchDepositERC1155, and depositEther functions must all revert if to is an address with zero code size (a regular account). Each function must also check whether to's accept function accepts the deposit, reverting if it does not return true, and must also revert if the asset transfer itself fails.
The accept interface of the receiving contract
The ERC20Receiver, ERC721Receiver, ERC1155Receiver, and EtherReceiver interfaces each define the functions acceptERC20Deposit, acceptERC721Deposit, acceptERC1155Deposit, acceptERC1155BatchDeposit, and acceptEtherDeposit. A receiving contract MAY implement only the functions it wants, and if it does not implement the accept function for a given asset type, deposits of that type MUST NOT be sent.
Design rationale
When a single contract handles all token transfers, users only need to approve a token once to deposit into multiple apps. Receiving contracts no longer need to entrust depositors with token spending approval, reducing trust requirements, and they also don't need to implement token transfer logic themselves, lowering implementation complexity. This lets the user experience be designed around a simple message: "enable this token for use in other apps."
Backward compatibility
This ERC is not backward compatible. Contracts that want to use this deposit system must implement the accept function themselves. Existing upgradeable contracts can add support later by adding an accept function, and can allow the existing method (approval on the contract itself) alongside the new deposit method, preserving existing approvals while guiding new users toward the new method.
The reference implementation's ERC-20 handling approach
The reference implementation's depositERC20 calls token's transferFrom via a low-level call, and if there is return data, decodes it as a bool to check whether it is true. This is backward-compatible handling so it also works with some ERC-20 tokens whose transferFrom does not return a bool as the standard specifies. All functions internally check first whether to is a contract (extcodesize > 0).
Proposal status
This ERC is in Stagnant status. It is no longer being actively discussed, so its content may change or it may never be adopted as a standard going forward.
Things to watch out for
- The implementation should be kept minimal: The unified deposit contract implementation should be as small as possible to reduce the risk of bugs, and simple enough that an engineer can read and understand it within a few minutes.
- The receiving contract must verify msg.sender: The receiving contract MUST verify that msg.sender equals the address of the unified deposit contract. Without this, anyone could call the accept function directly to fake-simulate a deposit.