<h1>Smart Contracts - How do they work?</h1>
<h2>Definition</h2>
<p>Smart Contracts are blocks of code which are stored and executed on blockchain nodes, and they do they automatically when a certain condition is met. Once deployed they cannot be changed.</p>
<p>The definition of Smart Contracts by several other companies might help you understand it more clearly -</p>
<blockquote>
<p>Smart contracts are simply programs stored on a blockchain that run when predetermined conditions are met. They typically are used to automate the execution of an agreement so that all participants can be immediately certain of the outcome, without any intermediary’s involvement or time loss. They can also automate a workflow, triggering the next action when conditions are met. </p>
</blockquote>
<ul>
<li><a href="https://www.ibm.com/topics/smart-contracts">IBM Blog</a></li>
</ul>
<h2>Languages to create Smart Contracts with.</h2>
<p>Now since we have an idea about what a Smart Contract is let's have a look at how it is made. There are several languages with which you can write Smart Contracts and the most used ones are - </p>
<ol>
<li><p>Solidity -
It is an object-oriented, high-level programming language tailored for the implementation of smart contracts. If you know about Java/C# then the structure of a solidity file would look like a C# or Java Class. </p>
</li>
<li><p>Vyper -
It is a programming language with a heavy influence of Python tailored particularly for smart contract development. Auditibility, Security and Simplicity are the three fundamental design principles that drive Vyper's development.</p>
</li>
<li><p>DAML -
It is an open-source programming language that is used to write distributed applications in correct, quick, and concise manner. More precisely, DAML is an easy, safe, and efficient smart contract language. DAML is intended for distributed ledgers. It provides high-level and unambiguous specification of business logic in real-time. Daml provides more privacy focused features than you would get from any of the other languages and deals with issues in a more modern way than any other language.</p>
</li>
</ol>
<p>Now, since you have a broad idea about what Smart Contracts are and in which languages you can write them, let's have a look at a sample Smart Contract in Solidity. </p>
<p>Since Solidity is the most famous and used one of all the languages and I also have some personal ideas about working with it. We are going to use it as an example. Before we jump into code, I want to highlight why we would use smart contracts rather than any other tool to build web3 applications. </p>
<h2>Benefits of using and working with Smart Contracts</h2>
<p>The reasons according to me are following -</p>
<ol>
<li>They run on blockchain nodes i.e. they are public in nature, decentralised and anyone can participate and work with them. </li>
<li>Since all the data, contracts are public and routinely verified by several nodes it is guaranteed to be tamper proof and data once put on blockchain is a little bit harder to get rid of. (I am still very curious about what implementations it can bring into current judicial systems)</li>
<li>Read transactions are free of cost and to write any data you require gas i.e. to change a bunch if things it would cost a lot of money even if someone wanted to do it.</li>
</ol>
<h2>Simple Example</h2>
<p>A simple example of a smart contract would look like this. </p>
<p>%[<a href="https://gist.github.com/SaifRehman/aa733dec9371c3fadb68dd814404f022%5D">https://gist.github.com/SaifRehman/aa733dec9371c3fadb68dd814404f022]</a></p>
<p>Here in the above example, you can see that our contract only has one field declared which can be changed i.e. <code>value</code> and there is a getter and setter function to change that value. </p>
<p>Once you deploy this contract on any Blockchain that supports it. Any of the clients can access the smart contract and read data from the getter function and set the value using the setter function and paying some gas fees. </p>
<p>Although this is a simple example. The main idea behind this example was to display that any class variable is the actual state of the contract. You can have different data types and store them on chain to be verifiable and immutable. </p>
<h2>Additional Information necessary to get you started.</h2>
<p>The best way to write Smart Contracts is using <a href="https://remix.ethereum.org/">Remix IDE</a>. Remix provides you with special tools to write solidity and deploy it to a VM or a local blockchain like <a href="https://www.trufflesuite.com/ganache">Ganache</a> or <a href="https://geth.ethereum.org/">Geth</a> . Both Ganache and Geth are nothing but implementations of blockchain that you can run to work with smart contracts and test them locally and they come with a bunch of tooling precluded with them. </p>
<p>This was enough information about blockchain and I think we should talk about it more in the next blog. </p>