Hit enter after type your search item
Wzy Word

HERE ARE THE WORLD'S NEWS

Solidity 0.6.0 Breaking Changes (Hands-On With Examples)

/
/
/
686 Views
img

Solidity 06 is out and it's time to have an in-depth look about what changes were made to the language itself

Instead of going through the changes here in the Solidity documentation and read them off I actually want to give you an in-depth look at what happened in solidity This is why I have a couple of files here in Remix You can download them in the video description or as a resource download and follow along! So the first thing that I want to talk about is the new "virtual" keyword which is functions can now ONLY be overwritten when they are either marked with the virtual keyword or defined in an interface Functions implemented outside of interfaces have to be marked as "virtual" When overriding a function or modifier the new keyword override must be used

When overriding a functional modifier defined in multiple parallel bases all bases must be listed in parentheses after the keyword like so: override(base1, base2) or the other way around There are a couple of questions that popped up in my mind when I read about this let's have a look at the first one then you override and the virtual keyword so when I switch over here to Solidity 05

15 and this smart contract works just fine but as soon as I add the new virtual keyword solidity 0515 cannot really do anything with this it gets an error so the first question that I had – that I asked myself is when I have a function in sol 06 and I used the virtual keyword does the function signature of the compiled smart contract still stay the same? And that's fairly easy to test

So I'm going to just to compile this one over here with 060 I'm gonna copy the byte code and I copy it over here into a text editor and then I'm gonna add the virtual keyword because sometimes it's just a language feature I compile this again and see how that changes – and in fact the source code – the bytecode – the compiled source code changed a slightly, a little bit I didn't deep dive into what exactly changed, I was just interested if the bytecode changes at all, or if this is just a language feature that you have to mark your functions as virtual

So, yes you have not only to mark it, it's also changing the byte code Let me get this a little bit closer look-up to see what changed here so here you can see that it slightly change the signature from I think from here on somewhere and well small parts changed so yes! the next thing is how does this override really works? Let's have a look at how this works in solidity 05

15 and then how this looked like in how it looks like now in Solidity 060 I'm going to switch over to 0515 and I have two parts in my code here

one is uncommented works in 0515 and one is commented, works in 060

So I'm first trying this one out and I comment this out and uncomment the other one and then we're going to see the difference So the way it worked so f ar in solidity 0515 is I have a contract A this has some event and the function and in my function I'm gonna emit a string called "from A" and then I have a contract B and the contract C In contract B the same function does nothing and in my contract C I'm going to extend the contracts A and B and the order is very important so we are going to overrride first contract A and then contract B so essentially our function "funA" here which is calling then "super

funA()" or fun a shouldn't do anything so let's give this a try let's run this over here they have to be careful which contract you are selecting I'm gonna select the contract C and when I hit funA then we see which events were emitted and we see it is just this one event from B because superfunA is doing nothing And if you are changing the order over here B,A then we are first overriding we're first overriding B and then after that we are overriding A, so A is the most recent function that is overwritten so we are going to let me call super

funA then we are calling superfunA in contract A over here let's redeploy contract C and then click on funA and we see that we have our two events over here first from our contract C this one from B the string is important and then we have from our super from a from our event from the function a Now let me comment this out and then let's comment the other one in which is now in 060 in zero six zero again I have my three smart contracts

I have a contract A and this time I'm going to mark my function my funA as virtual so Solidity knows that it can be overridden in my contract B I'm also marking this as virtual and now comes the interesting part in contract C I'm not only going to say this is derived from B and A and the order but also the function a itself needs to specify the order it's overriding so I have this override keyword and then the smart contracts that it's used to override and in this case I'm changing the order here so what is interesting for me is now does the order for the function override being used from this one over here or from this one over here? and as you can guess already which one it is but I'm still going to deploy this, I have to select the right compiler and compile, deploy my smart contract and call function a now we have two events one from here and one from over here Let's change the order over here Redeploy the smart contract and we see it is going to stay the same It doesn't matter what order you have here what is important is that you have the correct order over here Now we have just one event from our function here in the contract C and the event in the contract A is never fired

Right on to the next one! Member access to length of arrays is now always read-only even for storage arrays It is no longer possible to resize storage arrays assigning a new value to the length Use push push value or pop instead or assign a full array which will of course all right existing content The reason behind this is to prevent storage collisions by gigantic storage arrays Now let's give it a try how this look so far

Let me just reload my Remix here Go to my second example Again switch over to 0515 and start with the old version before we gonna go into the new version

In the old version we could do the following: when we are going to add a new element we could increase the length of the array by one and then just write on the new created element the number that we want or value that we want to write there So we read the new key to the new value and if you want to remove an element from the end then in the previous version we could just decrease the length and it will automatically delete one of the elements at the end of the array How does this work? This works as the following if I deploy my smart contract and I'm trying to access the first element I get an error Gonna add an integer over here then I can see that I get back my integer that I just assigned to my array and then when I remove it and all works fine and if I try to access again the first element of the array it gives me an error again So that works all fine and if I'm trying to do the exact same thing in solidity 0

