BT Assignments1
BT Assignments1
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 ];
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
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 −
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.
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.
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
address owner;
constructor()
{
owner=msg.sender;
}
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