Category: 02. Ethereum

https://cdn-icons-png.flaticon.com/512/6001/6001368.png

  • Interacting with the Contract

    When you click the deployed contract, you will see the various public methods provided by the contract. This is shown in the screenshot below.

    Deploy Public Methods

    The first method send contains an edit box in front of it. Here, you will type the parameters required by the contract method. The other two methods do not take any parameters.

    Sending Money

    Now, enter some amount such as 100 in front of the send function seen in the contract window. Click the send button. This will execute the contract send method, reducing the value of the contract value field and increasing the value of the amount field.

    Sending Money
    Ezoic

    Examining Contract Value

    The previous send money action has reduced the contract value by 100. You can now examine this by invoking the getBalance method of the contract. You will see the output when you click on the getBalance button as shown in the screenshot below −

    Examining Contract Value

    The contract value is now reduced to 900.

    Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

    Examining Collected Amount

    In this section, we will examine the amount of money collected so far on this contract. For this, click on the getAmount button. The following screen will appear.

    Examining Collected Amount

    The amount field value has changed from 0 to 100.

    Try a few send operations and examine the contract value and the amount fields to conclude that the deployed contract is executing as expected.

  • Deploying the Contract

    In this chapter, we will learn how to deploy contract on Ethereum. Click on the Run menu option to deploy the contract. The following screen will appear.

    Deploy Contract

    The contract name is shown in the highlighted list box. Below this, you will notice the Deploy button, click on it to deploy the contract. The contract will be deployed on the Remix built-in Blockchain. You will be able to see the deployed contract at the bottom of the screen. You can see this in the highlighted portion of the screenshot below.

    Deploy Highlighted Portion

    Notice, the presence of three method names in this highlighted region. Next, you will interact with the contract by executing the contract methods.

  • Compiling the Contract

    Once you write the complete contract code, compiling it in this IDE is trivial. Simply click on the Autocompile checkbox in the IDE as shown in the screenshot below −

    Autocompile checkbox

    Alternatively, you may compile the contract by clicking the button with the title “Start to compile”.

    Start to compile

    If there is any typo, fix it in the code window. Make sure the code is compiled fully without errors. Now, you are ready to deploy the contract.

  • Developing MyContract

    We will name our contract MyContract as in the following declaration −

    contract MyContract {
    

    We will declare two variables as follows −

    uint amount;
    uint value;
    

    The variable amount will hold the accumulated money sent by the contract executors to the contract creator. The value field will hold the contract value. As the executors execute the contract, the value field will be modified to reflect the balanced contract value.

    In the contract constructor, we set the values of these two variables.

    constructor (uint initialAmount, uint initialValue) public {
       amount = 0;
       value = 1000;
    }

    As initially, the amount collected on the contract is zero, we set the amount field to 0. We set the contract value to some arbitrary number, in this case it is 1000. The contract creator decides this value.

    To examine the collected amount at any given point of time, we provide a public contract method called getAmount defined as follows −

    function getAmount() public view returns(uint) {
       return amount;
    }

    To get the balanced contract value at any given point of time, we define getBalance method as follows −

    function getBalance() public view returns(uint) {
       return value;
    }

    Finally, we write a contract method (Send). It enables the clients to send some money to the contract creator −

    function send(uint newDeposit) public {
       value = value - newDeposit;
       amount = amount + newDeposit;
    }

    The execution of the send method will modify both value and amount fields of the contract.

    The complete contract code is given below −

    contract MyContract {
       uint amount;
       uint value;
    
       constructor (uint initialAmount, uint initialValue) public {
    
      amount = 0;
      value = 1000;
    } function getBalance() public view returns(uint) {
      return value;
    } function getAmount() public view returns(uint) {
      return amount;
    } function send(uint newDeposit) public {
      value = value - newDeposit;
      amount = amount + newDeposit;
    } }
  • Solidity for Contract Writing

    Solidity is an object-oriented language especially developed for contract writing. It is a high-level language, which inherits traits from C++, Python, and JavaScript. The Solidity compiler compiles your source code into bytecode that runs on Ethereum Virtual Machine (EVM).

    For quick understanding of the Solidity syntax, look at the sample code in the IDE.

    pragma solidity >=0.4.22 <0.6.0;
    contract Ballot {
    

    The first line is a directive to the compiler. The second line starts the definition of the contract. Within the contract, you declare variables such as −

    address chairperson;
    

    You can also define structures such as Proposal and create an array of these structure items. Examine this in the code window.

    You may then define a constructor which is invoked at the time of instantiating a contract.

    constructor(uint8 _numProposals) public {
    

    After the constructor, you will define several methods, which are the contract methods. In the sample contract, giveRightToVote is one such method having the following syntax −

    function giveRightToVote(address toVoter) public {
    

    The public keyword makes this method publicly invokable by any client who has access to the contract.

    Likewise, the sample contract defines three more methods called delegate, vote, and winningProposal. Examine these for your own understanding of the Solidity syntax. These are the prerequisites to writing your own contract. Explaining the full syntax of Solidity is beyond the scope of this tutorial.

  • Smart Contracts

    There are several tools available to develop and test contracts. One of the simplest tools is provided on the official Ethereum site itself. The tool is called Remix, we will use this for our contract development.

    Remix for Contract Development

    Open the Remix IDE by typing in the following URL in your browser.https://remix.ethereum.org

    The following screen will appear.

    Contract Development

    In the center window, you will see some default code, which is a sample Solidity code. You will type your contract code in this code editor. Your code may be auto-compiled. Upon successful compilation of the code, you will be able to run the code in the same IDE. When you execute the contract methods, the results will be displayed in the same IDE window. There are facilities to debug the code and to unit test your project. These can be seen in the menu bar at the top right hand side as shown in the IDE screenshot below. You will be using these options shortly.

    Remix options

    You will now start writing your contract.

  • Introduction

    A huge success of Bitcoin raised interest in the minds of several to create their own currencies. Looking at the advantages offered by Bitcoin – a digital currency, people wanted to use the concept of Blockchain in their own applications. People wanted to move out of their physical contracts to smart digital contracts where several issues like repudiation, transparency, security, etc. would be automatically addressed. The outcome of this effort resulted in the creation of Ethereum – a popular platform for creating distributed Blockchain applications that support smart contracts.

    In this tutorial, you will learn how to create a distributed application (DAPP) on Ethereum platform. More specifically, you will learn how to write a contract, test it on a local Blockchain and finally deploy it on an external Blockchain for deep testing and commercial use. You will use Solidity, an object-oriented language for contract development. You will also use Remix, an open source IDE for developing and testing contracts. To deploy the tested contract on an external Blockchain, you will use Ganache. To interact with the contract you will need a client application. We will use MyEtherWallet to create a wallet for each such client. The contract creator will publish the contract. Any other client will look at the contact value by using the interface provided by the contract and send some money to the creator for executing a part of the contract.

    So let us begin by writing the contract.

  • Ethereum Tutorial

    Looking at the advantages offered by Bitcoin − a digital currency, people wanted to use the concept of Blockchain in their own applications. People wanted to move out of their physical contracts to smart digital contracts where several issues like repudiation, transparency, security, etc. would be automatically addressed. The outcome of this effort resulted in the creation of Ethereum − a popular platform for creating distributed Blockchain applications that support smart contracts.

    Audience

    This tutorial is designed for those who wish to gain some insight on how Ethereum works. After completing this tutorial, you will find yourself at a moderate level of expertise from where you can take yourself to the next level.

    Prerequisites

    Before proceeding with this course, we assume the reader has basic understanding in Web Development, JavaScript, Ajax-Requests, AngularJS, Gulp/Grunt and the Node Package Manager.