Solidity Basics

Hi @bdoherty, hope you are well.

Now the require() function is used to verify that the arguments passed to the function are valid before the execution of themself, it does not have any gas cost, functions cost gas when they are about to being processed by a miner, the gas is for the miner basically.

Take an example on:
Lets say i have a function that must verify my age before it can proceed to the next step of his logic.
So if my age is above the assigned by my require it will not execute the function logic.

function payMe(uint _age) public{
 //verify that _age is inside range
require(_age < 50, "You'r age is above the range, can't continue" );
//if _age is valid, continue execution
doSomething();
} 

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

Carlos Z.

1 Like

Hi Carlos,

Im great! Thanks for the feedback. So its not costing gas yet because this happens before it gets to a miner. So in your example, where and who is doing the processing to check the require statements? Is it happening on the EVM? If so would there be a cost in relation to resources to check the requires etc? So if you were to repeatedly call some function with lots of requires would things start to fall over at any point?

Hope I make sense!
Brian

Yes, the EVM have access to the smart contract, any call on a function will be handle by the EVM.

There is basically no use of big resources to check the arguments that must be passed to a function.

Is a simple calculation for the network, computers are very precise in terms of calculations so is almost impossible that calling multiple times a function with different arguments will fall, maths are very precise and even more because the EVM goes on binary code which is the most tiny and exact representation of what the computer must calculate.

Basically, the EVM verify the arguments, since this a very simple operation, no big resources are used, also it does not matter how many times a function is called if this one has been programmed decently to verify the inserted data before itā€™s execution (thats why require is important, to verify data before execution).

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

Carlos Z.

1 Like

@filip
In the video series about storage you mention 3 locations:

  • Storage
  • Memory
  • Calldata

However, when I do the quiz afterwards, I only found 2 of these (Storage + Memory, Calldata was not in the list) which turned out to be wrong. In my breakdown, it shows ā€œStackā€ as third option. You never mentioned this in your videoā€™s.

Screenshot 2020-12-23 at 17.08.44

Is this the old name for Calldata perhaps? Or are we missing something here?

1 Like

hi everybody i am in solidity basics and when I click on deploy it give me this

sender doesnā€™t have enough funds to send tx. The upfront cost is: 3000000

does anybody can help me with that please , thanks!!!

1 Like

Hi @steve777, hope you are well.

Could you please share a screenshot? If you are using remix, i would like to verify your compiler config, maybe also the code can help us solve your issue :nerd_face:

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

Carlos Z.

1 Like

Hey @seer,

It seems that your post about the data location quiz has been missed. Sorry about that!

There was actually a problem with this quiz. It hadnā€™t been updated from the old course which only discussed Memory, Storage and the Stack. Itā€™s now been updated to coincide with the changes in the new course, and what youā€™ve been taught in the videos. So you may want to just retake the quiz, and you should find it makes sense now :slight_smile:

Apologies for the confusion and inconvenience!

Hey guys, they say that thereā€™s no such thing as a stupid question, so here goes. Mine is a follow-on to keitra1948ā€™s post above ā€“ but it relates to mapping.

In the video on Structs, Filip first demonstrates his ā€œclearestā€ way (i.e., the function createPerson . . .) to code adding a new person to the people array, and then he demonstrates the alternative (i.e., ā€œPerson memory newPerson . . .ā€). See: @10:05 of the Structs video.

The distinction between the two methods as well as the subsequently explained reasons why we might choose to use one versus the other seems obvious enough but I must be missing something because what I donā€™t understand is why, Filip, you didnā€™t code the presentation in the Mappings video using the function createPerson . . . people.push(Person(people.length, name, age, height)) method?

In the Mappings video, you removed the ā€œpeople.push(Person(people.length, name, age, height))ā€ altogether, without explanation, and proceeded to use the less ā€œcleanā€ newPerson method to complete your Mappings illustration.

So, my question is, how do I code the mapping using the functionCreatePerson . . . people.push(Person(people.length, name, age, height))? Iā€™ve tried to work this out on my own but Iā€™m just spinning wheels.

Best regards!

