0% found this document useful (0 votes)
60 views16 pages

Smart Contracts Unit4

Smart contracts allow transactions to be automatically executed when predetermined conditions are met. There are two types of accounts on Ethereum - externally owned accounts controlled by private keys and contract accounts controlled by their code. Transactions are executed by the Ethereum Virtual Machine which runs the code of smart contracts. Precompiled contracts provide optimized implementations of complex operations like elliptic curve operations.

Uploaded by

Shiraz Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views16 pages

Smart Contracts Unit4

Smart contracts allow transactions to be automatically executed when predetermined conditions are met. There are two types of accounts on Ethereum - externally owned accounts controlled by private keys and contract accounts controlled by their code. Transactions are executed by the Ethereum Virtual Machine which runs the code of smart contracts. Precompiled contracts provide optimized implementations of complex operations like elliptic curve operations.

Uploaded by

Shiraz Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Smart Contracts

Ethereum has two kinds of accounts, the externally owned account, and the contract account. Externally
Owned Account (EOA) is a kind of account controlled by private keys and without any code associated
with it. A Contract Account (CA) is an account assigned to a smart contract, controlled by contract. An
externally owned account can send a transaction, which can be a transfer transaction or a transaction
related to a smart contract to create or trigger a smart contract.

Each transaction of Ethereum will be converted into a Message object, which is passed to the EVM for
execution, and then EVM will convert the Message object into a Contract object. If it is a normal
transfer transaction, the corresponding account balance will be modified in StateDB. If it is a
transaction that creates or invokes a smart contract, the bytecode is loaded and executed by the
interpreter in the EVM, and the StateDB may be queried or modified during execution. As you can see
from the figure, the Contract object loads the corresponding contract code from the database according
to the address of the contract, and then sends it to the interpreter for execution.

Precompiled Contracts

Concept

Because the EVM is a stack-based virtual machine, it calculates gas based on the content of the
operation, so if it involves very complicated calculations, it can be very inefficient to execute the
operation in EVM and consume a lot of gas. For example, in zk-snark, the addition and subtraction of
the elliptic curve and the pairing operation are required. These operations are very complicated and
unrealistic to be executed in the EVM. This is the original intention of Ethereum to propose a
precompiled contract.

Precompiled contracts are a compromise used in the EVM to provide more complex library functions
(usually used for complex operations such as encryption, hashing, etc.) that are not suitable for writing
in opcode. They are applied to contracts that are simple but frequently called, or that are logically fixed
but computationally intensive. Precompiled contracts are implemented on the client-side with client
code, and because they do not require the EVM, they run fast. It also costs less for developers than
using functions that run directly in the EVM.

The precompiled contracts that Ethereum has implemented now are as follows:

At the code level, the so-called address is actually the index of the contract array, and an index
identifies a precompiled contract. The three precompiled contracts related to the confidential assets are
bn256Add(), bn256ScalarMul(), bn256Pairing().
Accounts -

There are 2 forms of accounts on the Ethereum blockchain: An Externally-Owned Account (EOA) and
a Contract Account.

An externally-Owned Account (EOA) is a kind of account on the Ethereum blockchain where any
user can create the account for free to store, send, and receive tokens or ETH built on the Ethereum
blockchain like ERC-20 tokens. The action of sending or receiving cryptocurrency assets requires
outside source action.

A Contract Account is a kind of account on the Ethereum blockchain that only completes actions
where certain conditions are met.

the EVM uses a set of opcode instructions to execute specific tasks. These (140 unique) opcodes allow
the EVM to be Turing-complete, which means the EVM is able to compute just about anything, given
enough resources.

Consensus mechanism

Ethereum and Bitcoin used to use the same consensus protocol for validating data and adding it to the
blockchain – known as proof-of-work (PoW). This involves mining nodes competing against one
another using energy-intensive machines to win the right to add the next block to the blockchain. This
happens roughly once every 10 minutes.

However, in 2022, Ethereum underwent a major transition known as "The Merge" which migrated the
network to a proof-of-stake blockchain (PoS.)

Instead of requiring mining nodes to run expensive equipment to discover new blocks, the new PoS
system requires users to deposit and lock away 32 ether – the native cryptocurrency of Ethereum (see
below) – to become network validators.
Ethereum Clients:

To use the Ethereum network, you need to have an Ethereum client. An Ethereum client is software
that allows you to interact with the Ethereum network. It is your gateway to the Ethereum network.
There are different types of Ethereum clients. The most popular ones are Geth and Parity. Geth is the
Go implementation of the Ethereum client. Parity is the Rust implementation of the Ethereum client.
There are also other clients like Aleth and Trinity.

