Add ERC1363 implementation (#4631)

Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
Co-authored-by: ernestognw <ernestognw@gmail.com>
This commit is contained in:
Vittorio Minacori
2024-01-24 09:38:25 +01:00
committed by GitHub
parent a51f1e1354
commit e5f02bc608
21 changed files with 1227 additions and 215 deletions

View File

@ -0,0 +1,14 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {IERC20} from "../../interfaces/IERC20.sol";
import {ERC20, ERC1363} from "../../token/ERC20/extensions/ERC1363.sol";
// contract that replicate USDT approval behavior in approveAndCall
abstract contract ERC1363ForceApproveMock is ERC1363 {
function approveAndCall(address spender, uint256 amount, bytes memory data) public virtual override returns (bool) {
require(amount == 0 || allowance(msg.sender, spender) == 0, "USDT approval failure");
return super.approveAndCall(spender, amount, data);
}
}

View File

@ -0,0 +1,34 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {IERC20, ERC20} from "../../token/ERC20/ERC20.sol";
import {ERC1363} from "../../token/ERC20/extensions/ERC1363.sol";
abstract contract ERC1363NoReturnMock is ERC1363 {
function transferAndCall(address to, uint256 value, bytes memory data) public override returns (bool) {
super.transferAndCall(to, value, data);
assembly {
return(0, 0)
}
}
function transferFromAndCall(
address from,
address to,
uint256 value,
bytes memory data
) public override returns (bool) {
super.transferFromAndCall(from, to, value, data);
assembly {
return(0, 0)
}
}
function approveAndCall(address spender, uint256 value, bytes memory data) public override returns (bool) {
super.approveAndCall(spender, value, data);
assembly {
return(0, 0)
}
}
}

View File

@ -0,0 +1,52 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {IERC1363Receiver} from "../../interfaces/IERC1363Receiver.sol";
contract ERC1363ReceiverMock is IERC1363Receiver {
enum RevertType {
None,
RevertWithoutMessage,
RevertWithMessage,
RevertWithCustomError,
Panic
}
bytes4 private _retval;
RevertType private _error;
event Received(address operator, address from, uint256 value, bytes data);
error CustomError(bytes4);
constructor() {
_retval = IERC1363Receiver.onTransferReceived.selector;
_error = RevertType.None;
}
function setUp(bytes4 retval, RevertType error) public {
_retval = retval;
_error = error;
}
function onTransferReceived(
address operator,
address from,
uint256 value,
bytes calldata data
) external override returns (bytes4) {
if (_error == RevertType.RevertWithoutMessage) {
revert();
} else if (_error == RevertType.RevertWithMessage) {
revert("ERC1363ReceiverMock: reverting");
} else if (_error == RevertType.RevertWithCustomError) {
revert CustomError(_retval);
} else if (_error == RevertType.Panic) {
uint256 a = uint256(0) / uint256(0);
a;
}
emit Received(operator, from, value, data);
return _retval;
}
}

View File

@ -0,0 +1,34 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {IERC20, ERC20} from "../../token/ERC20/ERC20.sol";
import {ERC1363} from "../../token/ERC20/extensions/ERC1363.sol";
abstract contract ERC1363ReturnFalseOnERC20Mock is ERC1363 {
function transfer(address, uint256) public pure override(IERC20, ERC20) returns (bool) {
return false;
}
function transferFrom(address, address, uint256) public pure override(IERC20, ERC20) returns (bool) {
return false;
}
function approve(address, uint256) public pure override(IERC20, ERC20) returns (bool) {
return false;
}
}
abstract contract ERC1363ReturnFalseMock is ERC1363 {
function transferAndCall(address, uint256, bytes memory) public pure override returns (bool) {
return false;
}
function transferFromAndCall(address, address, uint256, bytes memory) public pure override returns (bool) {
return false;
}
function approveAndCall(address, uint256, bytes memory) public pure override returns (bool) {
return false;
}
}

View File

@ -0,0 +1,47 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {IERC1363Spender} from "../../interfaces/IERC1363Spender.sol";
contract ERC1363SpenderMock is IERC1363Spender {
enum RevertType {
None,
RevertWithoutMessage,
RevertWithMessage,
RevertWithCustomError,
Panic
}
bytes4 private _retval;
RevertType private _error;
event Approved(address owner, uint256 value, bytes data);
error CustomError(bytes4);
constructor() {
_retval = IERC1363Spender.onApprovalReceived.selector;
_error = RevertType.None;
}
function setUp(bytes4 retval, RevertType error) public {
_retval = retval;
_error = error;
}
function onApprovalReceived(address owner, uint256 value, bytes calldata data) external override returns (bytes4) {
if (_error == RevertType.RevertWithoutMessage) {
revert();
} else if (_error == RevertType.RevertWithMessage) {
revert("ERC1363SpenderMock: reverting");
} else if (_error == RevertType.RevertWithCustomError) {
revert CustomError(_retval);
} else if (_error == RevertType.Panic) {
uint256 a = uint256(0) / uint256(0);
a;
}
emit Approved(owner, value, data);
return _retval;
}
}