-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathERC677.sol
58 lines (52 loc) · 1.26 KB
/
ERC677.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// SPDX-License-Identifier: MIT
pragma solidity >0.6.0 <0.8.0;
import "../../vendor/OpenZeppelin/openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
import "./token/IERC677.sol";
import "./token/IERC677Receiver.sol";
abstract contract ERC677 is IERC677, ERC20 {
/**
* @dev transfer token to a contract address with additional data if the recipient is a contact.
* @param to The address to transfer to.
* @param value The amount to be transferred.
* @param data The extra data to be passed to the receiving contract.
*/
function transferAndCall(
address to,
uint value,
bytes memory data
)
public
override
virtual
returns (bool success)
{
super.transfer(to, value);
emit Transfer(msg.sender, to, value, data);
if (isContract(to)) {
contractFallback(to, value, data);
}
return true;
}
// PRIVATE
function contractFallback(
address to,
uint value,
bytes memory data
)
private
{
IERC677Receiver receiver = IERC677Receiver(to);
receiver.onTokenTransfer(msg.sender, value, data);
}
function isContract(
address addr
)
private
view
returns (bool hasCode)
{
uint length;
assembly { length := extcodesize(addr) }
return length > 0;
}
}