New Ethereum talks, every Monday. The week's conference uploads by event, in your inbox.

Loading player…

Advanced Functions in Solidity: Function Visibility

BaseMon, Oct 9, 2023, 12:00 AM

Manipulate and modify smart contract data with ease! 📝💻 Watch as we create a set of powerful functions to set and compute data, along with a customizable multiplier. Master the art of internal and external function visibility, and gain insights into efficient gas usage. 📚 Head to Base Camp for full courses on smart contract development: https://docs.base.org/base-camp/docs/welcome ✅ Subscribe to our channel here: https://www.youtube.com/@BuildonBase ► Build on Base: https://base.org ► Follow us on Twitter: https://twitter.com/buildonbase ► Join our Discord: https://base.org/discord

Transcript

Very good. So, what are we going to do? We're going to create a set of functions to modify an internal state variable that we'll call, uh, data. Okay, our contract will have data and we'll have a function called set data. Another function called set computed data and another function called set multiplier.

Okay, what we'll do is , when we call this function, we'll simply set data to the new value we pass. OK. And then set computed data. What we are going to do is set data to the value we are passing multiplied by the multiplier. Understood.

Imagine that this multiplier can be an integer or a uint, as we did before, and it can be, say, 10. So we will pass new data, but this will be multiplied by this number. Okay. And then the result will be the new value of the data variable. OK.

Very good. Let's begin. Let's say data is a uint of 256. Let's say it's public and we'll call it data. Okay.

Now let's create a set data function. And what we will do is pass a value that we will call new data. Okay. This will be external because, well, with the information we have, we don't know if a function within the contract will call it. If that were the case, we would have to change it to public.

But for now , let's make it external. OK. And let's say it will return a simple bool. Okay. And then what we will do is make data equal to new data.

Good. Let's also initialize data in the constructor and say that data is equal to 100. We can also define that when deploying the contract we must pass the initial value of data. So it could be something like initial value and initial value. Okay.

Um, very good. Notice that I'm using this variable with the underscore. This was very common in some contracts to avoid confusion with other variables. Okay. All parameters used to have an underscore; It was easier for auditors and those who reviewed the code, since in large codebases it can become a mess.

I'm going to remove it just to be consistent, but keep it in mind. Okay. And now I'm going to return true and it's working correctly. Let's try it. I'm going to deploy.

But remember that I need to specify an initial value. So I'm going to set 200 as the initial value. It deployed correctly. Now I'm going to verify the data value. And it's 200.

Okay. So it's working. If I want to set a different value, say 300, I can simply use set data. Okay, so I call set data. The transaction was successful.

And then I simply call data again to see what the new value is. And it's 300. So yes, this is working correctly. Very good. So, now let's set computed data, which should look like this.

It will also receive new data, and this will also be external. In fact, I'm going to copy this part. However, remember that we need to have a multiplier and then calculate the computed data. So for that I'm going to create another function called compute data. This compute data will also receive some data; it will be internal.

Okay, that means only functions within this contract will be able to call it. Very good. And it will return a uint256. OK. So what we'll do is return some multiplied data.

This is an operator for multiplication. And then I need to multiply by the multiplier. So the multiplier does not yet exist. Therefore, I need to create it. And I will also make it public and simply call it a multiplier.

Very good. In fact call it a multiplier. Very good. In fact , we can also specify a multiplier at the beginning. Good.

And then we say that the multiplier is equal to the multiplier I am passing. So I'm going to use initial multiplier to avoid confusion. Very good. And then I simply multiply by that. This is internal and it's a view.

Why is it a view? Because it accesses the multiplier variable, which is part of the storage. Well, that's why it's a view. Great, so now we have a multiplier. In fact, we also need to be able to edit the multiplier, right?

So we will do set multiplier, which will receive the new multiplier, will be external and return a bool. And we simply say that the multiplier will be equal to the new multiplier. Good. And then we simply return true. Very good.

So now we have the ability to edit the multiplier, configure the data, and configure the computed data. But here we need to call compute data. Good we need to call compute data. Good . So what we're going to do is call compute data and, since compute data receives some data, we pass this value.

Okay, notice how compute data is called from this function. Then compute data will return a new value and this value will be assigned to data. So we can actually do something different here. What we can do is make this function public, and then what I can do here is declare a variable. Let's say data computed and this only lives within the scope of this function and then I can set the data by calling the set data function.

Good. And I say set data and pass the data that was calculated data and pass the data that was calculated . Okay, since data returns the same values ​​as set computed data, I can also do a return and say return set data and that's it. Good. So the important part here is that if you notice, set computed data is not being called by anyone in this smart contract, for that reason it is purely external.

Set data, on the other hand, is called set computed data and we initially thought it might be purely external. However, when another function calls this function, we say that it must be public. Okay, there are some benefits to gas that we'll see in the future. But it is important to try to maintain this discipline regarding the visibility of functions. It's very, very important.

And finally, note that compute data is internal. So these three functions are the only ones that can be accessed externally. Good. So let's try it. I'm going to click here and now I'm going to say that the initial value will be 100 and the initial multiplier 10 and I'm just going to transact and get the values ​​data 100 multiplier 10.

Okay, it looks like it's working. So if I want to set a new multiplier, say 20, the transaction was successful say 20, the transaction was successful . The multiplier is now 20. Very good. And data data is the same.

Let me set a new value for data. I'm calling and the transaction was successful. So let's check the data value. 200. Good.

And let's say now that I want to set computed data. So I'm going to pass 200. No, 250. So if I pass this value, we'll get a larger number. Good.

Let's see what happens larger number. Good. Let's see what happens . Okay, the transaction was successful. Let's see what the new data value is, and the value is 5,000.

Good.

Automatic transcript — names and jargon may be misspelled.