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:
committed by
GitHub
parent
a51f1e1354
commit
e5f02bc608
14
contracts/mocks/token/ERC1363ForceApproveMock.sol
Normal file
14
contracts/mocks/token/ERC1363ForceApproveMock.sol
Normal 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);
|
||||
}
|
||||
}
|
||||
34
contracts/mocks/token/ERC1363NoReturnMock.sol
Normal file
34
contracts/mocks/token/ERC1363NoReturnMock.sol
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
52
contracts/mocks/token/ERC1363ReceiverMock.sol
Normal file
52
contracts/mocks/token/ERC1363ReceiverMock.sol
Normal 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;
|
||||
}
|
||||
}
|
||||
34
contracts/mocks/token/ERC1363ReturnFalseMock.sol
Normal file
34
contracts/mocks/token/ERC1363ReturnFalseMock.sol
Normal 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;
|
||||
}
|
||||
}
|
||||
47
contracts/mocks/token/ERC1363SpenderMock.sol
Normal file
47
contracts/mocks/token/ERC1363SpenderMock.sol
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user