Ethereum – Solidity for Contract Writing

Last updated on Oct 20 2021
Avinash Malviya

Table of Contents

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.

Ethereum – 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;

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;

}

}

Ethereum – 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 −

Page 4 Image 1
Autocompile checkbox

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

Page 4 Image 2 1
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.

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

Page 5 Image 3
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.

Page 5 Image 4
Deploy button

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

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

Page 7 Image 5
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.

Page 7 Image 6
Sending Money

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 −

Page 8 Image 7
Examining Contract Value

The contract value is now reduced to 900.

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.

Page 8 Image 8
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.
So, this brings us to the end of blog. This Tecklearn ‘Ethereum – Solidity for Contract Writing’ blog helps you with commonly asked questions if you are looking out for a job in BlockChain or Ethereum. If you wish to learn BlockChain or Ethereum and build a career in BlockChain domain, then check out our interactive, Blockchain and Ethereum Developer Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/blockchain-and-ethereum-certification-training/

Blockchain and Ethereum Developer Training

About the Course

Tecklearn’s Blockchain and Ethereum Certification Training course in the blockchain technology that covers essential concepts like Blockchain programming, Ethereum, Solidity, Digital ledger types, Smart Contracts, Multichain, Bitcoin mining, Cryptocurrency, etc. The course provides an overview of the structure and mechanism of Blockchain. You will learn about the Ethereum ecosystem, how smart contracts are developed using Solidity and how to deploy a business network using Hyperledger Compose.

Why Should you take BlockChain and Ethereum Certification Training?

• The average salary of a Blockchain Ethereum Developer is $158,860 per annum – Paysa.com.
• Blockchain tech has gone far beyond its beginnings in banking and cryptocurrency: In 2019, businesses are expected to spend $2.9B on the technology.
• IBM reports that Blockchain markets are headed to $60 Billion worldwide by 2024.

What you will Learn in this Course?

Introduction and origin of Blockchain

• How does our current financial system work?
• What can be the possible solution
• What is a distributed system
• What is Blockchain
• How does a Blockchain work
• Components of Blockchain
• Business network
• Consensus, Provenances, immutability and finality

Cryptocurrency and Blockchain

• Distributed system
• Distributed Ledger technology
• Global Payments
• Why BlockChain
• BlockChain and use case needs
• Requirements of blockchain for business
• BlockChain benefits
• Types of BlockChain
• Hands on

Bitcoin Platform

• What is Bitcoin?
• Why use Bitcoins?
• Bitcoin Ecosystem
• Structure of a Bitcoin Transaction
• Merkel Trees
• Scripting language in Bitcoin
• Applications of Bitcoin script
• Nodes in a Bitcoin Network
• Bitcoin Economics
• What is Bitcoin Mining?
• Types of Mining
• Mining and Consensus
• Hands On

Introduction to Ethereum

• What is Ethereum?
• Ethereum Layers
• Introducing Smart Contracts
• Cryptocurrency in Ethereum
• Mining in Ethereum
• Consensus Mechanism
• Platform Functions in Ethereum
• Technologies that support Ethereum
• Ethereum Programming Language
• Components for the development of Ethereum DApps
• Editors and tools
• Frontend Development
• Ethereum Test Networks
• ERC Tokens
• Hands On

Solidity

• Introducing Solidity
• Sample Code, Layout of Source File
• Structure of a Contract
• State Variables, Functions Types, Reference Types
• Special Variables and Functions, Expressions and Control Structures
• Function Calls, Error Handling
• Visibility for Functions and State Variables
• Inheritance, Constructors
• Importing Smart Contracts
• Gas Limit and Loops
• Sending and Receiving Ether
• Recommendations
• Contract ABI
• Setting up the development environment and Deploying DApp
• Hands On

Hyperledger

• Introduction to Hyperledger
• Hyperledger architecture
• Hyperledger Fabric V1 Architecture
• Consensus
• Hyperledger API
• Hyperledger Application Model
• Hyperledger project and tools
• Network Topology
• Exploring Hyperledger frameworks
• Business Network Deployment on Hyperledger Composer Playground
• Sample Transaction
• Service invoices
• Hands On

Hyperledger Composer

• Development Environment using Composer
• Developing business networks
• Testing business networks
• Introduction to Hyperledger Fabric
• Hyperledger Fabric Model
• Ways to create Hyperledger Fabric Blockchain Network
• Hands On

Create and Deploy Your Private Blockchain On Multichain

• What Is MultiChain
• MultiChain Privacy and Permissions
• Mining in MultiChain
• Multiple configurable Blockchains using MultiChain
• Setting up a Private Blockchain
• Hands On

Blockchain Use Cases

• Potential use cases in Blockchain
• BlockChain project

 Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "Ethereum - Solidity for Contract Writing"

Leave a Message

Your email address will not be published. Required fields are marked *