Repository navigation
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
base: master
Are you sure you want to change the base?
Changes from all commits
bde331a
41892dc
899073d
45f8600
fe7ea43
f0d4989
bfc9090
18a7144
2c2a409
aa7a94a
9e8c90e
a06c4c6
f712571
6e21b84
9dbbd1a
a81d320
5f4e3c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,136 @@ | ||||||
| // 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(); | ||||||
|
|
||||||
| /// @dev Thrown when an account tries to encumber tokens to the zero address. | ||||||
| error ERC7246InvalidEncumberSpender(); | ||||||
|
|
||||||
| 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()); | ||||||
|
arr00 marked this conversation as resolved.
|
||||||
| require(spender != address(0), ERC7246InvalidEncumberSpender()); | ||||||
| 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; | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
and below:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Decreases readability for ~4 gas? Is it worth it?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Up to you 👌 |
||||||
|
|
||||||
| if (amountEncumbered != 0) { | ||||||
| uint256 encumberedToUse = Math.min(amount, amountEncumbered); | ||||||
| _releaseEncumbrance(owner, spender, encumberedToUse); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
with: and also called by
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, is it worth it for the small gas cost wins?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did not check gas, the purpose was to avoid reading storage again and removing useless check. |
||||||
| 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 { | ||||||
| if (from != address(0)) { | ||||||
| uint256 encumberedBalanceOfFrom = encumberedBalanceOf(from); | ||||||
| if (encumberedBalanceOfFrom != 0) { | ||||||
| uint256 availableBalanceOfFrom = availableBalanceOf(from); | ||||||
| require( | ||||||
| availableBalanceOfFrom >= amount, | ||||||
| ERC7246InsufficientAvailableBalance(availableBalanceOfFrom, amount) | ||||||
| ); | ||||||
| } | ||||||
| } | ||||||
| super._update(from, to, amount); | ||||||
| } | ||||||
| } | ||||||
Uh oh!
There was an error while loading. Please reload this page.