diff --git a/src/EncumberableToken.sol b/src/EncumberableToken.sol index a584dd5..30122d5 100644 --- a/src/EncumberableToken.sol +++ b/src/EncumberableToken.sol @@ -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 @@ -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` @@ -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; diff --git a/src/interfaces/IERC7246.sol b/src/interfaces/IERC7246.sol index fcbe7c6..dd4f967 100644 --- a/src/interfaces/IERC7246.sol +++ b/src/interfaces/IERC7246.sol @@ -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 * by `amount`. */ event Release(address indexed owner, address indexed taker, uint256 amount); @@ -78,4 +78,4 @@ interface IERC7246 { * Trivially implemented as `balanceOf(owner) - encumberedBalanceOf(owner)` */ function availableBalanceOf(address owner) external view returns (uint256); -} \ No newline at end of file +} diff --git a/test/EncumberableToken.t.sol b/test/EncumberableToken.t.sol index 8517f88..90248c2 100644 --- a/test/EncumberableToken.t.sol +++ b/test/EncumberableToken.t.sol @@ -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 @@ -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