0% found this document useful (0 votes)
7 views8 pages

BT Assignments1

ASSIGNMENTS1

Uploaded by

ghandatgaurav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

BT Assignments1

ASSIGNMENTS1

Uploaded by

ghandatgaurav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

ASSIGNMENT NO.

04
Experiment No 4

Title -

Write a program in solidity to create Student data. Use the following constructs:
1) Structures
2) Arrays
3) Fallback
Deploy this as a smart contract on Ethereum and Observe the transaction fee and Gas
values.

Objective -

To understand and explore the working of Blockchain technology and its application

Theory

• Solidity
Solidity is an object-oriented programming language for implementing smart
contracts on various blockchain platforms, most notably, Ethereum. It was developed
by Christian Reitwiessner, Alex Beregszaszi, and several former Ethereum core
contributors. Programs in Solidity run on Ethereum Virtual Machine.

Solidity was proposed in August 2014 by Gavin Wood; the language was later
developed by the Ethereum project's Solidity team, led by Christian Reitwiessner.
Solidity is the primary language on Ethereum as well as on other private blockchains,
such as the enterprise-oriented Hyperledger Fabric blockchain. SWIFT deployed a
proof of concept using Solidity running on Hyperledger Fabric.

• Solidity - Struct
Structs in Solidity allow 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; }
• Solidity - Arrays
In Solidity, an array can be of compile-time fixed size or of dynamic size. For storage
array, it can have different types of elements as well. In case of memory array,
element type can not be mapping and in case it is to be used as function parameter
then element type should be an ABI type.
All arrays consist of contiguous memory locations. The lowest address corresponds to
the first element and the highest address to the last element.
Declaring Arrays

To declare an array of fixed size in Solidity, the programmer specifies the type of the
elements and the number of elements required by an array as follows −
type arrayName [ arraySize ];

This is called a single-dimension array. The arraySize must be an integer constant


greater than zero and type can be any valid Solidity data type. For example, to
declare a 10-element array called balance of type uint, use this statement −
uint balance[10];

To declare an array of dynamic size in Solidity, the programmer specifies the type of
the elements as follows −
type[] arrayName;

Initializing Arrays

You can initialize Solidity array elements either one by one or using a single
statement as follows −
uint balance[3] = [1, 2, 3];

The number of values between braces [ ] can not be larger than the number of
elements that we declare for the array between square brackets [ ]. Following is an
example to assign a single element of the array −
If you omit the size of the array, an array just big enough to hold the initialization is
created. Therefore, if you write −
uint balance[] = [1, 2, 3];

You will create exactly the same array as you did in the previous example.
balance[2] = 5;
The above statement assigns element number 3rd in the array a value of 5.
Creating dynamic memory arrays

Dynamic memory arrays are created using new keywords.


uint size = 3;
uint balance[] = new uint[](size);

Accessing Array Elements

An element is accessed by indexing the array name. This is done by placing the index
of the element within square brackets after the name of the array. For example −
uint salary = balance[2];

The above statement will take 3rd element from the array and assign the value to
salary variable. Following is an example, which will use all the above-mentioned
three concepts viz. declaration, assignment and accessing arrays −

• Solidity – Fall Back Function


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.

Properties of a fallback function:


• Has no name or arguments.
• If it is not marked payable, the contract will throw an exception if it receives plain
ether without data.
• Can not return anything.
• Can be defined once per contract.
• It is also executed if the caller meant to call a function that is not available
• It is mandatory to mark it external.
• It is limited to 2300 gas when called by another function. It is so for as to make this
function call as cheap as possible.
Ethereum transaction fee

Ethereum transaction fees work differently in comparison to Bitcoin's. The fee takes
into account the amount of computing power needed to process a transaction, known
as gas. Gas also has a variable price measured in ether (ETH), the network's native
token.

While the gas needed for a specific transaction can stay the same, gas prices can rise
or fall. This gas price is directly related to network traffic. If you pay a higher gas
price, miners will likely prioritize your transaction.

How are Ethereum transaction fees calculated?

The total gas fee is simply a price that covers the cost, plus an incentive to process
your transaction. However, you should also consider the gas limit, which defines
what's the maximum price paid for that transaction or task.

In other words, the gas cost is the amount of work required, and the gas price is the
price paid for “each hour” of work. The relation between these two and the gas limit
defines the total fee for an Ethereum transaction or smart contract operation.

Let's pick a random transaction on Etherscan.io as an example. The transaction cost


21,000 gas, and the gas price was 71 Gwei. So, the total transaction fee was
1,491,000 Gwei or 0.001491 ETH.

As Ethereum makes its way towards a Proof of Stake model (see Casper), there is an
expectation that gas fees will decrease. The amount of gas needed to confirm a
transaction will be lower as the network will need only a fraction of the
computational power to validate transactions. But, network traffic can still affect
transaction fees as validators prioritize higher-paying transactions.
Program
//SPDX-License-Identifier: UNLICENSED

pragma solidity >= 0.7.0 <0.9.0;

// Build the Contract


contract MarksManagmtSys
{
// Create a structure for
// student details
struct StudentStruct
{
uint ID;
string fName;
string lName;
uint marks;
}

address owner;

uint public stdCount = 0;

//Create Array to store Student data


StudentStruct[] stdRecords;

constructor()
{
owner=msg.sender;
}

// Create a function to add


// the new records
function addNewRecords(uint _ID,
string memory _fName,
string memory _lName,
uint _marks) public payable
{
// Increase the count by 1
stdCount = stdCount + 1;

//Adding data into array


stdRecords.push(StudentStruct(_ID , _fName , _lName , _marks));

function getAllRecords() public view returns(StudentStruct[] memory)


{
return stdRecords;
}
}
Output

1) An analysis of the transaction fee and gas fee required for contract
deployment
2) An analysis of the transaction fee and gas fee required for smart contracts
transactions
Conclusion

Hence, we learned about the Basic Fundamentals of Solidity language and its
various attributes as well as the Ethereum transaction fee and Gas fee.

Questions

Q1) What is Solidity used for?


Q2) Which type of language is Solidity?
Q3) What types of applications can be developed using Solidity?
Q4) What are the main differences between Solidity and other programming
languages like Python, Java, or C++?
Q5) Why is there gas, more precisely?
Q6) What does the gas usage in a transaction depend on?
Q7) How is the transaction fee calculated?
Q8) If an execution of a Smart Contract costs less than the specified gas, does the
user get a refund?

You might also like