I made inequality allowances for gas charges on passing ETH around, I should look into how that’s done. I figured the values wouldn’t be exact due to gas friction, which I’ve no idea how to pre or post calculate yet. But upon playing with it I see the transferred values are exact. Seems Truffle is adding in the transaction fee without modifying the sent values. And that makes sense too in its own way.
Also had to keep account balances in mind when re-using accounts to create 2nd and 3rd Persons in the same test run. I wouldn’t expect them to reset. Its clear that they don’t in Ganache (which is French so in English it’s pronounced “gan-nahsh”, silent e.) between test cases, but do between test suite runs.
Mine seems to run and completely pass with simpler code (less currency conversions) than I see in your solution, so I am suspicious of some of the comparisons. I’ll dive deeper into that later.
it("should increase balance when a person is added by owner.", async function() {
await instance.createPerson("Narcasist", 29, 160, {from: accounts[0], value: web3.utils.toWei("1", "ether")});
await truffleAssert.passes(web3.eth.getBalance(instance.address) >= web3.utils.toWei("0.5", "ether"));
});
it("should decrease owner's balance when a person is added by owner.", async function() {
await instance.createPerson("Narcasist", 29, 160, {from: accounts[0], value: web3.utils.toWei("1", "ether")});
await truffleAssert.passes(web3.eth.getBalance(accounts[0]) < web3.utils.toWei("98", "ether"));
});
it("should increase balance when a person is added by a non-owner.", async function() {
await instance.createPerson("Shawn", 29, 160, {from: accounts[1], value: web3.utils.toWei("1", "ether")});
await truffleAssert.passes(web3.eth.getBalance(instance.address) >= web3.utils.toWei("1", "ether"));
});
it("should decrease originating balance when a person is added by a non-owner.", async function() {
await instance.createPerson("Sheep", 29, 160, {from: accounts[1], value: web3.utils.toWei("1", "ether")});
await truffleAssert.passes(web3.eth.getBalance(accounts[1]) <= web3.utils.toWei("98", "ether"));
});
it("internal balance variable should match on-chain contract balance.", async function() {
await instance.createPerson("Smith", 29, 160, {from: accounts[1], value: web3.utils.toWei("1", "ether")});
let balance = await instance.balance();
await truffleAssert.passes(web3.eth.getBalance(instance.address) == balance);
});
it("should increase balance by 1 Eth when a person is added.", async function() {
await truffleAssert.passes(web3.eth.getBalance(instance.address) == web3.utils.toWei("0", "ether"));
await instance.createPerson("Jones", 29, 160, {from: accounts[1], value: web3.utils.toWei("1", "ether")});
await truffleAssert.passes(web3.eth.getBalance(instance.address) == web3.utils.toWei("1", "ether"));
});
it("should increase balance by 2 Eth when two people are added.", async function() {
await truffleAssert.passes(web3.eth.getBalance(instance.address) == web3.utils.toWei("0", "ether"));
await instance.createPerson("Jones", 29, 160, {from: accounts[1], value: web3.utils.toWei("1", "ether")});
await truffleAssert.passes(web3.eth.getBalance(instance.address) == web3.utils.toWei("1", "ether"));
await instance.createPerson("JonesToo", 29, 160, {from: accounts[2], value: web3.utils.toWei("1", "ether")});
await truffleAssert.passes(web3.eth.getBalance(instance.address) == web3.utils.toWei("2", "ether"));
});
it("should allow contract owner to withdraw all of its balance.", async function() {
await instance.createPerson("Jones", 29, 160, {from: accounts[1], value: web3.utils.toWei("1", "ether")});
await instance.createPerson("JonesToo", 29, 160, {from: accounts[2], value: web3.utils.toWei("1", "ether")});
await instance.createPerson("OtherJones", 29, 160, {from: accounts[3], value: web3.utils.toWei("1", "ether")});
await instance.createPerson("JJonesAnon", 29, 160, {from: accounts[4], value: web3.utils.toWei("1", "ether")});
await truffleAssert.passes(instance.withdrawAll({from: accounts[0]}));
});
it("should increase contract owner's balance by same amount on full withdraw.", async function() {
await instance.createPerson("Jones", 29, 160, {from: accounts[1], value: web3.utils.toWei("1", "ether")});
await instance.createPerson("JonesToo", 29, 160, {from: accounts[2], value: web3.utils.toWei("1", "ether")});
await instance.createPerson("OtherJones", 29, 160, {from: accounts[3], value: web3.utils.toWei("1", "ether")});
await instance.createPerson("JJonesAnon", 29, 160, {from: accounts[4], value: web3.utils.toWei("1", "ether")});
await instance.withdrawAll({from: accounts[0]});
await truffleAssert.passes(web3.eth.getBalance(accounts[0]) >= web3.utils.toWei("103", "ether"));
});
it("should not allow non-contract owners to withdraw all of its balance.", async function() {
await instance.createPerson("Thief", 29, 160, {from: accounts[1], value: web3.utils.toWei("1", "ether")});
await instance.createPerson("Ericson", 29, 160, {from: accounts[2], value: web3.utils.toWei("1", "ether")});
await truffleAssert.fails(instance.withdrawAll({from: accounts[1]}));
await truffleAssert.fails(instance.withdrawAll({from: accounts[2]}));
await truffleAssert.fails(instance.withdrawAll({from: accounts[3]}));
});