BT Practical Writeup 4
BT Practical Writeup 4
5 5 5 5 20
Group C
Assignment No: 4
Title of the Assignment: 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.
Objective of the Assignment: Students should be able to learn about Solidity. Its datatypes and
implementations.
Prerequisite:
1. Basic Programming Logic
2. Basic knowledge of Solidity
Department of Computer Engineering Course : Laboratory Practice III
---------------------------------------------------------------------------------------------------------------
Contents for Theory:
1. Solidity - Arrays
2. Solidity - Structures
3. Solidity – Fallback
4. Implementation
---------------------------------------------------------------------------------------------------------------
Creating an Array
To declare an array in Solidity, the data type of the elements and the number of elements
should be specified. The size of the array must be a positive integer and data type should
be a valid Solidity type
Syntax:
Fixed-size Arrays :
The size of the array should be predefined. The total number of elements should not exceed the size
of the array. If the size of the array is not specified then the array of enough size is created which
is enough to hold the initialization.
Dynamic Array:
The size of the array is not predefined when it is declared. As the elements are added the size of
array changes and at the runtime, the size of the array will be determined.
Array Operations :
2. Solidity – Structures :
Structs in Solidity allows you to create more complicated data types that have multiple
properties. You can define your own type by creating a struct.
They are useful for grouping together related data.
Structs can be declared outside of a contract and imported in another contract. Generally,
it is used to represent a record. To define a structure struct keyword is used, which creates
a new data type.
Syntax:
struct <structure_name> {
<data type> variable_1;
<data type> variable_2;
}
For accessing any element of the structure, ‘dot operator’ is used, which separates the struct
variable and the element we wish to access. To define the variable of structure data type
structure name is used.
3. Solidity – Fallback :
The solidity fallback function is executed if none of the other functions match the
function identifier or no data was provided with the function call. Only one unnamed
function can be assigned to a contract and it is executed whenever the contract receives
plain Ether without any data. To receive Ether and add it to the total balance of the contract,
the fallback function must be marked payable. If no such function exists, the contract
cannot receive Ether through regular transactions and will throw an exception.
4. Implementation
Code -
pragma solidity ^0.5.0;
contract Crud {
struct User {
uint id;
string name;
}
User[] public users;
uint public nextId = 0;
function Create(string memory name) public {
users.push(User(nextId, name));
nextId++;
}
function Read(uint id) view public returns(uint, string memory) {
for(uint i=0; i<users.length; i++) {
if(users[i].id == id) {
return(users[i].id, users[i].name);
}
}
}
function Update(uint id, string memory name) public {
for(uint i=0; i<users.length; i++) {
if(users[i].id == id) {
users[i].name =name;
}
}
}
function Delete(uint id) public {
delete users[id];
}
function find(uint id) view internal returns(uint) {
for(uint i=0; i< users.length; i++) {
if(users[i].id == id) {
return i;
}
}
// if user does not exist then revert back
revert("User does not exist");
}
}
Output –
Step 2 – After the Successful compilation, Deploy the contract to see the output.
Step 3 - Now you can check the output. You can insert, update and delete the student data
using your smart contract.
Step 4 – After entering your data, you can read the data using ID.
Conclusion-
In this way we have created array, structure and used fallback function in solidity.
Assignment Question:
Reference link
● https://www.geeksforgeeks.org/solidity-arrays/?ref=lbp
● tutorialspoint.com/solidity/solidity_structs.htm