1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Smart Contract Development with Solidity

Starting, Stropping, and Interacting

Start course

This course provides a deep dive into the Solidity programming language which is used to implement smart contracts on blockchain platforms. This course will give you a practical understanding of Solidity and how to develop your own smart contracts.


In this lecture, I want to talk about how we can start, stop, and interact with smart contracts. Now this sounds very straightforward, but there are a couple of moving parts I want to highlight here. Now the first thing that we already discussed and we used in the previous lectures, is how to start a smart contract. By now, you probably know when you write solidity code, then it gets compiled through a compiler with a specific version, and the versions are constantly updated. So, be aware that in one week or two weeks down the road the version will be different and there might be other features, so you might want to lock in the version, but that is a different story. Really, when you deploy a smart contract, what happens there? And this is what I want to discuss in these few minutes now. So, I'm going back to my Run tab, I select the JavaScript environment, and I hit 'Deploy'. And what really happens here is that transaction is sent off, and the transaction has specific properties that are used to deploy a smart contract. And there are two properties that I want to highlight: the one is the "to" field in a transaction. In an ethereum transaction, that is empty. And whenever you send off a transaction without a to field set or with an empty to field, but with a data field set, which I don't know why in remix it's called input here, but if you send off raw transaction with ethereum, then it's called the data field. And when the data field is set, and the to field is empty then the miner will know, okay, this one is going to be a smart contract. And because it's a smart contract which is going to be deployed, it gets its own address. So, every smart contract is running on its own address, but the address is not random; the address is deterministic. And the address is created based on the address of the account that is sending this transaction, and in this case it would be this address, plus a so called norms. And the norms is a number that starts from zero and increases every time you send the transaction. Now, when you take a look here this HelloWorld smart contract here is running at a specific address 0x692 and so on 77B3a. Now when I hit deploy again, then another transaction will be sent off, and another smart contract is here. Same smart contract, but running under a different Address 0xbbf and so on 737db. Even if I remove this, and I hit 'Deploy' again, the new smart contract will have a completely different address. And what we have here is now free transactions, from this address here, that are deploying free smart contracts. Now, here's the deal. If I go and reload my browser window, then because I'm using this JavaScript virtual machine, then the JavaScript machine will start from scratch. It removes all the data that is stored in its memory, and will start from zero and nothing again, and we'll have again 100 either here. Incidentally also my norms will be back to zero. So, every time I start from scratch, let me start from scratch, reload this page, my norms will be zero. And when I hit 'Run' and 'Deploy' the smart contract, then the smart contract, the first one will have again the same address. You see here the address is deterministic. This is the first thing. So, whenever you start a smart contract, really, the solidity code or the code if you read it in viper or whatever, it will be compiled down to byte code, and this byte code will be brought into a hex format, and this hex format will be sent off in a transaction with the data field. You can actually look into this hex from it. If you go over to the Compile tab, and then you click on the details here, then you see here a pop up where it says BYTECODE, and here you see the actual, hex encoded byte code, and you see the the EVM up codes, to instructions to the EVM based on this BYTECODE. So, this is really, the smart contract that is compiled from this solidity source code. Very, very, cool thing. Then you need a couple of more things that I want to discuss in this lecture. So, by now you know, new transaction sent off, smart contract is running on the Blockchain. Now then we have this interface here, and this interface isn't random. This interface is set by, or if you compile, source code then you will get two things: the one is the Bytecode and the other one is the so-called ABI array. It stands for application, binary interface, ABI, application, binary interface. If you deploy a smart contract, then all you see is this byte code or this hex encoded byte code. You have no idea how to interact with this, code that is running there. There is no real way to find out, whether the interfaces, how to expect back from the smart contract where you can be compile it, but you will not get any beautiful names back anymore. You will get like cryptic names back; how to interact with the smart contract because, when you hit for example this 'Debug', let me run this here. Let me get  my Uint, and then hit the 'Debug' button. It should actually open up the debugger. No, you don't like me today. Let me redeploy this here. The debugger doesn't like me today, seems to have a bug today. Nevertheless, you see that whenever you try to interact with the smart contract, then you run through a stack of up codes. Now here's the thing. If you, don't know, this interfaces up front, then you don't know how to interact with your smart contract, what kind of arguments you have to send in, what kind of return arguments you get back, like uint256, and this must be known in advance, and that is the so-called ABI array. I'm going to hit the Details here in the Compile tab, Details, then you find here an ABI part, and this consists of several child objects. I'm going to open them up. You see immediately that those are representing the functions that we have from our smart contract, and how to interact with it. So, we have a name called, set my uint, and incidentally, it is the same name of our function here, SetMyUint. Okay, this set my uint has one input, which is a uint256 with the name _myUint. You shouldn't be surprised. There is a uint256 with an _myUint. Now, the function is public and it has no output parameters. It doesn't take any ether if you send them, and that's it. So, the second one is writeHelloWorld. It also has one input string _helloWorld. The getHelloWorld function is so-called constant, and disposing all the solidity versions below 0.5.0, below 0.4.24 or 0.4.17. The view keyword, was introduced before that. It has been named constant. So, whenever you see a constant, in this function line or describing what the function is doing, then you know that was for an older solidity version. We are not running constant anymore. We write view functions now. Okay and so on. So, based of this ABI array, remix will automatically create these patterns. So, setMyUint, writeHelloWorld are writing functions. They are not constant. They're not U functions. getHelloWorld, Hello world, myUint are view functions, they are not writing, they're just reading functions, they are going to be blue. And based on also on the ABI array, remix will create these input fields. So, when you have a function which gets to input arguments, then remix will automatically create this based of this ABI array. Which brings me to the next thing. Now you have the address of the smart contract, you have the byte code of the smart contract, and you have the interface of the smart contract. With this, you can actually, interact with the smart contract, in a way that the EVM understands your arguments, and your return parameters or return arguments. And when I copy this address here, and remove the smart contract, then you see there is no new transaction here. It just removes the smart contract out of this view. And I can always say, this smart contract which I have here on the left side is running under a specific address. Again, observe this log window here. I remove the smart contract, nothing is happening. I add this smart contract again, nothing is happening here so we're not sending off a transaction. All I did was tell remix that take the ABI array from this smart contract here, so take the interface from the smart contract, and show me something like a platform, to interact with it. And now I can simply type in five, I set my uint and return my uint, and it will give me five back. I will try to reload the page once more, because I really want to show you the debugger for this. It's very important. All right, here we go. Now the debugger is working. And when I scroll here then you see, over here on the top, that it will scroll through the actual up codes that are executed, when the transaction hits a note where the smart contract is running. So, this is basically, what it does, and when I send off a transaction with a specific data field. In this case, it would be 0xe49, and so on. Then it will take the input and push it onto a stack and then it will compare the first four bytes, which is the first eight hex characters, to something that is already deployed and when it fits this value then it will jump into the function. And that way, you need to have the function name first in order to create the signature. This signature is the k zak hash to be very exact of the function name including the function argument types. Then, from that the first four bytes, so the first eight hex characters. So, when you look at the input then you see 0x and here we have E49. So, keep this E49 in mind. And when I scroll around, then at some point it will say PUSH4 and then this value E492F and so on. And then it will ask, does it equal to the thing that is already on the stack? So, it will take this value and the value from the code and when this equals then it will jump to a specific point in the code. So, when you have a look at this 0101, 0104, 0105, then you will see that it will jump over here. You see this? It will jump directly into the function and you can also see this here when, where the function is highlighted in blue. So, it found the function based on the transaction. And we're going to do this later a lot more when we talk about debugging, but for now just keep in mind that you need the ABI array, this array that is generated from the compiler that tells the outside world how to interact with your smart contract because you need these actual function names and the function arguments in order to interact with the smart contract the right way. Now, the question is now, how can you actually stop a smart contract? Now, I deleted the smart contract. Let me copy this. I deleted this smart contract but then I can add the smart contract again, so it's not really stopped. How can you make a contract stop? And there is a specific keyword for this, let me stop the debugger here. There's a specific keyword for this and the keyword is actually a solidity command that says suicide. I'm going to have a function here and be very careful with this command, I'll just show you now how it works. We are also going to talk about later, how suicide works. So, I go type  a function, and I call this function killMe and I make this public and inside this killMe function, I will say suicide and that is actually deprecated. So, there is a new command for this which is called selfdestruct. That's deprecated, suicide is deprecated since version 050, I think. So you now want to use selfdestruct and it takes one argument and that is an address that gets the rest of the funds which are stored at the address of the smart contract. Sounds a little bit confusing if you use the msg.sender object which we're also going to talk later about the msg.sender object will always contain the address that sends the transaction to the smart contract. So, in our case when I hit 'Deploy' here and I have this address here selected then when I click on 'killMe' and the transaction is sent off to the smart contract which contains a from field with the address which is the same as this account here which I selected. And in our case, this address now will get the remaining funds which are, there are none in our smart contract but if there were, then this address would get the remaining funds and the smart contract will be destroyed. Now, when I try to interact with my smart contract now I will get an error message. And from this block onwards, my smart contract doesn't exist on the Blockchain anymore which doesn't mean that he is wiped off the Blockchain because once you deploy a smart contract, you send off a transaction. This transaction is baked into a specific block, let's say block number five, and then you interact with your smart contract and, let's say in block number 15, you call the selfdestruct function, and from block number 15 onwards, your contract will not be available anymore but the data is still available if you want to go back in history if you want to do all it or something data is not going to be removed from the Blockchain. It's just that from onwards, there it is not available anymore. And that's it for this lecture. It was quite a lot. There is no real challenge at this point but what I want you to try is take your smart contract, deploy your smart contract using the Javascript VM, and then just call this setMyUint or write helloworld and try to follow the debugger, how it goes through the code and see where it stops or where it goes into the actual function. Not necessarily understand what it does, but have the debugger used one time just to see how it works and then head off to the next lecture where we are talking about Getter and Setter functions. I'll see you in the next one.

About the Author

Tom is a CTO, senior back-end developer, and systems architect with over twenty years of hands-on development experience in a variety of languages and systems. He has a CS master's degree and has been working with Ethereum and blockchain technologies since 2016.

Covered Topics