Skip to content

Commit

Permalink
Merge pull request #17 from AugustoL/refactor/remove-not-erc20-methods
Browse files Browse the repository at this point in the history
refactor(contracts): Remove not native erc20 methods and fix line breaks
  • Loading branch information
Augusto Lemble authored May 21, 2019
2 parents 5a6a5ec + 3dbe76e commit e48628c
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 172 deletions.
59 changes: 7 additions & 52 deletions contracts/ERC827/ERC827.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ contract ERC827 is ERC20 {
* @param _data ABI-encoded contract call to call `_spender` address.
* @return true if the call function was executed successfully
*/
function approveAndCall(address _spender, uint256 _value, bytes memory _data)
public payable returns (bool)
{
function approveAndCall(
address _spender, uint256 _value, bytes memory _data
) public payable returns (bool) {
super.approve(_spender, _value);
_call(_spender, _data);
return true;
Expand All @@ -54,10 +54,9 @@ contract ERC827 is ERC20 {
* @param _data ABI-encoded contract call to call `_to` address.
* @return true if the call function was executed successfully
*/

function transferAndCall(address _to, uint256 _value, bytes memory _data)
public payable returns (bool)
{
function transferAndCall(
address _to, uint256 _value, bytes memory _data
) public payable returns (bool) {
super.transfer(_to, _value);
_call(_to, _data);
return true;
Expand All @@ -74,66 +73,22 @@ contract ERC827 is ERC20 {
*/
function transferFromAndCall(
address _from, address _to, uint256 _value, bytes memory _data
)
public payable returns (bool)
{
) public payable returns (bool) {
super.transferFrom(_from, _to, _value);
_call(_to, _data);
return true;
}

/**
* @dev Addition to ERC20 methods. Increase the amount of tokens that
* an owner allowed to a spender and execute a call with the sent data.
* approve should be called when allowed[_spender] == 0. To increment
* allowed value is better to use this function to avoid 2 calls (and wait
* until the first transaction is mined)
* @param _spender The address which will spend the funds.
* @param _addedValue The amount of tokens to increase the allowance by.
* @param _data ABI-encoded contract call to call `_spender` address.
*/
function increaseAllowanceAndCall(
address _spender, uint _addedValue, bytes memory _data
)
public payable returns (bool)
{
super.increaseAllowance(_spender, _addedValue);
_call(_spender, _data);
return true;
}

/**
* @dev Addition to StandardToken methods. Decrease the amount of tokens that
* an owner allowed to a spender and execute a call with the sent data.
* approve should be called when allowed[_spender] == 0. To decrement
* allowed value is better to use this function to avoid 2 calls (and wait
* until the first transaction is mined)
* @param _spender The address which will spend the funds.
* @param _subtractedValue The amount of tokens to decrease the allowance by.
* @param _data ABI-encoded contract call to call `_spender` address.
*/
function decreaseAllowanceAndCall(
address _spender, uint _subtractedValue, bytes memory _data
)
public payable returns (bool)
{
super.decreaseAllowance(_spender, _subtractedValue);
_call(_spender, _data);
return true;
}

/**
* @dev Call a external contract
* @param _to The address of the contract to call
* @param _data ABI-encoded contract call to call `_to` address.
*/
function _call(address _to, bytes memory _data) internal {

// solium-disable-next-line security/no-call-value, no-unused-vars
(bool success, bytes memory data) = address(proxy).call.value(msg.value)(
abi.encodeWithSelector(proxy.callContractFunctionSignature(), _to, _data)
);

require(success, "Call to external contract failed");
}

Expand Down
14 changes: 14 additions & 0 deletions contracts/ERC827/ERC827Mock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pragma solidity ^0.5.0;


import "./ERC827.sol";


// mock class using ERC827 Token
contract ERC827Mock is ERC827 {

constructor(address initialAccount, uint256 initialBalance) public ERC827() {
_mint(initialAccount, initialBalance);
}

}
9 changes: 3 additions & 6 deletions contracts/ERC827/ERC827Proxy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,16 @@ contract ERC827Proxy {
* @param _target address The address which you want to transfer to
* @param _data bytes The data to be executed in the call
*/
function callContract(address _target, bytes memory _data)
public payable returns (bool)
{
function callContract(
address _target, bytes memory _data
) public payable returns (bool) {
require(
msg.sender == address(token),
"Proxy only can execute calls from the token contract"
);

// solium-disable-next-line security/no-call-value, no-unused-vars
(bool success, bytes memory data) = _target .call.value(msg.value)(_data);

require(success, "Proxy call failed");

return true;
}

Expand Down
54 changes: 27 additions & 27 deletions contracts/ERC827/IERC827.sol
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
pragma solidity ^0.5.2;


import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";


/**
* @title ERC827 interface, an extension of ERC20 token standard
*
* @dev Interface of a ERC827 token, following the ERC20 standard with extra
* methods to transfer value and data and execute calls in transfers and
* approvals.
*/
contract IERC827 is IERC20 {
function approveAndCall(address _spender, uint256 _value, bytes memory _data)
public payable returns (bool);

function transferAndCall(address _to, uint256 _value, bytes memory _data)
public payable returns (bool);

function transferFromAndCall(
address _from,
address _to,
uint256 _value,
bytes memory _data
)
public payable returns (bool);

function increaseAllowanceAndCall(
address _spender, uint _addedValue, bytes memory _data
)
public payable returns (bool);

function decreaseAllowanceAndCall(
address _spender, uint _subtractedValue, bytes memory _data
)
public payable returns (bool);
interface IERC827 {

function transfer(address to, uint256 value) external returns (bool);

function approve(address spender, uint256 value) external returns (bool);

function transferFrom(address from, address to, uint256 value) external returns (bool);

function totalSupply() external view returns (uint256);

function balanceOf(address who) external view returns (uint256);

function allowance(address owner, address spender) external view returns (uint256);

function approveAndCall(address _spender, uint256 _value, bytes calldata _data)
external payable returns (bool);

function transferAndCall(address _to, uint256 _value, bytes calldata _data)
external payable returns (bool);

function transferFromAndCall(
address _from, address _to, uint256 _value, bytes calldata _data
) external payable returns (bool);

event Transfer(address indexed from, address indexed to, uint256 value);

event Approval(address indexed owner, address indexed spender, uint256 value);
}
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,21 @@
"dotenv": "^4.0.0",
"eslint": "^4.11.0",
"eslint-config-standard": "^10.2.1",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-import": "^2.17.2",
"eslint-plugin-node": "^5.2.1",
"eslint-plugin-promise": "^3.6.0",
"eslint-plugin-standard": "^3.0.1",
"ethereumjs-util": "^5.1.2",
"ethjs-abi": "^0.2.1",
"ganache-cli": "^6.1.0",
"ganache-cli": "^6.4.3",
"solidity-coverage": "^0.5.11",
"solium": "^1.2.3",
"truffle": "^5.0.14",
"solium": "^1.2.4",
"truffle": "^5.0.15",
"truffle-hdwallet-provider": "0.0.3",
"web3-utils": "^1.0.0-beta.48"
"web3-utils": "^1.0.0-beta.54"
},
"dependencies": {
"openzeppelin-solidity": "^2.2.0",
"zos": "^2.2.2"
"zos": "^2.2.3"
}
}
82 changes: 1 addition & 81 deletions test/ERC827/ERC827Token.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import EVMRevert from '../helpers/EVMRevert';
var Message = artifacts.require('MessageHelper');
var ERC827TokenMock = artifacts.require('ERC827TokenMock');
var ERC827TokenMock = artifacts.require('ERC827Mock');

require('chai').use(require('chai-as-promised')).should();
const assert = require('chai').assert;
Expand Down Expand Up @@ -116,51 +116,6 @@ contract('ERC827 Token', function (accounts) {
assert.equal(await web3.eth.getBalance(message.address), 1000);
});

it(
'should allow payment through increaseAllowance'
, async function () {
const extraData = message.web3Instance.methods.buyMessage(
web3.utils.toHex(123456), 666, 'Transfer Done'
).encodeABI();

await token.approve(message.address, 10);
assert.equal(await token.allowance(accounts[0], message.address), 10);

const transaction = await token.increaseAllowanceAndCall(
message.address, 50, extraData, { from: accounts[0], value: 1000 }
);

assert.equal(2, transaction.receipt.rawLogs.length);

assert.equal(
await token.allowance(accounts[0], message.address), 60
);
assert.equal(
await web3.eth.getBalance(message.address), 1000
);
});

it(
'should allow payment through decreaseAllowance'
, async function () {
await token.approve(message.address, 100);

assert.equal(await token.allowance(accounts[0], message.address), 100);

const extraData = message.web3Instance.methods.buyMessage(
web3.utils.toHex(123456), 666, 'Transfer Done'
).encodeABI();

const transaction = await token.decreaseAllowanceAndCall(
message.address, 60, extraData, { from: accounts[0], value: 1000 }
);

assert.equal(2, transaction.receipt.rawLogs.length);

assert.equal(await token.allowance(accounts[0], message.address), 40);
assert.equal(await web3.eth.getBalance(message.address), 1000);
});

it(
'should allow payment through transferFrom'
, async function () {
Expand Down Expand Up @@ -259,41 +214,6 @@ contract('ERC827 Token', function (accounts) {
assert.equal(await token.allowance(accounts[0], message.address), 100);
});

it(
'should return correct allowance after increaseAllowance (with data) and show the event on receiver contract'
, async function () {
const extraData = message.web3Instance.methods.showMessage(
web3.utils.toHex(123456), 666, 'Transfer Done'
).encodeABI();

await token.approve(message.address, 10);
assert.equal(await token.allowance(accounts[0], message.address), 10);

const transaction = await token.increaseAllowanceAndCall(message.address, 50, extraData);

assert.equal(2, transaction.receipt.rawLogs.length);

assert.equal(await token.allowance(accounts[0], message.address), 60);
});

it(
'should return correct allowance after decreaseAllowance (with data) and show the event on receiver contract'
, async function () {
await token.approve(message.address, 100);

assert.equal(await token.allowance(accounts[0], message.address), 100);

const extraData = message.web3Instance.methods.showMessage(
web3.utils.toHex(123456), 666, 'Transfer Done'
).encodeABI();

const transaction = await token.decreaseAllowanceAndCall(message.address, 60, extraData);

assert.equal(2, transaction.receipt.rawLogs.length);

assert.equal(await token.allowance(accounts[0], message.address), 40);
});

it(
'should return correct balances after transferFrom (with data) and show the event on receiver contract'
, async function () {
Expand Down

0 comments on commit e48628c

Please sign in to comment.