Posts

Showing posts with the label #Remix

"Building Security" Solidity Example

Solidity contract to track Building Entries pragma solidity ^0.4.0; contract BuildingSecurity { struct Visitor { string name; string information; string VisitorID; uint8 ID; string fbID; string opencv; address delegate; } //visitor structure for name and information struct TempVisitor{ string name; string id; } Visitor[] EmployeePermanent; //list of permanent entries enum State { ENTRY, EXIT, IN, NON } //building state for visitors struct BuildingDoors { string DoorName; string fullname; string cardnumber; } State public state = State.NON; // initialize on create address buildName; string buildingNameDisplay; mapping(address => TempVisitor) visitors; BuildingDoors[] AllDoors; function BuildingSecurity(uint8 _numEntryLimit, string _buildingDisplay) public { buildName = msg.sender; ...

"ICO Contract" Example (Send Token in Exchange for Ether)

In this post i can send Ether to the Creator Account as a donation from another Account Ive also created a Token Contract where you can s end Tokens to Another Account In this post, i will make use of both concepts with the following assumption: I have a company and i would like to go ICO. Initial coin offering  ( ICO ) is an unregulated and controversial means of crowdfunding via use of cryptocurrency, which can be a source of capital for startup  companies . So if someone or a company wants to help me setup my company and get some Initial Coins. My initial Coins are Tokens and i accept Ether in exchange for the Token. So the process goes as follows. 1. I Deploy my TOKEN contract with an initial of 1000 2. Another Account will send me Ether and i will send back some Coins. 3. My Exchange Rate is 1 to 1. So for Every Ether, i will send 1 Token back to the person who gave me Ether. In the Code below, i am giving 7 Tokens for any amount pragma solidity ^0....

Blockchain Business Example "Smart Property Contract"

This contract is for tangible and intangible property such as Cars, Houses or Patents. Can be used to record and verify property rights. First,  payable  is a modifier that can be added to a function. It's impossible to have  payable()  as a function name as it is a reserved keyword. You may use  payable  only in addition to existing functions  Second,  Payable  allows a function to receive ether while being called as stated in  docs .It's manadatory from solidity 0.4.x. If you try to send ether using  call pragma solidity ^0.4.18; contract LandAresh { address public owner; uint public defaultCost; uint public mainLandCost; mapping(uint => mapping(uint => address))public properties; mapping(uint => mapping(uint => address))public landContent; mapping(uint => mapping(uint => bool))public status; mapping(uint => mapping(uint => uint))public prices; funct...

Dear Diary Example "Blog Contract"

In this post, i will be creating a Blogging Contract meant for Diary Entries and posts of articles or News. Notice the use of "block.number" in this post as a way to see new blocks as they come in or get changed contract BlogAresh { address public owner; blog[] blogLogs; struct blog { string title; string headline; string content; string url; address owner; uint blocknumber; } uint counter; function BlogAresh() public { owner = msg.sender; counter = 1; blogLogs.push(blog("BlogAresh:", "My New Blog", "1.0","ethereumacademy.blogspot.com", owner, block.number)); } //anyone can add a post to the blog (multiple writers) function addPost(string title, string headline, string content, string link) public { blogLogs.push(blog(title, headline, content, link,msg.sender, block.number)); counter += 1; } ...

Blockchain Identity Example "Passport Contract"

So the basics The 1 MB size limit per Block is for the Bitcoin's blockchain. In Ethereum, there is theoretically no limit for the block size. However, blockchain is not meant for data storage and storing large documents will be very expensive. pragma solidity ^0.4.0; contract passportAresh{ //Declare a struct similar to C++ struct Passport{ string givenName; string middleName; string surname; string passportType; string countryCode; string passportNumber; string dateBirth; string nationality; string gender; string placeBirth; string dateIssue; string dateExpire; } address creator; mapping (bytes32 => Passport) AllCountryPassport; //modifier modifier onlyBy(address _account) { require(msg.sender == _account); // Do not forget the "_;"! It will // be replaced by the actual function // body when the m...

Solidity "Events" watcher with javascript

Creating Decentralized Apps Part 2

In this post i will be using javascript to call a contract deployed using Remix IDE and ethereum-js Test RPC(nodeJs) The contract being deployed is pragma solidity ^0.4.0; contract NumberGetNextNumberAresh{ uint index; function getNumber() returns (uint) { index = index + 1; return index; } function myConstantMethod(string a) constant returns (string d) { return a; } function myConstantNumber() constant returns (uint) { return index; } } To get the ABI or  "Interface ABI"   Follow this POST for the "Contract Address" Follow this POST After Deploying you should now be ready to create a UI (interface with html) The html will contain javascript as well <html> <head> <title>Smart Contract Ethereum</title> </head> <body> <fieldset> <button onclick="send()">Send Data</button> <button onclick="...

"Highscore Contract" Example

Keep a highscore of a certain game with the assumption that the game name is unique pragma solidity ^0.4.0; contract GameHighScoreAresh{ struct GameHighScore{ string username;//the name of the user who got the highscore (unique) uint score; //the score acquired string dateOfScore; //date of highscore string game; //name of the game uint8 flag; } struct gameName { string name; uint highscore; string username; bool isData; } mapping (bytes32 => gameName) ListOfGames; mapping (bytes32 => GameHighScore) TableOfHighScore; function GameHighScoreAresh() public{ } function AddUserHighScore(string _username, uint _score, string _game) public { GameHighScore memory currentHighScore = GameHighScore(_username, _score, "_dateofScore", _game, 1); bytes32 hash = keccak256(_username, _score, "_dateofScore", _game, 1); ...

"User Contract" Example

Image
In this example, i will be using a persons detail and creating a signature (hash value) from it. the hash value will be unique to each user given the assumption that there are no 2 people with the same details except TWINS. Things learned: The Ethereum Virtual Machine has three areas where it can store items.  “memory”, this is used to hold temporary values. It is erased between (external) function calls and is cheaper to use. pragma solidity ^0.4.0; contract UserAresh{ //Declare a struct similar to C++ struct Info{ string firstname; string lastname; string maiden; bytes32 birthday; } //Events event Log(string firstname, string lastname); //Create a mapping array mapping(bytes32 => Info) UserList; //whos the Creator. The Value is the Hex value of the account that consumes GAS/ETHER address creator; //this is a constructor, notice it has the same name as the Contract name functi...

"To Do List Contract" Example Version 2

Image
In this example im making use of a mapping of bytes. This works similar to creating an array. A mapping is used to structure value types, such as booleans, integers, addresses, and structs. It consists of two main parts: a  _KeyType  and a  _ValueType ; they appear in the following syntax: mapping (_KeyType => _ValueType) mapName pragma solidity ^0.4.0; contract ToDoAreshFlexible{ //Declare a struct similar to C++ holding details of a single ToDo Item struct Item{ string task; uint priority; } //Create a mapping array mapping(bytes32 => Item) ToDoList; //this is a constructor, notice it has the same name as the Contract name function ToDoAreshFlexible() public { } //Adds an item to the list using PUSH function AddToDo(string _task, uint _priority) public { //sha3" has been deprecated in favour of "keccak256" bytes32 hash = keccak256(_task); ToDoList[hash] = I...

"Phone Book Contract" Example Version 4

Image
In my previous post, any account could add and delete from the contract. so in this post i will be using "Modifiers" to check that only the creator of the contract can add and remove data but any one can view the phonebook Modifiers can be used to change the body of a function. Modifiers  let you wrap additional functionality to a method, so they're kind of like the decorator pattern in OOP. 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; ...

"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. 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 Pho...

"Phone Book Contract" Example Version 2

In this version of the phone book, i am making use of a function to verify if the name exists before adding it to the array what ive learned from this is: 1. Throw vs Revert 2. View vs Pure pragma solidity ^0.4.0; contract PhoneBookAresh{ //Declare a struct similar to C++ struct Person{ string fullName; 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(_f...

"Phone Book Contract" Example

In this example ive learned the following concepts. The use of a loop The use of an if conditional statement The Creator of the contract is the same as the account holding Ether String comparison requires utility functions. You can also use the "import" keyword to import external solidity contracts. but i kept getting errors on Remix. Basic usage for "import" import "github.com/Arachnid/solidity-stringutils/strings.sol"; pragma solidity ^0.4.0; contract PhoneBookAresh{ //Declare a struct similar to C++ struct Person{ string fullName; 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; } ...

"To Do List Contract" With Remix

Image
For this Post, i am using Remix to see the result of the contract. pragma solidity ^0.4.0; contract ToDoAreshFlexible{ //Declare a struct similar to C++ holding details of a single ToDo Item struct Item{ string task; uint priority; } //An Array of structs similar to C++ (flexible storage) and index start at 0 Item[] ToDoList; //this is a constructor, notice it has the same name as the Contract name function ToDoAreshFlexible() public { } //Adds an item to the list using PUSH function AddToDo(string _task, uint _priority) public { ToDoList.push(Item(_task,_priority)); } //returns a single item given an index since it requires more gas to iterate thru //returning multiple Items, both task and priority with a single call function getToDoList(uint _index) public returns(string, uint ){ return (ToDoList[_index].task, ToDoList[_index].priority); } function GetTa...

Returning Multiple Values for the "To Do List Contract" Example

In this post i learned how to add items to a fixed array . on this post i extended this contract by making use of a flexible array size . in both posts, i was only returning the task and no the priority. In this post i will be returning multiple values pragma solidity ^0.4.0; contract ToDoAreshFlexible{ //Declare a struct similar to C++ holding details of a single ToDo Item struct Item{ string task; uint priority; } //An Array of structs similar to C++ (flexible storage) and index start at 0 Item[] ToDoList; //this is a constructor, notice it has the same name as the Contract name function ToDoAreshFlexible() public { } //Adds an item to the list using PUSH function AddToDo(string _task, uint _priority) public { ToDoList.push(Item(_task,_priority)); } //returns a single item given an index since it requires more gas to iterate thru //returning multiple Items, both task and priorit...

Non fixed Number of Items for a "To Do List Contract" Example

In this example i will be using an array in storage compared to the Previous Post of a Fixed Number of items in the Array pragma solidity ^0.4.0; contract ToDoAreshFlexible{ //Declare a struct similar to C++ holding details of a single ToDo Item struct Item{ string task; uint priority; } //An Array of structs similar to C++ (flexible storage) and index start at 0 Item[] ToDoList; //this is a constructor, notice it has the same name as the Contract name function ToDoAreshFlexible() public { } //Adds an item to the list using PUSH function AddToDo(string _task, uint _priority) public { ToDoList.push(Item(_task,_priority)); } //returns a single item given an index since it requires more gas to iterate thru function getToDoList(uint _index) public returns(string ){ return ToDoList[_index].task; } }

The Language of Ethereum Smart Contracts

This Post will be updated Constantly as i progress SOURCE: https://solidity.readthedocs.io/en/develop/ The language to create Ethereum Smart Contracts is "Solidity" There are many integrations for solidity, i will be using Remix mostly as it does not require any additional installations and can run on the latest Chrome Browser Available Solidity Integrations Remix Browser-based IDE with integrated compiler and Solidity runtime environment without server-side components. Geth  For running Ethereum nodes, i will be using Geth https://github.com/ethereum/go-ethereum/wiki/geth as the command line interface. Some advantages with geth, is it allows interaction between the smart contract and creation of a private block-chain network for testing. Capabilities By installing and running  geth , you can take part in the ethereum frontier live network and mine real ether transfer funds between addresses create contracts and send transactions explore...