"Phone Book Contract" Example Version 3
In this version im making use of the following
1. Delete an item using an index from the array
2. Log the action using an "Event"
Events are dispatched signals the smart contracts can fire. DApps, or anything connected to Ethereum JSON-RPC API, can listen to these events and act accordingly. Event can be indexed, so that the event history is searchable later.
1. Delete an item using an index from the array
2. Log the action using an "Event"
Events are dispatched signals the smart contracts can fire. DApps, or anything connected to Ethereum JSON-RPC API, can listen to these events and act accordingly. Event can be indexed, so that the event history is searchable later.
pragma solidity ^0.4.0;
contract PhoneBookAresh{
//Declare a struct similar to C++
struct Person{
string fullName;
string number;
}
//Events
event Log(string name, string number);
//An Array of structs similar to C++ (flexible storage) and index start at 0
Person[] MyPhoneBook;
//whos phonebook it is. The Value is the Hex value of the account
address creator;
//this is a constructor, notice it has the same name as the Contract name
function PhoneBookAresh() public {
creator = msg.sender;
}
//Adds an item to the Phonebook using PUSH
function AddPhone(string _fullName, string _number) public
{
//if name doesnt exist. still uses GAS to go thru the loop
if (CheckName(_fullName) == false)
{
MyPhoneBook.push(Person(_fullName,_number));
Log (_fullName,_number);
}
else
{
//revert; will return unused gas whereas throw will continue to consume all available gas.
revert();
}
}
//used to remove or set the value to empty/zero
function RemovePerson(uint _index) public{
Log (MyPhoneBook[_index].fullName,MyPhoneBook[_index].number);
delete MyPhoneBook[_index];
}
function GetPerson(uint _index) public view returns (string)
{
return MyPhoneBook[_index].fullName;
}
function CheckName(string _fullName) public view returns (bool)
{
uint PhoneBookLength = MyPhoneBook.length;
//Must be uint and not int
for (uint i=0; i< PhoneBookLength; i++)
{
//As in Java, the == operator does not compare the literals of two strings.
if (stringsEqual(MyPhoneBook[i].fullName,_fullName))
{
return true;
}
}
return false;
}
function GetPhoneNumber(string _fullName) public view returns (string)
{
uint PhoneBookLength = MyPhoneBook.length;
//Must be uint and not int
for (uint i=0; i< PhoneBookLength; i++)
{
//As in Java, the == operator does not compare the literals of two strings.
if (stringsEqual(MyPhoneBook[i].fullName,_fullName))
{
return MyPhoneBook[i].number;
}
}
}
//Manually comparing strings function used as a utility function
//source https://github.com/ethereum/dapp-bin/blob/master/library/stringUtils.sol
//View This is generally the replacement for constant. It indicates that the function will not alter the storage state in any way.
//Pure This is even more restrictive, indicating that it won't even read the storage state.
function stringsEqual(string storage _a, string memory _b) internal view returns (bool) {
bytes storage a = bytes(_a);
bytes memory b = bytes(_b);
if (a.length != b.length)
return false;
// @todo unroll this loop
for (uint i = 0; i < a.length; i ++)
if (a[i] != b[i])
return false;
return true;
}
}
Comments
Post a Comment