0% found this document useful (0 votes)
15 views

Blockchain Development

Uploaded by

gunabalar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Blockchain Development

Uploaded by

gunabalar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

BLOCKCHAIN DEVELOPMENT

PART-A
Section-A (2 X 5 = 10)

1. Define smart contract. Give an example.


A smart contract is a self-executing contract with predefined rules and conditions, written in
code, and deployed on a blockchain. It automatically enforces and executes these rules
without the need for intermediaries.

Example: In blockchain development, a smart contract can be used for a decentralized


application (DApp) like a decentralized exchange (DEX).

2. Write the syntax for creating contract and structure in solidity.


The simplified syntax for creating a contract and its structure in Solidity, a programming
language used for smart contracts in blockchain development:
Syntax:
pragma solidity >=0.4.0 <0.7.0;
contract SimpleStorage {
uint storedData;
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint) {
return storedData;
}
}

3. List out the solidity data types.


In blockchain development using Solidity, the commonly used data types include:
uint: Unsigned integer types with various bit sizes (e.g., uint8, uint256).
int: Signed integer types with various bit sizes (e.g., int8, int256).
bool: Boolean type, representing true or false.
address: A 20-byte Ethereum address.
bytes: A dynamic byte array (e.g., bytes, bytes32).
string: A dynamically sized UTF-8 encoded string.
mapping: A data structure for creating key-value pairs.
struct: Custom user-defined data structures.
enum: User-defined enumerated types.

These data types are used to define variables and structures within Solidity smart contracts.

4. What is EVM and its uses in solidity?


EVM stands for Ethereum Virtual Machine. It's the decentralized computing environment that
executes smart contracts written in Solidity and other languages on the Ethereum blockchain.
EVM plays a crucial role in ensuring consensus across the network and validating
transactions, enabling decentralized applications (DApps) to run without relying on
centralized servers.

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.

5. Common Use Case*:


- Storing lists of data, such as addresses, balances, or historical records.
- Maintaining queues, stacks, or other data structures.
- Managing arrays of user-defined structs.

6. Example:

// Dynamic array of integers


uint[] public dynamicArray;

// Fixed-size array of addresses


address[5] public fixedArray;

// Initializing a dynamic array


function initializeDynamicArray() public {
dynamicArray.push(10);
dynamicArray.push(20);
}

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:

enum State { Created, InProgress, Completed }

State public currentState;

function setStateInProgress() public {


currentState = State.InProgress;
}

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;
}

Person public myPerson;

function createPerson(string memory _name, uint _age) public {


myPerson = Person(_name, _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;

// Dynamic array of strings


string[] public dynamicArray;

// Function to initialize the fixed-size array


function initializeFixedArray() public {
fixedArray[0] = 10;
fixedArray[1] = 20;
fixedArray[2] = 30;
fixedArray[3] = 40;
fixedArray[4] = 50;
}

// Function to add elements to the dynamic array


function addToDynamicArray(string memory element) public {
dynamicArray.push(element);
}

// Function to get the length of the dynamic array


function getDynamicArrayLength() public view returns (uint) {
return dynamicArray.length;
}
}

Here's what this Solidity smart contract does:

 It defines two arrays:


 `fixedArray`: A fixed-size array of integers with a length of 5.
 `dynamicArray`: A dynamic array of strings.
 The `initializeFixedArray()` function initializes the `fixedArray` with values
10, 20, 30, 40, and 50.
 The `addToDynamicArray(string memory element)` function allows you to
add elements to the `dynamicArray`.
 The `getDynamicArrayLength ()` function returns the current length of the
`dynamicArray`.

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;
}

function setParentData(uint _newValue) public {


parentData = _newValue;
}

function getParentData() public view returns (uint) {


return parentData;
}
}

// Child contract inheriting from Parent


contract Child is Parent {
uint public childData;

constructor(uint _initialValue, uint _childInitialValue) Parent(_initialValue) {


childData = _childInitialValue;
}

function setChildData(uint _newValue) public {


childData = _newValue;
}

function getChildData() public view returns (uint) {


return childData;
}
}

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.

Here's how you can interact with these contracts:

// Deploy the Child contract with initial values


Child childContract = new Child(100, 200);

// Access and modify parentData from the Child contract


childContract.setParentData(150);
uint parentValue = childContract.getParentData(); // Retrieves 150

// Access and modify childData from the Child contract


childContract.setChildData(250);
uint childValue = childContract.getChildData(); // Retrieves 250

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.

pragma solidity ^0.8.0;

contract StateVariableExample {
// State variable with public visibility
uint256 public stateVar1;

// State variable with private visibility


address private owner;

// Constructor to initialize state variables


constructor() {
stateVar1 = 42;
owner = msg.sender;
}

// Function to modify the state variable


function setStateVar(uint256 newValue) public {
require(msg.sender == owner, "Only owner can modify");
stateVar1 = newValue;
}
}

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.

pragma solidity ^0.8.0;

contract LocalVariableExample {
function calculateSum(uint256 a, uint256 b) public pure returns (uint256) {
// Local variables to store the sum
uint256 sum = a + b;
return sum;
}

function checkEven(uint256 number) public pure returns (bool) {


// Local variable to store the result of the check
bool isEven = number % 2 == 0;
return isEven;
}
}

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

You might also like