0% found this document useful (0 votes)
4 views

Week2 Program

5

Uploaded by

22p65a1207
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Week2 Program

5

Uploaded by

22p65a1207
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Introduction to Solidity


Solidity is a brand-new programming language created
by Ethereum which is the second-largest market of cryptocurrency
by capitalization, released in the year 2015 and led by Christian
Reitwiessner. Some key features of solidity are listed below:
 Solidity is a high-level programming language designed for
implementing smart contracts.
 It is a statically typed object-oriented(contract-oriented)
language.
 Solidity is highly influenced by Python, c++, and JavaScript
which run on the Ethereum Virtual Machine(EVM).
 Solidity supports complex user-defined programming,
libraries, and inheritance.
 Solidity is the primary language for blockchains running
platforms.
 Solidity can be used to create contracts like voting, blind
auctions, crowdfunding, multi-signature wallets, etc.

Ethereum

Ethereum is a decentralized open-source platform based on the


blockchain domain, used to run smart contracts i.e. applications
that execute the program exactly as it was programmed without
the possibility of any fraud, interference from a third party,
censorship, or downtime. It serves as a platform for nearly 2,60,000
different cryptocurrencies. Ether is a cryptocurrency generated by
Ethereum miners, used to reward for the computations performed
to secure the blockchain.

Ethereum Virtual Machine(EVM)

Ethereum Virtual Machine abbreviated as EVM is a runtime


environment for executing smart contracts in ethereum. It focuses
widely on providing security and execution of untrusted code using
an international network of public nodes. EVM is specialized to
prevent Denial-of-service attack and confirms that the program
does not have any access to each other’s state, also ensures that
the communication is established without any potential
interference.

Smart Contract
Smart contracts are high-level program codes that are compiled to
EVM byte code and deployed to the ethereum blockchain for further
execution. It allows us to perform credible transactions without any
interference of the third party, these transactions are trackable and
irreversible. Languages used to write smart contracts are Solidity (a
language library with similarities to C and JavaScript), Serpent
(similar to Python, but deprecated), LLL (a low-level Lisp-like
language), and Mutan (Go-based, but deprecated).
Example: In the below example, we have discussed a sample
solidity program to demonstrate how to write a smart contract in
Solidity.
 Solidity

// SPDX-License-Identifier: GPL-3.0
pragma solidity >= 0.4.16 < 0.9.0;
/// @title A contract for demonstrate how to write a smart contract

/// This contract just show to set the value of state variable,
calculate the sum and get this sum value from the smart contract

contract Test
{

// Declaring state variables


uint public var1;
uint public var2;
uint public sum;

// Defining public function


// that sets the value of
// the state variable
function set(uint x, uint y) public
{
var1 = x;
var2 = y;
sum=var1+var2;
}

// Defining function to
// print the sum of
// state variables
function get( ) public view returns (uint) {
return sum;
}
}
Output:

Explanation:
1. Version Pragma:
pragma solidity >=0.4.16 <0.9.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.9.0
2. The contract keyword:
contract Test
{
//Functions and Data
}
The contract keyword declares a contract under which is the code
encapsulated.
3. State variables:
uint public var1;
uint public var2;
uint public sum;
State variables are permanently stored in contract storage that
they are written in Ethereum Blockchain. The line uint public
var1 declares a state variable called var1 of type uint (unsigned
integer of 256 bits). Think of it as adding a slot in a database.
Similarly, goes with the declaration uint public var2 and uint public
sum.
4. A function declaration:
function set(uint x, uint y) public
function get() public view returns (uint)
 This is a function named set of access modifier
type public which takes a variable x and variable y of
datatype uint as a parameter.
 This was an example of a simple smart contract that
updates the value of var1 and var2. Anyone can call the
function set and overwrite the value of var1 and var2 which
is stored in the Ethereum blockchain. 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.
 The variable sum is calculated by adding the values of the
variables var1 and var2.
 Function get will retrieve and print the value of the state
variable sum.
How to Execute The Code:
There are practically two ways to execute a solidity smart contract:
1. Offline Mode: Running a solidity smart contract in Offline mode
requires three prerequisites and 4 major steps to be followed to get
the smart contract running:
 Prerequisites:
o Download and install node.js.
o Install Truffle globally.
o Install ganache-cli.
 Objectives:
o Create a truffle project and configure a
development network
o Create and deploy smart contracts
o Interact with the smart contract from Truffle
console
o Write tests for testing the main features
offered by Solidity.
2. Online Mode: Remix IDE is generally used to compile and run
Solidity smart contracts in the Online Mode. You can find the
complete articles with all the steps here.
 Solidity Version: The specified version range (0.6.12 to 0.7.0) is appropriate
for the provided code, ensuring compatibility and avoiding potential
vulnerabilities.
 License: SPDX(Software Package Data Exchange)The UNLICENSED
identifier indicates that the code is not subject to any specific license,
allowing for unrestricted use and modification.
 Contract Structure: The contract is well-structured, with clear variable
declarations, events, and functions.
 Minter Role: The minter address is used to control the minting process,
ensuring that only authorized parties can create new tokens.
 Token Balances: The balances mapping efficiently tracks the token balances
of individual addresses.
 Minting Function: The mint function allows the minter to create new tokens
and distribute them to specified addresses, with a reasonable limit of 1e60
tokens.(1e60 is a scientific notation for a very large number. In this context, it represents 1
followed by 60 zeros, or:
 1,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,00
0,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,00
0,000,000
 This is an extremely large number, far beyond the practical limits of any real-world
currency. It's likely used in this context to set a very high limit on the total supply of
tokens, preventing inflation and ensuring that the token remains valuable.)
 Sending Tokens: The send function enables token holders to transfer their
tokens to other addresses, ensuring sufficient balance and emitting a Sent
event for transparency.

The provided Solidity code defines a simple token contract called ExampleCoin. It
includes the following key features:

 Minter: The contract maintains a minter address that is responsible for


creating new tokens.
 Token Balances: A balances mapping tracks the token balances of individual
addresses.
 Minting: The mint function allows the minter to create new tokens and
distribute them to specified addresses.
 Sending Tokens: The send function enables token holders to transfer their
tokens to other addresses.
 Events: The Sent event is emitted whenever tokens are transferred.

Overall, the code provides a basic framework for creating and managing a simple
token on the Ethereum blockchain.

You might also like