Posts

Showing posts with the label #solidityByExample

"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 IoT Example "Iris Contract"

In this post i will be creating a contract that works with a device (a device that can acquire your IRIS (EYE) Data and use it for Unlocking or Security. How is it an IoT? Source The  Internet of things  ( IoT ) is the network of physical devices, vehicles, home appliances, and other items  embedded  with  electronics ,  software ,  sensors ,  actuators , and  network connectivity  which enable these objects to connect and exchange  data . There are many Cheap Hardware devices currently in the market that can provide and Iris Code. One of these devices is the Samsung Note 7 that has a built in Iris Scanner for Unlocking Phone as a Secondary Authentication   Source So assuming i have the hardware that can get My Iris Scanned. What will be the output of this Hardware? How do iris Scan work?  identifies around 240 unique features (about five times more "points of comparison" as fingerprint systems use). The...

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" Version 2

So in this example, on top of just information, i want to be saving the Users Picture just like the passport. I should be able to retrieve this picture and be able to view it. Given that there is no specific Datatype for image. i Will be using a "Base64" string of the image. In order to see an example of this Image to String of Base64: Use this https://www.base64-image.de  Following is some Sample Outputs and images size Filesize: 8.21 KB Encoded: 10.95 KB Width: 170 px Height: 86 px Filesize: 53.60 KB Encoded: 71.47 KB Width: 511 px Height: 511 px From the samples. The bigger the image, the more KB of size, and more Ether needs to be spent. Assuming i stick to a 72x72 image Filesize: 6.68 KB Encoded: 8.91 KB Width: 72 px Height: 72 px which works fine to be saved in the block-chain for the period of the passport Validity. The new code is added in Bold pragma solidity ^0.4.0; contract passportAresh{ //Declare a struct similar to C++ ...

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

Solidity Hash of Structs (Testing hash uniqueness)

So ive been informed by one of my Advisee ( Rain de Castro )  that when hashing a Struct in Solidity, the hash is the same for 2 data that is different. So i did a few tests with the following Code pragma solidity ^0.4.0; contract testHashAresh{ //Declare a struct similar to C++ struct Person{ string fullName; string number; } mapping (bytes32 => Person) allPersons; function returnHash(string _name, string _time) constant returns (bytes32,bytes32) { Person memory p = Person(_name, _time); bytes32 hash2 = sha256(p); bytes32 hash = sha256(_name, _time); return (hash2, hash); } } True enough The returned value for a data "Aresh","555" 0: bytes32: 0xac9c046be90fd91b4a31b9d04bb937659e885264dd8e6b8a7c9e79b806ef3ead 1: bytes32: 0x5ffc903b2aee670ea38ec2c35a8c5b8ac04754cf77536dd89d4d9985c4fd110f while the data for "Armin","555" is 0: bytes32...

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

Basic "Inventory Management Contract" Example

in this post i will be creating a Basic Inventory Management Contract Inventory management is the management of inventory and stock. As an element of supply chain managemen t, inventory management includes aspects such as controlling and overseeing ordering inventory, storage of inventory, and controlling the amount of product for sale. So here are the basics of the inventory system that i will be adding to the contract 1. Add a product to the Inventory (Only Creator) 2. Remove a product from the Inventory (Only Creator) 3. Replenish Products in the inventory (Only Creator) 4. allow anyone to order a product pragma solidity ^0.4.0; contract InventoryAresh{ struct Product{ string sku; //always unique to the product string itemName; string itemDescription; uint qty; //available qty uint thresholdQty; //minimum qty before reorder string location; UoM UnitOfMeasure; Status status; bool isValue; ...

"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; } }

Fixed Number of Items for a "To Do List Contract" Example

So in this post, i have a "To Do List" with constant number of items pragma solidity ^0.4.0; contract ToDoAreshConstant{ //Declare a struct similar to C++ holding details of a single ToDo Item struct Item{ string task; uint priority; } uint CurrentItem; //An Array of structs similar to C++ with 10 items (reserves storage) and index start at 0 Item[10] ToDoList; //this is a constructor, notice it has the same name as the Contract name function ToDoAreshConstant() public { CurrentItem = 0; //Assign to zero when contract is created. } //Adds an item to the list and increments the position of the index function AddToDo(string _task, uint _priority) public { ToDoList[CurrentItem] = Item(_task,_priority); CurrentItem++; } //returns a single item given an index since it requires more gas to iterate thru function getToDoList(uint _index) public returns(string ){ ...