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 t+oVoter) 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.

Similar Posts

  • Deploying Contract

    To deploy the contract, select the Contracts menu option as shown in the screenshot below − You will need to enter the contract’s bytecode on this screen. Remember, when you compile your Solidity contract code, it generated a bytecode that runs on EVM. You will now need to obtain this bytecode from Remix IDE. Go to the Remix…

  • Limitations of Remix

    The Remix IDE that you have used so far is good enough for development and initial testing of your contract. For real-life contracts, you need to test your functionality against various parameters. Remix cannot create real (non-test) user accounts to transfer funds between them. You have no control over the configuration of the Blockchain created…

  • Creating Contract Users

    In this chapter, we will learn the creation of contract users on Ethereum. To create a user for our published contract, we will create another MyEtherWallet client attached to the same Ganache Blockchain that you have been using in the previous steps. Go to the MyEtherWallet screen and create a new wallet. Click on the contracts menu and select the “Interact with…

  • Ganache for Blockchain

    Ganache is used for setting up a personal Ethereum Blockchain for testing your Solidity contracts. It provides more features when compared to Remix. You will learn about the features when you work out with Ganache. Before you begin using Ganache, you must first download and install the Blockchain on your local machine. Downloading Ganache You…

  • 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 − Alternatively, you may compile the contract by clicking the button with the title “Start to compile”. If there is any typo, fix it in the code window. Make…