You are on page 1of 9

Explain Smart Contract with Example

The smart contract is the software program implementing a set of rules or condition and
operates blockchain
This set of rules are used by different parties to the smart contract to transfer the digital assets
between them. A smart contract is a self-executing program based on if-then logic.
For example, vending machines are a ubiquitous presence in everyday life. It's also a simple
model of a smart contract: If someone inserts $2 and then presses B4, then the machine
dispenses the package of cookies held in the B4 slot. In other words, if the vending machine
receives the required item of value, it performs the requested action

How do smart contracts work?


A smart contract is just a digital contract with the security coding of the blockchain.
It has details and permissions written in code that require an exact sequence of events to take
place to trigger the agreement of the terms mentioned in the smart contract.
It can also include the time constraints that can introduce deadlines in the contract.
Every smart contract has its address in the blockchain. The contract can be interacted with by
using its address presuming the contract has been broadcasted on the network.
The idea behind smart contracts is pretty simple. They are executed on a basis of simple logic,
IF-THEN for example:
IF you send object A, THEN the sum (of money, in cryptocurrency) will be transferred to you.
IF you transfer a certain amount of digital assets (cryptocurrency, for example, ether, bitcoin),
THEN the A object will be transferred to you.
IF I finish the work, THEN the digital assets mentioned in the contract will be transferred to me.

What are the types of Smart contracts?


Smart Legal Contracts
These contracts are legally enforceable and require the parties to fulfill their contractual
obligations. Failure to do so may result in strict legal actions against them.
Decentralized Autonomous Organizations
These are blockchain communities that are bound to specific rules coded into blockchain
contracts combined with governance mechanisms. Hence, any action taken by the community
members gets replaced by a self-enforcing code.
Application Logic Contracts
These contracts contain an application-based code that remains in sync with other blockchain
contracts. It enables communication across different devices, such as the merger of the Internet
of Things with blockchain technology.
Smart contracting device
IOT in combination with the blockchain talks about the AIC which contains an application based
code that remains in-step with other blockchain contracts .they allow communication across
various devices and enable the merging of the IOT with blockchain technology

What is metamask?
Metamask is a browser plugin that hold an ethereum wallet and connects the computer with the
ethereum network. It can connect and network with other providers who offer free others to
accounts created in wallets of metamask.for instance an account created in metamask can be
connected with an external network named ropsten test network which can then inject free
ethers to a corresponding account
Demonstrate how to create and initialize dynamic arrays using any example in solidity?
In solidity to declare an array of dynamic size the programmer has to specify the type of the
elements as follows:
type[]arrayName;
Dynamic memory arrays are created using new keyboard
unit size = 3;
unit account[] = new unit[](size);
Following single statement initializes Solidity array elements
unit account[3] = [10,20,30];
Following is an example to assign a single element of the array:
account[2]=5;
Following is an example to assign element no 3rd in the array a value of 5
unit salary = account [2];
The above statement assigns the value of third element in array to salary value

What are the advantages and disadvantages of E-voting via blockchain?


Advantages- disadvantage-
enhanced security immutability of data
Accurate and faster election process voter should be aware about how to use and access web
Less expenditure for election process voters have to provide personal information
Faster and accurate counting of votes voter should have internet facility available

What is public blockchain? Enlist advantages and disadvantages of it.