Each client has its advantages and disadvantages. Geth is the most popular client. It is easy to use, and
it has a lot of features. Parity is more lightweight, and it is faster than Geth. Aleth is more focused on
security. Trinity is still in development. You can choose any client you want. It would be best if you
used a client you are comfortable with. If you are still deciding which client to use, you can try out
different ones and see which one you like the most.

What is Solidity?

Solidity is a smart contract programming language for the Ethereum blockchain. It was developed by
the Ethereum Foundation to enable developers to build decentralized applications (dapps) on the
Ethereum blockchain. Solidity is a statically typed, contract-oriented programming language. C++,
Python, and JavaScript influence it. Solidity is designed to target the Ethereum Virtual Machine
(EVM). Solidity is used to write smart contracts, self-executing contracts that live on the Ethereum
blockchain. A smart contract is like a traditional contract, but it is written in code and stored on the
blockchain. Solidity is a compiler that translates Solidity code into Ethereum Virtual Machine (EVM)
bytecode. The EVM is a decentralized platform that runs smart contracts. Solidity is still under active
development and still needs to be stable. However, it is already being used by several projects,
including Augur, Gnosis, and Maker.

Smart Contacts and Flight Insurance

Let's consider a real-life scenario in which smart contracts are used. Rachel is at the airport, and her
flight is delayed. AXA, an insurance company, provides flight delay insurance utilizing
Ethereum smart contracts. This insurance compensates Rachel in such a case. How? The smart
contract is linked to the database recording flight status. The smart contract is created based on terms
and conditions.
The condition set for the insurance policy is a delay of two hours or more. Based on the code, the
smart contract holds AXA's money until that certain condition is met. The smart contract is submitted
to the nodes on EMV (a runtime compiler to execute the smart contract code) for evaluation. All the
nodes on the network executing the code must come to the same result. That result is recorded on the
distributed ledger. If the flight is delayed in excess of two hours, the smart contract self-executes, and
Rachel is compensated. Smart contracts are immutable; no one may alter the agreement.

Smart contracts are simply programs stored on a blockchain that run when predetermined conditions
are met

How smart contracts work

Smart contracts work by following simple “if/when…then…” statements that are written into code on
a blockchain. A network of computers executes the actions  when predetermined conditions have been
met and verified. These actions could include releasing funds to the appropriate parties, registering a
vehicle, sending notifications, or issuing a ticket. The blockchain is then updated when the transaction
is completed. That means the transaction cannot be changed, and only parties who have been granted
permission can see the results.

Steps to Execute Solidity Smart Contract using Remix IDE


Remix IDE is generally used to compile and run Solidity smart contracts. Below are the steps for the
compilation, execution, and debugging of the smart contract. 
 

Step 1: Open Remix IDE on any of your browsers, select on the New File and click on Solidity to
choose the environment. 
Step 2: Write the Smart contract in the code section, and click the Compile button under the
Compiler window to compile the contract. 
Step 3: To execute the code, click on the Deploy button under Deploy and Run Transactions
window. 
Step 4: After deploying the code click on the method calls under the drop-down of deployed
contracts to run the program, and for output, check to click on the drop-down on the console.

Step 5: For debugging click on the Debug button corresponding to the method call in the console.
Here you can check each function call and variable assignments. 

How does Ethereum work for applications?

When a transaction triggers a smart contract, all nodes of the network execute every instruction. To do
this, Ethereum implements an execution environment on the blockchain called the Ethereum Virtual
Machine (EVM). All nodes on the network run the EVM as part of the block verification protocol. In
block verification, each node goes through the transactions listed in the block they are verifying and
runs the code as triggered by the transactions in the EVM. All nodes on the network do the same
calculations to keep their ledgers in sync. Every transaction must include a gas limit and a fee that the
sender is willing to pay for the transaction. Miners have the choice of including the transaction and
collecting the fee or not. If the total amount of gas needed to process the transaction is less than or
equal to the gas limit, the transaction is processed. If the gas expended reaches the gas limit before the
transaction is completed, the transaction does not go through and the fee is still lost. All gas not used
by transaction execution is reimbursed to the sender as Ether. This means that it's safe to send
transactions with a gas limit above the estimates.

 Externally Owned Accounts (User accounts is controlled by private keys)

 Contracts Accounts (controlled by code)


