Lecture 2.4.2 - Solidity

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

UNIVERSITY INSTITUTE OF

ENGINEERING
COMPUTER SCIENCE ENGINEERING
Bachelor of Engineering (Computer Science & Engineering)
Subject Name: Block chain Technology
Subject Code: 20_CST-412

Solidity
Mapped with CO4

Prepared By: DISCOVER . LEARN . EMPOWER


Er. Ankita Sharma 1
• Solidity is an object-oriented, high-level language for implementing
smart contracts.
• Its syntax is closer to both JavaScript and C.
• It is a statically typed language, which means that variable type
checking in Solidity is carried out at compile time.
• Solidity is also called a contract-oriented language.
• In Solidity, contracts are equivalent to the concept of classes in other
object-oriented programming languages.
• Solidity is a curly-bracket language designed to target the Ethereum
Virtual Machine (EVM).
• A Simple Smart Contract
• Let us begin with a basic example that sets the value of a variable and
exposes it for other contracts to access.
• Storage Example
• // SPDX-License-Identifier: GPL-3.0
• pragma solidity >=0.4.16 <0.9.0;
• contract SimpleStorage {
• uint storedData;
• function set(uint x) public {
• storedData = x;
• }
• function get() public view returns (uint) {
• return storedData;
• }
• The first line tells you that the source code is licensed under the GPL
version 3.0. Machine-readable license specifiers are important in a
setting where publishing the source code is the default.
• The next line specifies that the source code is written for Solidity
version 0.4.16, or a newer version of the language up to, but not
including version 0.9.0.
• The pragma keyword is used to enable certain compiler features or
checks.
• A contract in the sense of Solidity is a collection of code (its functions)
and data (its state) that resides at a specific address on the Ethereum
blockchain.
• The line uint storedData; declares a state variable called storedData of
type uint (unsigned integer of 256 bits).
Ethereum Virtual Machine
• Here are some interesting facts to keep in mind-
• Ethereum does not use a real machine to run your contract. It runs a
virtual machine known as the Ethereum Virtual Machine(EVM) to
execute the smart contract.
• It is not only sandboxed but actually completely isolated, which means that
code running inside the EVM has no access to network, filesystem or other
processes.
• Smart contracts even have limited access to other smart contracts.
• No matter which high-level language you go for, it will be compiled
down to a machine-readable format such as bytecode.
• Importing other Source Files
• Solidity supports import statements to help modularise your code .

• import "filename";
• import * as symbolName from "filename";
• import "filename" as symbolName;
• import {symbol1 as alias, symbol2} from "filename";
Comments
• Single-line comments (//) and multi-line comments (/*...*/) are
possible.

• // single line comment


• /* multi line
• Comments */
Functions
• Functions are the executable units of code. Functions are usually defined inside a
contract, but they can also be defined outside of contracts.
• Functions accept parameters and return variables to pass parameters and values
between them.
• // SPDX-License-Identifier: GPL-3.0
• pragma solidity >=0.7.1 <0.9.0;

• contract SimpleAuction {
• function bid() public payable { // Function
• // ...
• }
• }

• // Helper function defined outside of a contract


• function helper(uint x) pure returns (uint) {
• return x * 2;
Function Modifiers
• Modifiers can be used to change the behaviour of functions in a
declarative way. For example, you can use a modifier to automatically
check a condition prior to executing the function.
• Modifiers are inheritable properties of contracts and may be
overridden by derived contracts, but only if they are marked virtual.
• modifier onlyOwner {
• require(
• msg.sender == owner,
• "Only owner can call this function."
• );
• _;
• }
Constant and Immutable State Variables
• State variables can be declared as constant or immutable. In both cases,
the variables cannot be modified after the contract has been constructed.
• For constant variables, the value has to be fixed at compile-time, while
for immutable, it can still be assigned at construction time.
• Compared to regular state variables, the gas costs of constant and
immutable variables are much lower.
• For a constant variable, the expression assigned to it is copied to all the
places where it is accessed and also re-evaluated each time.
• Immutable variables are evaluated once at construction time and their
value is copied to all the places in the code where they are accessed.
• string constant TEXT = "abc";
• bytes32 constant MY_HASH = keccak256("abc");
• uint immutable decimals;
• uint immutable maxBalance;
• address immutable owner = msg.sender;
DataTypes
• Solidity is a statically typed language, which means that the type of
each variabe (state and local) needs to be specified.
• The following types are also called value types because variables of
these types will always be passed by value, i.e. they are always copied
when they are used as function arguments or in assignments.
• Booleans
• Integers
• Fixed Point Numbers
• Address
• Contract Types
• Booleans of the Solidity value types can be either true or false. The
boolean is defined with the bool keyword.

• There are two main Solidity types of integers of differing sizes:


• int - signed integers.
• uint - unsigned integers.
• Speaking of size, to specify it, you have keywords such as uint8 up to uint256,
that is, of 8 to 256 bits.

• There are two types of fixed point numbers:


• fixed - signed fixed point number.
• ufixed - unsigned fixed point number
• The address Solidity value type has two similar kinds:
• address holds a 20-byte value (size of an Ethereum address).
• address payable is the same as address, but have transfer and send members.

• address payable x = address(0x123);


• address myAddress = address(this);
• if (x.balance < 10 && myAddress.balance >= 10) x.transfer(10);

You might also like