Here’s my code and image of my transaction. The transaction gets stopped even though I put more than 1 ether in the value setting. I notice the account balance is still less than 100 so the gas transaction still goes through.
pragma solidity 0.5.12;
contract HelloWorld{
string public message = “Hello World”;
uint[] public numbers = [1,20,45];
// storage: eveything that's saved permanetly like the owner/mapping - lifetime of contract
//memory: only saved during function execution. such as-- string memory name. will be deleted after function done
//stack store local memory of value types <256 bits - int256, bool . deleted w/ function is executed
struct Person {
uint id;
string name;
uint age;
uint height;
bool senior;
} // definition of person --> object
address public owner; //avail for lifetime of contract
uint public balance;
event personCreated(string name, bool senior); //data any interested party may need -> can send event info
event personDeleteded(string name, bool senior, address deletedBy);
modifier onlyOwner(){
require(msg.sender == owner,“only the owner can do this!”);
_;
} // continue execution. have a piece of code that can be used by lots of functions
modifier costs(uint cost){
require(msg.value > cost);
_;
}
constructor() public{
owner = msg.sender; //person that initiated contract
}
mapping(address => Person) private people; //mapping better than array - dictionary/key pair
address[] private creators; //only want owner to access address
//Person[] public people;//array of people have some place to save new people public makes getter function to query array
function createPerson(string memory name, uint age, uint height) public payable costs(1 ether){
// address creator = msg.sender; // create separte function. don't want the user to add address in function - mistake instead use address of person creating contract
require(age <= 150, "Age need to below 150");
balance += msg.value;
// creates a Person
Person memory newPerson;
//newPerson.id = people.length; can't get length in mapping
newPerson.name = name;
newPerson.age = age;
newPerson.height = height;
if(age >= 65){
newPerson.senior = true;
}
else{
newPerson.senior = false;
}
// people.push(newPerson);
// people.push(Person(people.length,name,age,height)); another way to create people
insertPerson(newPerson);
creators.push(msg.sender);
// people[msg.sender] == newPerson, assert should always be true and never fire
assert(
keccak256
(abi.encodePacked(
people[msg.sender].name,
people[msg.sender].age,
people[msg.sender].height,
people[msg.sender].senior
)
)
== keccak256
(abi.encodePacked(
newPerson.name,
newPerson.age,
newPerson.height,
newPerson.senior
)
)
); // cant just compare people have to hash then compare. same input of hash ->same o/p
emit personCreated(newPerson.name,newPerson.senior); // send data to event
}
function insertPerson(Person memory newPerson) private {
address creator = msg.sender;
people[creator]=newPerson;
}
function getPerson() public view returns (string memory name, uint age, uint height, bool senior){
address creator = msg.sender;
return (people[creator].name,people[creator].age, people[creator].height,people[creator].senior);
}
function deletePerson(address creator) public onlyOwner{
string memory name = people[creator].name;// got to store in memory before delete then call event handler
bool senior = people[creator].senior;
delete people[creator]; //delete value of key ,person in any mapping- limit by the admin =>address that deployed contract
assert(people[creator].age == 0);
emit personDeleteded(name,senior,msg.sender);
}
function getCreator(uint index) public view onlyOwner returns (address){
return creators[index];
}
}