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

Loading player…

Errors Triage

BaseMon, Oct 9, 2023, 12:00 AM

Identify and resolve common errors in Solidity, including compile errors and logical errors. Discover how to debug and fix issues like type conversions, index out of bounds errors, gas errors, underflows, overflows, and division by zero errors. Understanding these errors will help you write more robust and error-free smart contracts. Avoid common pitfalls and gain confidence in Solidity programming! 🚀💻 📚 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

in this lesson we are gonna learn how to identify and resolve common errors in solidity we have separated two types of Errors for this lesson we have compiled errors and logical errors let's start by exploring common compile errors compile errors are the easiest ones to debug since normally the errors appear in the message and they usually tell us what is Quran and how to fix it in this case we are starting by seeing that one is not implicitly convertible to the expected type in this case we are expecting to return a u in however we are specifying one as a string in order to fix this error we can simply say one and that is working now okay now let's explore another compile error in this case conversion error in this case as you can see we have a end 8 that is called first however this function is expecting to return a uint so in order to solve this problem we need to explicitly cast however as you notice is not working yet sometimes we need to do multicasting in order to make it work all right as you can see now is compiling all right let's see another type of error this one is related with the operator in this example as you can see we are returning a uint but some is a unit so the first thing that we can do is we can fix this one but now let's see the error in the compiler it says that the operator plus is not compatible because first is a int 8 and second is are you in so we need to convert this one to uint and we do something similar to what we did in the previous example remember that sometimes you need to do casting multiple times and there it is now e words okay all right another common error is the stack depth limit one of the limitations of solidity is that we can declare at maximum 16 variables in a function so since solidity only allow us to have a maximum 16 variables in a scope at the time the way we solve this issue is actually by splitting the code into multiple sections so what we can do is for instance clear a variable called result a and then we create a block of code where we say that result 8 is essentially going to be one or first plus second third and so on until eight but if you notice we are encapsulating in this block of code okay now we can do something similar here essentially by splitting in let's say result B and then we simply say result B is going to be equals in this case 9 10 and so on okay and then finally we simply say return result a plus result B and in this way we are splitting result a result B and now at the end we simply sum each one of these values and that's how we solve this problem okay all right now we are gonna cover another category of errors in this case logical errors these logical errors occur when the code is syntactically correct but the results in a data state is violating the rules of the language when this happens we get what is called like a panic error because we are doing a illegal operation now in remix actually is a bit difficult to debug this because it hides that particular information however we are going to look how to find the error okay now in this piece of code if you remember arrays are index 0. okay so that means are based on index 0. so that means that the first element is going to have an index 0 the second one two and three so here we are trying to access because the Lan is equals to 4 because we have four elements we are trying to access the index 4 and there is nothing here so we're gonna get a index out of bounds error and that's gonna cause a panic now let's test it and here as you can see we are simply getting a report but we are not getting the details okay this is because as I mentioned ramix is hiding that however we are gonna see more by clicking debug now here let's jump to where the coil was reverted and let's find the memory what is happening in the memory in the memory we are seeing that the first element has this hexadecimal value now solidity says in the documentation that when there is an error of this type is going to return a panic this Panic error is actually this code but how do we know it's a panic well if you calculate the hash of panic and this you're in value we are gonna get the following for E for a 7B 7 1. and when you look at this can you see they match together so in that way you exactly know that it's a panic but this is just the way solidity encodes the value now after that we also have a code in this case it's authority too and if you see solidity has a table with a different code in this case 0x32 is you are accessing an array and you are getting an out of bounds error okay which is exactly what we are getting because we are accessing an element that doesn't exist okay all right let's go to the next example this is a gas error and that can happen if let's say we are trying to run an unpredictable function or unpredictable functionality in solidity so here I'm simply saying that this thing is going to run forever and I'm gonna try to run it as you can see also ceramics has a way to specify gas I'm gonna put a lower value and then I'm gonna call it so here I'm getting essentially the out of gas all right so don't run unpredictable code now let's review another flow example in this case we have to you ins and we are subtracting two from the first one that has a value of 1. so that is gonna create an issue because it's gonna result in a negative number and this value is not intended to be negative so let's run it and as you can see we're getting a rebirth now let's explore the error with the debugger and remember we can jump to the error find memory and as you can see we are still getting a panic however here we have the value 11.

and the 11 as you can see is the underflow or overflow all right so let's try to solve this issue by perhaps changing the data type and saying okay we accept that there could be negative value so we change the data type and now as you can see we get the right values because this data type is expected to also be negative all right all right now let's see the the buy by zero error let's run it and now I'm gonna try to call it okay as you can see we are getting a rebirth and now let's debug here let's go to memory as you can see I'm getting also a panic but now I'm getting an error 12. and if we check the table at 12 is an error if you divide by zero okay so don't divide by zero that's the solution

Automatic transcript — names and jargon may be misspelled.