Skip to content

Commit

Permalink
OnTokenTransferAdapter (#7)
Browse files Browse the repository at this point in the history
* Implement `OnTokenTransferAdapter` that rewrites 667 hooks as ERC-1363

* documentation

* Update OnTokenTransferAdapter.sol
  • Loading branch information
Amxx authored Oct 3, 2024
1 parent ff692d1 commit 4b0098b
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions contracts/token/OnTokenTransferAdapter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;

import {IERC1363Receiver} from "@openzeppelin/contracts/interfaces/IERC1363Receiver.sol";

/**
* @dev This contract exposes the 667 `onTokenTransfer` hook on top of {IERC1363Receiver-onTransferReceived}.
*
* Inheriting from this adapter makes your `ERC1363Receiver` contract automatically compatible with tokens, such as
* Chainlink's Link, that implement the 667 interface for transferAndCall.
*/
abstract contract OnTokenTransferAdapter is IERC1363Receiver {
function onTokenTransfer(address from, uint256 amount, bytes calldata data) public virtual returns (bool) {
// Rewrite call as IERC1363.onTransferReceived
// This uses delegate call to keep the correct sender (token contracts)
//
// Note that since 667 doesn't implement `transferFromAndCall`, this hook was called by a simple
// `transferAndCall` and thus the operator is necessarily the `from` address.
(bool success, bytes memory returndata) = address(this).delegatecall(
abi.encodeCall(IERC1363Receiver.onTransferReceived, (from, from, amount, data))
);
// check success and return as boolean
return
success &&
returndata.length >= 0x20 &&
abi.decode(returndata, (bytes4)) == IERC1363Receiver.onTransferReceived.selector;
}
}

0 comments on commit 4b0098b

Please sign in to comment.