Skip to content

Commit

Permalink
Better hook tests and fix test setup issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacques Dafflon committed Nov 4, 2018
1 parent 5a33e2d commit c9fae11
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 89 deletions.
22 changes: 19 additions & 3 deletions contracts/examples/ExampleTokensRecipient.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,25 @@ import { ERC820Implementer } from "eip820/contracts/ERC820Implementer.sol";
import { ERC820ImplementerInterface } from "eip820/contracts/ERC820ImplementerInterface.sol";
import { Ownable } from "openzeppelin-solidity/contracts/ownership/Ownable.sol";
import { ERC777TokensRecipient } from "../ERC777TokensRecipient.sol";
import { ERC777Token } from "../ERC777Token.sol";


contract ExampleTokensRecipient is ERC820Implementer, ERC820ImplementerInterface, ERC777TokensRecipient, Ownable {

bool private allowTokensReceived;
bool public notified;

mapping(address => address) public token;
mapping(address => address) public operator;
mapping(address => address) public from;
mapping(address => address) public to;
mapping(address => uint256) public amount;
mapping(address => bytes) public holderData;
mapping(address => bytes) public operatorData;
mapping(address => uint256) public balanceOf;

constructor(bool _setInterface) public {
if (_setInterface) { setInterfaceImplementation("ERC777TokensRecipient", this); }
allowTokensReceived = true;
notified = false;
}

function tokensReceived(
Expand All @@ -35,7 +43,15 @@ contract ExampleTokensRecipient is ERC820Implementer, ERC820ImplementerInterface
public
{
require(allowTokensReceived, "Receive not allowed");
notified = true;
token[_to] = msg.sender;
operator[_to] = _operator;
from[_to] = _from;
to[_to] = _to;
amount[_to] = _amount;
holderData[_to] = _holderData;
operatorData[_to] = _operatorData;
balanceOf[_from] = ERC777Token(msg.sender).balanceOf(_from);
balanceOf[_to] = ERC777Token(msg.sender).balanceOf(_to);
}

function acceptTokens() public onlyOwner { allowTokensReceived = true; }
Expand Down
36 changes: 26 additions & 10 deletions contracts/examples/ExampleTokensSender.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,47 @@ import { ERC820Implementer } from "eip820/contracts/ERC820Implementer.sol";
import { ERC820ImplementerInterface } from "eip820/contracts/ERC820ImplementerInterface.sol";
import { Ownable } from "openzeppelin-solidity/contracts/ownership/Ownable.sol";
import { ERC777TokensSender } from "../ERC777TokensSender.sol";
import { ERC777Token } from "../ERC777Token.sol";


contract ExampleTokensSender is ERC820Implementer, ERC820ImplementerInterface, ERC777TokensSender, Ownable {

bool private allowTokensToSend;
bool public notified;

mapping(address => address) public token;
mapping(address => address) public operator;
mapping(address => address) public from;
mapping(address => address) public to;
mapping(address => uint256) public amount;
mapping(address => bytes) public holderData;
mapping(address => bytes) public operatorData;
mapping(address => uint256) public balanceOf;

constructor(bool _setInterface) public {
if (_setInterface) { setInterfaceImplementation("ERC777TokensSender", this); }
allowTokensToSend = true;
notified = false;
}

function tokensToSend(
address operator, // solhint-disable no-unused-vars
address from,
address to,
uint amount,
bytes holderData,
bytes operatorData
) // solhint-enable no-unused-vars
address _operator,
address _from,
address _to,
uint _amount,
bytes _holderData,
bytes _operatorData
)
public
{
require(allowTokensToSend, "Send not allowed");
notified = true;
token[_to] = msg.sender;
operator[_to] = _operator;
from[_to] = _from;
to[_to] = _to;
amount[_to] = _amount;
holderData[_to] = _holderData;
operatorData[_to] = _operatorData;
balanceOf[_from] = ERC777Token(msg.sender).balanceOf(_from);
balanceOf[_to] = ERC777Token(msg.sender).balanceOf(_to);
}

function acceptTokensToSend() public onlyOwner { allowTokensToSend = true; }
Expand Down
16 changes: 8 additions & 8 deletions test/ReferenceToken.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ contract('ReferenceToken', function(accounts) {
token.burnOperator,
] });

after(async function() { await web3.currentProvider.connection.close(); });

beforeEach(async function() {
let erc820Registry = await EIP820Registry.deploy(web3, accounts[0]);
assert.ok(erc820Registry.$address);
Expand All @@ -59,13 +57,16 @@ contract('ReferenceToken', function(accounts) {
.send({ gas: 300000, from: accounts[0] });
};

token.genMintTxForAccount = function(account, amount, operator, gas) {
return token.contract.methods
.mint(account, web3.utils.toWei(amount), '0xcafe')
.send.request({ gas: gas, from: operator });
token.mintForAccount = async function(account, amount, operator) {
const mintTx = token.contract.methods
.mint(account, web3.utils.toWei(amount), '0xcafe');
const gas = await mintTx.estimateGas();
await mintTx.send({ gas: gas, from: operator });
};
});

after(async function() { await web3.currentProvider.connection.close(); });

describe('Creation', function() {
it('should not deploy the token with a granularity of 0', async function() {
const estimateGas = await deployContract.estimateGas();
Expand Down Expand Up @@ -110,8 +111,7 @@ contract('ReferenceToken', function(accounts) {
.getInterfaceImplementer(token.contract.options.address, erc20Hash)
.call();

assert.strictEqual(
erc20Addr, '0x0000000000000000000000000000000000000000');
assert.strictEqual(erc20Addr, utils.zeroAddress);
});
});

Expand Down
3 changes: 1 addition & 2 deletions test/utils/erc20Disabled.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ exports.test = function(web3, accounts, token) {
.getInterfaceImplementer(token.contract.options.address, erc20Hash)
.call();

assert.strictEqual(
erc20Addr, '0x0000000000000000000000000000000000000000');
assert.strictEqual(erc20Addr, utils.zeroAddress);

await token.contract.methods
.enableERC20()
Expand Down
73 changes: 66 additions & 7 deletions test/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ const testAccounts = [
const blocks = [];
let blockIdx = 0;

let log = (msg) => process.env.MOCHA_VERBOSE && console.log(msg);
const log = (msg) => process.env.MOCHA_VERBOSE && console.log(msg);
const zeroAddress = '0x0000000000000000000000000000000000000000';

module.exports = {
zeroAddress,
log,

formatAccount(account) {
Expand Down Expand Up @@ -56,13 +58,70 @@ module.exports = {
);
},

async mintForAllAccounts(web3, accounts, token, operator, amount, gas) {
const mintBatch = new web3.BatchRequest();
async mintForAllAccounts(web3, accounts, token, operator, amount) {
let erc820Registry = new web3.eth.Contract(
ERC820Registry.abi,
'0x991a1bcb077599290d7305493c9a630c20f8b798'
);
let hook;
for (let account of accounts) {
mintBatch.add(
token.genMintTxForAccount(account, amount, accounts[0], gas)
);
hook = await erc820Registry.methods.getInterfaceImplementer(
account, web3.utils.keccak256('ERC777TokensRecipient')).call();
if (hook === zeroAddress) { hook = '0x0'; }
log(`mint ${amount} for ${account} by ${operator} (hook: ${hook})`);
await token.mintForAccount(account, amount, operator);
}
await mintBatch.execute();
},

async assertHookCalled(
web3,
hook,
token,
operator,
from,
to,
holderData,
operatorData,
balance_from,
balance_to
) {
assert.strictEqual(
await hook.methods.token(to).call(),
web3.utils.toChecksumAddress(token)
);
assert.strictEqual(
await hook.methods.operator(to).call(),
web3.utils.toChecksumAddress(operator)
);
assert.strictEqual(
await hook.methods.from(to).call(),
web3.utils.toChecksumAddress(from)
);
assert.strictEqual(
await hook.methods.to(to).call(),
web3.utils.toChecksumAddress(to)
);
assert.strictEqual(await hook.methods.holderData(to).call(), holderData);
assert.strictEqual(
await hook.methods.operatorData(to).call(), operatorData);

assert.equal(
web3.utils.fromWei(await hook.methods.balanceOf(from).call()),
balance_from
);

assert.equal(
web3.utils.fromWei(await hook.methods.balanceOf(to).call()),
balance_to
);
},

async assertHookNotCalled(hook, to) {
assert.strictEqual(await hook.methods.token(to).call(), zeroAddress);
assert.strictEqual(await hook.methods.operator(to).call(), zeroAddress);
assert.strictEqual(await hook.methods.from(to).call(), zeroAddress);
assert.strictEqual(await hook.methods.to(to).call(), zeroAddress);
assert.strictEqual(await hook.methods.holderData(to).call(), null);
assert.strictEqual(await hook.methods.operatorData(to).call(), null);
},
};
2 changes: 1 addition & 1 deletion test/utils/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ exports.test = function(web3, accounts, token) {
await utils.assertBalance(web3, token, accounts[2], 10);

await token.contract.methods
.send('0x0000000000000000000000000000000000000000',
.send(utils.zeroAddress,
web3.utils.toWei('1'), '0x')
.send({ gas: 300000, from: accounts[1] })
.should.be.rejectedWith('revert');
Expand Down
Loading

0 comments on commit c9fae11

Please sign in to comment.