Solidity Basics

Hi Filip, hope you are doing well. Perhaps, my previous comment got stuck somewhere or just has not been seen. I hope you could really help me out with this. I really need some help. Could I please direct your attention to this topic?

For starters, I find it very useful to contrast the differences between Solidity and JavaScript to consolidate what I have learned so far and help me get along with smart contracting programming.

With that said, you used the setNumber function within the block and that means that the scope of this data is block-scope, right? So, if the setNumber function changes the data and this data is only accessible within this scope. So, if the setNumber function changes the data and this data is only accessible within this scope. Could you or could anyone share two real-life examples associated with a real task that programming a smart contract might entail and that we might need to confront where we would want to use a setNumber function, please? I am trying to connect how this specific output could interact with the rest of the contract. Specifically, how it might be used by a specific function in a different part of the contract

Thanks in advance.

1 Like

Ah thanks Leo, I had to use the fn key + insert, all working now!

2 Likes

Hi @JosephGarza,

That’s great you’re enjoying the course and feeling so motivated :muscle:

Yes, that is a helpful thing to do, because in Solidity there is a lot of overlap with Javascript in terms of syntax, structure and concepts. There are obviously key differences, but already knowing JavaScript certainly helps a lot. :smiley:

That’s great that you’re already thinking in terms of scope. I think that the following is the setNumber() example you are referring to. Please correct me if I’m wrong.

contract HelloWorld {
    int number;

    function setNumber(int newNumber) public {
        number = newNumber;
    }
    
    function getNumber() public view returns(int){
        return number;
    }
}

int number; is a state variable and defined within our contract, and so it is accessible from anywhere within the contract, including within the local scope of the functions i.e. from within the function bodies (what you are calling block scope, but we don’t use that term in Solidity).
If we define a variable within a function’s local scope, then this can only be referenced/accessed locally within that same function.
But in the example above, there is no local variable defined within setNumber(). Instead, this line…

number = newNumber;

… references the number value input as the argument/parameter newNumber, and assigns it to our number state variable, where it is stored within the contract state. This line of code within our function can do this, because state variables are accessible from anywhere within the contract. This is similar to how, in JavaScript, variables defined in global scope are accessible from within the block scope of functions (or function scope).

In our example contract, we can then call getNumber() which is able to access the number value input previously, and now stored in the state variable number, and return this to the caller.

However, what we cannot do is assign newNumber to a locally defined variable, and then call getNumber() to return it. e.g.

/* THIS CONTRACT WILL NOT COMPILE */
contract HelloWorld {

    function setNumber(int newNumber) public {
        int number = newNumber;
    }
    
    function getNumber() public view returns(int){
        return number; // compiler error here
                       // number is an undeclared identifier
    }
}

Values stored in local variables are lost when the function finishes executing, and so cannot then be accessed from within another function afterwards.

I hope that goes some way towards answering your question, or at least allows you to move on. I’m sure these concepts will become clearer to you over time as you see and work with more examples.

Let us know if anything is unclear, or if you have any further questions :slight_smile:

Hi again @JosephGarza,

Further to my previous post, I think I have answered this question of yours…

… but I’m not entirely sure what you mean by…

Do you mean that we wrote the setNumber function within the contract? i.e.

contract HelloWorld {
    // we wrote it here
}

In Solidity we have a concept called visibility. We have to declare the visibility of functions as either public , private, external or internal. If we don’t explicitly declare the visibility of a state variable (with either public , private or internal ) then by default it will have internal visibility. You will learn more about the difference between internal and private visibility later in the course, but for now you just need to know that both types allow access from within the contract, but not from outside the contract i.e. you cannot call a function marked private from Remix. Let’s put external visibility to one side for now (it will be introduced later in the course), and consider public — this allows access from within the contract and from outside i.e. you can call a function marked public from Remix.

So, an external user can call a public setter to input, assign and store data in a contract state
variable, but if the state variable has either private or internal visibility, this data cannot be retrieved directly from the state variable by the same external user. That’s why we have a public getter, which we can call externally to access the state variable on our behalf, and then return that data to us. If a state variable has public visibility, then Solidity automatically creates a getter for us (meaning we don’t have to write the code).

1 Like

In terms of more practical examples and real tasks, I would wait a bit, because a bit later in the course you will start to use setters and getters, which work based on the same principles as setNumber() and getNumber() , in contracts that aim to reflect more real-life use cases. You will be using them in a token contract, which then morphs into a bank contract. Setters and getters are fundamental to all smart contracts, so at the beginning you need to learn their basic mechanics, structure and syntax, before you can really appreciate how useful they are in practice.

Do let us know if you have any further questions, or if any of what I’ve explained is unclear :slight_smile:

1 Like

Hi @Leo1,

