- Ethereum Tutorial
- Ethereum - Home
- Ethereum - Introduction
- Ethereum - Smart Contracts
- Ethereum - Solidity for Contract Writing
- Ethereum - Developing MyContract
- Ethereum - Compiling the Contract
- Ethereum - Deploying the Contract
- Ethereum - Interacting with the Contract
- Ethereum - Limitations of Remix
- Ethereum - Ganache for Blockchain
- Ethereum - Ganache Server Settings
- Ethereum - A Quick Walkthrough
- Ethereum - MyEtherWallet
- Ethereum - Creating Wallet
- Attaching Wallet to Ganache Blockchain
- Ethereum - Deploying Contract
- Interacting with Deployed Contract
- Ethereum - Creating Contract Users
- Ethereum - Summary
- Ethereum Useful Resources
- Ethereum - Quick Guide
- Ethereum - Useful Resources
- Ethereum - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Ethereum - 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.