A public blockchain does not have restrictions. Anyone with an internet connection can get
access to the network and start validating blocks and sending transactions.
Also known as permissionless blockchains, public blockchains are completely open Bitcoin and
Ethereum are both examples of public blockchains. Anyone in the network can access the chain
and add blocks. Public blockchains are also largely anonymous, unlike private blockchains,
where the identity of the people involved in the transaction is not kept hidden.
Some of the advantages of public blockchain are:
1. No one person has control over the data
2. It can be used in public sectors like healthcare and education
3. It is immutable meaning once an entry is made on the blockchain and verified
by validators then it cannot be deleted or modified. Validation of an entry is
done by the majority agreeing that it is a valid entry.
4. It is decentralized, using peer to peer network of computers.
Some of the disadvantages of public blockchain are:
1. Consensus Mechanism: Some public blockchain like Bitcoin uses Proof of
Work consensus mechanism where the participants need to solve a complex
mathematical puzzle to validate a transaction. It requires the consumption of a
lot of resources which is a costly affair.
2. In public blockchains, one doesn’t need to prove his/her identity and just
commit your processing power to become a part of the network.
3. Speed: This is one of the biggest problems with some public blockchain like
bitcoin which process only 4.6 transactions per second whereas companies like
Visa process 1700 transactions per second.
Explain Inheritance in Solidity using example
Inheritance is one of the most important features of the object-oriented programming language.
It is a way of extending the functionality of a program,used to separate the code, reduces the
dependency, and increases the re-usability of the existing code.Solidity supports inheritance
between smart contracts where multiple contracts can be inherited into a single contract.The
contract from which other contracts inherit features known as a base contract while the contract
which inherits the features is called a derived contract. They are referred as parent-child
contracts.scope of inheritance in Solidity is limited to public and internal modifiers only.
1. Single Inheritance
In Single or single level inheritance the functions and variables of one parent contract are
inherited to only one child contract.
2. Multi-level Inheritance
It is very similar to single inheritance, but the difference is that it has levels of the relationship
between the parent and the child. The child contract derived from a parent also acts as a parent
for the contract which is derived from it.
3. Hierarchical Inheritance
In Hierarchical inheritance, a parent contract has more than one child contracts. It is mostly
used when a common functionality is to be used in different places.
4. Multiple Inheritance
In Multiple Inheritance, a single contract can be inherited from many contracts. A parent contract
can have more than one child while a child contract can have more than one parent.
pragma solidity ^0.8.2;
contract C {
unit private data ;
unit private info;
constructor() public{
info=10;
}
Function increment(unit a) private pure returns (unit) { return a+1; }
Function updateData(unit a) public {data = a; }
Function getData() public view returns(unit){ return Data; }
function component( unit a , unit b ) internal pure returns (unit) { returns a+b;) }
contract E is C {
unit private result;
C private c;
constructor {} public {
c = new C ();
}
function getComputedResult() public{
result=compute (3,5);
}
function getResult () public view returns(unit){ return result; }
function getData () public view returns(unit){ return c.info(); }
}
Compare ethereum and bitcoin
Parameter Bitcoin Ethereum

1 Concept Digital money Smart contracts

2 Algorithm SHA-256 Ethash

3 Blocks time 10 minutes 12-14 second

4 Market cap $18 billion $1 billion

5 Release date 9 jan 2008 30 July 2015

6 Symbol BTC ETH

7 Purpose Bitcoin is a digital currency Ethereum is a token capable of


facilitating smart contracts

8 Transaction 7 transaction per second 30 transaction per second


throughput

9 Consensus time 1hour 3minutes

10 Assets Bitcoin Ether

What is constructor in solidity?


A constructor is a special method in any object-oriented programming language which gets
called whenever an object of a class is initialized. It is totally different in case of Solidity, Solidity
provides a constructor declaration inside the smart contract and it invokes only once when the
contract is deployed and is used to initialize the contract state. A default constructor is created
by the compiler if there is no explicitly defined constructor.
Creating a constructor
A Constructor is defined using a constructor keyword without any function name followed by
an access modifier. It’s an optional function which initializes state variables of the contract. A
constructor can be either internal or public, an internal constructor marks contract as abstract.
Syntax:constructor() <Access Modifier> { }
pragma solidity ^0.5.0;
contract constructorExample {
string str;
constructor() public {
str = "GeeksForGeeks";
}
function getValue(
) public view returns (
string memory) {
return str;
}
}
What are Components of Ethereum Network?
Component-1 :
Nodes – There are two types of nodes in an Ethereum network. They are as follows.
Mining Node – These nodes are responsible for writing all the transactions that have occurred
in the Ethereum network in the block.
Ethereum Virtual Machine Node – These are the nodes in the Ethereum network in which
Smart Contracts (it is a type of contract between supporter and developer in which there are a
set of rules based on which both the parties agree to interact with each other. The agreement
will be automatically executed when the predefined rules are met.) are implemented. By default,
this node utilizes a 30303 port number for the purpose of communication
Component-2 :
Ether – Ether is a type of cryptocurrency used in the Ethereum network just like a bitcoin is used
in a blockchain network. It is a peer-to-peer currency, similar to Bitcoin. It tracks and promotes
each transaction in the network.It is the second-largest cryptocurrency in the world. The first one
is Bitcoin. Other cryptocurrencies can be used to get ether tokens, but vice versa is not true.
It means that ether tokens can’t be interchanged by other cryptocurrencies to render computing
power for Ethereum transactions. Ether is paid as a commission for any execution that affects
the state in Ethereum.It is used in the Ethereum algorithm as an incentive for miners who
connect blocks to the blockchain using a proof-of-work method.
It is the only currency that can be used to pay transaction costs, which go to miners as well. The
block reward, as well as transaction fees, provide miners with an opportunity to keep the
blockchain rising.Aside from paying for transactions, ether is often used to purchase gas, which
is used to pay for the computation of any transaction on the Ethereum network.
Component-3 :
Gas – Gas is an internal currency of the Ethereum network. We need gas to run applications on
the Ethereum network, much as we need gas to run a vehicle.
To complete every transaction on the Ethereum network, a consumer must first make a payment
send out ethers and the intermediate monetary value is known as gas. Gas is a unit of
measurement on the Ethereum network for the computing power used to execute a smart
contract or a transaction.The price of gas is very low compared to Ether. The execution and
resource utilization costs are predetermined in Ethereum in terms of Gas units, called gwei.
Component-4 :
Ethereum Accounts – There are two types of Ethereum accounts. They are as follows.
Externally owned account – These accounts are used to store transactions.
Contract account – name itself suggests, these accounts store the details of Smart Contracts.
Component-5 :
Nonce – For externally owned accounts, nonce means the number of transactions via this
account. For a contract account, nonce means the number of contracts generated via this
account.
Component-6 :
Storage Root – It is the main root node of a Merkle tree. Hash of all details of the account is
stored here. The root of the Merkle tree is the verification of all transactions.
What is a transaction in Ethereum?
a blockchain is basic building block is a transaction currency is transferred from one address to
another during a transaction
a transaction is a piece of cryptographically signed instruction issue by externally owned
account serialized and then uploaded who have option
On Ethereum a transaction is a data packet that has been digitally sign using a private key and
contents the instruction that when followed either lead to a message call on the establishment of
a contract