How many types of Ethereum accounts are there?

 Two different types of Ethereum accounts exist. The first is called an externally owned account
(EOA). The second is called a contract account. Externally owned accounts are controlled by
anyone with the private keys. Contract accounts are smart contracts deployed to the network
and controlled by code. Both types are identified by an Ethereum address. (1) Externally
owned accounts (EOA)
 EOAs are controlled by users. This control occurs often through software such as a wallet
application. Externally owned accounts are simple accounts without any associated code or
data storage. This type of Ethereum account is controlled by and cryptographically signed
using a private key in the "real world."

(2) Contract accounts (smart contracts)


 A contract account is controlled by code executed by the Ethereum Virtual Machine. It is also
referred to as a smart contract. Contract accounts have associated code and data storage, but
not private keys. They "control themselves.” These accounts do so in the way determined by
their smart contract code.
 Externally Owned Account is abbreviated as EOA. This is the type of account that is owned
by people on the Ethereum network.

 An individual makes an account on Ethereum and a public-private key pair is generated for
the account. The account is not identified by the person’s name or any personal information.

 The private key of EOA gives control and access to one’s assets and contracts. The private key is
kept secured by the user.
 While, as the name suggests, the public key of the account is open. This key acts as the identity of
the account.

 The public key is generated from the private key via a one-way cryptographic function.

Consensus mechanism(Mining)

Ethereum and Bitcoin used to use the same consensus protocol for validating data and adding it to the
blockchain – known as proof-of-work (PoW). This involves mining nodes competing against one
another using energy-intensive machines to win the right to add the next block to the blockchain. This
happens roughly once every 10 minutes.
However, in 2022, Ethereum underwent a major transition known as "The Merge" which migrated the
network to a proof-of-stake blockchain (PoS.)

Instead of requiring mining nodes to run expensive equipment to discover new blocks, the new PoS
system requires users to deposit and lock away 32 ether – the native cryptocurrency of Ethereum (see
below) – to become network validators.

Smart Contacts and Flight Insurance

Let's consider a real-life scenario in which smart contracts are used. Rachel is at the airport, and her
flight is delayed. AXA, an insurance company, provides flight delay insurance utilizing
Ethereum smart contracts. This insurance compensates Rachel in such a case. How? The smart
contract is linked to the database recording flight status. The smart contract is created based on terms
and conditions.

The condition set for the insurance policy is a delay of two hours or more. Based on the code, the
smart contract holds AXA's money until that certain condition is met. The smart contract is submitted
to the nodes on EMV (a runtime compiler to execute the smart contract code) for evaluation. All the
nodes on the network executing the code must come to the same result. That result is recorded on the
distributed ledger. If the flight is delayed in excess of two hours, the smart contract self-executes, and
Rachel is compensated. Smart contracts are immutable; no one may alter the agreement.

Smart contracts are simply programs stored on a blockchain that run when predetermined conditions
are met

How smart contracts work

Smart contracts work by following simple “if/when…then…” statements that are written into code on
a blockchain. A network of computers executes the actions  when predetermined conditions have been
met and verified. These actions could include releasing funds to the appropriate parties, registering a
vehicle, sending notifications, or issuing a ticket. The blockchain is then updated when the transaction
is completed. That means the transaction cannot be changed, and only parties who have been granted
permission can see the results.
 The private key of EOA gives control and access to one’s assets and contracts. The private key is
kept secured by the user.

 While, as the name suggests, the public key of the account is open. This key acts as the identity of
the account.

 The public key is generated from the private key via a one-way cryptographic function.

1. Nonce
2. Balance
3. CodeHash
4. StorageRoot

1)Nonce:

 The nonce of an account is actually a counter that tells us about the number of transactions sent from
that account for an externally owned account.

 While, for a contract account, this counter indicates the number of contracts created by that account.

2.Balance:

 This property shows the balance of the account as the name suggests.

 The balance is actually in Wei, a unit of ether (ETH) where 1 eth = 1x10e18 Wei.

3.CodeHash:

 This field gives the hash value of the contract code of the account.

 In the case of contract accounts, the smart contract code is hashed and the resulting value is stored in
this field.

 While for externally owned accounts, this field contains the hash value of an empty string.
4. StorageRoot

 The data of an account is stored in the world state trie.

 Each leaf of this trie represents one account.

 The storageRoot contains the hash value of the root of the storage trie.

 This field of account is empty by default.

Smart Contracts Vs. Traditional Contract Systems

