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


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 the previous lecture we were talking about mappings and I'm pretty stuck with very much the same smart contract that we had in the previous lecture. It's just the last version where we deployed a smart contract with a deposit mapping, withdraw mapping and this MyBalanceForAddress mapping, which stores the balance of the deposited amount of way in this MyBalanceForAddress mapping. And you can also find the same smart contract in the file C08Mapping instructing our course repository. Now, let me reload remix. So, I start with a clean log file here, log window here. And one thing that you are probably running into at some point during your smart contract development is if I deploy this smart contract and I'm going to deposit let's say one ether then okay it's in there but I don't know which address already sent, which address already sent, anything at all which address already interacted with my smart contract. So, I would need another mapping that maybe says something like (address => bool) public myAddressInteracted and then I can say ;myAddressInteracted[msg.sender] = true. So, I know that this address interacted with my smart contract and then I maybe have another mapping which is maybe a mapping(address => uint) and then I could say lastTimestampOfAction; And then I would say lastTimestampOfAction[msg.sender] = now; and we are talking about the times in solidity a little bit later. Now is a global keyword that is available and gives you the current timestamp of the mining of the current transaction that is happening during execution of that code. So, it is pretty much is the current timestamp, but it can vary a little bit depending on who is going to be the miner of the current transaction. All right. This is all not very good if you have so many mappings and they're doing so many things which are actually the same. So, wouldn't it be better to just merge that into one new data structure which behaves very much like an object and before you go ahead now and say well let's create a contract with a lot of public variables that is an unsigned integer for balance, boolean for if the address interacted or not and an unsigned integer for getting the timestamp and then make an object of that smart contract and then apply this here. There is a better way which saves you gas and this is all where it comes down to on the solidity programming language around the ethereum Blockchain you want to save gas. You want to make it as gas saving as possible. So, this is where structs come in and structs start with the keywords struct and then you have a name for your struct. And in this case I just call it my struct and then come curly brackets and inside the curly brackets you define which data types you want to have, now, which data that was members you want to have and which data types. So, in our case it's a uint balance; and you have a semicolon here at the end it's not like a coma, it's a semicolon and then a bool isSet; and then maybe another uint lastUpdated; And then you can use this struct with the name MyStruct as a data type very much like u in the boolean, you can just for example take this key and the value type and say the value type is going to be myStruct and remove the other tool and then you come to the point where you have to access the balance in myBalanceForAddress mapping and that works a little bit different than before because before you had a simple, a simple value which was an unsigned integer but now you have to assign this to the balanced member of the struct which is over here in my balance for address mapping inside for a specific key and you do this by adding the dot and then balance. So, in this case you are going into your mapping for the message sender for the address and inside here you have to struct them not only unsigned integer, but several different members. And to access one of these members you have to add the dot and then the, the name of the member. And here you can assign it to a specific member than the value that you want to have. Same goes with the... isSet. So, that would be true and the last updated isn't working much differently. That is going to be now. All right, now obviously our withdrawal mapping or withdrawal function also has to be changed and it's not much more complicated than before but it's going to be the part of our current coding challenge. So, if you want to do the coding challenge then just expand your mapping that you have in remix now with the struct, assign the struct here's a value type and use the deposit mapping function. Actually, I'm going to change this to deposit struct. So, you know the difference in our smart contract  in the repository. So, everything has a neat name and then expand this function in order to have a normal withdrawal functionality. If you want to do the coding challenge then pause the video now or else just keep it running and I will explain to you in a second how this works. All right now, let me explain to you how this works. It's not that hard. You add, you have to access the right balance here. So, the member of the balance is accessed with a .balance and if the balance is large enough then you have to deduct from the balanced amount and transfer the amount over. Let's give this a try. I deployed a smart contract, I'm going to deposit one. Now, let me first get for my address the current mapping and the mapping is going to have the myStruct for my address and also this one is initialized with the default values. So, the balance is zero is set as false. Last updated is zero. And when I deposit one ether now and I reread for my address the mapping then I see I have now balanced one ether is set is true because we're setting this to true every time somebody deposits something and last updated is the timestamp where I accessed this deposit struct function. Now, let's withdraw the money again and I'm going to withdraw one ether 10000000000 withdraw and I got my ether back, I guess I hope. and maybe I made a mistake, maybe I did not. I missed one zero. So, let me just copy and paste this here. Now , my balance is zero here and I got my ether back here and everything is working just fine. All right, this is how you use the mappings and structs and we're going to use them quite a lot and even if you are looking into smart contracts out there, any of them usually use mappings and struts in one way or another. They are really, really powerful and good concepts to work in a gas saving way when a resource saving way [inaudible] and this is the main part now. In the next lecture, we are going to talk about view and pure functions. We were working with them before but I really want to highlight a few parts of them again. And after that we're talking about concepts of time exceptions and then we're pretty done with this section. And you can, we can start focusing on our game mechanics and you can also go out and already read, I would say 99.9 of the smart contracts out there, you should be able to understand them. All right. I'll see you in the next lecture. If you have any problems, if you have any troubles, head over to the course Q&A and I'll see you there.

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