P.S. I see that youā€™ve also removed the ā€œpeople.push(Person(people . . .ā€ expression from your GitHub structs.sol file. Is there a reason for that as well?

1 Like

Great question @novosel,

Have a look at this post. That should answer your question, but please come back and let us know if anything is still unclear or if you have any further questions :slight_smile:

1 Like

Ah! Jon_m, THANK YOU!!

Do you happen to know why Filip abandoned using this method?

If itā€™s a no-harm, no-foul way to use exclusively the expressions that donā€™t write to the state, then Iā€™ll simply focus on that route. Iā€™m a beginner so Iā€™ll have to think this through.

Cheers!

1 Like

And just a follow-up on your other questionā€¦
Both methods are valid, and Iā€™m only hazzarding a guess when I say that the first method may have been removed to avoid the potential confusion that you yourself have highlighed.
If you have only just started this older Solidity course, you may wish to restart with the new updated course instead. Itā€™s literally just been released about a couple of weeks ago and has been updated for the newer versions of Solidity and the examples and assignmnets are based on v0.7.5 (instead of 0.5.12 in the course you are following).
Weā€™ve kept the old course available for students who already started it, so by all means continue with it, and finish that one first, if you prefer. But please bear in mind that Solidity has been evolving and so there are some important changes that you will eventually need to be aware of.

Just answered that oneā€¦ our communication paths have crossed :wink:

No, you can use either method. The more long-winded method, using more steps and including the temporary ā€œmiddle-manā€ local variable can be better when multiple values and/or more complex data structures are involved, simply because the code is much clearer, easier to understand, and probably safer to manipulate and build on. It can also be easier for beginners to understand because itā€™s broken down into smaller logical steps rather than these being less visible with the one-giant-leap approach. Personally, I like the conciseness achieved with the 1st approach, so itā€™s also a matter of style and personal preference. Obviously each particular use case will be different, and there may be other case-specific reasons which favour either one method or the other.

Does that go most of the way to clearing up your uncertainties?

That all makes perfect sense. Thx jon_m.

1 Like

Maybe I jumped the gun. Can someone syntax-check me on the above?

Iā€™m getting a TypeError message using that code:

"TypeError: Type address is not implicitly convertible to expected type uint256. people[creator] = Person(name, age, height); ^------^ "

1 Like

Hi @novosel,

You have probably coded either your struct or mapping slightly differently to the ones I based my solution on. You need to adapt the structure in the solution Iā€™ve given you to your own contract. Here is my full contract which contains the code youā€™ve tried to use. This compiles successfully in Remix, and should help you to identify why itā€™s throwing an error when you try to implement it in your contract. If this doesnā€™t help to identify the issue, then post the full contract that you are getting the error with, and I should be able to work out the problem from that.

pragma solidity 0.5.12;

contract MappingExample {
    
    struct Person {
        string name;
        uint age;
        uint height;
        
    }
    
    mapping(address => Person) private people;
    
    function createPerson(string memory name, uint age, uint height) public {
        address creator = msg.sender;
        people[creator] = Person(name, age, height);
        /*
        Person memory newPerson;
        newPerson.name = name;
        newPerson.age = age;
        newPerson.height = height;
        people[creator] = newPerson;
        */
    }
    
    function getPerson() public view returns(string memory name, uint age, uint height) {
        address creator = msg.sender;
        return (people[creator].name, people[creator].age, people[creator].height);
    }
    
}
1 Like

Just a couple of questions I had regarding the special variablesā€¦

  1. Does interacting with the msg.sender and/or msg.value considered interacting something outside the function and therefore not a ā€œpureā€ function?
  2. If floating point variables arenā€™t supported how is the msg.value represented? I was under the impression you can send fractional ethereum in a transaction.

Thanks,
Jeremy

1 Like

Well this is my second time around, I dont understand the UPDATE process, but im re-taking the updated course from scratch. I know theres a few things that are updated. i dont mind really, but I guess this time it should stick, lol.

There should be a smart contract bootcamp at the academy.
:face_with_monocle:

1 Like

Ok, im doing this course over again because its 0.7.5 now, and its Updated. So now I am having this issue please take a look at the screenshot, if you can help with this , Its well appreciated. Thank you.

I dont know why I am receiving error from compiler, im following along step by step with Filip. Who state that variable can be outside of function. As you can see I keep getting error, why is this?

``pragma solidity 0.7.5;

// this is a complete contract able to run IDE

string message = "Hello World";

// contract statement

contract HelloWorldNew{
// all variables and functions

function hello() public returns(string memory){
    return "Hello World";
}

}`
type or paste code here

:thinking:
1 Like

Hey @Javier_Flores. I guess the concern is that you declared the string message outside the contract. Variable can be outside the function but should still be inside the contract header.
Iā€™m not really sure if my answer is right but my code is just fine putting the string message inside the contract.
I really hope this helps! :smiley:

3 Likes

Oh man I see it now, geez, thank so much!

JF

2 Likes