This video teaches how to implement function modifiers in Solidity smart contracts to control access and behavior. The instructor demonstrates creating custom modifiers like 'only owner' that restrict function calls to specific accounts by comparing the caller's address (using MSG.sender) with a stored owner address, and implementing time-based access control using block.timestamp to restrict function execution to specific time windows. These modifiers help enforce business logic and security rules within smart contracts.
Solidity Smart Contract Functions: Visibility, Modifiers, and Time
Added:hey everybody this is Gregory from dap University so in this video we're talking about writing ethereum smart contracts with the solidity programming language and I'm picking back up in a series on solidity we're talking about writing functions inside of smart contracts and you know what you can do with them so be sure to subscribe to this channel if you haven't already and click the like button down below that really helps these videos get found so that more people can learn how to build blockchain technology I'm going to go ahead and pick up where we left off in the last video I'm going to use this you know my cont track that we built where we added a person uh person struct to this mapping of people um if you haven't seen that last video go ahead and check that out it's not necessary but you can probably follow along with this video if you want to what I'm going to do now is show you a little more about these modifiers like public um and things like that I think we talked about that in the first video but I'm going to go just a little further so what I can do is you know show you another modifier which is basically like internal um so that's different from public you know public is a function that can be called uh you know on the public interface of the smart contract you can see you know the public functions listed here on the side so go and create a private function or an internal function excuse me we'll say function uh we're just going to take this people count and wrap it in its own function to show you how that works we'll say uh increment count we'll just make this a function and we can call it uh internal that's a different modifier we'll just take this people count we'll paste it inside of here here all right and we'll just say increment count call that function and we'll run it see the smart contract We'll add the person we'll say dap oops dap University and I'll click add person and there we go it incremented the count we can see the people count has been changed you say Joe Blow and the Count's changed so that's a way that you can you know use other functions that are going to be internal and we can see this increment count function isn't uh added to this list over here it's not exposed to you know things outside of the smart contract external callers can't use it so that's an example of you know other you know types of visibility of functions and solidity let's talk about function modifiers so we can add you know more you know words and terms to the end of this function to change how behaves and I'll show you an example so we're going to create our own custom modifier actually inside of this smart contract so that only certain people can call this add person function all right so what I'm going to do is basically you know make this smart contract uh have an owner or like an admin and we're going to say that only the owner can call this ad person function and any other account connected to the network you know whenever they try to you know add a person they won't be able to and that'll show you how we can add an extra modifier to this function to make that happen basically we'll just add a modifier that say that only the owner of this smart contract can do that so first we need to well actually that'll just look like this we'll say only owner that's what the modifier will look like now this only owner modifier doesn't exist yet so let's go ahead and create it all right so we'll do that like this first need to keep track of an owner we'll say the owner is um you know address I'm not sure if we talked about this in the data types uh video but you know an address is a data type inside of solidity you know like an address that's on the network and account so this will be the address we're just going to declare it here we're not going to set it just yet uh we can set it like this we can do it inside the Constructor well actually not that just yet um so that would be the owner we basically just create a modifier like this we say modifier say only owner look like a function and what we'll do inside of here is write the logic that makes sure that you know whoever's you know calling the smart contract is the owner so that's what we're to find here only owner and only owner so how do we do that how do we say the person who's calling this function is the owner of this smart contract well we're going to compare it to this owner right here but how do we know who's calling the function well solidity has a global keyword called uh MSG which basically stands for the function metadata that's passed in we're not actually going to pass in any metadata here um it's going to be implied so basically we have uh access to mg.
sender and this is basically a special uh you know thing inside of solidity that tells us the account you know this address who called the function and basically we can just say you know is this person the owner we can just you know compare equality of the person who's calling the function with this owner that we're going to store here in a second all right and if they're not we actually want to throw an error all right so this is going to show you another Concept in solidity about error handling so we can throw an error in solidity like you know I think earlier we had some errors happen like here's an error we actually want to trigger an error if this person um you know is not the owner we want to we want to revert the transaction and we can do that like this we say require um sorry MSG do senders equal to the owner so basically anything inside of this require um if if it evaluates to true then this passes if it evaluates to false then this will throw an error we basically are saying require that whatever you put inside of here is true all right and then after this we can basically just do this all right and now our um only only own modifier is complete so basically now we have this modifier that's you know defined here and we add it here and basically we say if the person who is you know calling this function is the owner uh then you know we can actually run this function and if they're not we're going to you know create a failure we're going to revert the transaction and also whoever is doing this you know whatever uh like this code won't run so they won't pay this gas fee all right so now we need to set the owner and I'm just going to do that inside of Constructor we'll use MSG do sender as well say uh function Constructor and we'll just open this curly braces and we'll say owner equals to MSG sorry MSG do sender sorry we don't need the function keyword here and we also need to make the public all right so I'll save that so whenever you deploy the smart contract like you know this Constructor gets run and the MSG do sender is actually the account that deploys the smart contract and they're going to get set to this owner State variable right this address owner and basically that same you know address is the only person that's going to be able to call this add person function all right and if they um you know this code will run and we'll be able to add a person and if we switch accounts to some other account over here then it won't work so let's just try that I deploy this we will uh you know deploy with this account so let's change to that account We'll add the person actually I don't know if it deployed that account or not let's try it again so make sure on the first account we'll deploy all right add a person we'll say dap University add the person all right let's see here all right it worked your people count is one now we'll say you know Joe Blow and we'll change accounts to this one we'll try to call it again add person and we see it has failed and the people count has not changed at all so it worked so we've kept track of the owner we say only owner can do it now while we're here I'm going to show you a little code formatting that I like to do sometimes this is getting kind of long and sometimes you know if you're like me and you have a text editor with a whole bunch of pains open uh I like to keep my columns kind of short sometimes especially with solidity and it kind of makes it easier to maintain these functions when the arguments start getting long and you're using git and things like that sometimes I will just break these up like this all right and then I'll actually put the modifiers if you have a bunch of modifiers sometimes this makes it easier to read and also when I'm writing my own solidity source code I only use two spaces I don't use for um but yeah that's sometimes how I break these functions up so I can see the function name and then the arguments um then I can see the modifiers and then I can actually see the code that gets executed inside here and it can make your smart contract kind of long but it can be sometimes this will catch you if you have too many you know if you have three visibility modifiers on here sometimes it can be kind of tricky so I like doing this all right so next I'm going to show you how to work with time in solidity okay so what we can do is basically compare time and solidity and instead of um this saying only owner can do this let's say you can only call this function if a certain time has passed so let's pretend like you know this is uh a contract that is only open at a certain time so we'll say instead of only owner we'll say only while open only while open all right and this opening uh state is going to be determined by a time so once we've passed a certain time in history in the future then we'll let you call this function if it's before that we won't do it so that's really useful if like you're building a crowd sale or something like that that has an opening time and get an Ico smart contract and you say hey we can only let you contribute ether like after you know the first of the month well you just figure out what time that is and say Hey you know if you make a contribution before them we'll throw an error so I'll say only while open all right and instead of requiring that uh MSG that sender is the owner right we actually going to just take this out we're going to say uh we're going to make sure that the current time is uh in the fut Beyond a certain like uh opening time so let's do ENT 256 opening time all right so how do we do this well we need to set this opening time somehow all right so the opening time is actually expressed in seconds and you know inside solidity or these you time time stamps are expressed in seconds inside solidity and you know seconds of what well it's it's epic time which if you're not familiar with that it's um Concept in computer science that's like the Epic is a is a is a specific time I can't remember the actual date it's like a date in the 60s or 70s or something like that um and basically we just add seconds since that point in time in history so this is the current epic timestamp is this many seconds uh since the Epic time and I guess you there probably a link here where you can read more about that yeah the Unix epic clock um yeah I'm sure you can get on Wikipedia and figure all that out but basically uh this is the current epic time and it's changing so we use these seconds values so this is like the current you know epic timestamp we'll say this all right so this is how we could set this and it's you know un 256 is going to store this big number of of seconds since that time stamp and that's going to be our opening time and so how do we compare that to now so how do you get now in solidity well there's no perfect way to do it but the best way is to get the current blocks timestamp all right so how do we do that well just like MSG there's a global uh variable in solid uh called block Dot and we can say block.
timestamp all right and we can say is the block. uh Tim stamp is it greater than or equal to uh the opening time all right so basically if it's after the opening time we're going to let you call this function and if it's not um then we won't all right so let's give this a try I'm going to update this time stamp let's refresh it let's see this is the current one I'll paste this in here uh so 15 I'm going to add 60 seconds so a minute um so I'm going to deploy this and it will be uh it shouldn't work right now so we can try to call add person I'll try to add dap University okay we'll add person and we'll see that it reverted okay and now we can check the time stamp when not there yet so I'll just pause the video and wait for this to pass 75 I think that's the actual time yeah 75 all right I'll pause the video pause the video giving us plenty of time we're past the time stamp you can see at 793 and we're at uh 775 so let's deploy it again well actually let's not deploy it again let's just do Joe Blow add person and it worked so that's how you can use um time and solidity you can get the current time and set a time value with seconds and so that's what I'm going to call today guys I hope you all like this video let me know how you're enjoying these solidity videos let me know if I missed anything or there's something you want to learn or if you're just not liking them that's okay too just let me know down in the comment section below and also be sure to subscribe to the channel if you haven't already and click the like button down below because that really helps these videos get found so that more people can learn how to build blockchain technology and it also helps keep me making these videos for you all all right until next time thanks for watching DPP University
Up Next

EVM Explained: A Deep Dive into the Ethereum Virtual Machine's Architecture
@uttam_singhk
10.2K views•2023-03-07

Torrent File Format & Bencoding: A Technical Deep Dive
@AsliEngineering
12.5K views•2022-08-08

Solidity Programming: Complete Blockchain Developer Course
@DappUniversity
156.2K views•2020-05-22

Understanding Ethereum: A Comprehensive Beginner's Overview
@99Bitcoins
3.1M views•2018-06-26
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Blockchain & Crypto














![สร้าง Smart Contract ด้วย Solidity | สำหรับผู้เริ่มต้น [จบในคลิปเดียว]](https://i.ytimg.com/vi/WoGIjHPIc8A/hqdefault.jpg)





![SOLIDITY – БАЗОВЫЙ КУРС [Урок 3]](https://i.ytimg.com/vi/EYZUabLpgUc/maxresdefault.jpg)






















