Skip to content

Commit

Permalink
revert allow transferAndCall to EOA
Browse files Browse the repository at this point in the history
  • Loading branch information
Amxx committed Jun 17, 2022
1 parent c24145f commit a3ef6b4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 48 deletions.
44 changes: 18 additions & 26 deletions contracts/token/ERC20/extensions/ERC1363.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,17 @@ abstract contract ERC1363 is IERC1363, ERC20, ERC165 {
uint256 value,
bytes memory data
) private returns (bool) {
if (to.isContract()) {
try IERC1363Receiver(to).onTransferReceived(_msgSender(), from, value, data) returns (bytes4 retval) {
return retval == IERC1363Receiver.onTransferReceived.selector;
} catch (bytes memory reason) {
if (reason.length == 0) {
revert("ERC1363: transfer to non ERC1363Receiver implementer");
} else {
/// @solidity memory-safe-assembly
assembly {
revert(add(32, reason), mload(reason))
}
try IERC1363Receiver(to).onTransferReceived(_msgSender(), from, value, data) returns (bytes4 retval) {
return retval == IERC1363Receiver.onTransferReceived.selector;
} catch (bytes memory reason) {
if (reason.length == 0) {
revert("ERC1363: transfer to non ERC1363Receiver implementer");
} else {
/// @solidity memory-safe-assembly
assembly {
revert(add(32, reason), mload(reason))
}
}
} else {
return true;
}
}

Expand All @@ -130,21 +126,17 @@ abstract contract ERC1363 is IERC1363, ERC20, ERC165 {
uint256 value,
bytes memory data
) private returns (bool) {
if (spender.isContract()) {
try IERC1363Spender(spender).onApprovalReceived(owner, value, data) returns (bytes4 retval) {
return retval == IERC1363Spender.onApprovalReceived.selector;
} catch (bytes memory reason) {
if (reason.length == 0) {
revert("ERC1363: transfer to non ERC1363Spender implementer");
} else {
/// @solidity memory-safe-assembly
assembly {
revert(add(32, reason), mload(reason))
}
try IERC1363Spender(spender).onApprovalReceived(owner, value, data) returns (bytes4 retval) {
return retval == IERC1363Spender.onApprovalReceived.selector;
} catch (bytes memory reason) {
if (reason.length == 0) {
revert("ERC1363: transfer to non ERC1363Spender implementer");
} else {
/// @solidity memory-safe-assembly
assembly {
revert(add(32, reason), mload(reason))
}
}
} else {
return true;
}
}
}
42 changes: 20 additions & 22 deletions test/token/ERC20/extensions/ERC1363.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ contract('ERC1363', function (accounts) {

describe('transferAndCall', function () {
it('to EOA', async function () {
const { receipt } = await this.token.methods['transferAndCall(address,uint256)'](other, value, { from: holder });
expectEvent(receipt, 'Transfer', {
from: holder,
to: other,
value,
});
await expectRevert(
this.token.methods['transferAndCall(address,uint256)'](
other,
value,
{ from: holder },
),
'function call to a non-contract account',
);
});

describe('to receiver', function () {
Expand Down Expand Up @@ -107,17 +109,15 @@ contract('ERC1363', function (accounts) {
});

it('to EOA', async function () {
const { receipt } = await this.token.methods['transferFromAndCall(address,address,uint256)'](
holder,
other,
value,
{ from: operator },
await expectRevert(
this.token.methods['transferFromAndCall(address,address,uint256)'](
holder,
other,
value,
{ from: operator },
),
'function call to a non-contract account',
);
expectEvent(receipt, 'Transfer', {
from: holder,
to: other,
value,
});
});

describe('to receiver', function () {
Expand Down Expand Up @@ -197,12 +197,10 @@ contract('ERC1363', function (accounts) {

describe('approveAndCall', function () {
it('to EOA', async function () {
const { receipt } = await this.token.methods['approveAndCall(address,uint256)'](other, value, { from: holder });
expectEvent(receipt, 'Approval', {
owner: holder,
spender: other,
value,
});
await expectRevert(
this.token.methods['approveAndCall(address,uint256)'](other, value, { from: holder }),
'function call to a non-contract account',
);
});

describe('to receiver', function () {
Expand Down

0 comments on commit a3ef6b4

Please sign in to comment.