I’m not sure I fully understand your question, or whether the squiggly line has anything to do with it…

Do you mean how to code, compile, deploy and interact with your Solidity smart contract in Visual Studio IDE (not Visual Studio Code, text editor)? I’ve never used the IDE, only the text editor, and I’ve never used that for coding in Solidity. I use Atom to code smart contracts in Solidity, but then Truffle and Ganache to compile and deploy them. You will be introduced to Truffle and Ganache in the 201 course. I would stick with Remix for this 101 course.

However, I’m tagging my colleague @thecil, as your question may mean more to him, and he might know something about using Remix with Visual Studio that I’m not aware of…

The screen shot you’ve posted isn’t very clear, and I’m not sure what you’re trying to show us. From the code, it looks like you are already some way through the course, so if your question relates to a later section or a specific assignment, then please post it in the relevant discussion topic rather than here. If you need us to actually check your code, then please post a formatted copy of your code and not a screen shot. Thanks :slight_smile:

2 Likes

hi John,

Thank you for your response. the squiggle is an glitch. Yes i meant how to interact with a smart contract using VS IDE. It is not as straight forward as remix.
For instance in the screenshot i wanted to highlight the left side bar where Filip change the variables on the contract.
I think i will stick to remix for the 101 course and move to VS code for 201.

Thanks for your help.
Leo

2 Likes

:rofl: I thought it might be :wink:

That’s sounds like a sensible decision :ok_hand: :sweat_smile:

1 Like

@Leo1

Hehe, not really.

Remix can be use to interact easily with contracts, but if you want to use VS, you also might need to know about truffle, that way you must compile, deploy and test your contract manually (while remix does all of this for you).

So basically you can stick with remix for 101, in Eth programming 201 you will learn how to use truffle and write unit test for it, then the same process can be applied for the 101 contracts, but you have to write manually your tests.

Anyway im sure there are some lessons in 201 that teach you how to interact with an external contract through their interface. :nerd_face:

Carlos Z

2 Likes

Thanks, Jon_M. Definitely encourages me to move forward with the programming which I aimed to initially. Thank you very much.

1 Like

Hi,

Im new to the course also.

Are you interested in wrting flash loans?

in the lecture of
Implementing visibility he said its away to restrict access to functions and state variables in our smart contracts. Which part of the code offers that restriction?

contract Classes{

mapping(address => uint) balance;

function addBalance(uint _toAdd)public returns(uint){

balance[msg.sender] += _toAdd;

return balance[msg.sender];

}

function getBalance()public view returns(uint){

return balance[msg.sender];

}

function transfer(address recipient, uint amount)public{

_transfer(msg.sender,recipient, amount);

}

function _transfer(address from, address to , uint amount)private{

balance[from]-= amount;

balance [to] += amount;

}

}

1 Like

Opps It;s the private part of the function header offers that right?

1 Like

Hi @jahh

Correct :ok_hand:
Marking a function with private visibility means that the function can only be called from within the same contract. e.g.

function transfer(address recipient, uint amount) public {
   _transfer(msg.sender, recipient, amount);
}

function _transfer(address from, address to , uint amount) private {
   balance[from]-= amount;
   balance [to] += amount;
}

After deploying the contract that contains these 2 functions, transfer() can be called from Remix, but _transfer() can only be called from somewhere within the same contract: which it is — it’s called from within transfer().

Marking a function with public visibility means it can be called from anywhere: from an external service (e.g. Remix, or from the frontend of a dapp), from within the same contract, or from a different contract.

State variables also have visibility. With state variables the visibility determines where they can be accessed from. A state variable with private visibility can only be accessed/referenced from somewhere within the same contract.

That’s an awesome explanation, thanks for that.
I have to ask you , how did you get so good at this?

also i everyday i write out the code from the beginning of the course up to implementig visibility. i want to do this with no errors before i move onto practicing the next part of the course. Do you think that is a good way to get good at doing solidity code?

1 Like

Hey @jahh,

Glad the explanation was helpful :smiley:

…by working for Ivan On Tech Academy for a year now! :sweat_smile:
…also, when I was first learning this material, by not going through the course too quickly. I really took my time, read a lot of posts in the forum, asked lots of questions in the forum etc.

Each person has a different way of learning that works best for them, so the secret is to find out what method works for you. Having said that, I think what you’ve said you’re doing is a really good idea, because it will stop you going too quickly through the course; and by trying to write the code from the lectures from memory, you will discover which bits you find more difficult and maybe don’t understand well enough. That will then help you to know what sort of questions to ask here in the forum.

Repetition and practice is key: you’ve already built in some repetition by rewriting the code from the lectures. What you can also start to do is experiment with the code, making a few changes here and there to see if it still compiles, or if you get some errors. If you get errors, then read the error messages and see if you can work out what you need to change. Try to personalise the code from the lectures a bit, by adding your own little adaptations. This stops the learning process from becoming too repetitive…