What is a block in ethereum?


the block is the basic building block of a blockchain that stores transaction
every block in a blockchain will have a block header and multiple transaction
ethereum progressively switching from its current proof of work consensus process to proof of
stake since proof of work consumes too much power it is not environmentally beneficial proof of
stake is Quikr than group of work and may mine blocks according to how much money the minor
has ethereum block contains a number of significant pieces of information including the difficulty
timestamp preceding block hash and merkly tree based root hash
Ethereum block header -
Parent hash nonce Timestamp ommersHash

number gasLimit gasUsed mixhash

Beneficiary logsBloom difficulty extraData


Timestamp: times time represents the time when block mining ends
block number: counting how many blocks there are the number increases in steps in genesis
block is 0
difficulty: the value of difficulty indicates how challenging is to locate a hash. the effort required
to mind the block
mix hash: a unique identifier for that block / A 256- bit hash that pair with the nonce confirms
that enough computational work has been completed
parent hash: block hash store in the previous block
beneficiary: block fee and transaction fee destination address
extra data: A 32byte array containing data related to this block can be left empty
gasUsed: indicates the total quantity of gas utilise in all transaction in this block
gasLimit: the current maximum amount of gas that maybe spent per block
Define usage of mist wallet?
Create contract Test consists of a structure Book, and functions are defined to set and get
values of the elements of the structure
pragma solidity ^0.5.0;
// Creating a contract
contract test {
// Declaring a structure
struct Book {
string name;
string writter;
uint id;
bool available;
}
// Declaring a structure object
Book book1;
// Assigning values to the fields
// for the structure object book2
Book book2
= Book("Building Ethereum DApps",
"Roberto Infante ",
2, false);
// Defining a function to set values
// for the fields for structure book1
function set_book_detail() public {
book1 = Book("Introducing Ethereum and Solidity",
"Chris Dannen",
1, true);
}
// Defining function to print
// book2 details
function book_info(
)public view returns (
string memory, string memory, uint, bool) {
return(book2.name, book 2.writer,
book2.id, book2.available);
}
// Defining function to print
// book1 details
function get_details(
) public view returns (string memory, uint) {
return (book1.name, book1.id);
}
}
Create contract Types consist of an enumerator week_days, and functions are defined to set
and get the value of a variable of the type enumerator
pragma solidity ^0.5.0;
// Creating a contract
contract Types {
// Creating an enumerator
enum week_days
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
// Declaring variables of
// type enumerator
week_days week;
week_days choice;
// Setting a default value
week_days constant default_value
= week_days.Sunday;
// Defining a function to
// set value of choice
function set_value() public {
choice = week_days.Thursday;
}
// Defining a function to
// return value of choice
function get_choice(
) public view returns (week_days) {
return choice;
}
// Defining function to
// return default value
function getdefaultvalue(
) public pure returns(week_days) {
return default_value;
}
}

You might also like