Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

R009 Aryan Bondgulwar

Laboratory Manual
Blockchain Technology

Academic Year 2023 - 2024


B.Tech and MBA Tech AI Semester
VI
R009 Aryan Bondgulwar

Experiment 5
PART A
(PART A: TO BE REFERRED BY STUDENTS)
Lab 8 - Part 1
1. What is Solidity, and why is it commonly used for smart contract development?
2. Explain the basic structure of a Solidity smart contract for fund transfer.
3.What are the key components of a fund transfer smart contract in Solidity?
4. Describe the role of the payable modifier in Solidity and its significance in fund transfer contracts.
5.How do you handle errors and exceptions in a Solidity fund transfer contract?
6. Explain the difference between address. transfer() and address.send() functions in Solidity and
when to use each in fund transfer contracts.
7.What are fallback functions in Solidity, and how can they be utilized in fund transfer contracts?
8. Discuss the importance of security considerations in the development of fund transfer smart
contracts in Solidity.
9. Explain how gas fees work in Ethereum and how they relate to fund transfer transactions in
Solidity contracts.
10. Describe potential scalability challenges associated with fund transfer smart contracts on the
Ethereum blockchain and possible solutions.

Lab 8 Part 2:
1. Write a Solidity smart contract that allows users to transfer Ether from one address to another.
Include functions for depositing Ether into the contract and withdrawing Ether from the contract.
Extend the ABOVE basic fund transfer contract to include a timed withdrawal feature. Allow users to
schedule fund transfers to occur at a specified future time. 2. Develop a Solidity smart contract that
enables conditional fund transfers. Users should be able to specify conditions under which the transfer
will occur, such as reaching a certain block number or meeting specific criteria.
Example Solution:
pragma solidity ^0.8.0; contract ConditionalFundTransfer {
function transfer(address _to, uint256 _amount) public {
require(block. number > 100000, "Transfer not allowed before block 100000");
// Additional conditions can be added here
payable(_to).transfer(_amount);
}
}
Explanation: transfer: Allows users to transfer Ether to another address, but only after a certain block
number (in this example, 100000).
Conditional Transfer: The transfer function enables users to transfer Ether to another address, but only
if the condition specified in the function is met (require(block.number > 100000, "Transfer not
allowed before block 100000")).
Block Number Condition: In this example, the condition requires that the current block number
(block.number) must be greater than 100000 for the transfer to be allowed.
This contract allows users to specify conditions under which fund transfers will occur, adding
flexibility to the transfer functionality. 3. Implement a multi-signature wallet contract in Solidity
where funds can only be transferred if multiple parties (specified by their addresses) provide their
consent. 4. Create an escrow smart contract in Solidity that holds funds until predefined conditions
are met. Once the conditions are fulfilled, the funds are released to the designated recipient;
otherwise, they are returned to the sender.
R009 Aryan Bondgulwar

PART B
(PART B: TO BE COMPLETED BY STUDENTS)

Students must submit the soft copy as per the following segments within two hours of the
practical. The soft copy must be uploaded to the portal at the end of the practical.

The filename should be: BT_Batch_RollNo_ExperimentNo

Roll No.: R009 Name: Aryan Bondgulwar

Prog/Yr/Sem: III Semester VI Batch: B1

Date of Experiment: Date of Submission:

Lab 8 - Part 1
1. What is Solidity, and why is it commonly used for smart contract development?
Solidity is a programming language specifically designed for writing smart contracts that run on
blockchain platforms, with Ethereum being one of the most prominent examples. It is a
statically-typed language influenced by C++, Python, and JavaScript. Solidity is used to define the
rules and behaviors of smart contracts, which are self-executing contracts with the terms of the
agreement directly written into code.
Solidity is commonly used for smart contract development because:
● Ethereum Compatibility: It is the primary language for developing smart contracts on the
Ethereum blockchain, which is one of the most widely used blockchain platforms.
● Sophisticated Features: Solidity supports various features required for smart contracts, such
as inheritance, libraries, and user-defined types.
● Security Focus: Solidity is designed to prioritize security, given the irreversible and
transparent nature of blockchain transactions.

2. Explain the basic structure of a Solidity smart contract for fund


transfer.

// SPDX-License-Identifier: MIT pragma


solidity ^0.8.0;

contract FundTransfer {
// State variables address
public owner;
mapping(address => uint256) public balances;

// Events
event Transfer(address indexed from, address indexed to, uint256 amount);

// Constructor
constructor() {
R009 Aryan Bondgulwar

owner = msg.sender;
}

// Modifiers
modifier onlyOwner() {
require(msg.sender == owner, "Only the owner can call this function");
_;
}

// Function
function deposit() public payable {
// Deposit funds to the sender's balance
balances[msg.sender] += msg.value;

// Emit a Transfer event


emit Transfer(address(0), msg.sender, msg.value);
}

function withdraw(address to, uint256 amount) public onlyOwner {


// Check if the contract has sufficient funds require(amount
<= balances[to], "Insufficient funds");

// Update the balance and transfer funds balances[to] -


= amount; payable(to).transfer(amount);

// Emit a Transfer event


emit Transfer(msg.sender, to, amount);
}
}
R009 Aryan Bondgulwar

1. Pragma Directive:
- `pragma solidity ^0.8.0;`: Specifies the version of the Solidity compiler to be used.
2. Contract Declaration:
- `contract FundTransfer {`: Declares the start of the smart contract named `FundTransfer`.
3. State Variables:
- `address public owner;`: Declares a state variable to store the address of the contract owner.
- `mapping(address => uint256) public balances;`: Declares a mapping to associate
addresses with their balances.
4. Events:
- `event Transfer(address indexed from, address indexed to, uint256 amount);`: Declares
an event that will be emitted whenever funds are transferred.
5. Constructor:
- `constructor() { owner = msg. sender; }`: Initializes the contract by setting the creator's
address as the owner.
6. Modifier:
- `modifier onlyOwner() { require(msg.sender == owner, "Only the owner can call this
function");
_; }`: Defines a modifier `onlyOwner` to restrict certain functions to be called only by the
contract owner.
7. Functions:
- `function deposit() public payable { ... }`: Allows users to deposit funds to their balance.
- `function withdraw(address to, uint256 amount) public onlyOwner { ... }`: Allows the
owner to withdraw funds from the contract to a specified address.
8. Closing Bracket:
- `}`: Marks the end of the smart contract.

Q3.What are the key components of a fund transfer smart contract in Solidity?
State Variables: In the example, owner and balances are state variables. owner represents the
contract owner, and balance is a mapping of addresses to their respective balances.
● Constructor: The constructor initializes the contract with the address of the
creator as the owner.
● Events: The Transfer event is emitted whenever funds are deposited or withdrawn,
providing a transparent record of these actions.
● Modifiers: The onlyOwner modifier restricts certain functions to be called
only by the contract owner, enhancing security.
● Functions: The deposit function allows users to add funds to their balance, and the
withdraw function enables the contract owner to transfer funds from the contract to a
specified address.
R009 Aryan Bondgulwar

Q4.Describe the role of the payable modifier in Solidity and its significance in fund
transfer contracts.
The `payable` modifier in Solidity indicates that a function can receive Ether. In fund transfer
contracts, it allows users to send Ether when depositing funds and facilitates the receipt and
transfer of cryptocurrency within the contract. It is essential for handling transactions
involving Ether in smart contracts.
Role:
● When a function is marked as payable, the function can receive Ether as part
of the transaction that invokes it.
● The function can receive Ether through the msg.value keyword, which
represents the amount of Ether sent with the transaction.
Significance in Fund Transfer Contracts:
● Deposit Functionality: In fund transfer contracts, the payable modifier is
commonly used in functions that allow users to deposit funds into their
account within the smart contract. Users can send Ether along with the
deposit transaction, and the payable function will handle the reception of
funds.
● Sending Ether to the Contract: For a fund transfer contract, the contract
itself may need to receive Ether for various purposes, such as holding
balances or facilitating transactions. The contract owner might need to
send Ether to the contract for it to have sufficient funds for withdrawals.
● Transfer of Ether to Other Addresses: In a fund transfer contract, when
transferring funds from one account to another, the transfer function may
involve the payable modifier to facilitate the transfer of Ether between the
sender and the recipient.

Q5.How do you handle errors and exceptions in a Solidity fund transfer


contract?
1. Use `require` statements to validate conditions.
2. Employ the `revert` function to revert the contract state on error.
3. Implement custom modifiers for common error-checking logic.
4. Explore try-catch blocks in Solidity 0.8.0 and above.
5. Emit events for transparent and auditable logs of contract interactions.
6. Design functions to return status codes or values indicating success or failure.
7. Implement a fallback function to handle Ether sent to the contract without a match.
8. Consider gas costs and prioritize efficiency in error-handling strategies.

Q6.Explain the difference between address.transfer() and address.send() functions in


Solidity and when to use each in fund transfer contracts.
R009 Aryan Bondgulwar

Q7.What are fallback functions in Solidity, and how can they be utilized in fund
transfer contracts?
Fallback functions in Solidity are special functions that are executed when a contract
receives Ether without a specific function call or when a contract is the target of a simple
Ether transfer.
Fallback functions have the following
characteristics: No Function Signature:
● Fallback functions don't have a name or parameters, and they are identified
solely by the fallback keyword.
Payable by Default:
● Fallback functions are automatically marked as payable, allowing them to
receive Ether.
Gas Limit:
● Fallback functions have a gas limit of 2300, making them suitable
for basic operations but insufficient for complex tasks.
Fallback functions can be useful in fund transfer contracts for various
purposes: Handling Incoming Ether:
● Fallback functions can be used to handle Ether sent to the contract without a
specific function call.
● This is useful for logging the receipt of funds or updating contract state
based on incoming Ether.
Automatic Deposits:
● Users can send Ether directly to the contract address, triggering the fallback
function to handle the deposit automatically.
Simple Transfer Operations:
● Basic transfer operations, such as updating a balance or emitting an event,
can be performed within the fallback function.

Q8.Discuss the importance of security considerations in the development of fund


transfer smart contracts in Solidity.
R009 Aryan Bondgulwar

1. Reentrancy Attacks:
- Guard against reentrancy by using patterns like reentrancyGuard.
2. Gas Limitations:
- Be cautious with gas limits; avoid complex logic to prevent incomplete operations.
3. Denial-of-Service (DoS) Attacks:
- Implement gas limits and prevent resource exhaustion in functions.
4. Fallback Function Security:
- Safely handle incoming Ether in fallback functions; avoid complex logic.
5. Access Control:
- Use modifiers for access control; restrict critical functions to authorized users.
6. Input Validation:
- Validate and sanitize inputs to prevent malicious behavior.
7. Secure Randomness:
- Be cautious with randomness; consider external solutions for critical needs.
8. Event Logging:
- Log critical actions with events for transparency and auditing.
9. Code Audits:
- Conduct thorough code audits or engage third-party auditors for vulnerability checks.
10. Upgradability Considerations:
- Ensure secure upgrade mechanisms to maintain integrity.
11. Fallback to Known Standards:
- Consider established libraries for common functionalities (e.g., OpenZeppelin).
12. Documentation and Communication:
- Clearly document contract functionality and communicate security considerations.
R009 Aryan Bondgulwar

Q9.Explain how gas fees work in Ethereum and how they relate to fund transfer
transactions in Solidity contracts.
In Ethereum, gas fees are the costs associated with executing operations and transactions on
the Ethereum network. Gas is a unit that measures the computational work done by the
Ethereum Virtual Machine (EVM) when processing transactions or smart contract functions.
Every operation in Ethereum consumes a certain amount of gas, and users need to pay for
these gas costs in Ether (ETH).

1. Gas Units: Measure of work in Ethereum transactions.


2. Gas Price: Cost per gas unit, denoted in Gwei.
3. Transaction Cost: Gas consumed multiplied by gas price.
4. Gas Limit: Maximum gas a user is willing to pay.
5. Ethereum Miners: Process transactions, and prioritize based on gas price.
6. Gas Costs for Fund Transfers: Operations in fund transfer contracts consume gas.
7. Gas Efficiency: Optimize code for lower transaction costs.
8. Fallback Functions: May consume gas in fund transfer contracts.
9. Gas Price Strategy: Adjust for faster processing.
10. Gas Limit Setting: Prevent transaction failures with appropriate limits.
11. Consideration of Gas Fees: Users and developers factor in gas fees in design.

Q10. Describe potential scalability challenges associated with fund transfer smart
contracts on the Ethereum blockchain and possible solutions.
Scalability Challenges:

1. Network Congestion:
- High demand leading to delays and increased gas prices.
2. Gas Costs:
R009 Aryan Bondgulwar

- Expensive gas fees for transactions, impacting fund transfers.


3. Throughput Limitations:
- Ethereum's limited throughput affects scalability.

Possible Solutions:

1. Layer 2 Solutions:
- Implement Optimistic Rollups or zk-rollups for offloading transactions.
2. Ethereum 2.0 Upgrades:
- Await Ethereum 2.0 for improved scalability.
3. Optimized Smart Contracts:
- Optimize fund transfer contracts for gas efficiency.
4. Batch Transactions:
- Consolidate fund transfers into batched transactions.
5. Off-Chain Solutions:
- Use off-chain solutions like state channels for frequent transfers.
6. Gas Price Predictors:
- Utilize gas price predictors for optimal fee adjustments.
7. Hybrid Solutions:
- Explore on-chain/off-chain hybrid models.

Lab 8 Part 2

1. Write a Solidity smart contract that allows users to transfer Ether from one address
to another. Include functions for depositing Ether into the contract and withdrawing
Ether from the contract. Extend the ABOVE basic fund transfer contract to include a
timed withdrawal feature. Allow users to schedule fund transfers to occur at a specified
future time.

2. Develop a Solidity smart contract that enables conditional fund transfers. Users
should be able to specify conditions under which the transfer will occur, such as
reaching a certain block number or meeting specific criteria.
Example Solution:
pragma solidity ^0.8.0;
contract ConditionalFundTransfer {
function transfer(address _to, uint256 _amount) public
{ require(block.number > 100000, "Transfer not allowed before block
R009 Aryan Bondgulwar

100000");
// Additional conditions can be added here
R009 Aryan Bondgulwar

payable(_to).transfer(_amount);
}
}
Explanation:
transfer: Allows users to transfer Ether to another address, but only after a certain block
number (in this example, 100000).
Conditional Transfer: The transfer function enables users to transfer Ether to another address,
but only if the condition specified in the function is met (require(block.number > 100000,
"Transfer not allowed before block 100000")).
Block Number Condition: In this example, the condition requires that the current block number
(block.number) must be greater than 100000 for the transfer to be allowed.
This contract allows users to specify conditions under which fund transfers will occur,
adding flexibility to the transfer functionality.

3. Implement a multi-signature wallet contract in Solidity where funds can only be


transferred if multiple parties (specified by their addresses) provide their consent.
R009 Aryan Bondgulwar

4. Create an escrow smart contract in Solidity that holds funds until predefined
conditions are met. Once the conditions are fulfilled, the funds are released to the
designated recipient; otherwise, they are returned to the sender.

You might also like