Blockchain Technology - 4
Blockchain Technology - 4
Date of Completion:
Problem Definition:
Write a program in solidity to create Student data. Use the following constructs:
• Structures
• Arrays
• Fallback
Deploy this as smart contract on Ethereum and Observe the transaction fee and Gas
values.
Theory:
Solidity:
Key Features:
Statically Typed: Solidity requires variable types to be defined at compile time, which
helps catch errors early in the development process.
Smart Contract Focused: Solidity is specifically designed for writing smart contracts,
which are self-executing contracts with the terms directly written into code. These
contracts facilitate, verify, or enforce the negotiation or performance of a contract.
Constructs:
1. Structures
Definition: Structures (or structs) are custom data types that group related variables
under a single name. They allow developers to create complex data types by
combining different value types.
Eg:
struct StructName {
dataType1 fieldName1;
dataType2 fieldName2;
// ... more fields
}
2. Arrays
Definition: Arrays are collections of elements of the same data type. Solidity supports
both fixed-size and dynamic arrays.
Types:
Fixed-size arrays: The size is defined at compile time.
Dynamic arrays: The size can change at runtime.
Eg:
dataType[] public dynamicArray; // Dynamic array
dataType[5] public fixedArray; // Fixed-size array
3. Fallback Function
Definition: The fallback function is a special function that does not have a name and
does not take any arguments. It is executed when a contract receives Ether and no
other function matches the call.
Characteristics:
Cannot have a return value.
Can be used to handle plain Ether transfers.
Only one fallback function is allowed per contract.
Eg:
fallback() external payable {
// Code to execute when Ether is sent to the contract without matching function
}
4. Receive Function
Definition: The receive function is a specific type of fallback function that is called
when a contract receives Ether and the call data is empty. It allows contracts to react
specifically to Ether transfers.
Characteristics:
Can only be defined once in a contract.
Must be marked as payable.
Eg:
receive() external payable {
// Code to execute when Ether is sent directly to the contract
}
5. Events
Definition: Events allow contracts to log information on the blockchain, which can be
listened to by external applications. Events provide a way to communicate that
something has happened in the contract.
Usage: They are particularly useful for tracking changes in contract state or for
notifying clients of important actions.
Eg:
event EventName(dataType1 indexed param1, dataType2 param2);
Code:
Conclusion:
Thus, we have successfully understood how to write the solidity code for creating a
smart contract simulating student database.