-
Notifications
You must be signed in to change notification settings - Fork 36
/
MockERC223.sol
84 lines (78 loc) · 3.14 KB
/
MockERC223.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//SPDX-License-Identifier: Unlicense
// https://github.com/Dexaran/ERC223-token-standard/blob/development/token/ERC223/ERC223.sol
pragma solidity ^0.8.0;
import {IERC223} from "../interfaces/IERC223.sol";
import {IERC223Recipient} from "../interfaces/IERC223Recipient.sol";
import {Address} from "@openzeppelin/contracts/utils/Address.sol";
/**
* @title Reference implementation of the ERC223 standard token.
*/
contract MockERC223 is IERC223 {
mapping(address => uint256) public override balanceOf;
string public constant override name = "M223Token";
string public constant override symbol = "M223";
uint8 public constant override decimals = 18;
string public constant override standard = "erc223";
uint256 public immutable override totalSupply;
/**
* @dev Sets the values for {name} and {symbol}, initializes {decimals} with
* a default value of 18.
*
* To select a different value for {decimals}, use {_setupDecimals}.
*
* All three of these values are immutable: they can only be set once during
* construction.
*/
constructor(uint256 initialSupply) {
totalSupply = initialSupply;
balanceOf[msg.sender] = initialSupply;
emit Transfer(address(0), msg.sender, initialSupply);
}
/**
* @dev Transfer the specified amount of tokens to the specified address.
* Invokes the `tokenFallback` function if the recipient is a contract.
* The token transfer fails if the recipient is a contract
* but does not implement the `tokenFallback` function
* or the fallback function to receive funds.
*
* @param _to Receiver address.
* @param _value Amount of tokens that will be transferred.
* @param _data Transaction metadata.
*/
function transfer(
address _to,
uint256 _value,
bytes calldata _data
) public override returns (bool success) {
balanceOf[msg.sender] -= _value;
balanceOf[_to] += _value;
if (Address.isContract(_to)) {
IERC223Recipient(_to).tokenReceived(msg.sender, _value, _data);
}
emit Transfer(msg.sender, _to, _value);
emit TransferData(_data);
return true;
}
/**
* @dev Transfer the specified amount of tokens to the specified address.
* This function works the same with the previous one
* but doesn't contain `_data` param.
* Added due to backwards compatibility reasons.
*
* @param _to Receiver address.
* @param _value Amount of tokens that will be transferred.
*/
function transfer(address _to, uint256 _value) public override returns (bool success) {
// Standard function transfer similar to ERC20 transfer with no _data .
// Added due to backwards compatibility reasons .
bytes memory _empty = hex"00000000";
balanceOf[msg.sender] -= _value;
balanceOf[_to] += _value;
if (Address.isContract(_to)) {
IERC223Recipient(_to).tokenReceived(msg.sender, _value, _empty);
}
emit Transfer(msg.sender, _to, _value);
emit TransferData(_empty);
return true;
}
}