Skip to main content

Command Palette

Search for a command to run...

Solidity Note 22/08/2024

Summary of Lessons Learned on Day 4, Week 3 in Web3

Published
•4 min read•View as Markdown
Solidity Note 22/08/2024
C

I am a software engineer

Modifiers

Modifiers in solidity are like middlewares in express.js, you use them to sort of like authenticate endpoints, in solidity they are used to authenticate or carry out a check on functions, to ensure that the function runs only when a particular condition is met. then a last "_;" is like the point where it will break if that condition is not met, it's like the "break" or "return" statement in javascript.

modifier onlyOwner() {

require(msg.sender === owner, "You are not the owner, get out");
_;

}

Require

The require function is a logical operator, used to check if a condition is true, it takes 2 arguments, the first argument is the condition to check for, and the second argument is the message to send if the condition is not met, that's if it returns false.

💡
every statement should end with a semi-colon

Constructor

The constructor is a function that runs just once and that is during deployment and after that, it does not have any use again, and it can take arguments, if any argument is being passed it must be passed during deployment else the contract will not deploy

constructor(){
owner = msg.sender;
}

MSG.SENDER

The "msg.sender", is a keyword that is available globally in solidity and can be accessed anywhere in any solidity file, and it represents the wallet address that is carrying out that transaction, in web 2 you can say "user email", or better still a means to identify the logged in user", so in the above code we are setting the owner of the contract as the contract just so that we can protect some functions, and this can happen only once.

💡
"owner" is a variable that was initiated earlier in the code. so it can be named anything, like "admin" or whatever.

Arrays

Unlike other programming languages, arrays in solidity can only hold data of the same type, and you need to define the type of data the array is going to hold.

uint[] public numbers = [1,2,3,4,5];
string[] public fruits = ["mango","apple","orange"];

uint public numberOfInitialFruits = fruits.length;

// in solidity you can modify/ manipulate arrays just like javascript

// adding to the fruits array
function addFruits() public {
fruits.push("Banana");
}

// modify the fruits array by the index

function modifyFruits(uint index, string memory newValue) public {
fruits[index] = newValue;
}

// remove from the fruit array, the last item
function removeFruit() public {
fruits.pop();
}

// get a particular index in the array

function getAParticularItemByIndex(uint index) public view returns(string) {
return fruits[index];
}

Memory

// modify the fruits array by the index

function modifyFruits(uint index, string memory newValue) public {
fruits[index] = newValue;
}

In the above code, we added the memory keyword when we were taking the newValue argument from the function, and that is because solidity by default would want to store any argument passed in a function into persistent storage, and storage in solidity can be very expensive in terms of gas. So the "memory" keyword tells solidity that this value is temporary and that it will be deleted immediately after the function has been executed. Storage in solidity is persistent and stays on the blockchain, while memory is temporary.

💡
In solidity it does not matter the position of your function or variable, you can access it anywhere, you can declare a variable at the end of the file and call it at the beginning, this is because the code compiles.

Struct

Struct in solidity can be seen as interface in typescript or Schema in MongoDB, this is like a custom datatype, you can relate it to the word STRUCTURE in the English language.

struct Person {
string name;
uint age;
address account;
}

// an array that is gonna contain this type of data

Person[] public people;

//using a struct

function addPerson(string memory _name, uint _age, address _account){

Person memory person;
person.name = _name;
person.age = _age;
person.account = _account;

people.push(person);

// the mentor mentioned that this is a long process, but we will do better as we go

}

Mapping

Mapping in Solidity can be seen as Object In JavaScript, they are created by providing a key and a value.

mapping(address => uint) public balances;
// in the above example address is the datatye that the key should be
// and uint is the data type that the value should be

function addBalance(address account, uint balance) public {

balances[account] = balance;

}

You can have mapping inside of mapping, which is like nested objects in javascript.

mapping(uint => mapping(address => uint)) public account;

Writing a Test Contract.

contract {

struct Account {

string name;
uint balance;
address accountNumber;

}

Account[] public accounts;

function createAccount(string memory _name){

Account memory account;
account.name = _name;
account.accountNumber = msg.sender;
accounts.push(account);

}

function deposit(string memory _name, address _accountNumber, uint amount) public{

Account memory account;

account.name = _name;
account.balance += amount;

}

}

// i will finish this code up

Thanks!.