Solidity is a statically-typed, object-oriented programming language for implementing smart contracts on blockchain platforms, featuring essential elements such as data types (strings, booleans, integers, addresses), state and local variables, functions with visibility modifiers (public, private, internal, external), built-in modifiers (view, pure, payable), custom modifiers, constructors, global variables, operators, conditionals, arrays, mappings, structs, events, ether handling, error management (require, revert), inheritance, and contract interfacing, enabling developers to create decentralized applications including tokens, NFTs, and DeFi protocols.
Solidity Programming Tutorial: Smart Contract Basics in 20 Minutes
Added:today i'm going to teach you the solidity programming language in less than 20 minutes so that you can become a blockchain master in no time so you might be thinking like how is that even possible well today i'm going to give you a really fast-paced introduction to the really important parts of the solidity programming language you're going to use over and over again and if you learn all these you're going to get up to speed really quickly and even if you know solidity already i guarantee you're going to learn something out of this video so trust me you're not going to want to miss this you're going to save this video bookmark it and reference it over and over again while you're learning the programming language so before we get started you know i put a lot of effort into making this short video just for you and the only thing i ask in return is that you like this video that really helps youtube know that you got value out of it and shows to more people also subscribe to this channel and share with your friends so if you're new around here hey i'm gregory and on this channel i turn you into a blockchain master and so if you get a lot of value in this video and you want to learn real world blockchain skills land your first web 3.0 job then i can show you how to do that over at that university dot com forward slash boot camp all right so with all out of the way let's get started alright so let's start with the basics so what even is solidity in the first place so it's an object-oriented high-level language for implementing smart contracts so what are smart contracts they're the building blocks of blockchain-based applications so they're called smart contracts because they represent digital agreements and the code cannot change and that's a feature of the solidity programming language once it's put on the blockchain so you can see the example of a web 2.0 application where you have a browser and a website that talks to a server backend like a database that may be a server written in python or php and you have a blockchain based application here where you have a browser that connects to a blockchain directly and all the code for that application is written in solidity with the smart contracts deployed to the blockchain so the most common use cases for solidity smart contracts are tokens so that's one of the beauty of smart contracts you can create a cryptocurrency without creating a brand new blockchain for nfts which are non-fungible tokens which represent in most cases digital collectibles although there are many other things you can do with nfts and then also decentralize finance or defy with things like savings and lending yield farming trading and also flash loans so you can do all these things with solidity because they fully feature programming language or a turing complete programming language and it's relatively beginner friendly so let's dive into the features of the language that you see how that works so you can write solidity smart contracts directly in your web browser if you go to remix.ethereum.org you can write all the source code here you can compile them also deploy them to blockchain so i've got a simple smart contract here called my contract okay so this is how you create an ethereum smart contract in the first place you start off with the solidity programming language version 0.8.0 and then you give it the contract keyword followed by the arbitrary name of your contract in this case my contract and you put all your code inside these curly braces here so let's start off with the basic features of the programming language like the data types and variables and functions so in terms of data types solidity is a statically typed language which means you have to declare the type anytime you're declaring a variable and that type should not change and so you can see that here like we say string because this is a data type string and this is a variable called my string which we assign here and this is a special variable called a state variable that means this value is actually written to the blockchain so when we put the smart contract on the blockchain we can call it public which is the variable visibility and solidity will give us a function where we can read the value of this outside the smart contract and you can see some of the other common types here like booleans and unsigned integers integers and also blockchain addresses and so these are some common types and examples of state variables so let's talk about local variables so in order to do that we have to also look at how functions work inside of solidity so you can see an example here solidity lets you create your own functions with arbitrary names we can see these are examples here like set name and get name and you can see that these functions take arguments like this string memory name okay this just sets the state variable here of example one so if i were to call this function i could update the value of this state variable name so in this case this name right here is a local variable that's only accessible inside this function and this is a state variable which is accessible outside the smart contract if we call it public okay or you can do get name here and it's also available everywhere else inside the smart contract so in solidity there are two main types of functions write functions and then also read functions so whenever you're writing your information to the blockchain you have to pay a gas fee so you'll pay some cryptocurrency fee whatever you call this function whenever you read information for the blockchain it's completely free and doesn't cost any gas so just like variables functions also have visibility so you can see example this function is public so it's accessible outside the smart contract and if i were to reset this function to internal it would be only accessible inside the smart contract and here's an example of all the different visibilities you can apply to a variable so a name a variable is no visibility available private visibility which means it's only accessible inside the smart contract an internal variable which is also accessible inside the smart contract but can be inherited and then public which can be called inside outside and also inherited and for functions you can see that like this here is a public function it can be called outside the smart contract it can also be called by another function a private function which cannot be called outside the smart contract but can be called inside of another functions of the same contract an external function which can only be called outside the smart contract and an internal function which can only be called inside the same smart contract like this from another function so functions also have built-in modifiers and solidity like view for example this means that the contract does not modify the state of the blockchain but it can read the state like read the state variable value name for example pure functions which do not modify the state and also cannot read the states they cannot read name for example and then also payable functions which are allowed to receive ether cryptocurrency whenever the transaction is submitted and solidity also lets you create your own custom modifiers which can be applied to functions so here's an example you could do modifier only owner this restricts who can call a specific function this requires the message sender the person who's calling the function is the owner of the contract so you create it like this and then you apply it to your function like this so there's one more function you should know about in solidity it's a special function called the constructor function which you can see this keyword here this is a function that's run once and only once whenever the contract is initialized or put on the blockchain so it accepts arguments just like other functions you can make it payable you can send ether and works pretty much like everything else except you can only call it once likewise there's a few more variables you should know about in solidity these are global variables which are available inside of any smart contract so some examples are this which corresponds to the address of the current smart contract that you're coding against message which has a few more values attached to it like sender which is the person calling the function uh tx which is the transaction you can get the origin of that transaction uh message.value which is the amount of cryptocurrency same with that transaction we also have block which is the current block on the chain you can get the number you get the time stamp the chain id and many other values as well so now let's talk about operators so a lot of times you're doing math inside of solidity with value types and you're comparing those types or you're adding those types together doing some math in some way so you can see it supports basic math like plus minus multiply divide exponents modulus increment decrement and here's an example of a bunch of different math operations all in one you can also check that things are equal not equal greater than less than greater than or equal to less than or equal to this applies for other types not just numbers and you have all the other standard logical operators like and making sure that both things are true or that either one of these is true or not to check something is not true and you can use these operators most commonly inside of conditionals to create if else statements so you can see example like this if this is true then return this else return this okay you can see it like this if something is true return this and then you know you don't have an else condition here and you can also express this with a ternary expression like this so solidity supports arrays which are just sorted lists of information you can declare an array like this by giving it the data type okay and then you can you know declare it in line just like this you can also initialize an array without a specific size you can initialize it with a specific size like this is an example of an unsigned integer array with 10 zeros you're starting out you can see an array of strings just like this and you can also do things like read from arrays and manipulate them from inside of functions with the normal functions like reading just like this this just gets the value out of a given index there's your base index just like most other programming languages you can get the length of an array like this you can add a new item to the end lift push like this you can get the last element remove it with pop and then you can also delete an element at a specific index like this so one of the most common ways to store information inside smart contracts is with mappings so these are key value pairs that work like associative arrays or hash tables and other programming languages where you have a key with a corresponding value so in this case you have a mapping declared with the mapping keyword okay you give it a key which in this case is an unsigned integer and a string which corresponds to the value so in this case this might be a mapping of names where you have a uint like an id and a string that corresponds to the name that you want to store here so you can also see a mapping of addresses just like blockchain addresses this is pretty common balances where you have an address corresponding to a number of maybe cryptocurrency that's held in the smart contract an address with a boolean for to see if someone might have voted or taken a specific action you can also do nested mappings where you give it an address that corresponds to another mapping within this mapping which might be uh you know a boolean value on whether a user has taken a specific action based on a un value and then you can access and manipulate mappings inside and outside the smart contract like this so public will give you a reader function where you can fetch a value from a mapping by passing in a key as the id okay you can see an example of a mapping read inside of a function like this simply by passing in the value for the of the uh the key and then setting the value by passing in the key and then assigning a new value like this and then also removing an item from the mapping by passing in the id and then calling delete so solidity lets you create your own types with something called a struct so you can create a struct like this and give it a name this example of a book struck i've just created this out of thin air so i created a book struct with a capital b and i've given it several attributes like a string title string author and the string completed whether i've read the book or not and then here i can use that struct inside an application like this here's an array of books in my collection okay i can add a new book to the collection like this i can create a new book like this saying book then passing in title author and saying false that i haven't read it yet i can push it to the array just like this and then i can get the uh book from the array like this i just get book storage book and then i return the attributes of the book from this function and i can also mark that book as completed in my collection with this complete function i read it from storage and then i fetch the book like this and then i just mark it as completed and that will automatically update it so another feature of the solidity programming language you should know about are events so what are they well solidity lets you subscribe to events from an external consumer to find out anytime a function or something inside of a function has been called so in this case we have an event here that i've created called message updated all right this has some different arguments or attributes i should say the user who called the function and also the message so you can see that here the update message it just takes a message and then it updates the message inside the smart contract and it emits the event so you trigger events like this with the emit keyword and then you pass in the values which you specified here and so anytime that this function is called there will be a record of it on the blockchain that i can go back and look at later very easily and by finding this event so i can also get a notification anytime this function is called in real time because i can subscribe to this event externally and maybe get a push notification or something like that so i can also get an entire history of every single time this uh message has been emitted and that's another useful use case for events as well so i should also note that these you know attributes inside the event can be any data type that you want to and you can have up to 17 of these and you can also index up to three of these so what does that mean index well in this case we can see indexed user so that means that i can filter through events or subscribe to events that only pertain to this particular user so instead of getting a push notification every single time an event is called i can filter out events that are only called with a specific address in this case so another thing you should know about inside solidity is working with the native cryptocurrency of ethereum blockchain ether so you can see different expressions of ether coded out in the smart contract right here so ether is divisible by 18 decimal places so just like the us dollar is divisible by two decimal places ether has 18 zeros after the decimal place so the smallest unit here is whey another common value is gray which is used to pay for gas most commonly so you can see those in the contract you can see shortcuts for this one way is equal to one one gray is equal to this much and one ether is equal to one followed by 18 decimal places you can receive ether directly to a smart contract with the receive function if you call it external and payable you can also use the fallback function if no receive function is implemented and if msg or message data is not present and inside both of these functions you can also implement any type of arbitrary business logic that you want so you can see an example of here as a line that up updates this count by one you can check the balance of any address inside of a smart contract just like this in this case i'm checking the contract balance address this than just calling dot balance and you can also send ether to a different address just like this with call now there are other ways to do this but this is my preferred way because you can check for the success of this message and you can add a require statement just like this and the last thing i'll say about ether instead of smart contracts is that any function can receive ether whenever the transaction is sent as long as you use the payable modifier built into solidity and then you can access this value inside the function with message.value and then also the sender with message dot sender and so speaking of this require statement right here i'll make a quick note on error handling inside of solidity so we'll see two examples here the most common way is to use this require statement so this is a essentially a function inside solidity that checks for the value is true or false so you can see here examples just seeing if a value is greater than 10 if the value is greater than 10 then this function will succeed and you'll see you know everything after this function continue if this value is less than 10 then this function will stop execution and this error message will be raised okay another common uh function is the revert function okay as you can see uh value's less than or equal to 10 then revert and then you know if it's okay then continue on execution with the function as well so both these two will get you really far you can also define your own custom errors and use things like assert but these will get you started so another thing you should know about is that solidity supports inheritance so you can create smart contracts that inherit behavior from other smart contracts so in this case you can see my contract inherits from this other contract called ownable you see contract ownable this basically encapsulates all the behavior of a contract that restricts function calls to the person who deployed the contract to the blockchain or in this case the owner so inside the constructor we set the owner as message sender we stored as a state variable here we create a custom modifier that says only the owner can call a specific function in which this modifier is applied to so we can inherit that all that behaviors have our smart contract with one line we can say my contract is ownable so you can either import this from a different file or you can just have it in view in the same file okay and then here you can see we have this uh ownable modifier that we can apply to uh this set name function and also note that the constructor right here is going to automatically be inherited from this contract when we put on the blockchain all right so the final thing you should know about with solidity is how to talk to other smart contracts within a smart contract so i'll show you an example of that there's a few different ways we can see this contract here is a secret vault which basically just stores a secret on chain you can set the secret and also get the secret so we'll see how to interact that in a different contract so you can see my contract uh inside the constructor what we do is take the address for the other contract that's deployed to the blockchain and then we create uh we save it here so we actually have a type called secret vault which is the same name as this contract here it saves it as a state variable so we can access and call its functions later so you can see an example of setting the secret basically we just say secret vault uh this is the contract that's saved here and we call set secret okay inside this function and we just pass in whatever uh you know argument we need for that function we can also get the secret like this to say secret vault uh return you know get secret it's pretty straight forward another common way to talk to other smart contracts is to use interfaces so this is where you don't need the complete code of another smart contract on chain so in this case let's say an erc20 token this is a common uh cryptocurrency standard on the blockchain so all you have to know is the specific functions that you want to call so in this case we're going to call the transfer from function the erc20 token which you can create an interface for it with the interface keyword ierc20 and then just give it a description for how the transfer from function works and then we can use that inside of our smart contract like this we have a deposit function which basically takes the token address and the amount and then we we say ie rc20 then we pass in the token address and then we can call any function we want to on that smart contract from this right here we can also say this to a variable if we want to but we'll just do it in one liner okay and then call transfer from and then pass them whatever values we want to like the message sender who's calling this function the address of the contract who might own these tokens and also the amount which is passed in from the uh function argument here all right so that's an overview of the solidity programming language in less than 20 minutes so these are the most important aspects of the programming language that you're going to see over and over again and if you understand these you're going to be up to speed really quickly and on your way to becoming a real world blockchain master so again i put a lot of time into making this video for you so if you got value out of this make sure you smash that like button down below for the youtube algorithm that really does help youtube show this video to more people make sure you subscribe to the channel and if you like this video you did get value out of it you went to the next step and master real world blockchain skills i could show you that step-by-step finish over at dap university dot com forward slash boot camp you really want to be an expert get started today i've helped people with zero coding experience you know breaking the blockchain industry land their first blockchain job in a matter of months so that's all i've got until next time thanks for watching dap university
Up Next

The Curve Wars Explained: A Complete Guide to DeFi Governance
@JustinBram
47.8K views•2022-01-04

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

![CURSO DE PROGRAMACION con JAVASCRIPT desde cero [2021] | Aprende a programar](https://i.ytimg.com/vi/-rj-zxmdGHA/maxresdefault.jpg)










![Blockchain - Smart Contracts avec Solidity et Remix Ethereum-IDE [Webinar]](https://i.ytimg.com/vi/VkAQNEbKHho/maxresdefault.jpg)
















![Fundamentals Lecture #12 [SP23]](https://i.ytimg.com/vi/5tCLOL2tuK4/maxresdefault.jpg)
![How to Become a Blockchain Developer? [Complete web3 Developer Roadmap]](https://i.ytimg.com/vi_webp/q54j35z3fPQ/maxresdefault.webp)













