Skip to content
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

replace _spendEncumbrance with _releaseEncumbrance #26

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions src/EncumberableToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ contract EncumberableToken is ERC20, IERC20Permit, IERC7246 {
if (amount > encumberedToTaker) {
uint256 excessAmount = amount - encumberedToTaker;
// Exceeds Encumbrance, so spend all of it
_spendEncumbrance(src, msg.sender, encumberedToTaker);
_releaseEncumbrance(src, msg.sender, encumberedToTaker);

// Having spent all the tokens encumbered to the mover,
// We are now moving only "available" tokens and must check
Expand All @@ -114,24 +114,13 @@ contract EncumberableToken is ERC20, IERC20Permit, IERC7246 {

_spendAllowance(src, msg.sender, excessAmount);
} else {
_spendEncumbrance(src, msg.sender, amount);
_releaseEncumbrance(src, msg.sender, amount);
}

_transfer(src, dst, amount);
return true;
}

/**
* @dev Spend `amount` of `owner`'s encumbrance to `taker`
*/
function _spendEncumbrance(address owner, address taker, uint256 amount) internal {
uint256 currentEncumbrance = encumbrances[owner][taker];
require(currentEncumbrance >= amount, "ERC7246: insufficient encumbrance");
uint256 newEncumbrance = currentEncumbrance - amount;
encumbrances[owner][taker] = newEncumbrance;
encumberedBalanceOf[owner] -= amount;
}

/**
* @notice Increases the amount of tokens that the caller has encumbered to
* `taker` by `amount`
Expand Down Expand Up @@ -176,13 +165,13 @@ contract EncumberableToken is ERC20, IERC20Permit, IERC7246 {
* @param amount Amount of tokens to decrease the encumbrance by
*/
function release(address owner, uint256 amount) external {
_release(owner, msg.sender, amount);
_releaseEncumbrance(owner, msg.sender, amount);
}

/**
* @dev Reduce `owner`'s encumbrance to `taker` by `amount`
*/
function _release(address owner, address taker, uint256 amount) private {
function _releaseEncumbrance(address owner, address taker, uint256 amount) private {
require(encumbrances[owner][taker] >= amount, "ERC7246: insufficient encumbrance");
encumbrances[owner][taker] -= amount;
encumberedBalanceOf[owner] -= amount;
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/IERC7246.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface IERC7246 {
event Encumber(address indexed owner, address indexed taker, uint256 amount);

/**
* @dev Emitted when the encumbrance of a `taker` to an `owner` is reduced
* @dev Emitted when the encumbrance of an `owner` to a `taker` is reduced
scott-silver marked this conversation as resolved.
Show resolved Hide resolved
* by `amount`.
*/
event Release(address indexed owner, address indexed taker, uint256 amount);
Expand Down Expand Up @@ -78,4 +78,4 @@ interface IERC7246 {
* Trivially implemented as `balanceOf(owner) - encumberedBalanceOf(owner)`
*/
function availableBalanceOf(address owner) external view returns (uint256);
}
}
4 changes: 4 additions & 0 deletions test/EncumberableToken.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ contract EncumberableTokenTest is Test {

// bob calls transfers from alice to charlie
vm.prank(bob);
vm.expectEmit(true, true, true, true);
emit Release(alice, bob, 40e18);
wrappedToken.transferFrom(alice, charlie, 40e18);

// alice balance is reduced
Expand Down Expand Up @@ -175,6 +177,8 @@ contract EncumberableTokenTest is Test {

// bob calls transfers from alice to charlie
vm.prank(bob);
vm.expectEmit(true, true, true, true);
emit Release(alice, bob, 20e18);
wrappedToken.transferFrom(alice, charlie, 40e18);

// alice balance is reduced
Expand Down
Loading