From 17884e91a1cf9b3f243fe6e3fcd7141f700cec6e Mon Sep 17 00:00:00 2001 From: Facundo Spagnuolo Date: Tue, 9 Jan 2018 12:25:50 -0300 Subject: [PATCH] Emit Trasnfer event on SimpleToken creation --- contracts/examples/SimpleToken.sol | 1 + test/SimpleToken.test.js | 41 ++++++++++++++++++++++++++++++ test/helpers/decodeLogs.js | 8 ++++++ 3 files changed, 50 insertions(+) create mode 100644 test/SimpleToken.test.js create mode 100644 test/helpers/decodeLogs.js diff --git a/contracts/examples/SimpleToken.sol b/contracts/examples/SimpleToken.sol index 51edf0873..f7cf62413 100644 --- a/contracts/examples/SimpleToken.sol +++ b/contracts/examples/SimpleToken.sol @@ -24,6 +24,7 @@ contract SimpleToken is StandardToken { function SimpleToken() public { totalSupply = INITIAL_SUPPLY; balances[msg.sender] = INITIAL_SUPPLY; + Transfer(0x0, msg.sender, INITIAL_SUPPLY); } } diff --git a/test/SimpleToken.test.js b/test/SimpleToken.test.js new file mode 100644 index 000000000..9a1870f20 --- /dev/null +++ b/test/SimpleToken.test.js @@ -0,0 +1,41 @@ +import decodeLogs from './helpers/decodeLogs'; +const SimpleToken = artifacts.require('examples/SimpleToken.sol'); + +contract('SimpleToken', accounts => { + let token; + const creator = accounts[0]; + + beforeEach(async function () { + token = await SimpleToken.new({ from: creator }); + }); + + it('has a name', async function () { + const name = await token.name(); + assert.equal(name, 'SimpleToken'); + }); + + it('has a symbol', async function () { + const symbol = await token.symbol(); + assert.equal(symbol, 'SIM'); + }); + + it('has 18 decimals', async function () { + const decimals = await token.decimals(); + assert(decimals.eq(18)); + }); + + it('assigns the initial total supply to the creator', async function () { + const totalSupply = await token.totalSupply(); + const creatorBalance = await token.balanceOf(creator); + + assert(creatorBalance.eq(totalSupply)); + + const receipt = web3.eth.getTransactionReceipt(token.transactionHash); + const logs = decodeLogs(receipt.logs, SimpleToken, token.address); + assert.equal(logs.length, 1); + assert.equal(logs[0].event, 'Transfer'); + assert.equal(logs[0].args.from.valueOf(), 0x0); + assert.equal(logs[0].args.to.valueOf(), creator); + assert(logs[0].args.value.eq(totalSupply)); + }); +}); diff --git a/test/helpers/decodeLogs.js b/test/helpers/decodeLogs.js new file mode 100644 index 000000000..c990eedcb --- /dev/null +++ b/test/helpers/decodeLogs.js @@ -0,0 +1,8 @@ +const SolidityEvent = require('web3/lib/web3/event.js'); + +export default function decodeLogs (logs, contract, address) { + return logs.map(log => { + const event = new SolidityEvent(null, contract.events[log.topics[0]], address); + return event.decode(log); + }); +}