Writing Smart contracts and why we need them.

in LeoFinance3 years ago

I'm learning to write smart contracts on the blockchain, using the solidity object oriented programming language and I started just as I said here with this first topic. Since I already have some programming knowledge, I do not have to go through the prerequisites of learning to write JavaScript or at least some programming language and can dive straight into learning.

andrea-de-santis-zwd435-ewb4-unsplash.jpg
image source

The first thing I had to do was download the Remix IDE. An IDE is simply an integrated development environment and different programming languages have IDEs specific to their environment for example Eclipse for Java and Netbeans. Visual Studio code is what I usually use for JavaScript development. I was a little afraid that I would need to get an IDE that would take much space and reduce the running capacity of my old laptop, but that was not to be as Remix is actually an IN browser IDE, quite similar to using codepen for example which you can check out here.

Here is a screenshot of my own work environment when I opened it:

image.png

Now let's go back to smart contracts. What are they? From what I now understand, they are simply a way to ensure that certain agreements run efficiently and correctly. For example if you have a will written, you will need a lawyer to ensure that it is executed according to the terms and conditions specified.

Smart contracts eliminate the middleman, the human so to say and automate such processes making it less error prone and secure. It reduces costs as you can imagine, you would not need to pay for a lawyer or pay for a will to be drafted and verified. I can imagine many use cases in insurance, in banking, housing, in dealing with intellectual rights and so on. In the end though, a smart contract is really just code written on the blockchain and in solidity, they have the .sol extension as you can seen in the folder titled 'contracts' which contain some contracts I learnt to write (Hello.sol, SimpleStorage.sol, and AdvancedStorage.sol).

One thing to note is that smart contracts cannot be changed :) Once written, they remain like that, so there is no duplicity, and this means that since we are human and make a lot of errors, we should test very careful to check that our smart contracts are written accurately before deploying to production.

cdd20-wNnGHhZ9QHA-unsplash.jpg
image source

None of the contracts I learnt to write were complex as they are meant to be beginner friendly as I get introduced to syntax in solidity. So far, I have learnt about how to make a call and what the difference is between that and a transaction. Basically a call function causes a contract to run without making any changes on the blockchain. A getter function is a call for example and a transaction can be processed by miners. In the console at the bottom of the Remix IDE, these two things are clearly differentiated in the log messages after running the code. A transaction results in gas fees and changes the state of the block chain and I learnt that you get ten different and free accounts on Remix with some fake ether worth 100 ether each and this greatly helps in the development process.

I also learnt that you need to specify data types in solidity, unlike in JavaScript where you sort of understand by the code what exactly is an integer, array and so on. A type could be a string or an integer and so on. So for strings, you would simply type 'string' as a parameter in your function. Something like this:

pragma solidity ^0.5.0;

contract HelloWorld{
 function hello() pure public returns(string memory){
     return 'Hello World';
 }
}


It is also interesting for me to see how it uses 'returns' as well as 'return' and clearly states which functions are pure or read only and which ones can be changed. Also, writing solidity code means you need to clearly write 'memory' as without this command it will not be able to store a chunk of data. Like if you had an array of things, no space will be allocated for storing that if you did not indicate it needed one. For types like integers, I saw that it might not be necessary.

Also, writing the first line 'pragma solidity ^0.5.0;' is important as it tells the compiler what version you're on and if you run on a lower version, or different version altogether, you will get errors.

So far, just like in all programming languages, you just need to learn the syntax of the language. The things you will need to do most of the time are the same and it is just the style of doing them that changes. So, just like you have CRUD operations in other programming languages, in solidity, you have the same.

People are aware of the high gas fees on the Ethereum blockchain and I learnt that if you set low gas fees in your IDE for example, your transaction could fail(just like in real life with real ether). In this case, you just need to reload the Remix IDE and it will refill your account back with the fake ether. So I think for today, I have satisfied a bit of my curiosity about blockchain development and I hope to slowly continue learning.

Sort:  

Yay! 🤗
Your content has been boosted with Ecency Points, by @lizzyblue2.
Use Ecency daily to boost your growth on platform!

Support Ecency
Vote for new Proposal
Delegate HP and earn more

Dear @lizzyblue2,

The current HiveBuzz proposal will expire in a few days.

Do you mind supporting our proposal for 2022 so our team can continue its work next year?
You can do it on Peakd, ecency,

Hive.blog / https://wallet.hive.blog/proposals
or using HiveSigner.
https://peakd.com/me/proposals/199

Thank you. We wish you a Happy New Year!