|
| 1 | +const helpers = require('./helpers'); |
| 2 | + |
| 3 | +const CommonToken = artifacts.require("./CommonToken.sol"); |
| 4 | +var token; |
| 5 | +contract('CommonToken', accounts => { |
| 6 | + const testTokenName = "CommonToken"; |
| 7 | + const testTokenSymbol = "CMN"; |
| 8 | + beforeEach( async function() { |
| 9 | + token = await CommonToken.new(); |
| 10 | + await token.initialize(testTokenName,testTokenSymbol,18,[accounts[0]],[accounts[0]]); |
| 11 | + }); |
| 12 | + it("should put 0 Coins in the first account", async () => { |
| 13 | + |
| 14 | + let balance = await token.balanceOf.call(accounts[0]); |
| 15 | + assert.equal(balance.valueOf(), 0); |
| 16 | + }); |
| 17 | + |
| 18 | + it("should be owned by its creator", async () => { |
| 19 | + |
| 20 | + assert.equal(await token.isMinter(accounts[0]), true); |
| 21 | + }); |
| 22 | + |
| 23 | + it("should mint tokens to owner account", async () => { |
| 24 | + await helpers.etherForEveryone(accounts); |
| 25 | + |
| 26 | + let owner, totalSupply, userSupply; |
| 27 | + |
| 28 | + totalSupply = await token.totalSupply(); |
| 29 | + owner = accounts[0]; |
| 30 | + userSupply = await token.balanceOf(owner); |
| 31 | + assert.equal(totalSupply, 0); |
| 32 | + assert.equal(userSupply, 0); |
| 33 | + |
| 34 | + await token.mint(owner, 1000); |
| 35 | + totalSupply = await token.totalSupply(); |
| 36 | + userSupply = await token.balanceOf(owner); |
| 37 | + assert.equal(totalSupply, 1000); |
| 38 | + assert.equal(userSupply, 1000); |
| 39 | + |
| 40 | + await token.mint(accounts[2], 1300); |
| 41 | + totalSupply = await token.totalSupply(); |
| 42 | + userSupply = await token.balanceOf(accounts[2]); |
| 43 | + assert.equal(totalSupply, 2300); |
| 44 | + assert.equal(userSupply, 1300); |
| 45 | + |
| 46 | + }); |
| 47 | + |
| 48 | + it("should allow minting tokens only by owner", async () => { |
| 49 | + await helpers.etherForEveryone(accounts); |
| 50 | + |
| 51 | + let owner = accounts[0]; |
| 52 | + let totalSupply = await token.totalSupply(); |
| 53 | + |
| 54 | + // calling 'mint' as a non-owner throws an error |
| 55 | + try { |
| 56 | + await token.mint(owner, 1000, { 'from': accounts[1] }); |
| 57 | + throw 'an error'; |
| 58 | + } catch (error) { |
| 59 | + helpers.assertVMException(error); |
| 60 | + } |
| 61 | + |
| 62 | + // and so the supply of tokens should remain unchanged |
| 63 | + let newSupply = await token.totalSupply(); |
| 64 | + assert.equal(totalSupply.toNumber(), newSupply.toNumber()); |
| 65 | + }); |
| 66 | + |
| 67 | + it("log the Transfer event on mint", async () => { |
| 68 | + |
| 69 | + |
| 70 | + const tx = await token.mint(accounts[1], 1000, { from: accounts[0] }); |
| 71 | + |
| 72 | + assert.equal(tx.logs.length, 1); |
| 73 | + assert.equal(tx.logs[0].event, "Transfer"); |
| 74 | + assert.equal(tx.logs[0].args.to, accounts[1]); |
| 75 | + assert.equal(tx.logs[0].args.value.toNumber(), 1000); |
| 76 | + }); |
| 77 | + |
| 78 | + it("mint should be reflected in totalSupply", async () => { |
| 79 | + |
| 80 | + |
| 81 | + await token.mint(accounts[1], 1000, { from: accounts[0] }); |
| 82 | + let amount = await token.totalSupply(); |
| 83 | + |
| 84 | + assert.equal(amount, 1000); |
| 85 | + |
| 86 | + await token.mint(accounts[2], 500, { from: accounts[0] }); |
| 87 | + amount = await token.totalSupply(); |
| 88 | + |
| 89 | + assert.equal(amount.toNumber(), 1500); |
| 90 | + }); |
| 91 | + |
| 92 | + it("mint should be reflected in balances", async () => { |
| 93 | + |
| 94 | + |
| 95 | + await token.mint(accounts[1], 1000, { from: accounts[0] }); |
| 96 | + |
| 97 | + const amount = await token.balanceOf(accounts[1]); |
| 98 | + |
| 99 | + assert.equal(amount.toNumber(), 1000); |
| 100 | + }); |
| 101 | + |
| 102 | + it("totalSupply is 0 on init", async () => { |
| 103 | + |
| 104 | + |
| 105 | + const totalSupply = await token.totalSupply(); |
| 106 | + |
| 107 | + assert.equal(totalSupply.toNumber(), 0); |
| 108 | + }); |
| 109 | + |
| 110 | + it("burn", async () => { |
| 111 | + |
| 112 | + await token.mint(accounts[1], 1000, { from: accounts[0] }); |
| 113 | + |
| 114 | + var amount = await token.balanceOf(accounts[1]); |
| 115 | + |
| 116 | + assert.equal(amount.toNumber(), 1000); |
| 117 | + |
| 118 | + await token.burn(accounts[1] ,100); |
| 119 | + |
| 120 | + amount = await token.balanceOf(accounts[1]); |
| 121 | + |
| 122 | + assert.equal(amount.toNumber(), 900); |
| 123 | + |
| 124 | + const totalSupply = await token.totalSupply(); |
| 125 | + |
| 126 | + assert.equal(totalSupply.toNumber(), 900); |
| 127 | + }); |
| 128 | + |
| 129 | + |
| 130 | + describe('onlyOwner', () => { |
| 131 | + it('mint by owner', async () => { |
| 132 | + try { |
| 133 | + await token.mint(accounts[1], 10, { from: accounts[0] }); |
| 134 | + } catch (ex) { |
| 135 | + assert(false, 'owner could not mint'); |
| 136 | + } |
| 137 | + }); |
| 138 | + |
| 139 | + it('mint by not owner', async () => { |
| 140 | + |
| 141 | + try { |
| 142 | + await token.mint(accounts[1], 10, { from: accounts[1] }); |
| 143 | + } catch (ex) { |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + assert(false, 'non-owner was able to mint'); |
| 148 | + }); |
| 149 | + |
| 150 | + it('burn by owner', async () => { |
| 151 | + await token.mint(accounts[1], 10); |
| 152 | + try { |
| 153 | + await token.burn(accounts[1], 10, { from: accounts[0] }); |
| 154 | + } catch (ex) { |
| 155 | + assert(false, 'owner could not mint'); |
| 156 | + } |
| 157 | + }); |
| 158 | + |
| 159 | + it('burn by not owner', async () => { |
| 160 | + await token.mint(accounts[1], 10, { from: accounts[0] }); |
| 161 | + try { |
| 162 | + await token.burn(accounts[1], 10, { from: accounts[1] }); |
| 163 | + } catch (ex) { |
| 164 | + return; |
| 165 | + } |
| 166 | + |
| 167 | + assert(false, 'non-owner was able to mint'); |
| 168 | + }); |
| 169 | + }); |
| 170 | +}); |
0 commit comments