UNPKG

@daostack/infra-experimental

Version:

Base layer DAO's components

294 lines (213 loc) 11.3 kB
const helpers = require('./helpers'); var Reputation = artifacts.require("./Reputation.sol"); const ReputationTestHelper = artifacts.require("./ReputationTestHelper.sol"); var reputation; contract('Reputation', accounts => { beforeEach( async function() { reputation = await Reputation.new(); await reputation.initialize(accounts[0]); }); it("test setting and getting reputation by the owner", async () => { let value; await reputation.mint(accounts[1], 3131); value = await reputation.balanceOf(accounts[1]); assert.equal(value.valueOf(), 3131); }); it("should be owned by the main account", async () => { let owner = await reputation.owner(); assert.equal(owner, accounts[0]); }); it("check permissions", async () => { await reputation.mint(accounts[1], 1000); // only the owner can call mint try { await reputation.mint(accounts[2], 1000, { from: accounts[2] }); throw 'an error'; // make sure that an error is thrown } catch (error) { helpers.assertVMException(error); } let account0Rep = await reputation.balanceOf(accounts[0]); let account1Rep = await reputation.balanceOf(accounts[1]); let account2Rep = await reputation.balanceOf(accounts[2]); let totalRep = await reputation.totalSupply(); assert.equal(account1Rep, 1000, "account 1 reputation should be 1000"); assert.equal(account2Rep, 0, "account 2 reputation should be 0"); assert.equal(parseInt(totalRep), parseInt(account0Rep) + parseInt(account1Rep), "total reputation should be sum of account0 and account1"); }); it("check total reputation", async () => { await reputation.mint(accounts[0], 2000); await reputation.mint(accounts[1], 1000); await reputation.mint(accounts[1], 500); await reputation.mint(accounts[2], 3000); // this tx should have no effect let account0Rep = await reputation.balanceOf(accounts[0]); let account1Rep = await reputation.balanceOf(accounts[1]); let account2Rep = await reputation.balanceOf(accounts[2]); // assert.equal(account0Rep, 2001, "account 0 reputation should be 2000"); assert.equal(account1Rep.valueOf(), 1500, "account 1 reputation should be 1000 + 500"); assert.equal(account2Rep.valueOf(), 3000, "account 2 reputation should be 3000"); let totalRep = await reputation.totalSupply(); assert.equal(parseInt(totalRep), parseInt(account0Rep) + parseInt(account1Rep) + parseInt(account2Rep), "total reputation should be sum of accounts"); }); it("check total reputation overflow", async () => { let BigNumber = require('bignumber.js'); let bigNum = ((new BigNumber(2)).toPower(128).sub(1)).toString(10); await reputation.mint(accounts[0], bigNum); let totalRepBefore = await reputation.totalSupply(); try { await reputation.mint(accounts[1], 1); throw 'an error'; // make sure that an error is thrown } catch (error) { helpers.assertVMException(error); } let totalRepAfter = await reputation.totalSupply(); assert(totalRepBefore.eq(totalRepAfter),true); }); it("test reducing reputation", async () => { let value; await reputation.mint(accounts[1], 1500); await reputation.burn(accounts[1], 500); value = await reputation.balanceOf(accounts[1]); let totalRepSupply = await reputation.totalSupply(); assert.equal(value.valueOf(), 1000); assert.equal(totalRepSupply.valueOf(), 1000); }); it("totalSupply is 0 on init", async () => { const totalSupply = await reputation.totalSupply(); assert.equal(totalSupply.toNumber(), 0); }); it("log the Mint event on mint", async () => { let tx = await reputation.mint(accounts[1], 1000, { from: accounts[0] }); assert.equal(tx.logs.length, 1); assert.equal(tx.logs[0].event, "Mint"); assert.equal(tx.logs[0].args._to, accounts[1]); assert.equal(tx.logs[0].args._amount.toNumber(), 1000); }); it("log negative Mint event on negative mint", async () => { await reputation.mint(accounts[1], 1000, { from: accounts[0] }); let tx = await reputation.burn(accounts[1], 200, { from: accounts[0] }); assert.equal(tx.logs.length, 1); assert.equal(tx.logs[0].event, "Burn"); assert.equal(tx.logs[0].args._from, accounts[1]); assert.equal(tx.logs[0].args._amount.toNumber(), 200); tx = await reputation.burn(accounts[1], 1000, { from: accounts[0] }); assert.equal(tx.logs.length, 1); assert.equal(tx.logs[0].event, "Burn"); assert.equal(tx.logs[0].args._from, accounts[1]); assert.equal(tx.logs[0].args._amount.toNumber(), 800); }); it("mint (plus) should be reflected in totalSupply", async () => { await reputation.mint(accounts[1], 1000, { from: accounts[0] }); let totalSupply = await reputation.totalSupply(); assert.equal(totalSupply.toNumber(), 1000); await reputation.mint(accounts[2], 500, { from: accounts[0] }); totalSupply = await reputation.totalSupply(); assert.equal(totalSupply.toNumber(), 1500); }); it("mint (plus) should be reflected in balances", async () => { await reputation.mint(accounts[1], 1000, { from: accounts[0] }); const amount = await reputation.balanceOf(accounts[1]); assert.equal(amount.toNumber(), 1000); }); it("mint (minus) should be reflected in totalSupply", async () => { await reputation.mint(accounts[1], 1000, { from: accounts[0] }); let totalSupply = await reputation.totalSupply(); assert.equal(totalSupply.toNumber(), 1000); await reputation.burn(accounts[1], 500, { from: accounts[0] }); totalSupply = await reputation.totalSupply(); assert.equal(totalSupply.toNumber(), 500); await reputation.burn(accounts[1], 700, { from: accounts[0] }); totalSupply = await reputation.totalSupply(); assert.equal(totalSupply.toNumber(), 0); }); it("mint (minus) should be reflected in balances", async () => { await reputation.mint(accounts[1], 1000, { from: accounts[0] }); await reputation.burn(accounts[1], 500, { from: accounts[0] }); let amount = await reputation.balanceOf(accounts[1]); assert.equal(amount.toNumber(), 500); await reputation.burn(accounts[1], 700, { from: accounts[0] }); amount = await reputation.balanceOf(accounts[1]); assert.equal(amount.toNumber(), 0); }); describe('onlyOwner', () => { it('mint by owner', async () => { try { await reputation.mint(accounts[1], 10, { from: accounts[0] }); } catch (error) { assert(false, 'owner could not mint'); } }); it('mint by not owner', async () => { try { await reputation.mint(accounts[1], 10, { from: accounts[1] }); throw 'some exception'; } catch (error) { helpers.assertVMException(error); } }); }); it("account balance cannot be negative", async () => { await reputation.mint(accounts[1], 1, { from: accounts[0] }); let amount = await reputation.balanceOf(accounts[1]); assert.equal(amount.toNumber(), 1); await reputation.burn(accounts[1], 2, { from: accounts[0] }); let rep = await reputation.balanceOf(accounts[1]); assert.equal(rep.toNumber(), 0); }); it("totalSupply cannot be negative", async () => { await reputation.mint(accounts[1], 1, { from: accounts[0] }); let amount = await reputation.totalSupply(); assert.equal(amount.toNumber(), 1); await reputation.burn(accounts[1], 2, { from: accounts[0] }); let rep = await reputation.totalSupply(); assert.equal(rep.toNumber(), 0); }); it("balanceOf = balances", async () => { const rep1 = Math.floor(Math.random() * 1e6); const rep2 = Math.floor(Math.random() * 1e6); const rep3 = Math.floor(Math.random() * 1e6); await reputation.mint(accounts[1], rep1, { from: accounts[0] }); await reputation.mint(accounts[2], rep2, { from: accounts[0] }); await reputation.mint(accounts[3], rep3, { from: accounts[0] }); const balanceOf1 = await reputation.balanceOf(accounts[1]); const balanceOf2 = await reputation.balanceOf(accounts[2]); const balanceOf3 = await reputation.balanceOf(accounts[3]); assert.equal(balanceOf1.toNumber(), rep1); assert.equal(balanceOf2.toNumber(), rep2); assert.equal(balanceOf3.toNumber(), rep3); }); it("reputation at ", async () => { const rep1 = Math.floor(Math.random() * 1e6); await reputation.mint(accounts[1], rep1, { from: accounts[0] }); var tx = await reputation.mint(accounts[1], rep1, { from: accounts[0] }); await reputation.mint(accounts[3], rep1, { from: accounts[0] }); assert.equal (await reputation.totalSupply(),rep1+rep1+rep1); assert.equal (await reputation.totalSupplyAt(tx.receipt.blockNumber),rep1+rep1); assert.equal (await reputation.totalSupplyAt(tx.receipt.blockNumber-1),rep1); assert.equal (await reputation.balanceOfAt(accounts[1],tx.receipt.blockNumber),rep1+rep1); assert.equal (await reputation.balanceOfAt(accounts[1],tx.receipt.blockNumber-1),rep1); assert.equal (await reputation.balanceOfAt(accounts[3],tx.receipt.blockNumber),0); }); it("multible mint at the same block ", async () => { var reputationTestHelper = await ReputationTestHelper.new(reputation.address); await reputation.transferOwnership(reputationTestHelper.address, {from:accounts[0]}); var rep = 10; var times = 3; await reputationTestHelper.multipleMint(accounts[1], rep,times); assert.equal (await reputation.totalSupply(),rep*times); assert.equal (await reputation.balanceOf(accounts[1]),rep*times); await reputationTestHelper.multipleBurn(accounts[1], rep, 2); assert.equal (await reputation.totalSupply(),rep); assert.equal (await reputation.balanceOf(accounts[1]),rep); }); it("balanceOfAt before first mint should be 0 ", async () => { const rep1 = Math.floor(Math.random() * 1e6); var tx = await reputation.mint(accounts[1], rep1, { from: accounts[0] }); assert.equal (await reputation.totalSupply(),rep1); assert.equal (await reputation.totalSupplyAt(tx.receipt.blockNumber),rep1); assert.equal (await reputation.totalSupplyAt(tx.receipt.blockNumber-1),0); assert.equal (await reputation.balanceOf(accounts[1],tx.receipt.blockNumber),rep1); assert.equal (await reputation.balanceOfAt(accounts[1],tx.receipt.blockNumber),rep1); assert.equal (await reputation.balanceOfAt(accounts[1],tx.receipt.blockNumber-1),0); }); });