Skip to content

Commit

Permalink
make SuperToken 2771 recipient which trusts DMZForwarder, example use…
Browse files Browse the repository at this point in the history
… with increaseAllowance
  • Loading branch information
d10r committed Nov 5, 2024
1 parent 0b33f76 commit 8e6a20a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,11 @@ interface ISuperfluid {
*/
function forwardBatchCall(Operation[] calldata operations) external payable;

/**
* @dev Get the current DMZForwarder
*/
function getDMZForwarder() external view returns(address);

/**************************************************************************
* Function modifiers for access control and parameter validations
*
Expand Down
32 changes: 29 additions & 3 deletions packages/ethereum-contracts/contracts/superfluid/SuperToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from "../interfaces/superfluid/ISuperfluid.sol";
import { SuperfluidToken } from "./SuperfluidToken.sol";
import { ERC777Helper } from "../libs/ERC777Helper.sol";
import { IRelayRecipient } from "../interfaces/utils/IRelayRecipient.sol";
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import { SafeMath } from "@openzeppelin/contracts/utils/math/SafeMath.sol";
import { SafeCast } from "@openzeppelin/contracts/utils/math/SafeCast.sol";
Expand All @@ -35,7 +36,8 @@ interface IConstantInflowNFT {}
contract SuperToken is
UUPSProxiable,
SuperfluidToken,
ISuperToken
ISuperToken,
IRelayRecipient
{

using SafeMath for uint256;
Expand Down Expand Up @@ -543,8 +545,10 @@ contract SuperToken is
}

function increaseAllowance(address spender, uint256 addedValue)
public virtual override returns (bool) {
_approve(msg.sender, spender, _allowances[msg.sender][spender] + addedValue);
public virtual override returns (bool)
{
address msgSender = _msgSender();
_approve(msgSender, spender, _allowances[msgSender][spender] + addedValue);
return true;
}

Expand Down Expand Up @@ -886,6 +890,28 @@ contract SuperToken is
_downgrade(msg.sender, account, to, amount, "", "");
}

/**************************************************************************
* IRelayRecipient
*************************************************************************/

/// @dev IRelayRecipient.isTrustedForwarder implementation
function isTrustedForwarder(address forwarder) public view override returns(bool) {
return forwarder == _host.getDMZForwarder();
}

/// @dev IRelayRecipient.versionRecipient implementation
function versionRecipient() external override pure returns (string memory) {
return "v1";

Check warning on line 904 in packages/ethereum-contracts/contracts/superfluid/SuperToken.sol

View check run for this annotation

Codecov / codecov/patch

packages/ethereum-contracts/contracts/superfluid/SuperToken.sol#L903-L904

Added lines #L903 - L904 were not covered by tests
}

function _msgSender() internal virtual view returns (address) {
if(msg.data.length >= 20 && isTrustedForwarder(msg.sender)) {
return address(bytes20(msg.data[msg.data.length - 20:]));
} else {
return msg.sender;
}
}

/**************************************************************************
* Modifiers
*************************************************************************/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,10 @@ contract Superfluid is
return "v1";
}

function getDMZForwarder() external view override returns(address) {
return address(DMZ_FORWARDER);
}

/**************************************************************************
* Internal
**************************************************************************/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -628,4 +628,18 @@ contract SuperfluidBatchCallTest is FoundrySuperfluidTester {
assertEq(address(forwarder).balance, 0, "DMZForwarder: balance still not 0");
assertEq(bob.balance, amount, "DMZForwarder: where did the money go?");
}

function testIncreaseAllowanceUsing2771ForwardCall(uint256 allowanceAmount) public {
ISuperfluid.Operation[] memory ops = new ISuperfluid.Operation[](1);
uint256 aliceToBobAllowanceBefore = superToken.allowance(alice, bob);
ops[0] = ISuperfluid.Operation({
operationType: BatchOperation.OPERATION_TYPE_ERC2771_FORWARD_CALL,
target: address(superToken),
data: abi.encodeCall(superToken.increaseAllowance, (bob, allowanceAmount))
});
vm.prank(alice);
sf.host.batchCall(ops);
uint256 aliceToBobAllowanceAfter = superToken.allowance(alice, bob);
assertEq(aliceToBobAllowanceAfter, aliceToBobAllowanceBefore + allowanceAmount);
}
}

0 comments on commit 8e6a20a

Please sign in to comment.