Skip to content

Commit 4b0098b

Browse files
authored
OnTokenTransferAdapter (#7)
* Implement `OnTokenTransferAdapter` that rewrites 667 hooks as ERC-1363 * documentation * Update OnTokenTransferAdapter.sol
1 parent ff692d1 commit 4b0098b

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity ^0.8.20;
4+
5+
import {IERC1363Receiver} from "@openzeppelin/contracts/interfaces/IERC1363Receiver.sol";
6+
7+
/**
8+
* @dev This contract exposes the 667 `onTokenTransfer` hook on top of {IERC1363Receiver-onTransferReceived}.
9+
*
10+
* Inheriting from this adapter makes your `ERC1363Receiver` contract automatically compatible with tokens, such as
11+
* Chainlink's Link, that implement the 667 interface for transferAndCall.
12+
*/
13+
abstract contract OnTokenTransferAdapter is IERC1363Receiver {
14+
function onTokenTransfer(address from, uint256 amount, bytes calldata data) public virtual returns (bool) {
15+
// Rewrite call as IERC1363.onTransferReceived
16+
// This uses delegate call to keep the correct sender (token contracts)
17+
//
18+
// Note that since 667 doesn't implement `transferFromAndCall`, this hook was called by a simple
19+
// `transferAndCall` and thus the operator is necessarily the `from` address.
20+
(bool success, bytes memory returndata) = address(this).delegatecall(
21+
abi.encodeCall(IERC1363Receiver.onTransferReceived, (from, from, amount, data))
22+
);
23+
// check success and return as boolean
24+
return
25+
success &&
26+
returndata.length >= 0x20 &&
27+
abi.decode(returndata, (bytes4)) == IERC1363Receiver.onTransferReceived.selector;
28+
}
29+
}

0 commit comments

Comments
 (0)