Module 3 - Programming For Blockchain
Module 3 - Programming For Blockchain
Blockchain
A "smart contract" is simply a program that runs on the Ethereum blockchain. It's a collection of
code (its functions) and data (its state) that resides at a specific address on the Ethereum blockchain.
A Smart Contract (or cryptocontract) is a computer program that directly and automatically
controls the transfer of digital assets between the parties under certain conditions. A smart
contract works in the same way as a traditional contract while also automatically enforcing the
contract. Smart contracts are programs that execute exactly as they are set up(coded,
programmed) by their creators. Just like a traditional contract is enforceable by law, smart
contracts are enforceable by code.
● The bitcoin network was the first to use some sort of smart contract by using them to
transfer value from one person to another.
● The smart contract involved employs basic conditions like checking if the amount of
value to transfer is actually available in the sender account.
● Later, the Ethereum platform emerged which was considered more powerful,
precisely because the developers/programmers could make custom contracts in a
Turing-complete language.
● It is to be noted that the contracts written in the case of the bitcoin network were
written in a Turing-incomplete language, restricting the potential of smart contracts
implementation in the bitcoin network.
● There are some common smart contract platforms like Ethereum, Solana, Polkadot,
Hyperledger fabric, etc.
conditions of the smart contract and they cannot be changed by one of the parties. A
smart contract is replicated and distributed by all the nodes connected to the network.
2. Deterministic: Smart contracts can only perform functions for which they are
designed only when the required conditions are met. The final outcome will not vary,
no matter who executes the smart contract.
3. Immutable: Once deployed smart contract cannot be changed, it can only be
between the parties. No intermediaries are involved which minimizes bullying and
grants full authority to the dealing parties. Also, the smart contract is maintained and
executed by all the nodes on the network, thus removing all the controlling power
from any one party’s hand.
5. Customizable: Smart contracts have the ability for modification or we can say
blockchain due to which the code is visible to everyone, whether or not they are
participants in the smart contract.
7. Trustless: These are not required by third parties to verify the integrity of the process
9. Self-enforcing: These are self-enforcing when the conditions and rules are met at all
stages.
There are legal guarantees for smart contracts. They follow the format seen in contracts: “If this
occurs, then this will occur.” Legal smart contracts provide more openness between contracting
entities than traditional documents because they are stored on blockchain and cannot be altered.
Contracts are executed by the parties using digital signatures. If certain conditions are met, such
as paying a debt when a predetermined date is reached, smart legal contracts may operate on
their own. If stakeholders don’t comply, there may be serious legal ramifications.
DAOs are democratic organisations with voting powers granted by a smart contract. A
decentralised autonomous organisation, or DAO, is a blockchain-based entity with a shared goal
under collective governance. There is no such thing as an executive or president. Instead, the
organization’s operations and the distribution of assets are governed by blockchain-based
principles that are incorporated into the contract’s code. One example of this kind of smart
contract is VitaDAO, which uses technology to power a community dedicated to scientific
inquiry.
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.
Figure :- working of smart contract
1. Real Estate: Reduce money paid to the middleman and distribute between the parties
track of vehicle maintenance and ownership. The smart contract can, for example,
enforce vehicle maintenance service every six months; failure of which will lead to
suspension of driving license.
3. Music Industry: The music industry could record the ownership of music in a
blockchain. A smart contract can be embedded in the blockchain and royalties can be
credited to the owner’s account when the song is used for commercial purposes. It can
also work in resolving ownership disputes.
4. Government elections: Once the votes are logged in the blockchain, it would be very
hard to decrypt the voter address and modify the vote leading to more confidence
against the ill practices.
5. Management: The blockchain application in management can streamline and
automate many decisions that are taken late or deferred. Every decision is transparent
and available to any party who has the authority(an application on the private
blockchain). For example, a smart contract can be deployed to trigger the supply of
raw materials when 10 tonnes of plastic bags are produced.
6. Healthcare: Automating healthcare payment processes using smart contracts can
prevent fraud. Every treatment is registered on the ledger and in the end, the smart
contract can calculate the sum of all the transactions. The patient can’t be discharged
from the hospital until the bill has been paid and can be coded in the smart contract.
blockchain and can be accessed along with the complete audit trail. However, the
parties involved can be secured cryptographically for full privacy.
2. Autonomy: There are direct dealings between parties. Smart contracts remove the
need for intermediaries and allow for transparent, direct relationships with customers.
3. Reduce fraud: Fraudulent activity detection and reduction. Smart contracts are
stored in the blockchain. Forcefully modifying the blockchain is very difficult as it’s
computation-intensive. Also, a violation of the smart contract can be detected by the
nodes in the network and such a violation attempt is marked invalid and not stored in
the blockchain.
4. Fault-tolerance: Since no single person or entity is in control of the digital assets,
one-party domination and the situation of one party backing out do not happen as the
platform is decentralized and so even if one node detaches itself from the network,
the contract remains intact.
5. Enhanced trust: Business agreements are automatically executed and enforced. Plus,
technology(and related technology like smart contracts, mining, and use cases like
cryptocurrency) makes these technologies difficult to oversee.
2. Difficult to implement: Smart contracts are also complicated to implement because
it’s still a relatively new concept and research is still going on to understand the smart
contract and its implications fully.
3. Immutable: They are practically immutable. Whenever there is a change that has to
be incorporated into the contract, a new contract has to be made and implemented in
the blockchain.
4. Alignment: Smart contracts can speed the execution of the process that span multiple
parties irrespective of the fact whether the smart contracts are in alignment with all
the parties’ intention and understanding.
structure of a smart contract, focusing on its key components and their purposes:
1. Pragma Directive
● Purpose: Specifies the compiler version to ensure compatibility and prevent issues with
code execution.
● Example:
2. Contract Declaration
● Purpose: Defines the smart contract as a distinct entity on the blockchain. Contracts can
hold data and logic.
● Example
3. State Variables
● Purpose: Store the contract’s data permanently on the blockchain. State variables define
the state of the contract.
● Characteristics:
○ Visibility: Can be public, private, or internal.
○ Data Types: Various types (e.g., uint, address, bool, string).
● Example
4. Events
● Purpose: Enable logging of significant actions or changes in the contract state. Events
help external applications (like UIs) to track contract activity.
● Characteristics: Events are emitted to the blockchain and can be indexed for efficient
searching.
● Example
5. Modifiers
● Purpose: Functions that can alter the behavior of other functions, often used for access
control or validations.
● Characteristics: They allow reusable code and can enforce conditions before executing a
function.
● Example
6. Constructor
● Purpose: A special function that initializes the contract when it is deployed. It can set
initial values for state variables.
● Characteristics: It runs only once and does not have a return type.
● Example
7. Functions
● Purpose: Define the contract's operations and logic. Functions can read from and modify
state variables.
● Characteristics:
○ Visibility: Can be public, private, internal, or external.
○ Return Types: Can return values or void.
● Example
8. Fallback Function
● Purpose: A special function that executes when the contract receives Ether or when a
function call does not match any existing functions.
● Characteristics: Useful for accepting payments or handling unexpected calls.
● Example
9. Access Control
● Purpose: Ensures that only certain addresses can execute specific functions, enhancing
security.
● Implementation: Often achieved using modifiers or role-based systems.
● Example: