-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Add ERC-20 Encumber Extension (ERC-7246) #6250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
arr00
wants to merge
12
commits into
OpenZeppelin:master
Choose a base branch
from
arr00:feat/erc-7246
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
bde331a
progress
arr00 41892dc
up
arr00 899073d
fix references
arr00 45f8600
virtual
arr00 fe7ea43
add tests and docs
arr00 f0d4989
correct pragmas
arr00 bfc9090
revert template change
arr00 18a7144
Update test/token/ERC20/extensions/draft-ERC7246.test.js
arr00 2c2a409
fix docs
arr00 aa7a94a
change update flow to behave like ERC20
arr00 9e8c90e
Apply suggestions from code review
arr00 a06c4c6
Apply suggestions from code review
arr00 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| pragma solidity >=0.6.2; | ||
|
|
||
| import {IERC20} from "./IERC20.sol"; | ||
|
|
||
| interface IERC7246 is IERC20 { | ||
| /// @dev Emitted when `amount` tokens are encumbered from `owner` to `spender`. | ||
| event Encumber(address indexed owner, address indexed spender, uint256 amount); | ||
|
|
||
| /// @dev Emitted when the encumbrance of a `spender` to an `owner` is reduced by `amount`. | ||
| event Release(address indexed owner, address indexed spender, uint256 amount); | ||
|
|
||
| /** | ||
| * @dev Returns the total amount of tokens owned by `owner` that are currently encumbered. | ||
| * | ||
| * - MUST never exceed `balanceOf(owner)` | ||
| * - Any function which would reduce `balanceOf(owner)` below `encumberedBalanceOf(owner)` MUST revert | ||
| */ | ||
| function encumberedBalanceOf(address owner) external view returns (uint256); | ||
|
|
||
| /** | ||
| * @dev Convenience function for reading the unencumbered balance of an address. | ||
| * Trivially implemented as `balanceOf(owner) - encumberedBalanceOf(owner)` | ||
| */ | ||
| function availableBalanceOf(address owner) external view returns (uint256); | ||
|
|
||
| /** | ||
| * @dev Returns the number of tokens that `owner` has encumbered to `spender`. | ||
| * | ||
| * - This value increases when {encumber} or {encumberFrom} are called by the `owner` or by another permitted account. | ||
| * - This value decreases when {release} or {transferFrom} are called by `spender`. | ||
| */ | ||
| function encumbrances(address owner, address spender) external view returns (uint256); | ||
|
|
||
| /** | ||
| * @dev Increases the amount of tokens that the caller has encumbered to `spender` by `amount`. | ||
| * Grants `spender` a guaranteed right to transfer `amount` from the caller's by using `transferFrom`. | ||
| * | ||
| * - MUST revert if caller does not have `amount` tokens available | ||
| * (e.g. if `balanceOf(caller) - encumberedBalanceOf(caller) < amount`). | ||
| * - Emits an {IERC7246-Encumber} event. | ||
| */ | ||
| function encumber(address spender, uint256 amount) external; | ||
|
|
||
| /** | ||
| * @dev Increases the amount of tokens that `owner` has encumbered to `spender` by `amount`. | ||
| * Grants `spender` a guaranteed right to transfer `amount` from `owner` using transferFrom. | ||
| * | ||
| * - The function SHOULD revert unless the owner account has deliberately authorized the sender of the message via some mechanism. | ||
| * - MUST revert if `owner` does not have `amount` tokens available | ||
| * (e.g. if `balanceOf(owner) - encumberedBalanceOf(owner) < amount`). | ||
| * - Emits an {IERC7246-Encumber} event. | ||
| */ | ||
| function encumberFrom(address owner, address spender, uint256 amount) external; | ||
|
|
||
| /** | ||
| * @dev Reduces amount of tokens encumbered from `owner` to caller by `amount` | ||
| * | ||
| * - Emits a {IERC7246-Release} event. | ||
| */ | ||
| function release(address owner, uint256 amount) external; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| pragma solidity ^0.8.26; | ||
|
|
||
| import {ERC20} from "../ERC20.sol"; | ||
| import {IERC7246} from "../../../interfaces/draft-IERC7246.sol"; | ||
| import {Math} from "../../../utils/math/Math.sol"; | ||
|
|
||
| /** | ||
| * @title ERC7246 | ||
| * @dev An extension of {ERC20} that adds support for encumbrances of token balances. Encumbrances are a | ||
| * stronger version of allowances: they grant the `spender` an exclusive right to transfer tokens from the | ||
| * `owner`'s balance without reducing the `owner`'s balance until the tokens are transferred or the | ||
| * encumbrance is released. | ||
| */ | ||
| abstract contract ERC7246 is ERC20, IERC7246 { | ||
| /// @dev Thrown when the result of an {_update} or {_encumber} call would result in negative {availableBalanceOf}. | ||
| error ERC7246InsufficientAvailableBalance(uint256 available, uint256 required); | ||
|
|
||
| /// @dev Thrown when an account tries to release more encumbered tokens than it has. | ||
| error ERC7246InsufficientEncumbrance(uint256 encumbered, uint256 required); | ||
|
|
||
| /// @dev Thrown when an account tries to encumber tokens to itself. | ||
| error ERC7246SelfEncumbrance(); | ||
|
|
||
| mapping(address owner => mapping(address spender => uint256)) private _encumbrances; | ||
| mapping(address owner => uint256) private _encumberedBalances; | ||
|
|
||
| /// @inheritdoc IERC7246 | ||
| function encumberedBalanceOf(address owner) public view virtual returns (uint256) { | ||
| return _encumberedBalances[owner]; | ||
| } | ||
|
|
||
| /// @inheritdoc IERC7246 | ||
| function availableBalanceOf(address owner) public view virtual returns (uint256) { | ||
| return balanceOf(owner) - encumberedBalanceOf(owner); | ||
| } | ||
|
|
||
| /// @inheritdoc IERC7246 | ||
| function encumbrances(address owner, address spender) public view virtual returns (uint256) { | ||
| return _encumbrances[owner][spender]; | ||
| } | ||
|
|
||
| /// @inheritdoc IERC7246 | ||
| function encumber(address spender, uint256 amount) public virtual { | ||
| _encumber(msg.sender, spender, amount); | ||
| } | ||
|
|
||
| /// @inheritdoc IERC7246 | ||
| function encumberFrom(address owner, address spender, uint256 amount) public virtual { | ||
| _spendAllowance(owner, msg.sender, amount); | ||
| _encumber(owner, spender, amount); | ||
| } | ||
|
|
||
| /// @inheritdoc IERC7246 | ||
| function release(address owner, uint256 amount) public virtual { | ||
| _releaseEncumbrance(owner, msg.sender, amount); | ||
| } | ||
|
|
||
| /** | ||
| * @dev Encumber `amount` of tokens from `owner` to `spender`. Encumbering tokens grants an exclusive right | ||
| * to transfer the tokens without removing them from `owner`'s balance. Release the tokens by calling | ||
| * {release} or transfer them by calling {transferFrom}. | ||
| */ | ||
| function _encumber(address owner, address spender, uint256 amount) internal virtual { | ||
| require(owner != spender, ERC7246SelfEncumbrance()); | ||
| uint256 availableBalance = availableBalanceOf(owner); | ||
| require(availableBalance >= amount, ERC7246InsufficientAvailableBalance(availableBalance, amount)); | ||
|
|
||
| // Given that the `availableBalanceOf` is `balanceOf(owner) - encumberedBalanceOf(owner)`, | ||
| // we know that the new `_encumberedBalances[owner] <= balanceOf(owner)` and thus no overflow is possible. | ||
| // `_encumberedBalances[owner] >= _encumbrances[owner][spender]`, so no overflow is possible there either. | ||
| unchecked { | ||
| _encumbrances[owner][spender] += amount; | ||
| _encumberedBalances[owner] += amount; | ||
| } | ||
|
|
||
| emit Encumber(owner, spender, amount); | ||
| } | ||
|
|
||
| /** | ||
| * @dev Release `amount` of encumbered tokens from `owner` to `spender`. | ||
| * | ||
| * - Will revert if there are insufficient encumbered tokens. | ||
| * - Emits the {IERC7246-Release} event. | ||
| */ | ||
| function _releaseEncumbrance(address owner, address spender, uint256 amount) internal virtual { | ||
| uint256 encumbered = encumbrances(owner, spender); | ||
| require(encumbered >= amount, ERC7246InsufficientEncumbrance(encumbered, amount)); | ||
|
|
||
| unchecked { | ||
| _encumbrances[owner][spender] -= amount; | ||
| _encumberedBalances[owner] -= amount; | ||
| } | ||
|
|
||
| emit Release(owner, spender, amount); | ||
| } | ||
|
|
||
| /** | ||
| * @dev See {ERC20-_spendAllowance}. Encumbrances are consumed first, then the remaining amount | ||
| * is passed to `super._spendAllowance`. | ||
| */ | ||
| function _spendAllowance(address owner, address spender, uint256 amount) internal virtual override { | ||
| uint256 amountEncumbered = encumbrances(owner, spender); | ||
| uint256 remainingAllowance = amount; | ||
|
|
||
| if (amountEncumbered != 0) { | ||
| uint256 encumberedToUse = Math.min(amount, amountEncumbered); | ||
| _releaseEncumbrance(owner, spender, encumberedToUse); | ||
| unchecked { | ||
| remainingAllowance -= encumberedToUse; | ||
| } | ||
| } | ||
|
|
||
| super._spendAllowance(owner, spender, remainingAllowance); | ||
| } | ||
|
|
||
| /// @dev See {ERC20-_update}. Ensures that `from` has sufficient {availableBalanceOf} to cover the `amount` being transferred. | ||
| function _update(address from, address to, uint256 amount) internal virtual override { | ||
| super._update(from, to, amount); | ||
| if (from != address(0)) { | ||
| uint256 balanceOfFrom = balanceOf(from); | ||
| uint256 encumberedBalanceOfFrom = encumberedBalanceOf(from); | ||
| require( | ||
| balanceOfFrom >= encumberedBalanceOfFrom, | ||
| ERC7246InsufficientAvailableBalance(balanceOfFrom + amount - encumberedBalanceOfFrom, amount) | ||
| ); | ||
| } | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.