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

Solidity Basic

The document discusses where mappings are stored in Solidity based on where they are declared. It states that if a mapping is declared inside a function or contract with the memory keyword, it will be stored in memory temporarily. However, if it is declared outside of any function or contract, it will be stored permanently on the blockchain.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Solidity Basic

The document discusses where mappings are stored in Solidity based on where they are declared. It states that if a mapping is declared inside a function or contract with the memory keyword, it will be stored in memory temporarily. However, if it is declared outside of any function or contract, it will be stored permanently on the blockchain.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

In Solidity, the location of a mapping is specified by the programmer when it is declared.

If a mapping is declared inside a function or a contract with the memory keyword, then it will be stored
in memory. For example:

function addStudent(uint id, string memory name) public {

mapping (uint => Student) students;

students[id] = Student(name);

In the above code, the students mapping is declared inside the addStudent function with the memory
keyword. This means that the students mapping will be stored in memory.

On the other hand, if a mapping is declared outside of any function or contract, then it will be stored on
the blockchain. For example:

contract Enrollment {

mapping (uint => Student) students;

mapping (string => Course) courses;

function addStudent(uint id, string memory name) public {

students[id] = Student(name);

In the above code, the students and courses mappings are declared as state variables of the Enrollment
contract. Since they are declared outside of any function or constructor, they will be stored on the
blockchain.

In summary, the location of a mapping in Solidity depends on where it is declared and whether the
memory keyword is used. If a mapping is declared inside a function or a contract with the memory
keyword, then it will be stored in memory. If it is declared outside of any function or contract, then it
will be stored on the blockchain.

--=============Memory Variable====
In Solidity, variables can be stored in different locations, such as storage, memory, or the stack.
In the addStudent method of the Students Course Enrollment System smart contract, the string
variable _name is declared as a memory variable.

memory is a temporary storage area in Solidity that is used to hold variables during function
execution. Memory variables are typically used for dynamic data types like strings and arrays,
which have variable sizes and are stored in memory rather than on the blockchain.

When a memory variable is declared in a Solidity function, the variable is allocated in memory
for the duration of the function execution. This means that the variable is not stored on the
blockchain and is destroyed when the function execution completes.

In the addStudent method, the _name parameter is a string variable, which is a dynamic data
type that can have variable lengths. By declaring _name as a memory variable, the function
allocates temporary memory to store the string during function execution.

In summary, memory is a temporary storage area in Solidity that is used to hold variables during
function execution, and in the addStudent method, _name is declared as a memory variable to
store the student's name during function execution.

The line of code require(bytes(courses[_code].code).length != 0, "Course does not exist"); is checking


whether a Course object exists in the courses mapping with a given _code.

Here's how this code works:

courses[_code] retrieves the Course object associated with the given _code from the courses mapping.

courses[_code].code retrieves the code field of the Course object.

bytes(courses[_code].code) converts the code field to a bytes type.

.length retrieves the length of the bytes type.

!= 0 checks if the length is not equal to zero.

If the length is equal to zero, the error message "Course does not exist" is returned by the require
statement, and the transaction is reverted.
In other words, this line of code is checking if a Course object exists in the courses mapping with the
given _code, and if not, it will revert the transaction with an error message. This is a helpful check to
ensure that a Course object actually exists before attempting to perform any operations on it, such as
adding or removing a student from the course.

In Solidity, there is no built-in way to check whether a string is empty or not, and attempting to
check the length of an empty string can result in an error. However, by converting the string to a
bytes type first, we can safely check its length, even if it is empty.

In Solidity, require is a built-in function that is used to check for conditions that must
be true for a function to execute successfully. It is similar to the assert statement in
Java, but with a few key differences.

The require function takes a single boolean argument and an optional error message.

The expression keccak256(bytes(students[_id].courses[i])) is a hash function that


takes the i-th element of the courses array of the Student object with the given _id,
converts it to a bytes type, and computes its keccak256 hash value.

Here's how this code works:

1. students[_id] retrieves the Student object associated with the given _id from the
students mapping.
2. students[_id].courses[i] retrieves the i-th element of the courses array of the
Student object.
3. bytes(students[_id].courses[i]) converts the i-th element of the courses array to
a bytes type.
4. keccak256(bytes(students[_id].courses[i])) computes the keccak256 hash value
of the bytes type.

The keccak256 hash function is a cryptographic hash function that takes an input
message of any length and produces a fixed-size, 256-bit hash value. The keccak256
function is often used in Solidity to compute a unique identifier for a particular piece of
data.
In this specific case, the expression keccak256(bytes(students[_id].courses[i])) is
being used to compute a unique identifier for a course that a particular student is
enrolled in. This unique identifier can be used to track the student's enrollment status
and ensure that they are not double-enrolled in the same course

students[_id].courses.push(_code) is a Solidity statement that appends a new element


_code to the end of the courses dynamic array of the Student object associated with the
given _id.

In other words, this code adds a new course to the list of courses that a particular student is
enrolled in. The courses array is dynamic, which means that its size can grow or shrink at
runtime as new elements are added or removed. The push() function is a built-in Solidity
function for appending a new element to the end of a dynamic array.

n Solidity, arrays can either be fixed-size or dynamic.

A fixed-size array has a specific, fixed length that is determined at the time of its
declaration. For example, the following statement declares a fixed-size array of 5
integers:

A dynamic array, on the other hand, has a variable length that can be changed at
runtime. The length of a dynamic array can be increased or decreased by appending or
removing elements. Dynamic arrays are declared using the [] notation, without
specifying a size. For example, the following statement declares a dynamic array of
strings:

In the case of string[] courses, the courses array is a dynamic array of strings. This
means that the length of the array can be changed at runtime by appending or
removing elements.

The getStudent function is a Solidity function that retrieves a Student object from the
students mapping based on the given _id, and returns it to the caller. Here's how this
code works:

1. The function takes a single argument _id, which is a uint (unsigned integer) value
representing the ID of the student that the caller wants to retrieve.
2. The function is declared as public, which means that it can be called by any external
account on the blockchain.
3. The function is declared as view, which means that it doesn't modify the state of the
contract and only reads data from it.
4. The function returns a Student object by value, which means that it creates a copy of
the object and returns it to the caller. The memory keyword indicates that the returned
object should be stored in memory instead of in storage.
5. The function retrieves the Student object associated with the given _id from the
students mapping by simply calling students[_id]. Since students is a mapping,
Solidity automatically returns a default Student object if the _id doesn't exist in the
mapping.
6. The function returns the retrieved Student object to the caller.

In summary, the getStudent function is a simple read-only function that allows external
accounts to retrieve a Student object from the students mapping based on the
student's ID. The function doesn't modify the state of the contract and is declared as
view to indicate this.

In Solidity, there are four types of functions:

1. External functions: These functions can be called from outside the contract by external
accounts, such as other contracts or user accounts. They are declared using the
external keyword.
2. Public functions: These functions can be called both from outside and inside the
contract. They are typically used to expose contract functionality to external accounts.
Public functions are declared using the public keyword.
3. Internal functions: These functions can only be called from within the contract, or from
contracts that inherit from the contract. They are used to encapsulate internal logic or
helper functions. Internal functions are declared using the internal keyword.
4. Private functions: These functions can only be called from within the contract. They are
used to encapsulate internal logic or helper functions that are not intended to be called
externally or by inheriting contracts. Private functions are declared using the private
keyword.

It's important to note that functions in Solidity can also be declared as view, pure, or
payable, depending on their behavior:
1. View functions: These functions don't modify the state of the contract and only read
data from it. They are typically used for querying the contract's state. View functions are
declared using the view keyword.
2. Pure functions: These functions don't read or modify the state of the contract and only
perform computations based on their inputs. They are typically used for utility functions
that don't depend on the contract's state. Pure functions are declared using the pure
keyword.
3. Payable functions: These functions can receive ether (the native cryptocurrency of the
Ethereum blockchain) from external accounts. They are typically used for functions that
require payment, such as buying tokens or transferring ether. Payable functions are
declared using the payable keyword.

By combining these function types and behavior modifiers, Solidity provides a rich set of
tools for implementing complex smart contract logic.

State mutability specifiers in Solidity are used to indicate whether a function modifies
the state of the contract or not. There are four state mutability specifiers in Solidity:

1. Pure: Functions declared as pure do not read or modify the state of the contract. They
are used for functions that perform computations based solely on their input
parameters.
2. View: Functions declared as view do not modify the state of the contract, but they can
read from it. They are used for functions that retrieve and return data from the contract.
3. Payable: Functions declared as payable can receive ether (the native cryptocurrency of
the Ethereum blockchain) as part of a transaction. They are typically used for functions
that accept payments or transfers of ether.
4. Non-payable: Functions that do not have any state mutability specifier or are declared
as non-payable are assumed to be able to modify the state of the contract. They can
also be used to retrieve data from the contract, but it is not recommended to use them
for this purpose, as it can lead to unnecessary gas consumption.

By specifying the state mutability of a function, you can help prevent accidental
modifications to the contract state and make it clear to other developers how the
function should be used.
if a function doesn't have any state mutability specifier, it is assumed to be non-payable. This
means that the function can modify the state of the contract, but it cannot receive ether as part
of a transaction.

You might also like