SOLIDITY SMART CONTRACT

What is Ethereum?

Before getting started with smart contracts or Solidity let us first get an overview of what Ethereum is: Ethereum is a decentralized open-source blockchain with support for a Turing-complete programming language, Solidity. What we normally call computer programs are called smart contracts in Ethereum. Launched in 2015, Ethereum has been gaining significant popularity over the last 5 years. It is a permissionless, public blockchain, which means anyone can use the blockchain, though, for every write operation, you need to pay ETH (Gas).

What is a Smart Contract?

The smart contract term was coined by Nick szabo in 1997. Smart contracts are nothing but programs that exist on the blockchain. These "smart contracts" can be used by making outside method calls or calls from other smart contracts. These smart contracts execute in the EVM (Ethereum virtual machine). Smart contracts can reduce malicious exceptions, fraudulent losses, and a need for trusted intermediator when properly written and audited.

Ethereum supports Turing complete smart contracts, which means you can perform almost any type of operation you want. (Remember, you need to pay ETH for every write operation).

What is Solidity?

As we saw that smart contracts are nothing but programs and you need a programming language to write these programs. Ethereum core contributors invented a programming language called Solidity to write smart contracts (aka computer programs that run on the blockchain). Solidity is a high-level, object-oriented language inspired by JavaScript, C++, and Python - it has a syntax very similar to JavaScript. Other blockchains and Ethereum forks support Solidity - such as Tron. Solidity is not the only language you can use to write smart contracts though. Other languages can be used to write smart contracts, like Vyper, the most popular and adopted smart contract language after solidity

The steps this tutorial will go through:

  • 1. Set up the project using NPM
  • 2. Install the necessary dependencies
  • 3. Make the project’s folder structure
  • 4. Overview of the smart contract you’ll be writing
  • 5. Writing the smart contract
  • 6. Writing a javascript function that will compile & deploy the smart contract
  • 7. Create an Alchemy account & Metamask wallet
  • 8. Configure hardhat.config.js with alchemy
  • 9. Deploy the smart contract
  • 10. Conclusion/recap

1. Version Pragma:

pragma solidity >=0.4.16 < 0.7.0;
Pragmas are instructions to the compiler on how to treat the code. All solidity source code should start with a “version pragma” which is a declaration of the version of the solidity compiler this code should use. This helps the code from being incompatible with the future versions of the compiler which may bring changes. The above-mentioned code states that it is compatible with compilers of version greater than and equal to 0.4.16 but less than version 0.7.0.

2. The contract keyword:

contract Storage{
//Functions and Data
}
The contract keyword declares a contract under which the code is encapsulated.

3. State variables:

uint public setData;
State variables are permanently stored in contract storage, that is they are written in Ethereum Blockchain. The line uint setData declares a state variable called setData of type uint (unsigned integer of 256 bits). Think of it as adding a slot in a database.

4. A function declaration:

function set(uint x) public
function get() public view returns (uint)

This is a function named set of access modifier type public which takes a variable x of datatype uint as a parameter.

This was an example of a simple, smart contract that updates the value of setData. Anyone can call the function set and overwrite the value of setData, which is stored in the Ethereum blockchain and there is possibly no way for anyone to stop someone from using this function. This is an example of a decentralized application that is censorship proof and unaffected to the shutdown of any centralized server. As long as someone is running a single node of the Ethereum blockchain, this smart contract will be accessible.

Function get will retrieve and print the value of the state variable.

Output:

Smart Contracts: Smart Contracts are the block of instructions that are not dependent on any third party or centralized database. They are executed in a decentralized environment. The benefits of smart contracts are as follows:

  • It removes centralized issues.
  • It is safe and more secure.