Smart Contracts
Smart Contracts
By
Dr. Shashidhar R
Assistant Professor, Dept of CSE,
Bennett University-Greater Noida
What are smart contracts?
• Smart contracts are lines of code that are stored on a blockchain and
automatically execute when predetermined terms and conditions are met.
• It’s a computer protocol. It is called smart because of its ability to verify
and execute a contract without any help from third parties. The contract
exists in the decentralized Blockchain network and contains all the terms
of a particular agreement.
• The blockchain is then updated when the smart contract is completed.
What are the benefits of smart contracts?
• Speed and accuracy: Smart contracts are digital and automated, so you
won’t have to spend time processing paperwork and correcting the errors.
• (It saves time: instead of taking days, a contract can be verified in
minutes.)
• Trust: Smart contracts automatically execute transactions following
predetermined rules, and the encrypted records of those transactions are
shared across participants.
• Security: Blockchain transaction records are encrypted, and that makes
them very hard to hack. Because each individual record is connected to
previous and subsequent records on a distributed ledger.
• Savings: Smart contracts remove the need for intermediaries because
participants can trust the visible data and the technology to properly
execute the transaction.
• (It saves money: there is no need to pay fees to any intermediaries (such
as lawyers, real estate).)
Solidity Programming
The following types are also called value types because variables of these types will
always be passed by value.
1. Boolean: The possible values are constants i.e., true or false (Keyword: Bool)
2. Integers: Signed and unsigned integers of various sizes. (Keyword: int/uint)
contract MySample
{
uint x =50;
}
3. Address: Holds a 20-byte value (size of an Ethereum address). Address types
also have members and serve as a base for all contracts.
Keyword: address
• Members of Addresses: Balance & Transfer:
• It is possible to query the balance of an address using the property balance and to
send Ether to an address using the transfer function.
address x = 0x123;
address myAddress = this;
if (x.balance < 10 && myAddress.balance > = 10)
x.transfer(10);
4. Strings: String literals are written with either double or single-quotes “foo” or
‘bar’.
string language = "Solidity";
Storage Locations in Solidity
• Storage: where all the contract state variables reside. Every contract has
its own storage and it is persistent between function calls.
• Memory: hold temporary values and gets erased between (external)
function calls and is cheaper to use.
• Stack: hold small local variables and is almost free to use, but can only
hold a limited amount of values
Operators
• Any text between a // and the end of a line is treated as a comment and is
ignored by Solidity Compiler.
}
Solidity - Special Variables
if (expression)
{
Statement(s) to be executed if expression is true
}
if...else statement
if (expression) {
Statement(s) to be executed if expression is true
} else {
Statement(s) to be executed if expression is false
}
Solidity - if...else if... statement.
if (expression 1) {
Statement(s) to be executed if expression 1 is true
} else if (expression 2) {
Statement(s) to be executed if expression 2 is true
} else if (expression 3) {
Statement(s) to be executed if expression 3 is true
} else {
Statement(s) to be executed if no expression is true
}
Solidity - While Loop
while (expression)
{
Statement(s) to be executed if expression is true
}
Solidity - do...while loop
do {
Statement(s) to be executed;
} while (expression);
Solidity - For Loop
View functions can read contract storage variables but cannot modify them. They provide a way to
retrieve information from the contract state without changing it.
pragma solidity ^0.8.0;
contract ViewExample {
uint private data;
Pure functions do not read or modify the state of the contract. Use pure functions for mathematical
calculations, conversions, and any logic that doesn't depend on external state.
contract PureExample {
function add(uint a, uint b) public pure returns (uint) {
return a + b;
}
}
• View Functions: Ensure that they will not modify the state. A function
can be declared as view.
• Pure Functions: Ensure that they not read or modify the state. A function
can be declared as pure.
pragma solidity ^0.5.0;
contract Test {
function getResult() public pure returns(uint product, uint sum){
uint a = 1;
uint b = 2;
product = a * b;
sum = a + b;
}
}
The return Statement
You can have multiple definitions for the same function name in the same
scope.
The definition of the function must differ from each other by the types
and/or the number of arguments in the argument list.
pragma solidity ^0.5.0;
contract Test {
function getSum(uint a, uint b) public pure returns(uint){
return a + b;
}
function getSum(uint a, uint b, uint c) public pure returns(uint){
return a + b + c;
}
function callSumWithTwoArguments() public pure returns(uint){
return getSum(1,2);
}
function callSumWithThreeArguments() public pure returns(uint){
return getSum(1,2,3);
}
}
Solidity - Arrays
Syntax:
type arrayName [ arraySize ];
Example: uint balance[10];
To declare an array of dynamic size in Solidity, the programmer specifies the
type of the elements as follows −
type[] arrayName;
Initializing Arrays
balance[2] = 5;
Creating dynamic memory arrays