Posts

Showing posts with the label #TestingSolidity

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

Crypto-Currency "Coin Contract" Example

Image
So here is my starting point pragma solidity ^0.4.18; //Used like an Interface in C# or Java contract Token { function totalSupply() public constant returns (uint256 supply) {} function balanceOf(address _owner) public constant returns (uint256 balance) {} function transfer(address _to, uint256 _value) public returns (bool success) {} function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {} function approve(address _spender, uint256 _value) public returns (bool success) {} function allowance(address _owner, address _spender) public constant returns (uint256 remaining) {} //needed to Log the Events event Transfer(address indexed _from, address indexed _to, uint256 _value); event Approval(address indexed _owner, address indexed _spender, uint256 _value); } contract StandardToken is Token { mapping (address => uint256) balances; mapping (address => mapping (address => uint25...

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

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

"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); ...

"Donation Contract" Example

Image
So in this Post i will be writing a Donation Contract. The idea is, The Creator Creates the contract. another person wants to Donate a certain amount of Ether to the Creator. Warning: Tests done with Remix, i can be the creator and send ether to myself without deduction of my own ether. (Im not sure why it allows this.) ideally if i dont have enough ether, it has to revert back, but it doesnt.  (Please help me by commenting Below) pragma solidity ^0.4.0; contract DonationAresh { address creator; //this is Aresh function DonationAresh() public{ creator = msg.sender; } //fall back function that gets an amount of ether and sends to Creator function () payable public{ safeMoney(msg.value); } //send the amount to the creator function safeMoney(uint amountRaised) private{ creator.send(amountRaised); } } So from the Remix IDE there are 5 Accounts. im using the first account as the Creator of the Contr...

Creating Decentralized Apps Part 1

Image
In order to create decentralized Apps such as Web applications, Websites, Mobile Apps. we need a way to interact with the blockchain. Solidity Language is merely for creating a programmable interface that runs on the blockchain So in order for us to interact with the Ethereum network (also called a node) we need to use a Client implementation.  There are many client implementations written in languages such as C++,Go,Python, Java,Haskell etc. Once you have a node using one of the clients, you can sync with the blockchain, create wallets and send and receive real ether. So there are 2 well known client implementations. 1. Test RPC - Node.js based Ethereum client for testing and development 2. Geth - Command line interface written in GO language for running a full Ethereum node Source: https://karl.tech/intro-guide-to-ethereum-testnets/ TestRPC is a lightweight Ethereum nodes used for small scale local testnets. Geth is heavyweight Ethereum nodes used for...

Testing using MetaMask Part 1 "Getting Ether"

Image
Download MetaMask On Chrome Browser Accept The "Privacy Notice" Accept The "Terms of Use" Give it a "Password" you wont forget Copy the "Vault" Created to an external text file and save it Select the "Ropsten Test Network" You currently have Zero Ether. Click "Buy" Click the "Ropsten Test Faucet" to open the website Click the "Request 1 Ether from Faucet" and wait for a few seconds Verify from your MetaMask that you have the 1 Ether. Whenever you run out of test ether, you can always request again using the same process