Skip to content

Commit

Permalink
create EncumbranceUpdate event
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-silver committed Aug 1, 2023
1 parent 58b4f33 commit 91705a4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
16 changes: 9 additions & 7 deletions src/EncumberableToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ contract EncumberableToken is ERC20, IERC20Permit, IERC7246 {
* @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;
uint256 previousEncumbrance = encumbrances[owner][taker];
require(previousEncumbrance >= amount, "ERC7246: insufficient encumbrance");
encumbrances[owner][taker] -= amount;
encumberedBalanceOf[owner] -= amount;
emit EncumbranceUpdate(owner, taker, previousEncumbrance, previousEncumbrance - amount);
}

/**
Expand All @@ -147,9 +147,10 @@ contract EncumberableToken is ERC20, IERC20Permit, IERC7246 {
*/
function _encumber(address owner, address taker, uint256 amount) private {
require(availableBalanceOf(owner) >= amount, "ERC7246: insufficient available balance");
uint256 previousEncumbrance = encumbrances[owner][taker];
encumbrances[owner][taker] += amount;
encumberedBalanceOf[owner] += amount;
emit Encumber(owner, taker, amount);
emit EncumbranceUpdate(owner, taker, previousEncumbrance, previousEncumbrance + amount);
}

/**
Expand Down Expand Up @@ -183,10 +184,11 @@ contract EncumberableToken is ERC20, IERC20Permit, IERC7246 {
* @dev Reduce `owner`'s encumbrance to `taker` by `amount`
*/
function _release(address owner, address taker, uint256 amount) private {
require(encumbrances[owner][taker] >= amount, "ERC7246: insufficient encumbrance");
uint256 previousEncumbrance = encumbrances[owner][taker];
require(previousEncumbrance >= amount, "ERC7246: insufficient encumbrance");
encumbrances[owner][taker] -= amount;
encumberedBalanceOf[owner] -= amount;
emit Release(owner, taker, amount);
emit EncumbranceUpdate(owner, taker, previousEncumbrance, previousEncumbrance - amount);
}

/**
Expand Down
11 changes: 3 additions & 8 deletions src/interfaces/IERC7246.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,10 @@ pragma solidity ^0.8.20;
*/
interface IERC7246 {
/**
* @dev Emitted when `amount` tokens are encumbered from `owner` to `taker`.
* @dev Emitted when the encumbrance of a `taker` to an `owner` is altered,
* by creating an encumbrance, spending it or releasing it
*/
event Encumber(address indexed owner, address indexed taker, uint256 amount);

/**
* @dev Emitted when the encumbrance of a `taker` to an `owner` is reduced
* by `amount`.
*/
event Release(address indexed owner, address indexed taker, uint256 amount);
event EncumbranceUpdate(address indexed owner, address indexed taker, uint256 previousAmount, uint256 newAmount);

/**
* @dev Returns the total amount of tokens owned by `owner` that are
Expand Down
21 changes: 12 additions & 9 deletions test/EncumberableToken.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { IERC20Metadata } from "@openzeppelin/contracts/interfaces/IERC20Metadat
import { EncumberableToken } from "../src/EncumberableToken.sol";

contract EncumberableTokenTest is Test {
event Encumber(address indexed owner, address indexed taker, uint amount);
event Release(address indexed owner, address indexed taker, uint amount);
event EncumbranceUpdate(address indexed owner, address indexed taker, uint256 previousAmount, uint256 newAmount);

ERC20 public underlyingToken;
EncumberableToken public wrappedToken;
Expand Down Expand Up @@ -108,9 +107,9 @@ contract EncumberableTokenTest is Test {
deal(address(wrappedToken), alice, 100e18);
vm.startPrank(alice);

// emits Encumber event
// emits EncumbranceUpdate event
vm.expectEmit(true, true, true, true);
emit Encumber(alice, bob, 60e18);
emit EncumbranceUpdate(alice, bob, 0, 60e18);

// alice encumbers some of her balance to bob
wrappedToken.encumber(bob, 60e18);
Expand Down Expand Up @@ -141,8 +140,12 @@ contract EncumberableTokenTest is Test {
assertEq(wrappedToken.balanceOf(charlie), 0);

// bob calls transfers from alice to charlie
vm.prank(bob);
vm.startPrank(bob);
// emits EncumbranceUpdate event
vm.expectEmit(true, true, true, true);
emit EncumbranceUpdate(alice, bob, 60e18, 20e18);
wrappedToken.transferFrom(alice, charlie, 40e18);
vm.stopPrank();

// alice balance is reduced
assertEq(wrappedToken.balanceOf(alice), 60e18);
Expand Down Expand Up @@ -247,9 +250,9 @@ contract EncumberableTokenTest is Test {

// but bob tries to encumber more than his allowance
vm.prank(bob);
// emits an Encumber event
// emits an EncumbranceUpdate event
vm.expectEmit(true, true, true, true);
emit Encumber(alice, charlie, 60e18);
emit EncumbranceUpdate(alice, charlie, 0, 60e18);
wrappedToken.encumberFrom(alice, charlie, 60e18);

// no balance is transferred
Expand Down Expand Up @@ -280,9 +283,9 @@ contract EncumberableTokenTest is Test {

// bob releases part of the encumbrance
vm.prank(bob);
// emits Release event
// emits EncumbranceUpdate event
vm.expectEmit(true, true, true, true);
emit Release(alice, bob, 40e18);
emit EncumbranceUpdate(alice, bob, 100e18, 60e18);
wrappedToken.release(alice, 40e18);

assertEq(wrappedToken.balanceOf(alice), 100e18);
Expand Down

0 comments on commit 91705a4

Please sign in to comment.