I still didnt figure out the problem. My current guess is that sth is wrong with my javascript code…
I have created the following minimal test:
const People = artifacts.require("People");
const truffleAssert = require("truffle-assertions");
contract("People", async function(accounts){
let instance;
before(async function(){
instance = await People.deployed();
});
it("souldnt create if not payed appropriately (-1Wei)", async function(){
await truffleAssert.fails(instance.createPerson("Bob",60,180,{value:(10**18-1)}),truffleAssert.ErrorType.REVERT);
});
it("souldnt create if not payed appropriately (-10Wei)", async function(){
await truffleAssert.fails(instance.createPerson("Bob",60,180,{value:(10**18-10)}),truffleAssert.ErrorType.REVERT);
});
it("souldnt create if not payed appropriately (-100Wei)", async function(){
await truffleAssert.fails(instance.createPerson("Bob",60,180,{value:(10**18-100)}),truffleAssert.ErrorType.REVERT);
});
});
As mentioned in my earlier post the first and second call create a Person though they shouldnt.
Thx already for your ideas!
Edit: just after writing this reply I may have found the solution myself. Apparently JS is rounding large numbers (i guess i would have known that if i had taken the JS course first…). I found the following BlogPost which explains it:
Can you please check what am I doing wrong in the code below:
it("should have equal number between balance variable and the balance of the contract", async function(){
let instance = await People.new();
console.log(await web3.eth.getBalance("0x4f8eE19Da2ff65FC99e4f8BFCDDFB4cEDB6B8734"));
await instance.createPerson("Ros", 25, 172, {from: accounts[1], value: web3.utils.toWei("3", "ether")});
let bal = await instance.balanace;
assert(bal === await (web3.eth.getBalance("0x4f8eE19Da2ff65FC99e4f8BFCDDFB4cEDB6B8734"))
, "Balance of the contract is not equal" );
console.log(await web3.eth.getBalance("0x4f8eE19Da2ff65FC99e4f8BFCDDFB4cEDB6B8734"));
});
I could have not used theOwner and nonOwner but i understand it better this way.
it("delete can not be performed by nonOwner", async function(){
let instance = await People.deployed();
let nonOwner = accounts[1];
await instance.createPerson("Bob", 65, 190, {value: web3.utils.toWei("1", "ether")});
await truffleAssert.fails(instance.deletePerson(nonOwner, {from: nonOwner}), truffleAssert.ErrorType.REVERT);
});
it("delete can be performed by theOwner", async function(){
let instance = await People.deployed();
let theOwner = accounts[0];
let nonOwner = accounts[1];
await instance.createPerson("Hank", 45, 183, {from: nonOwner, value: web3.utils.toWei("1", "ether")});
await truffleAssert.passes(instance.deletePerson(nonOwner, {from: theOwner}), truffleAssert.ErrorType.REVERT);
});
it("should have equal number between balance variable and the balance of the contract", async function(){
let instance = await People.new();
console.log(await web3.eth.getBalance(instance.address));
await instance.createPerson({from: accounts[1], value: web3.utils.toWei("3", "ether")});
let bal = await web3.eth.getBalance(instance.address);
assert(bal == 3 * 10**18);
console.log(await web3.eth.getBalance(instance.address));
});
I have replaced your contract address with instance.address.
let bal = await web3.eth.getBalance(instance.address);
assert(bal == 3 * 10**18);
After depositing 3 ether by calling createPerson() we do once again check the contract balance and we assign the result to bal.
Then we assert that bal is equal to 3 eth (3 * 10**18);
Thanks for the correction.
Question about assert function. I see that you have used only 2 equal signs and 2 multiplication signs. Can you explain why there are not 3 equal sings and 1 multiplication sign?
it("should have equal number between balance variable and the balance of the contract", async function(){
let instance1 = await People.new();
console.log(await web3.eth.getBalance(instance1.address));
await instance1.createPerson("Ros", 25, 172, {from: accounts[1], value: web3.utils.toWei("3", "ether")});
let bal = await web3.eth.getBalance(instance1.address);
assert((bal == 3* 10**18)
, "Balance of the contract is not equal to 3" );
console.log(await web3.eth.getBalance(instance1.address));
});
it("should withdraw all ", async function(){
let instance1 = await People.new();
await instance1.createPerson("Ros", 25, 172, {from: accounts[5], value: web3.utils.toWei("3", "ether")});
await truffleAssert.passes(instance1.withdrawAll(), "This method should pass");
let bal = await web3.eth.getBalance(instance1.address);
assert(bal == 0, "Balanace is not 0");
console.log(await web3.eth.getBalance(instance1.address));
});
it("should fail to withdraw all ", async function(){
await instance.createPerson("Ros", 25, 172, {from: accounts[5], value: web3.utils.toWei("3", "ether")});
await truffleAssert.fails(instance.withdrawAll({from: accounts[5]}), truffleAssert.ErrorType.REVERT);
});
Special thanks to @ dani69654 for helping with the first test.
@Taha
I am not sure i understand well :
{from: accounts[0] }
{from: accounts[1] }
{from: accounts[2] }
I get confused when we do things like:
await instance.createPerson(“Lisa”, 35, 160, {from: accounts[2], value: web3.utils.toWei(“1”, “ether”)});
Does this mean we are taking 1 eth from that other account? And would the owner of this be accounts[0]?
Do you have a link that I could read and understand this better?
/*
Value Assignment
1) When a person is added the balance of the contract is increased and match the balance address of the contract
2) The contract Owner can withdraw that balance and the balance became zero in the balance storage
and in the contract address balance, and that the owner balance is increased
*/
const People = artifacts.require("People");
const truffleAssert = require("truffle-assertions");
const AssertionError = require("assertion-error");
async function getBalance(_instance) {
let balance = await _instance.balance()
//console.log("getBalance TEST: ", parseFloat(balance))
return parseFloat(balance)
}
contract("People", accounts => {
let instance;
let contractAddress;
before(async function(){
instance = await People.deployed()
contractAddress = instance.address;
});
const owner = accounts[0];
const user = accounts[1];
//const contractAddress = instance.address;
it("should increase the balance in both storage and contract address when a person is added", async () => {
// Test that balance is increased
const origBalance = await getBalance(instance)
await instance.createPerson("Bob", 20, 190, {value: web3.utils.toWei("1", "ether"), from: user});
const nowBalance = await getBalance(instance)
assert(nowBalance > 0, "Balance increase error!");
//console.log(`BALANCE: orig=${origBalance} now=${nowBalance}`);
assert(origBalance < nowBalance, "Balance was not increased!");
// Test that contract and storage address are the same
let contractBalance = await web3.eth.getBalance(contractAddress);
contractBalance = parseFloat(contractBalance);
//console.log(`BALANCE: contract=${contractBalance} now=${nowBalance}`);
assert(contractBalance === nowBalance, "storage balance and contract balance are not equal!");
});
it("Only owner can withdraw", async () => {
await truffleAssert.fails(instance.withdrawAll({from: user}), truffleAssert.ErrorType.REVERT, null, "User can't withdraw");
await truffleAssert.passes(instance.withdrawAll({from: owner}), truffleAssert.ErrorType.REVERT, null, "Owner was not able to withdraw");
})
it("when the owner withdraw the contract balance, both of storage and contract balance have to be 0, and owner balance increased" , async () => {
instance = await People.new()
const origOwnerBalance = await web3.eth.getBalance(owner)
await instance.createPerson("Bob", 20, 190, {value: web3.utils.toWei("1", "ether"), from: user});
await instance.withdrawAll()
const nowOwnerBalance = await web3.eth.getBalance(owner)
const storageBalance = await getBalance(instance)
let contractBalance = await web3.eth.getBalance(contractAddress);
contractBalance = parseFloat(contractBalance);
assert(storageBalance === 0, "Storage balance is not 0!")
assert(contractBalance === 0, "Contract balance is not 0!")
//console.log(`BALANCE: orig=${origOwnerBalance} now=${nowOwnerBalance}`)
assert(nowOwnerBalance > origOwnerBalance, "Owner balance was not increased!")
})
});
By default the accounts[0] is the one that creates the contract so accounts[0] is the owner.
The owner of the contract People is the only one that can withdraw ether from the contract.
from: accounts[2]
means you are using the ether from the account[2] for creating a person.