In conventional contract systems, you sign an agreement, then you trust and hire a third party for its
execution. The problem is that in this type of process, data tampering is possible. With smart contracts,
the agreement is coded in a program.

A centralized authority does not verify the result; it is confirmed by the participants on the Ethereum
blockchain-based network. Once a contract is executed, the transaction is registered and cannot be
altered or tampered, so it removes the risk of any data manipulation or alteration.

Let’s take an example in which someone named Zack has given a contract of $500 to someone named
Elsa for developing his company’s website. The developers code the agreement of the smart contract
using Ethereum’s programming language.

The smart contract has all the conditions (requirements) for building the website. Once the code is
written, it is uploaded and deployed on the Ethereum Virtual Machine (EVM).

EVM is a runtime compiler to execute a smart contract. Once the code is deployed on the EVM, every
participant on the network has a copy of the contract. When Elsa submits the work on Ethereum for
evaluation, each node on the Ethereum network will evaluate and confirm whether the result given by
Elsa has been done as per the coding requirements.

Once the result is approved and verified, the contract worth $500 will be self-executed, and the
payment will be paid to Elsa in ether. Zack’s account will be automatically debited, and Elsa will be
credited with $500 in ether.
The Ethereum tutorial video includes a demo on the deployment of an Ethereum smart contract.

Ethereum is a decentralized blockchain platform that establishes a peer-to-peer network that


securely executes and verifies application code, called smart contracts. Smart contracts allow
participants to transact with each other without a trusted central authority

1. The cryptocurrency – A piece of code representing some digital assets.


2. The Blockchain – The underlying framework upon which the currency functions.
Ethereum is a one-of-its-kind programmable blockchain.

Real estate

In the traditional world, real estate brokers are a necessary evil. Considering the act of selling a house
is nothing short of long and convoluted, owners will hire a broker to manage the confusing parts for
them, such as the paperwork and finding a buyer. While that sounds ideal for the seller, remember that
brokers take a significant fee of the house’s sell price.

A smart contract can take the place of a broker, streamlining the house-transfer process while ensuring
it’s just as secure as with an intermediary. This is where the “trustless” moniker comes into play.

Imagine the deed to your house is tokenized on the Ethereum blockchain. If you’re ready to sell it,
you’d create a smart contract with the buyer. That contract would hold the deed in escrow until the
buyer’s funds are properly submitted. Then, and only then, it will be released.

Everyone wins. The seller saves money as they don’t have to pay an intermediary and the buyer gets
the house much sooner than they would have otherwise.

Ethereum Gas

Just like we need fuel to run a car, we need gas to run applications on the Ethereum network. To
perform any transaction within the Ethereum network, a user must make a payment, in this case
paying out ethers, to get a transaction done, and the intermediary monetary value is called gas.
On the Ethereum network, gas is a unit that measures the computational power required to run a smart
contract or a transaction. So, if you must do a transaction that updates the blockchain, you would have
to shell out gas, and that gas costs ethers.

In Ethereum, the transaction fees are calculated using a formula (see screenshot below). For every
transaction, there is gas and its correlated gas price. The transaction fees equal the amount of gas
required to execute a transaction multiplied by the gas price. “Gas limit” refers to the amount of gas
used for the computation and the amount of ether a user is required to pay for the gas.

Below is a screenshot from the Ethereum network showing the transaction cost. You can see for this
particular transaction, the gas limit was 21,000, the gas used by the transaction was 21,000, and the
gas price was 21 Gwei, which is the lowest denomination of ether. So, 21 Gwei * 21,000 gave the
actual transaction fees: 0.000441 ethers, or about 21 cents as of today. As mentioned, the transaction
fee goes to the miner, who has validated the transaction.

To understand the gas limit and price, let’s consider an example using a car. Suppose your vehicle has
a mileage of 10 kilometers per liter and petrol costs $1 per liter. Under these parameters, driving a car
for 50 kilometers would cost you five liters of petrol, which is worth $5. Similarly, to perform an
operation or to run code on Ethereum, you need to obtain a certain amount of gas, like petrol, and the
gas has a per-unit price, called gas price.

If the user provides less than the amount of gas to run an operation, then the process will fail, and the
user will be given the message “out of gas.” And Gwei, as noted above, is the lowest denomination of
ether used for measuring a unit of a gas price.

The smallest unit of ETH is called a wei, which is equivalent to 10^-18 ETH. In other words, one ETH
is equal to 1,000,000,000,000,000,000 wei — or one wei is equal to 0.000000000000000001 ETH.

You might also like