Skip to content

Commit

Permalink
replace _spendEncumbrance with _releaseEncumbrance (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-silver authored Sep 6, 2023
1 parent 58b4f33 commit 28d1fa3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
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
* 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

0 comments on commit 28d1fa3

Please sign in to comment.