Smart Contracts and where to find them
Purpose of this article is to get you started with ethereum smart contracts,
At the end of this article ,
· you will be able to define the structure of a smart contract,
· apply this knowledge to understand with a real contract written in Solidity language,
· use a web development environment Remix to invoke and interact with a smart contract.
Structural and meta-level attributes of a transaction are verified at the blockchain protocol level.
How were the application-specific constraints?
The answer is in the critical role played by the smart contract.Smart contract work with the application-specific semantics and constraints of the transaction and verify, validates, and executes them.
Most of all, since it is deployed on the blockchain, the smart contract leverages the immutable recording and trust model of the blockchain.
Since a smart contract is deployed in the blockchain, it is an immutable piece of code, and once deployed, it cannot be changed. We will have to redeploy the code as a new smart contract, or somehow redirect the calls from an old contract to the new one.
Smart contract can store variables in it called state variables.
We could retrieve how these variables change over the blocks.
Lets talk about smart contracts in ethereum blockchain and later hopefully we can move on to smartcontracts in hyperldger too….
Contract in the Ethereum blockchain basically has below listed attributes’
· pragma directive,
· name of the contract,
· data or the state variable that define the state of the contract,
· collection of function to carry out the intent of a smart contract.
Other items, we’ll discuss in the later
Identifiers representing these elements are restricted to ASCII character set. Make sure you select meaningful identifiers and follow camel case convention in naming them.
We’ll learn these concepts using
simple contracts written in high level language called Solidity.
In this article We will use a web integrated development environment, IDE, called Remix to create, deploy, execute, and explore the working of few representative smart contracts.
Say Hello to Remix IDE.
This is available at Remix.ethereum.org.
Here is the environment that is available as a web interface looks like.
On the left side, you see the file browser, where you can see all the smart contracts that you have created. You can create a new one, and it’ll have an entry here.
In the middle is the editor window, where you’ll type in the smart contract.
At the bottom is a console or the output window.(gray area)
On the right hand side, you have the tools compile, run, settings, analysis, debugger, and support.
At the bottom will be the web interface, and ability to create a smart contract will be somewhere in this middle.
With all these features,
Remix is indeed a one-stop environment to
develop, deploy, and test a smart contract.
We will examine a very simple smart contract,which i will name as the
greeter smart contract
This is a modified versions of the example given in Solidity documentation.
Our goal is to get an overview of the structure of a smart contract without getting into the details of the Solidity language.
However, I’ll explain every item in the smart contract we plan to explore.
What are the three steps in the development of a smart contract?
Design, code, and test. Right obviously ?
Here is the design of the Greeter contract.
This is the Hello World of smart contract.
Greeter has a string variable named yourName, the constructor Greeter,
a set function to set the name,
and a hello function that returns a string name so that you can use it to greet the world.
Here, we see the code for the Greeter contract. It begins with the pragma that provides a version number so that the compiler knows which version of Solidity this was developed in.
You also see the
name of the contract Greeter,
the state variable yourName.
Note that it is in camel notation.
This is followed by functions,
the constructor Greeter that initializes yourName variable,
set function that sets a variable to a name supplied by the users message as a parameter,
and a hello function that retrieves the name for use by the invoking application.
Remix is where we test.
Now, let’s look at the Greeter contract.
Here is a Greeter contract.
At the first line,
you see pragma Solidity. This provides the version of the Solidity.
Then, it starts with the contract name, followed up by the data or the state variables, followed up by the functions.
There are three functions here:
Greeter,which is a constructor,
and a set function which is setting the data variable, and then,
a hello function which extracts the values of the state variable and returns it.
The state variable here is yourName and it has a public visibility modifier,
and the function Greeter,
the constructor initializes yourName to World,
and the function set initializes yourName to the name provided by the sender.
Hello returns whatever value that was set in the state variable.
Let’s run it and see.
I’m going to start by compiling it,
and then, running it. (run button in right top corner)
and I’m going to create it.
When I create it,
this is the web interface where you will see all the public variables.
YourName is a public variable that’s available there.
Hello is a public function that is available there,
and set is another public function that is available there.
When I just simply say yourName,
the current yourName given is World that shows up.
I’m going to set the name to Buffalo.click on set
If I set the name to Buffalo and I click on yourName,
yourName shows up at Buffalo.
If I click on the function hello,
hello will return the current yourName that happens to be Buffalo, and it will show up.