Skip to content

Commit

Permalink
fix: addresses #32, allowance() amount should reflect transferable am…
Browse files Browse the repository at this point in the history
…ounts for transferFrom() and transfer()
  • Loading branch information
zlace0x committed Dec 5, 2022
1 parent fb18570 commit 6a3030e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Funnel.sol
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,20 @@ contract Funnel is IFunnel, NativeMetaTransaction, MetaTxContext, Initializable
return _remainingAllowance(_owner, _spender);
}

// @notice fetch remaining allowance between _owner and _spender while accounting for base token allowance.
function _remainingAllowance(address _owner, address _spender)
private
view
returns (uint256)
{
RenewableAllowance memory a = rAllowance[_owner][_spender];

uint256 baseAllowance = _baseToken.allowance(_owner, address(this));
uint256 recovered = a.recoveryRate * (block.timestamp - a.lastUpdated);
uint256 remainingAllowance = a.remaining + recovered;
return remainingAllowance > a.maxAmount ? a.maxAmount : remainingAllowance;
uint256 currentAllowance = remainingAllowance > a.maxAmount
? a.maxAmount
: remainingAllowance;
return currentAllowance > baseAllowance ? baseAllowance : currentAllowance;
}

/// @notice fetch approved max amount and recovery rate
Expand Down
9 changes: 9 additions & 0 deletions test/Funnel.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ contract FunnelTest is ERC5827TestSuite {
funnel.approveRenewable(user2, 100, 101);
}

function testBaseTokenAllowance() public {
vm.startPrank(user1);
token.approve(address(funnel), 100);
assertEq(token.allowance(user1, address(funnel)), 100);

funnel.approveRenewable(address(spender), type(uint256).max, 1);
assertEq(funnel.allowance(user1, address(spender)), 100); // reflects base token allowance
}

function testInfiniteApproveTransferFrom() public {
vm.prank(user1);
funnel.approve(address(this), type(uint256).max);
Expand Down

0 comments on commit 6a3030e

Please sign in to comment.