60 then it will tell you it cannot do this there is a member length is read-only so let's just comment this one out and comment the other one in This is how the new version works and it working is working in both, in 0515 and 0

60, so I have the same array and I'm going to use push to push a new value onto the array and pop to pop one array value off the array and reduce the size by one and let's just give this a try I'm gonna deploy the smart contract over here I get an error when I try to access the first element and I'm gonna try to add an element this works fine Gonna read the element so I'm getting back my integer that I just stored

And then I remove the element again works also fine and then when I try to read the first element again I get the same error Then I want to skip a couple of things that are not so interesting the next one that I quickly want to talk about is state variable shadowing is now disallowed A derived contract can only declare a state variable X if there is no visible state variable with the same name in any of its bases Let's have a look how this looks like So what we can do – let's try that one with 0

515 Alright, we have a contract A and this has a state variable X and that is one two three then we could have a contract B which derives from A the same one it's overriding more or less the same state variable X with a new value If I'm going to select a new compiler then it will tell you it cannot do this because the identifier is already declared and you're not allowed to do that So when you're going to use the new version 0 6 0 and go down and you can see that as long as there is no visibility in the new smart contract then you can very well do that and it just lets you compile this

Right on to the next one! the next one that I want to talk about I really found is very useful is the a new receive function So previously we had a fallback function without the name was an unnamed function and this was split up now in to a new fallback function that is now defined using the fallback keyword So it has a real keyword and a receive either function defined with the receive keyword So let's give this a try A fallback function over here and that is something that I just did for 0 6 0

So I'm going to have, or I'm going to emit one event in the receive function just to see how the fallback function is called in combination with this fall back button over here from Remix because there is no receive button So this kind of confused me at the beginning a little bit but it's very very straightforward So when I'm going to send 12 Wei to my smart contract then – a quick guess which one of these ones is gonna be called? It's gonna be the receive function automatically because we are not sending any messagedata along It's going to emit this event which emits the message

sender address and the amount so the messagesender address is my account over here and the amount is 12 because I sent it 12 Wei And if we are going to send some calldata along in some Wei then it should trigger a revert automatically because our fallback function is not payable and if we are not sending any Wei along then it's just going through because our fallback function doesn't do anything The last one that I want to talk about is the new try-catch statement that is kind of really interesting, because it is it kind of turns out that you can catch errors and with that I had the feeling that atomicity of transactions are not given anymore

So when you can catch errors from external function calls then in your external function call there is a problem then it kind of is like going back to this low-level function calls with callvalue()() where you can check if the the return boolean is either true or false and I found this kind of interesting that you can do this now with actual external function calls so let me give this a try over here I also have no 0515 example because in 0

515 it just didn't work this way I just want to give you an example of how it looks like in the new Solidity 060

I have a contract A and I have three different failures that can happen over here I have one function a require failure so that's gonna send require false and an error string I have a function B revert failure so that's gonna call a revert Then I have a function C assert failure It's gonna have an assert false and assert doesn't take any error strings at the moment

All right then I have a contract B which has a contract A instance creating a new instance of contract a in the constructor and then it has one function called testRequiredTryCatch and in this case it's for now it's calling try instAfunctionARequireFailure() and then it has these catches and for me it's gonna be interesting what kind of event is going to be emitted because we have two different events The one is an error event that you find over here and the other one is a low level error event and I want to see which kind of error is going to be in which branch of the catch statement So let's give this a try and I'll remove this one over here and contract B is the one we want to deploy so we deploy that one and try this you see that we are just returning a false over here so we don't get an area for our transaction I have to be careful about that extremely careful about that so you might want to think about how you handle errors and how you try and catch those errors just it gives us a lot of new opportunities to have bugs in our smart contracts and over here we have the event "Error" that is emitted so in this case is going into this Error-String error branch of the catch statement and it's emitting an event called "Error" over here so let's give it a try with the other function the revert failure function gonna save this, gonna deployed this smart contract and give this a try so we are also having an error event it is also going into this well this part of the catch statement

Whoop this was the wrong button, let's try this one so let's try the assert failure or the evaluation of false of an assert statement and this is going to give us the low-level error and currently there is no reason given it is just, well, empty but it's gonna be the low-level error event that is being emitted yeah then are a couple of new features that I don't wanna talk about so much but these were the ones that are really outstanding for me especially in terms of development and introducing new ways of developing your smart contracts in combination with possible bugs and possible problems from a security standpoint there's definitely a lot more things coming up I'm gonna be interested in that and if you like this video then subscribe to this channel There's gonna be a lot more of this and I'll see you in the next videos [Music] Checkout my Website: https://vomtomat

Source: Youtube

This div height required for enabling the sticky sidebar