Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factory tests #132

Open
wants to merge 4 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ npm run compile
## Tests
The repo has a comprehensive test suite. You can run it with `npm run test`.

## ethpm
The contracts in this repo are published under `tokens` on EPM. EPM is the recommended means of consuming token contracts in this repo. Copy-pasting code is highly discouraged.
## Ethereum Package Management (ethpm)
The contracts in this repo are published under `tokens` on [EPM](https://www.ethpm.com/registry/packages). EPM is the recommended means of consuming token contracts in this repo. Copy-pasting code is highly discouraged.

## Contributing
Pull requests are welcome! Please keep standards discussions to the EIP repos.
Expand Down
87 changes: 82 additions & 5 deletions test/eip20/eip20Factory.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,88 @@
const EIP20Factory = artifacts.require('EIP20Factory');
const EIP20Bytecode = artifacts.require('EIP20').bytecode;

contract('EIP20Factory', (accounts) => {
it('Verify a Human Standard Token once deployed using both verification functions.', async () => {
it('Verify an EIP20 token once deployed using both verification functions.', async () => {
const initialAmount = 100000;
const name = 'Simon Bucks';
const decimals = 2;
const symbol = 'SBX';
const args = [initialAmount, name, decimals, symbol, { from: accounts[0] }];

// new instance
const factory = await EIP20Factory.new();

// simulate: factory create a new EIP20 token
const newTokenAddr = await factory.createEIP20.call(...args);

// tx: factory create a new EIP20 token
await factory.createEIP20(...args);

// verify: new token's bytecode === EIP20's bytecode
const result = await factory.verifyEIP20.call(newTokenAddr, { from: accounts[0] });
assert.strictEqual(result, true, 'the bytecode at newTokenAddr '
+ 'was not the same as the bytecode of an EIP20 token');

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for consistency, would add // verify: new token's address is listed in the isEIP20 mapping

// verify: new token's address is listed in the `isEIP20` mapping
const isEIP20 = await factory.isEIP20.call(newTokenAddr);
assert.strictEqual(isEIP20, true, 'is not eip20');
});

it('should verify that the `created` mapping includes a newly created token', async () => {
const initialAmount = 1000000;
const name = 'Maurelian Moolah';
const decimals = 3;
const symbol = 'MOO';
const args = [initialAmount, name, decimals, symbol, { from: accounts[0] }];

// new instance
const factory = await EIP20Factory.new();

// simulate: factory create a new EIP20 token
const newTokenAddr = await factory.createEIP20.call(...args);

// tx: factory create a new EIP20 token
await factory.createEIP20(...args);

// verify: created mapping at index 1 is the newly deployed token
const result = await factory.created.call(accounts[0], 1);
assert.strictEqual(
result,
newTokenAddr,
'the `created` mapping does not include the expected token',
);
});

it('should return true if given a contract address with the same bytecode', async () => {
const factory = await EIP20Factory.new();
const newTokenAddr = await factory.createEIP20.call(100000, 'Simon Bucks', 2, 'SBX', { from: accounts[0] });
await factory.createEIP20(100000, 'Simon Bucks', 2, 'SBX', { from: accounts[0] });
const res = await factory.verifyEIP20.call(newTokenAddr, { from: accounts[0] });
assert(res, 'Could not verify the token.');

// deploy contract using correct eip20 bytecode
const txHash = await web3.eth.sendTransaction({
from: accounts[0],
gas: 4700000,
gasPrice: 100000000000,
data: EIP20Bytecode,
});

const { contractAddress } = await web3.eth.getTransactionReceipt(txHash);
const result = await factory.verifyEIP20.call(contractAddress);
assert.strictEqual(result, true, 'should have returned true because the bytecode was exact');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is failing for me. Here's some output from inserting a debugger statement on the line above, and inspecting the run time:

code == EIP20Bytecode
false
code.length
6262
EIP20Bytecode.length
7076

It looks like the hardcoded bytecode above was generated by a different truffle/solc version than the one listed in package.json here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed this test by changing line 2 to
const EIP20Bytecode = artifacts.require('EIP20').bytecode;

It feels a bit circular, but it translates to: "does the bytecode from compiling EIP20, match the bytecode deployed by the EIP20Factory".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is also broken when rebased onto staging because the factory is using SafeEIP20 there, so this is dependent on that.

});

it('should return false when given a contract address with a modified bytecode', async () => {
const factory = await EIP20Factory.new();

// deploy contract using modified eip20 bytecode
const modifiedBytecode = EIP20Bytecode.slice(0, -10).concat('1234567890');
const txHash = await web3.eth.sendTransaction({
from: accounts[0],
gas: 4700000,
gasPrice: 100000000000,
data: modifiedBytecode,
});

const { contractAddress } = await web3.eth.getTransactionReceipt(txHash);
const result = await factory.verifyEIP20.call(contractAddress);
assert.strictEqual(result, false, 'should have returned false because the bytecode was not exact');
});
});