Solidity Basics

Hello, I think I did what filip does exactly in the video lecture for mappings but I keep on encountering an error: “SolidityBasics.sol:27:9: ParserError: Expected primary expression. returns balance[msg.sender]; ^-----^”

I also used pragma solidity 0.7.5

Thanks for the help

mapping(address => uint) balance;

function addBalance(uint _toAdd) public returns(uint){
    balance[msg.sender] += _toAdd; 
    returns balance[msg.sender];
}

function getBalance() public view returns(uint){
    returns balance[msg.sender];
}
1 Like

I think I got it, I should’ve used “return” instead of “returns”, but what’s the difference between the two? Many thanks

2 Likes

The method for the keyword are different, returns is used on the solidity function header to let know this function should return a value type.

While return is the keyword that you use on solidity and also many programming languages to achieve 2 goals:

  • run a function that returns a value, can be used for calcs.
  • the function procedure ENDs when it returns a value.

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

1 Like

Mant thanks @thecil, I’ll make sure to remember these and add them to my notes! Thanks again for the help.

1 Like

how much does 1 gas cost in eth?

1 Like

I think you are confusing terms, GAS is the term used for the transaction fees, the amount of wei that you are willing to pay for the tx.

wei is the term used to define the most smallest unit for ethereum, like bitcoin which is 100 million satoshis, ether is a trillion of wei, you can find more on internet, there are many tutorials for it.

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

Hi team!! My question is about how we can verify if a specific value exists in a array (in this case address but this applies to any value).
For example:

address[] arrayOfAddresses = [address1, address2, address3];

address addressToFind = someAddress;

How we can valdiate if this addressToFind exists in the array without using expensive operations like loops in order to avoid the elevated gas costs?

Thanks!!

Hi @lucaszanek

You have to iterate the array and check one by one if array[i] == address.
No other options if you use array.

Cheers,
Dani

image

Does anybody know why I can’t make a new file, when I clicked new file nothing happened.

Hi @Lane11

Which browser are you using? Have you tried with a different one?

Brave, I did try in too Chrome but the same thing. Month ago I could use remix and write smart contracts but now I don’t know where is a problem. Now I downloaded desktop remix and it works fine.

Hi Filip or anyone,

can you help me to understand what does ** Person [ ] people ** mean in below program ?
Can it be Person [ ] = people; ?

pragma solidity 0.7.5;
contract HelloWorld {
struct Person {
uint age;
string name;
}
Person [ ] people;

function addPerson(uint _age, string memory _name) public{
Person memory newPerson = Person ( _age, _name) ;
people.push(newPerson);
}

function getPerson(uint _index) public view returns ( uint , string memory ) {

Person memory personToReturn = people [ _index];
return ( personToReturn.age, personToReturn.name);
}
}

1 Like

Person [] people means an array based on the struct.

your struct is:

struct Person {
uint age;
string name;
}

so it is an struct , when you create Person [] people means, an array called people and the array data is based on Person struct, so the position 0 in the array people, should be:

people[0] = { age, name}

and so on, that way you can have an array that contains data referred to your struct.

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

1 Like

HI Carlos,

i saw one mapping typed as :
mapping (address=>uint) balance;

Is this above mapping written to indicate that address indicated will bear a value called balance ?

I saw another mapping in the data location assignment :
mapping(uint => User) users;

struct User{
uint id;
uint balance;
}

I noticed the uint is placed in front instead of behind like the first mapping above.
So this is a mapping called users, based on struct User, and
users[0] = { id, balance} ?

And why is there a difference in where the uint is placed between the two mapping?
Does the order matter or not ?

Thank you

1 Like

Hey @M.K, hope you are great.

correct, mappings are the most efficient way to get a KEY => VALUE, that one points to an address => uint which we call balance.

exactly like that, the users mapping will have a uint that points to values based on the struct data, so has your example is basically users at uint 0 will have structs values for that position.

that depends on what data you need to save on the mapping, you can have a mapping to points from uint to something else, or address to something else, it is a flexible way to store data in a efficient and easy to request.

I advice you to rewatch the lessons for mappings, where filip explain it quite well in the black board and then in code.

Carlos Z

1 Like

Hi Carlos, Thanks for your replies, it helped a lot.

I have a question about below codes

mapping(address => uint) balance;

function addBalance( uint _toAdd) publi returns(uint){
balance[msg.sender] += _toAdd;
return balance[msg.sender];
}

Since the mapping called balance is created with parentheses as :
mapping ( address => uint) balance;

when we use balance in the function, we actually use brackets instead of parentheses :
balance [msg.sender]+= _toAdd;

So does this mean a mapping is similar to array ? When we use a mapping , we type the code as if we are using an array ?

It is just that mapping returns the value instantly while array needs to search for the value within its many values within the array

1 Like

Yes, you invoke them like an array, but is not exactly an array because you dont know all the keys on it, so there is no way to iterate and get all values if you dont know all the keys, but its more efficient to get data faster than arrays.

Carlos Z

2 Likes

HI Carlos, hope you are well.
I have a question

If i define a struct with string and uint as below:

struct User {
string nameofHolder;
uint address;
uint balance;
}

Then how do i create the mapping for above struct ?
mapping ( string, uint => User) users ? Or how ?

Is it possible to create a mapping with such struct ? Or is it necessary in any situation where we need to have string and uint in a struct ?

Please advise.
Thank you

1 Like

HI Carlos,

One more question after i see data location assignment solution
pragma solidity 0.7.5;
contract MemoryAndStorage {

mapping(uint => User) users;

struct User{
    uint id;
    uint balance;
}

function addUser(uint id, uint balance) public {
    users[id] = User(id, balance);
}

function updateBalance(uint id, uint balance) public {
User storage user = users [id]; 

user.balance = balance;
}

function getBalance(uint id) view public returns (uint) {
    return users[id].balance;
}

}

Are 1) and 2) the same, or does the order make a difference to the meaning of the code ?

  1. User storage user = users [id] ;
  2. users[id] = User storage user; 
    

Thank you

You are still confuse with it, you can have a mapping that returns an struct, but not with too many arguments like your example.

you can have something like:

mapping(address => User) users;

for each key address it will return the struct values.

The order have a meaning, 1) creates an storage variable which data type is referred to the struct users on that id position, the other one is the same but backward, it does not have any sense to me.

Carlos Z

2 Likes