Writing a First Smart Contract Contract Using Solidity
By: Saikrishna Reddem
Tags: Smart Contract Blockchain, Solidity, Ethereum

“I call these new contracts “smart”, because they are far more functional than their inanimate paper-based ancestors. No use of artificial intelligence is implied. A smart contract is a set of promises, specified in digital form, including protocols within which the parties perform on these promises” — Nick Szabo, 1996

Solidity

“Solidity is a contract-oriented, high-level language for implementing smart contracts. It was influenced by C++, Python and JavaScript. It is designed to target the Ethereum Virtual Machine (EVM).”

Lets us interpret the above highlighted words one-by-one :

Contract-Oriented: Contracts are a breakthrough technique to reduce the programming effort for large projects. Contracts are the concept of preconditions, post conditions, errors, and invariant .

“Contract” in this context is actually not an Ethereum contract, or a smart contract; it is the name used for a (currently informal) definition of a function or some other unit of functionality. The basic idea is that a function should be separated into pre-conditions, the body, and post-conditions, both in the description (the contract) and the code.

High-Level Language : High level programming languages are associated with human understandable , top layer of abstraction which humans can understand the code and use according their need in projects.Ex: Java , Python etc.

Smart Contracts :act as the back-end logic and storage. A contract is written in Solidity, a smart contract language, and is a collection of code and data that resides at a specific address on the Ethereum blockchain. It’s very similar to a class in Object Oriented Programming, where it includes functions and state variables. Smart Contracts, along with the Blockchain, are the basis of all Decentralized Applications. They are, like Blockchain, immutable and distributed, which means upgrading them will be a pain if they are already on the Ethereum Network. Fortunately, here are some ways to do that.

For all intents and purposes, a smart contract is a programmable escrow of sorts, an independent middleman or a fair judge capable of managing a financial transaction between various parties and autonomously arbitrating a dispute.

Ethereum Virtual Machine(EVM): handles the internal state and computation of the entire Ethereum Network. Think of the EVM as this massive decentralized computer that contains “addresses” that are capable of executing code, changing data, and interacting with each other.

Lets go for an Interesting Example

Say a grandfather who is in his last years wants nothing more than to have his inheritance delivered down to his family through means of a will when he passes. In a traditional will, it would be clear who gets what, how much and when, solidified in a legally bound document. When the time comes to actually distribute the sauce, a judge from a court of law needs to revise the document and make decisions accordingly. A common problem that arises within families is an argument about who gets what, creating tension at best and destroying relationships at worst. During the court hearing, This could influence the decision of the judge, which in turn could produce unfair results and possibly create further damage to family bonds.

So that being said, what if we could streamline this process by drastically reducing this problem?

If the will was a smart contract, a judge would not be necessary. Here’s where it gets awesome. Gramps can actually make the contract hold assets on his behalf, and have them programmatically released to his liking after his passing. In this scenario, the code written in the contract itself would dictate the outcome, effectively eliminating the need for an arbitrator. Sarah gets $10,000, Ben gets $5,000 and Julia gets $2,000. The code executes and the assets in the form of a token or cryptocurrency get automatically distributed to these parties without the need for human intervention. While everyone involved may not be happy with the outcome, nobody can really argue with lines of code on a screen. Sounds practical, right?

Keeping this example in mind, let’s get to the part you’ve all come here for. In this lesson we will build gramps a simple will through a smart contract written in solidity.

Lets get Hands on Practice now ,

First you have to download a Contract Development Tool.In such Context ,you can do with Remix (both offline and online).

Environment Setup to Write Your First Smart Contract in Ethereum

Remix this serves as Development Platform for Writing your Smart Contracts.You will see same window

Head over to the top left drop down “Environment” and make sure “JavaScript VM” is selected. This means that all the code will run as a standalone, and not affect the actual Ethereum network in any way. Click the + icon in the top left corner to create a new file, and call it “will”. A tab will appear at the top which represents which file is currently being edited.

Define the Version

Structure of a Contract

Contracts in Solidity are similar to classes in object-oriented languages. Each contract can contain declarations of State Variables, Functions, Function Modifiers, Events, Struct Types and Enum Types. Furthermore, contracts can inherit from other contracts.

Contract Contract_name{ }

We Write a basic Contract named Student holding name and age:

contract Student {

string name;//1.)state variables

uint age;

//2.)getter and setter functions

function setName(string newName){

name = newName;



}

function setAge(uint newAge){

age = newAge;

}



function getName()public constant returns(string){

return name;

}

function getAge()public constant returns(uint){

return age;

}

}

State variables are values which are permanently stored in contract storage. See the Types section for valid state variable types and Visibility and Getters for possible choices for visibility.

Functions

Functions are the executable units of code within a contract. Function Calls can happen internally or externally and have different levels of visibility (Visibility and Getters) towards other contracts.

On the Top Right of the window you can see Compile , compile the contract you wrote,make sure that you choose the version which you have used in your program .And Start the Compile (Ctrl-S)

Next Click on the Run

Choose the Contract(Student) which you have compiled from the Dropdown their.

And Deploy the Contract.

Cheers !!!!!!! You have Done with First Contract on Remix Development Tool

Please Feel Free to Comment (regards any mistake)

Thank You,

R.Saikrishna