Skip to content

Commit b0522b9

Browse files
authored
Merge pull request #518 from AugustoL/add-smart-token
Add ERC827 Token
2 parents 0cdc5e1 + 969466b commit b0522b9

File tree

6 files changed

+560
-1
lines changed

6 files changed

+560
-1
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
pragma solidity ^0.4.13;
2+
3+
4+
import "../token/ERC827Token.sol";
5+
6+
7+
// mock class using ERC827 Token
8+
contract ERC827TokenMock is ERC827Token {
9+
10+
function ERC827TokenMock(address initialAccount, uint256 initialBalance) public {
11+
balances[initialAccount] = initialBalance;
12+
totalSupply = initialBalance;
13+
}
14+
15+
}

contracts/mocks/MessageHelper.sol

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
pragma solidity ^0.4.11;
2+
3+
contract MessageHelper {
4+
5+
event Show(bytes32 b32, uint256 number, string text);
6+
7+
function showMessage( bytes32 message, uint256 number, string text ) public returns (bool) {
8+
Show(message, number, text);
9+
return true;
10+
}
11+
12+
function fail() public {
13+
require(false);
14+
}
15+
16+
function call(address to, bytes data) public returns (bool) {
17+
if (to.call(data))
18+
return true;
19+
else
20+
return false;
21+
}
22+
23+
}

contracts/token/ERC827.sol

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
pragma solidity ^0.4.13;
2+
3+
4+
import "./ERC20.sol";
5+
6+
7+
/**
8+
@title ERC827 interface, an extension of ERC20 token standard
9+
10+
Interface of a ERC827 token, following the ERC20 standard with extra
11+
methods to transfer value and data and execute calls in transfers and
12+
approvals.
13+
*/
14+
contract ERC827 is ERC20 {
15+
16+
function approve( address _spender, uint256 _value, bytes _data ) public returns (bool);
17+
function transfer( address _to, uint256 _value, bytes _data ) public returns (bool);
18+
function transferFrom( address _from, address _to, uint256 _value, bytes _data ) public returns (bool);
19+
20+
}

contracts/token/ERC827Token.sol

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
pragma solidity ^0.4.13;
2+
3+
import "./ERC827.sol";
4+
import "./StandardToken.sol";
5+
6+
/**
7+
@title ERC827, an extension of ERC20 token standard
8+
9+
Implementation the ERC827, following the ERC20 standard with extra
10+
methods to transfer value and data and execute calls in transfers and
11+
approvals.
12+
Uses OpenZeppelin StandardToken.
13+
*/
14+
contract ERC827Token is ERC827, StandardToken {
15+
16+
/**
17+
@dev Addition to ERC20 token methods. It allows to
18+
approve the transfer of value and execute a call with the sent data.
19+
20+
Beware that changing an allowance with this method brings the risk that
21+
someone may use both the old and the new allowance by unfortunate
22+
transaction ordering. One possible solution to mitigate this race condition
23+
is to first reduce the spender's allowance to 0 and set the desired value
24+
afterwards:
25+
https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
26+
27+
@param _spender The address that will spend the funds.
28+
@param _value The amount of tokens to be spent.
29+
@param _data ABI-encoded contract call to call `_to` address.
30+
31+
@return true if the call function was executed successfully
32+
*/
33+
function approve(address _spender, uint256 _value, bytes _data) public returns (bool) {
34+
require(_spender != address(this));
35+
36+
super.approve(_spender, _value);
37+
38+
require(_spender.call(_data));
39+
40+
return true;
41+
}
42+
43+
/**
44+
@dev Addition to ERC20 token methods. Transfer tokens to a specified
45+
address and execute a call with the sent data on the same transaction
46+
47+
@param _to address The address which you want to transfer to
48+
@param _value uint256 the amout of tokens to be transfered
49+
@param _data ABI-encoded contract call to call `_to` address.
50+
51+
@return true if the call function was executed successfully
52+
*/
53+
function transfer(address _to, uint256 _value, bytes _data) public returns (bool) {
54+
require(_to != address(this));
55+
56+
super.transfer(_to, _value);
57+
58+
require(_to.call(_data));
59+
return true;
60+
}
61+
62+
/**
63+
@dev Addition to ERC20 token methods. Transfer tokens from one address to
64+
another and make a contract call on the same transaction
65+
66+
@param _from The address which you want to send tokens from
67+
@param _to The address which you want to transfer to
68+
@param _value The amout of tokens to be transferred
69+
@param _data ABI-encoded contract call to call `_to` address.
70+
71+
@return true if the call function was executed successfully
72+
*/
73+
function transferFrom(address _from, address _to, uint256 _value, bytes _data) public returns (bool) {
74+
require(_to != address(this));
75+
76+
super.transferFrom(_from, _to, _value);
77+
78+
require(_to.call(_data));
79+
return true;
80+
}
81+
82+
/**
83+
* @dev Addition to StandardToken methods. Increase the amount of tokens that
84+
* an owner allowed to a spender and execute a call with the sent data.
85+
*
86+
* approve should be called when allowed[_spender] == 0. To increment
87+
* allowed value is better to use this function to avoid 2 calls (and wait until
88+
* the first transaction is mined)
89+
* From MonolithDAO Token.sol
90+
* @param _spender The address which will spend the funds.
91+
* @param _addedValue The amount of tokens to increase the allowance by.
92+
* @param _data ABI-encoded contract call to call `_spender` address.
93+
*/
94+
function increaseApproval(address _spender, uint _addedValue, bytes _data) public returns (bool) {
95+
require(_spender != address(this));
96+
97+
super.increaseApproval(_spender, _addedValue);
98+
99+
require(_spender.call(_data));
100+
101+
return true;
102+
}
103+
104+
/**
105+
* @dev Addition to StandardToken methods. Decrease the amount of tokens that
106+
* an owner allowed to a spender and execute a call with the sent data.
107+
*
108+
* approve should be called when allowed[_spender] == 0. To decrement
109+
* allowed value is better to use this function to avoid 2 calls (and wait until
110+
* the first transaction is mined)
111+
* From MonolithDAO Token.sol
112+
* @param _spender The address which will spend the funds.
113+
* @param _subtractedValue The amount of tokens to decrease the allowance by.
114+
* @param _data ABI-encoded contract call to call `_spender` address.
115+
*/
116+
function decreaseApproval(address _spender, uint _subtractedValue, bytes _data) public returns (bool) {
117+
require(_spender != address(this));
118+
119+
super.decreaseApproval(_spender, _subtractedValue);
120+
121+
require(_spender.call(_data));
122+
123+
return true;
124+
}
125+
126+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"truffle-hdwallet-provider": "0.0.3"
5757
},
5858
"dependencies": {
59-
"dotenv": "^4.0.0"
59+
"dotenv": "^4.0.0",
60+
"ethjs-abi": "^0.2.1"
6061
}
6162
}

0 commit comments

Comments
 (0)