Anyway, I hope those few ideas are helpful… but, basically, from your question earlier about visibility, and from what you’ve said you’ve already started to do, I think you are already approaching your study in the right way :slight_smile: :muscle:


Also, once you get onto the assignments, here is a list of some learning and study activities, which you may find useful and could help you get more out of the course…

  • Post your work-in-progress (or just part of it) here in the forum, by following the discussion topic link given to you for each section of the course. Include a specific question about a particular problem you are encountering. This can be an effective approach if you would like help before you look at the solution.
  • Have a look at other students’ posts in the relevant forum discussion topic, with their attempts, solutions, and the help, comments and feedback posted as replies. There are a lot of comments and explanations posted here. It’s well worth spending your time browsing and reading, not only to get help with the exercises, but also after finishing a section or particular assignment as a way to review what you’ve learnt, and to discover alternative coding solutions or answers.
  • When you look at the assignment solutions, if they are different to your code/answers, you should spend some time analysing and thinking about them. This is a really important stage in the learning process. You can learn a lot by working out what the solution code does and how it does it, and also how to go about “bridging the gap” from what you managed to do (or not) on your own. However, it’s important to remember that there are usually several different alternative approaches and solutions to these exercises, so if you’ve coded yours differently, but it still works, then it may well be valid. If you’re not sure, then post it here in the forum, and we’ll review it for you.
  • Another good learning technique to use (after having already done the tasks described above) is to then try the exercise again from scratch without looking back at the answer (like a memory test). You can also try to do this after having left it for a certain period of time (say, the next day, then after a few days, then a week etc. etc.) This builds up longer-term retention. This is a good approach to take when reviewing a course you’ve already done, or earlier parts of the course you’re currently doing. Instead of just looking back at your solutions to the exercises, test yourself to see how many of them you can redo from scratch — only checking your previous code and notes, and rewatching the videos, if you need to remind yourself about certain things.
  • Another important point to remember is that it can take some time before you fully understand all of the code in an exercise, so another good strategy is to come back and revisit an exercise , which you didn’t fully understand, after you’ve progressed a bit more through the course. You may well find that it’s much easier then.
  • Don’t forget to do some research yourself on the Internet. Here is a link to a playlist from the YouTube channel Eat The Blocks . I’ve seen some of this guy’s videos myself and I think they are particularly helpful for learning Solidity. The videos are quite short and the explanations clearly explained and well demonstrated with clear examples. You’ll find that several of the videos in this playlist correspond to specific sections of this course, and so serve as an ideal starting point for your own further research.
    https://www.youtube.com/playlist?list=PLbbtODcOYIoE0D6fschNU4rqtGFRpk3ea
  • Finally … play around with the code, experiment , and try to come up with your own examples, no matter how basic, short or simple. Even by just making a few small changes and adaptations to the code presented in the course, any amount of personalisation that you are able to add to your code will help it to mean more to you, and will therefore help you to internalise it better.

Jon, you’re beyond helpful! thanks for the great advice !!!

1 Like

Hi Filip!
Just starting this class. I hope I don’t give you a heart attack but … I just got into Remix trying to get everything set up. I did create a “Workspace Name” however, when it comes to type the first lines of Smart Contract, it doesn’t even type. How do I get to make this work? What did I miss? Please, any other community member with time to deal with a student first time exposed to this programming, will be highly appreciated, so we can save the super busy instructors and more advanced students from a heart attack.
Thank you,
Maia

Hi @Maia,

Watch the video again carefully from the beginning, play it in slow motion (you can change the speed in the settings) and pause it several times to see exactly what steps Filip takes when he first opens Remix and creates a file. Bear in mind that the version of Remix you have just downloaded and are using will have a slightly different interface layout to the version in the video.

You need to create a file with a file name ending in .sol to be able to start writing your first Solidity smart contract. The create file icon is the first one in the line of icons below the workspace name (above is for creating new workspaces). You need to click on that icon to create a new file within your workspace. Once you’ve typed in your file name and pressed return/enter, the file should open automatically with the cursor flashing at the start of the first line, ready for you to start typing code. To open a file you’ve already created, click on the file name in the file explorer menu.

Once you have a new .sol file open, typing the code is done in the same way as any other text editor. If you were able to create files and type JavaScript in Atom for the JavaScript Programming for Blockchain Developers course, you shouldn’t have any great difficulties writing/typing your first smart contract in Remix :slight_smile:

1 Like

Dear Fillip,

It is no longer possible to use 0.7.5 on remix. So I updated it to 0.8.2. I did not have a choice and I am not a rebel.