Blockchain Development
Blockchain Development
PART-A
Section-A (2 X 5 = 10)
These data types are used to define variables and structures within Solidity smart contracts.
1
5. Differentiate between ERC20 and ERC721.
ERC20 ERC721
Fungible: All tokens are identical and Non-Fungible: Each token is unique and
interchangeable. not interchangeable.
Common Use: Used for Common Use: Used for non-fungible
cryptocurrencies, utility tokens, and tokens (NFTs) representing unique
assets with uniform value. assets like digital art and collectibles.
Example: DAI, USDC. Example: CryptoKitties, digital art NFTs.
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
2
PART-B
Section-B (4 X 10 = 40)
1. Explore the features of array, enum and struct in solidity.
Certainly, let's delve into more detail about the features and usage of arrays, enums, and
structs in Solidity:
Arrays:
1. Definition and Types:
- Arrays in Solidity are collections of elements, all of the same data type.
- They can be either of fixed size (static) or dynamic (variable size).
- Arrays can be one-dimensional or multi-dimensional (arrays of arrays).
2. Accessing Elements:
- Array elements are accessed using an index, starting from 0.
- Elements can be retrieved, updated, or iterated through in loops.
3. Dynamic Arrays:
- Dynamic arrays allow the size to change during contract execution.
- You can add elements using the `push () ` function and remove elements using `pop()`.
4. Initialization:
- You can initialize an array with default values or leave it empty.
- For dynamic arrays, it's common to initialize them as empty and populate them later.
6. Example:
3
Enums:
1. Definition:
- Enums (enumerated types) are user-defined data types that consist of a finite set of named
values.
- They provide a way to represent discrete and limited choices or states.
2. Declaration:
- You define an enum using the `enum` keyword followed by the possible values in braces.
3. Use Cases:
- Representing states or status in a contract.
- Creating options or choices for user interactions.
4. Example:
Structs:
1. Definition:
- Structs are user-defined data structures that allow you to group related data variables
together under a single name.
- They are similar to classes or records in other programming languages.
2. Declaration:
- You define a struct using the `struct` keyword followed by the fields and their data types.
3. Usage:
- Structs are useful for organizing and managing complex data.
- They can be used as a template to create instances (objects) with multiple fields.
4. Example:
struct Person {
string name;
uint age;
}
4
In Solidity, arrays, enums, and structs are essential for data storage, organizing contract logic,
and representing meaningful data structures within smart contracts. They provide flexibility
and clarity to your contract code, allowing you to work with various data types and structures
effectively.
5
2. Write a program to demonstrate how to create and initialize fixed size & dynamic size
arrays.
Certainly, I'll provide you with a simple Solidity smart contract that demonstrates how to
create and initialize both fixed-size and dynamic-size arrays. In this example, we'll use two
functions to initialize these arrays:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract ArrayExample {
// Fixed-size array of integers with a length of 5
int[5] public fixedArray;
6
You can deploy this contract on an Ethereum-compatible blockchain, and then interact with it
using functions like `initializeFixedArray()`, `addToDynamicArray()`, and
`getDynamicArrayLength()` to observe how the arrays are initialized and modified.
7
3. Write a solidity programming using inheritance and functions.
Certainly! Inheritance is a key feature in Solidity that allows you to create new smart
contracts by reusing and extending the functionality of existing ones. In this example, we'll
create a simple inheritance structure with two contracts, `Parent` and `Child`, and demonstrate
how to use functions and inheritance in Solidity.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// Parent contract
contract Parent {
uint public parentData;
constructor(uint _initialValue) {
parentData = _initialValue;
}
8
In this example:
1. We define a `Parent` contract with a state variable `parentData`, a constructor to set its
initial value, and two functions: `setParentData` to modify `parentData` and `getParentData`
to retrieve its value.
2. We create a `Child` contract that inherits from `Parent` using `is Parent`. This means that
`Child` inherits all the state variables and functions from `Parent`.
3. The `Child` contract has its own state variable `childData`, a constructor that sets both
`parentData` and `childData`, and two functions: `setChildData` to modify `childData` and
`getChildData` to retrieve its value.
This demonstrates the use of inheritance to create a `Child` contract that inherits the
functionality of the `Parent` contract while adding its own state variables and functions.
9
4. Explain in detail about state and local variables with its syntax.
In Solidity, variables are classified into two main categories: state variables and local
variables. They serve different purposes and have distinct characteristics in the context of
blockchain development. Here, we'll explain each type in detail, along with their syntax and
use cases.
State Variables:
1. Definition:
- State variables are declared at the contract level, outside of any functions.
- They are stored on the blockchain and have persistent storage across function calls and
transactions.
- Changes to state variables are recorded on the blockchain, incurring gas costs for
transactions.
2. Syntax:
- To declare a state variable, use the `storage` or `memory` keyword (if applicable) followed
by the data type and variable name.
- You can also specify the visibility (e.g., `public`, `internal`, `private`) and other modifiers.
3. Use Cases:
- Storing data that needs to persist across transactions.
- Representing contract state and configuration.
- Typically used for balances, ownership, settings, and contract state.
contract StateVariableExample {
// State variable with public visibility
uint256 public stateVar1;
10
Local Variables:
1. Definition:
- Local variables are declared within a function and exist only within the scope of that
function.
- They do not persist across function calls and are not stored on the blockchain.
- Changes to local variables are temporary and do not incur gas costs.
2. Syntax:
- Declare local variables within a function by specifying the data type and variable name.
3. Use Cases:
- Temporary storage for calculations or intermediate results.
- Input validation and processing within functions.
- Data manipulation within a specific function.
contract LocalVariableExample {
function calculateSum(uint256 a, uint256 b) public pure returns (uint256) {
// Local variables to store the sum
uint256 sum = a + b;
return sum;
}
In summary, state variables in Solidity are declared at the contract level, have persistent
storage on the blockchain, and retain their values across transactions. On the other hand, local
variables are declared within functions, exist only temporarily, and do not persist between
function calls. Both types of variables serve important roles in smart contract development,
depending on the need for data storage and manipulation.
11