Solidity, Blockchain, and Smart Contract Course – Beginner to Expert Python Tutorial

Solidity, Blockchain, and Smart Contract Course – Beginner to Expert Python Tutorial

SUBTITLE'S INFO:

Language: English

Type: Robot

Number of phrases: 27901

Number of words: 162382

Number of symbols: 721017

DOWNLOAD SUBTITLES:

DOWNLOAD AUDIO AND VIDEO:

SUBTITLES:

Subtitles generated by robot
00:00
hello everybody and welcome to the free code camp blockchain and solidity beginner to expert full course python edition we're going to go through everything you need to know for developing on a blockchain using solidity and engaging in the future of finance we're going to cover topics that will teach you all the skills you need to become a smart contract expert that you can take with you no matter which blockchain you're developing on blockchain engineers are in extreme demand and they're building billion dollar applications almost every day at this point at the time of filming
00:30
protocols like ave yearn.finance and synthetics have billions of dollars of locked value in them allowing people to engage in decentralized finance or defy this allows people to make censorship resistant moves and so much more some of these protocols with billions of dollars are even less than a year old so whether or not you're brand new to this space or you've been in here a while hi my name is patrick collins and i live in the smart contract world i'll be taking you through your journey to becoming a blockchain and smart contract expert a little bit of background on myself i'm
01:02
an engineer and developer advocate on the chain link protocol and i also have my own blockchain infrastructure company called alphachain where i run different nodes and different infrastructure for blockchains including one of the main technologies we're going to be talking about today which is ethereum i love being a pioneer in the smart contract ecosystem and i love taking new users like yourself along to the journey with us and we are going to teach you to become a smart contract expert building blockchain and solidity applications is
01:32
building a world of more trust and accountability it means building a financially free future and we get to be the ones that will go down in the history books as the pioneers of this space additionally blockchain and smart contract engineering skills are some of the most sought after in the world with an average salary of a solidity developer being between 150 and 175 000 in this video we're going to teach you how to become one of these developers and go out into the world and participate in the world of smart contracts in the world of blockchain
02:04
this course is ideally for engineers who know a little bit of python programming and you can have any level of smart contract engineering whether you're a complete beginner to blockchains and you don't even know what one is or you're an advanced solidity engineer and you're looking to learn more this is the perfect place for you having a little bit of experience in other object-oriented programming language like javascript as well will be helpful here too and if you're brand new to coding in general that's all right because we're going to take you step by step through everything if you do want a little bit more in-depth python coding
02:34
video there is a fantastic free code camp video in the description if you do prefer javascript we will also be releasing a javascript edition of this video as well but everything that you learn here will be applicable there and if you watch both you'll learn even more you can find the entire itinerary for this entire course along with all of the code associated with everything that we do and additionally discussions and support and everything else in this smart contract kit full blockchain solidity course pi github repository it has a table of
03:04
contents and then the entire itinerary of everything that we're going to go over in this course and like i said with helpful tips resources for getting support and resources for getting help now for your convenience every single piece of code that we're going to go over in this video has a github repository associated with it so be sure to grab the link in the description grab that github repository and look through all the different repos that we're going to give you if you ever get lost or need to refer to some code or want to copy paste some code all of it will be there for you it'll also be a great place to
03:35
reference in the future when you're working on some project and you want to remember how to do something so be sure to start and refer back to it as you watch this video so let's talk a little bit about some best practices for watching this video this space moves really quickly so if we show you some documentation it might be a good idea for you to open that documentation up as well read through it as we do so you can stay up to speed now we've packed a ton of information in this video and studies have shown if you try to digest a massive amount of information in a short
04:05
period of time your retention isn't as good so it's highly recommended that every 25 minutes to half an hour you take a five minute break and then every two hours maybe you take an additional 30 minute or an hour long break you can pause bookmark areas and come back later and learn at your own speed there are timestamps in the description that will help you come back to where you left off and you don't even have to go in order if you want to bounce around from topic to topic you're absolutely free to do so we're also going to get really technical with the fundamentals of blockchain and
04:36
if you want to just jump right into solidity you can jump down the timestamps below and get right into it and if you're watching this on youtube you can adjust the speed that i talk and then i give this presentation so if i'm talking way too quickly for you you can slow it down or if i'm talking too slowly for you you can have me speed up so be sure to set me at the pace that you like best you are highly encouraged to pause come back and ask questions the blockchain in smart contract world is a very collaborative community so if you
05:06
have questions some of the best places that you can go going to stack overflow and tagging your question with the specific technologies that you're working on make an issue on the github repo that we're working with go to stack exchange eth and make a question there as well jump into the discord of the technology that you're working with or even on github discussions if those are there learning to become a blockchain and solidity engineer is actually a lot more than just learning solidity becoming comfortable with all the tools in the space is going to be just as essential as becoming familiar with
05:37
solidity itself and continuing the conversation on maybe twitter or reddit or any of these other channels and maybe even showing your stuff in the next ethereum or chainlink hackathon are going to be majorly beneficial to increasing your skill as an engineer now before we get actually coding a lot of people want to understand what is actually happening with all this blockchain stuff what is blockchain what is a smart contract how did this all get started and understanding these core fundamentals will actually shape the way you code and architect your smart
06:08
contract applications so learning these is really really critical however if you're already familiar with blockchain and you just want to jump into the solidity feel free to grab a timestamp from the description and jump to that section now since you're here though you've probably heard of bitcoin before bitcoin was one of the first protocols to use this revolutionary technology called blockchain the bitcoin whitepaper was released by the pseudo-anonymous satoshi nakamoto and it outlined how bitcoin could be used to make peer-to-peer
06:43
transactions in a decentralized network this network is powered by cryptography and allows people to engage in censorship resistant finance in a decentralized manner due to some of the features of bitcoin a lot of people took it to be as a superior store of value over another asset like let's say gold and that's why it's commonly referred to as digital gold similar to gold there is a scarce and set amount of it on the planet and people use it to buy and sell similar to other assets you can read
07:15
more about the original vision in the white paper and there's a link to it in the description now this was a fantastic breakthrough and in a little bit we're actually going to look through how blockchains can actually work and how all of this is possible but some people took this and saw this technology and thought that they could do even more a few years later a man named metallic buterin released a white paper describing a new protocol called ethereum which used this same blockchain infrastructure but
07:44
with an additional feature and in 2015 they released this project called ethereum him and a number of other co-founders took this blockchain technology and applied it in ways that people can make entirely decentralized applications decentralize organizations and build smart contracts and engage in agreements without a third-party intermediary or centralized governing force their idea was to take the same pieces that made bitcoin great and add smart contracts to it and in fact this
08:15
technically wasn't even a new idea back in 1994 a man named nick zabo proposed a technology called smart contracts a smart contract is a self-executing set of instructions that is executed without a third party intermediary they come to life on a blockchain and these smart contracts are really going to be the core thing that we're going to be working with and we're going to be developing smart contracts are similar to regular traditional contracts that people make between each other but
08:46
instead of writing these contracts down on pen and paper or typing that on the computer it's entirely written in code the terms of the agreement are written in code and automatically executed by the decentralized blockchain network instead of being written pen and paper and executed by the two parties or three parties or however many parties involved this is one of the main differentiators between the ethereum protocol and the bitcoin protocol now technically bitcoin does also have smart contracts however they're not touring complete meaning
09:17
that they don't have the full range of capabilities as a turing complete application like ethereum this is actually an intentional move by the bitcoin developers they view the bitcoin network as an asset whereas ethereum and the ethereum and developers viewed that acid as an asset and also a utility for people to build these smart contracts now these smart contracts are revolutionary technologies and we're going to talk a little bit more about what their advantage is in a little bit but they actually come with a fatal flaw with what's known as the oracle problem
09:48
these blockchains are deterministic systems and we'll learn why they're deterministic very soon and this determinism means that they're a walled garden meaning that everything that happens in these smart contracts and on this blockchain happens in this little box now of course if you want these smart contracts to actually be these digital superior agreements then they need some way to interact with the real world and get real data and external outside the blockchain computation this is where oracles come into play oracles are devices that bring data into a blockchain or
10:19
execute some type of external computation so great so oracles are the solution now blockchains can talk to the real world right well not quite our blockchains and smart contracts are these decentralized applications and in order for them to stay decentralized that means they would also need to get their data and external computation from a decentralized manner as well your on-chain logic will be decentralized on the blockchain but you'll also need your off-chain data and external computation decentralized as well combining these on-chain logic
10:49
settlement layers and these off-chain data and external computation builds what's called hybrid smart contracts and a large majority of d5 applications in the largest applications today are these hybrid smart contracts this is where the protocol chain link comes into play chain link is a decentralized modular oracle network that allows you to bring data into your smart contracts and do external computation and it's these hybrid smart contracts that can have this on-chain settlement and interact with the real world in some meaningful way chain link
11:19
is an incredibly powerful oracle network because it allows us to get data get randomness do some type of upkeep or really customize our smart contracts in any way we want and elevate them to do anything that we want them to do now throughout the course when we're talking about smart contracts oftentimes we are also talking about hybrid smart contracts smart contracts is used a little bit interchangeably with hybrid smart contracts but just know that when we say hybrid smart contract we're talking specifically about smart contracts with an off chain component now throughout this video you'll hear
11:50
people say smart contract you'll hear people say decentralized protocol decentralized application or dap and they kind of all are a little bit interchangeable a decentralized application is usually a combination of several smart contracts and when we start coding some solidity you'll see what a singular smart contract or singular contract looks like smart contracts are going to be what we code write and deploy for the majority of this video and learning some of these fundamental concepts will allow us to be
12:21
better smart contract and better solidity developers now since its inception the ethereum protocol has given rise to many new paradigms and industries including d5 nfts dows or decentralized autonomous organizations layer twos and so much more and a couple of other protocols have taken this ethereum vision and gone in a different direction with it like polygon polka dot or avalanche if we learn the core basics of smart contract development on the ethereum platform all
12:52
these skills translate to these other chains as well so don't worry about learning a specific tool or chain because most of them work together pretty seamlessly now there are a few exceptions to this rule and there are some smart contract platforms aka blockchains that don't use solidity however learning the fundamental skills here will still translate to every single other blockchain and ethereum is by far the most popular and most used smart contract blockchain or smart contract protocol you'll also
13:23
hear those words used a little interchangeably as well sometimes i'll say blockchain or sometimes i'll say smart contract platform smart contract protocol and the like similarly chain link is the most popular and powerful decentralized oracle network and is going to be the one that we're going to focus on here chain link is also blockchain and smart contract platform agnostic meaning it'll work on ethereum avalanche polygon polka or really any blockchain or smart contract platform out there even in this introduction we've already learned a lot so let's do a quick
13:54
summary of what we've talked about bitcoin was the first application to take the blockchain technology into the limelight and into a meaningful way bitcoin is a sort of digital gold able to make transactions between users as almost a sort of currency ethereum takes this blockchain technology one step further but you can also build smart contract or decentralized applications decentralized autonomous organizations and more because you can code with smart contracts these smart contracts can then access external data and external
14:25
computation outside the blockchain using what's called oracles chain link is the most powerful decentralized oracle network and allows us to build these hybrid smart contracts which is a combination of decentralized on-chain logic settlement layer and any decentralized external off-chain data or computation hybrid smart contracts and smart contracts are often used interchangeably now you're probably asking yourself a lot of questions right now like what makes bitcoin so interesting what makes it like a digital gold and how are these smart contracts going to add any value to the real world
14:57
and that's what we're going to go into now so before we get into the nitty-gritty of how these blockchains and how these smart contracts actually work from a low level let's go high level and talk about some of the features and massive advantages that blockchains and smart contracts have over our traditional environments the first feature that these have is they are decentralized and you'll hear this term used a lot because it has a massive benefit blockchains are decentralized meaning there's no centralized source
15:27
that controls the blockchain the individuals that make up blockchain are known as node operators and they are the independent individuals running the software that connects the whole blockchain together it's all these different independent individuals that make the blockchain and blockchain like networks decentralized we'll dive deeper into that concept later great example of why this is so fundamentally groundbreaking is if we go back to what happened recently even with robinhood and gamestop gamestop shares were no
15:59
longer allowed to be bought because a centralized entity didn't want them to be bought anymore so they flipped a switch and nobody could buy that stock anymore essentially having a single entity controlling the entire financial market the fact that a single entity has the power to make these choices for us is a travesty and blockchain is here to solve that there's a narrative here called the bankless narrative where users can actually live in a world where they don't have a bank banks while good in their own right have
16:31
a history of doing some shady things they also have the power to potentially freeze your funds not letting you withdraw or move or do anything because they are a centralized entity that again can flip a switch and control how you interact with your life every day being free of these centralized entities have this much power and this much control over your life has widespread positive ramifications transparency and flexibility everything that's done on a blockchain and all the rules that are made can be
17:02
seen by everyone there's no backdoor deals there's no shady happenings everything that happens on chain you can see this means that there's no special information that a few have everyone has to play by the same rules and everyone can see exactly what those rules are now additionally this doesn't mean that everything you do is tracked the blockchain is pseudo-anonymous so you can create different accounts and you can interact with it in many different ways this leads me to my freedom point
17:33
but i'll get there in a second speed and efficiency have you ever tried to make a withdrawal from the bank and it took three to five days all the bank is doing is adding and subtracting numbers basic first grade math so why does it take so long because blockchains are verified by a decentralized collective the settlement or withdrawal period in this case is substantially faster and depending on the blockchain that you're using it can be from 10 minutes all the way down to just a couple of seconds in the stock trading
18:05
or hedge fund world it can actually take up to a week for your buy or sell of a stock to go through security and immutability blockchains are immutable which means they can't be changed and because of this it means that they can't be tampered with or corrupted in any way shape or form this allows us to have massive security on our data on our transactions and anything of the like if your computer goes down and your backup computers go down in the regular world your data is gone
18:36
if all your data is on those two computers you're out of luck on a blockchain if several nodes go down it doesn't matter because as long as one node and the entire system is running the data is safe and secure there are thousands or hundreds of thousands of nodes running these blockchain softwares meaning that everything that happens happens and is immutable and won't change hacking the blockchain is nearly impossible and substantially harder than hacking a centralized entity and this is
19:07
also much more secure in the asset sense as well instead of having gold in a vault or contract written on a piece of paper or on your computer you have a asset that is locked on the blockchain forever and all you need to do to access it is have a private key or mnemonic which is essentially a password so you don't have to lug your gold around or lug your contracts around with you it is always on the blockchain smart contracts in particular remove a massive conflict
19:38
of interest in the traditional world when we engage with users or individuals they don't always have our best interests at heart a lot of them are usually self-motivated in some sense and there's nothing wrong with that that's how a lot of people are however when we make an agreement with them this agreement can have a massive conflict of interest with the user who's supposed to execute that agreement let's take insurance for example if i pay an insurance provider 100 a month i'm paying them a hundred dollars and in the event that i get hit by a bus we've
20:09
made an agreement or a contract that they're going to pay my medical bills or bail me out however they have this massive conflict of interest insurance companies aren't in the business of giving out money they're in the business of making money so even though they've signed this agreement when this event occurs they still don't want to pay this money out to me and if they can find a loophole in the contract they will because that is what they are motivated to do so they sign this agreement but
20:40
it's not in their best interest to do so so they have this massive conflict of interest and this is native in all of the agreements that we make today they are the ones who decides whether or not they're going to execute their agreement giving execution power to the party that doesn't want to execute something has often led to frustration now the follow-up is you can always sue them and go through this process but now you're wasting all this time going through this long process to get something that you should have originally gotten in the first place
21:10
this leads me to one of the biggest value adds to smart contracts smart contracts allow us to engage in trustless and trust minimized agreements we currently live in a world of brand-based agreements if i engage in some agreement and i don't like the service that i'm provided my alternative to this is to waltz down the street to another brand to another service who's going to make the exact same set of promises to me and then i have to trust them that they're going to
21:41
execute faithfully smart contracts allow us to move from this brand based agreements to math-based agreements these math-based agreements we don't even have to trust that they're going to do the right thing hence the name trustless one plus one is always going to equal two in a math world whatever the code determines is the input output that's exactly what's gonna happen every single time now for me these really all add up to two major pieces
22:12
freedom and trustless all these pieces allow us to live in a world that's more accountable more trusting more friendly and just better it allows us to work in an environment and a universe where things just work it allows us to do the freedom to engage with other people how we wish because there's no centralized controlling body influencing every action that we make all the rules are the same and nobody's getting special treatment this brings out this new world of
22:43
economic opportunity as well and as our lives become more and more digital we're constantly being bombarded with centralized services that want us to use their interface so they can profit on how we interact and force us or push us to making the decisions that they're motivated for us to make smart contracts decentralized applications and blockchain allows us to be free of these repressors and live in an environment that's truly free and trustless
23:15
so with all that high level being said let's do a quick summary of what we just learned blockchains are decentralized meaning that they are not controlled by a single centralized entity it is run by a network of independent users transparency blockchains are transparent everything that happens on a blockchain everybody else can see and everybody else can work with and see that everyone's playing by the same rules blockchains are quick and efficient
23:46
especially when it comes to monetary policy settlement on blockchains are fast and easy immutability and security blockchains can't be changed or tampered with or corrupted and are incredibly incredibly secure smart contracts remove the massive conflict of interest traditional agreements have smart contracts allow us to move away from political brand-based agreements to secure math-based agreements smart contracts allow us to engage in trustless and
24:16
trust minimized agreements smart contracts are a set of instructions which when placed on a blockchain are self-executing pieces of code not run by any centralized intermediary in addition smart contracts are typically paired with some type of oracle to get some information about the real world when smart contracts are paired with an oracle they're called hybrid smart contracts chain link is a secure decentralized modular oracle network used to bring data into your smart contracts and also make some type of external computation i also briefly
24:47
want to mention dao's or decentralized autonomous organizations you'll hear this referred to a lot as well decentralized autonomous organizations are organizations that live online and live in these smart contracts they're similar to a regular organization in the traditional world however they have people who maybe hold governance tokens to make voting decisions or they do all their governance on chain on this decentralized settlement layer giving us the freedom to engage with each other as we please so now that we've taken our first dive
25:18
into blockchains and smart contracts and at least from a high level understood why they're so advantageous let's jump in let's get an ethereum wallet and let's make our first transaction on a live blockchain are you ready now let's jump into ethereum we're going to make our first interaction with the ethereum blockchain and once we do this interaction once we make this transaction then we're going to go back and look at what actually happened what were the technical implications that allowed this transaction to go through so in order
25:50
for us to interact with the blockchain the first thing that we're going to need is an ethereum wallet so i'm going to go ahead and go to metamask because it's one of the most popular wallets and one of the easiest to use we're going to go ahead and download it i'm using the brave browser but it works for chrome firefox or really any other browsers and it's just going to be a little extension in the top right hand of your browser this way we can really easily see at any time what we have in our wallet
26:21
this will store all of our ethereum based currencies so i'm going to go ahead and install metamask for brave add to brave add extension and now we can go ahead and get started with working with brave this is the first step you absolutely need to take when starting your journey and one of the easiest steps to take so we're going to go ahead and get started and we're going to create a brand new wallet so we're going to go ahead and hit create wallet if you already have a wallet you can actually import it via i have a seed
26:53
phrase and we'll talk about the seed phrase or secret phrase in a little bit so let's go ahead and create a new wallet and sure we'll agree to help out metamask now we will create our password make sure that this is really secure for the purpose of this demo my passwords are just gonna be password but please don't have that be a password now when i'm doing my testing when i'm doing my coding i actually use a separate account from the account that i actually have real money in however if you want to use this account and actually put real ethereum and put real money into it you absolutely 100
27:25
need to back this up so we're gonna go ahead and click reveal secret words i'm showing you guys here because uh this is just a demo and i don't really care however if you show this secret phrase to anybody else they will have access to all funds so everything that we're gonna do in this tutorial we're gonna use fake money we're gonna use not real money so it doesn't matter however if you're going to actually put money in here you absolutely need to have this written down because if you lose access to this and or your private keys which we'll
27:56
talk about in a little bit you will lose access to your wallet and you will lose access to all your funds so they give some tips like store this phrase and a password manager like one password write this phrase down on a piece of paper put it in a secure location memorize it whatever you want to do just make sure you have this backed up somewhere i'm just going to go ahead and hit download this for now and i'm going to save it on my computer it's not best practice to save to your computer it is much better to use a password manager or write it down on a piece of paper or something but again because we're just demoing
28:26
here i'm going to show you it here and we're not going to put any real funds into this so we're going to go ahead and hit next and we're it's going to ask us to make sure we and it's going to ask us to verify that we actually have it written down and we're gonna go ahead and hit confirm and great and it gives us a couple other tips remember definitely take these tips very seriously especially if you're gonna use this for real money like i said for this demo we're just gonna use test money so it's not as big of a deal but if you put real money in you absolutely need to back up this seed
29:00
phrase or secret phrase or we're going to refer to it as our mnemonic phrase awesome now we can see the user interface of this wallet and depending on your browser if you actually go ahead and look in your extensions you can pin it to your browser and you can even just click it and get the same interface here let's take some inventory of what is actually going on in here and what we actually have our mnemonic phrase that secret phrase that we got has given us access to a new account and here's the address of our account we can
29:32
use a tool like etherscan to view different addresses and what's been going on with them so if we look at this address that we just created on etherscan we can see that no transactions have happened it's empty it has zero ether in it it has zero dollars worth of value in it and this address here is our unique address this address represents exclusively this single account that we just created we'll talk a little bit more about etherscan in a bit as it's a tool that we're going to use more and
30:01
more now we can even click this circle here and we can even create more accounts and give it a different account name we'll call it account 2. this one has a different address so if we go ahead go back to etherscan and look this up this one has a different address here so we can have multiple addresses in here and now if i click this i have two accounts account one and account two the mnemonic that we've been given gives us access to create multiple
30:33
accounts with that same mnemonic so having that mnemonic will give us access to every single account that's ever created with that mnemonic this is why securing your mnemonic is so crucial especially if you create multiple different accounts now each account has a unique identifier with them as well so so this right here is the public address when we copy this this is the public address of that account however there's also a private key to work with this account a secret key
31:03
and we can go ahead and view it by clicking these three dots go to account details and export private key put our password in and confirm so this is going to be our private key so this is a single password associated with this account if you give somebody else access to this private key they will have access to my account too they won't have access to my account one because the private key of account two is only associated with account two the mnemonic however is associated with all accounts and this is
31:34
why when people say store your private keys in a safe place or store your keys in a safe face they're usually referring to both your mnemonic and your private keys if you lose your private key you lose access to this account if you lose your mnemonic you lose access to all your accounts so long story short back up your mnemonic since it has access to everything and back up your private keys too but just keep in mind they only have access to the individual accounts and great those are some of the main security pieces here now what else is going on in metamask is
32:04
we can see this section here that says ethereum mainnet if we click it we actually see a bunch of other networks in here so when you buy eth and when you work with eth you're working on the ethereum mainnet when you interact with smart contracts or d5 or any things that we're going to talk about later on mainnet with real value you're going to be working on the mainnet however since we're engineers oftentimes we're going to want to test our applications or do some type of integration tests or just make sure our code actually works
32:35
so there's also what's called test nets these are networks that resemble ethereum and work exactly the same way as ethereum does however they're not with real money and it's just for testing your applications so we can even go to ether scan and look up brinkp ether scan we can see the rink be test on explorer we look up at our address and it's the exact same information here nothing has gone on on rink b and this is totally different so when we make a
33:06
transaction these are all different networks and it says test network it's made to be made without real money later on we're actually going to show you how to work with other evm compatible chains don't worry about what evm compatible means for now but we can work with avalanche polygon and other applications through this networks interface as well so remember a testnet blockchain is a blockchain where the currency doesn't have any real value but it resembles and acts exactly like the ethereum mainnet so we can test our applications so we can test and practice our
33:37
applications in fact what we're going to do right now is make our first transaction on the rink b test net and this will simulate exactly what it's like to make a transaction on the ethereum mainnet so we're going to go to this application called the rink b faucet this is where we're going to make our first transaction ring b is going to be one of two test nets that we're actually going to work with the other test net that we're going to work with is going to be coven it's important to know how to switch between test nets and evm compatible chains which is why we're
34:09
going to be working with both for now we're just going to be working with rink b a faucet is a tested application that gives us free test ethereum hence why it has no value because anybody can get it for free from one of these faucets so to get tested ethereum with this application we actually have to post a tweet or a facebook post with this tweet so i'm actually going to i'm going to sign in real quick and now that i'm signed in i can post this tweet requesting faucet funds into
34:39
and this is where i'm going to put my address on the rinkby ethereum test network i'm going to go ahead and tweet that out now that i have this we're gonna copy link to tweet and we're gonna place it in here and we're gonna hit give me ether and we're gonna say 18.75 ether for three days and it said funding request accepted for patrick alpha c into this and what we can do then is if we take this address again
35:10
we go over to rink b ether scan we now see that the balance is 18.75 and we can even see that in our wallet on the ringbeat network we have 18.75 eth but again if we look at mainnet we have nothing there if we look at robson we have nothing there if we look at rink b we have 18.75 right so these are very different networks and we've just made our first transaction we've been given 18.75 eth and if we refresh this page we also see that this is our first
35:46
transaction that was made some account sent us 18.75 ether from this account to us and we can actually even look at the details of this transaction etherscan is what's known as a block explorer block explorers are applications that allow us to see details of things that happen on a blockchain easily we can see the transaction details of this transaction here and whenever we work with smart contracts we will also see them in a transaction similar to what we're seeing
36:16
right here and again we'll talk about that soon now we can see a number of information here we see a unique transaction hash this hash or this unique identifier uniquely identifies this transaction as the key of what this transaction is we see that it was a successful transaction this is the block number which we'll talk about in a little bit we see it was from this unique account which looks like they did a ton of transactions because this is the faucet account to our account that we created
36:47
value was 18.75 ether and then we have these transaction fees gas price gas limit and gas used now gas refers to the fee paid to node operators for successfully including a transaction in a blockchain now exactly how this works is actually going to change pretty soon but the concept is basically anytime you want to change the state of blockchain whether this is sending some ethereum or making any type of transaction you actually have to pay a little bit of
37:18
ethereum or a little bit of that native blockchain token to actually execute that transaction whenever we do something on the blockchain it costs gas and if we do something that would take a lot of energy for the blockchain to do it will cost more gas so if i send a single transaction that's going to cost 21 000 gas however if i were to do if i were to send a transaction that called the smart contract function and did a whole bunch of other stuff it would cost more gas so we see here when we got sent
37:49
18.75 eth whoever sent us that eth also paid the blockchain miners or the blockchain validators a little bit of ethereum to include our transaction now we actually get to pick how much of a fee we want to send with our transactions so let's look at another example so in our accounts in metamask let's even expand the view here we have two different accounts we have account one and account two account one has 18.75 account two has zero we can actually send money from account
38:22
one to account two and again remember this is all fake money so so we're gonna go ahead and hit transfer between my accounts so we're gonna send money to account two and here's where we can see some transaction details and we see the asset that we're going to send which we only have ethereum in this wallet so we're only going to send ethereum later on we'll learn how to get different assets into this wallet we're going to choose an amount i'm just going to choose to send one and then we have these pieces here associated with the transaction fee so we have a gas price in guay and a gas
38:55
limit so when we send a transaction we can choose a gas limit we can say hey if this transaction is going to spend more gas than 21 000 gas we're not going to do it we also get to set a gas price in guay but here's the quick example of guay versus ethereum [Music] one ether is this many gray and one gray is this much ether because if we just said hey could you send me .00001
39:29
that would be kind of really obnoxious so we just just say send me one way or send me one way so i know we've been throwing this gas term around for a little bit but here is it basically simplified gas is going to be the measurement of how much computation something uses the gas price is going to be how much it costs per unit of gas a gas limit is going to be the maximum amount of gas that can be used in transaction so for example if we make a transaction that uses 21 000 gas
40:01
and each one gas is one way in price that means we're going to pay 21 000 way in transaction fee so back in our transaction we have we're saying the gas price is going to be one way so the transaction fee is going to be the gas that we use which will be up to this gas amount times the gas price so it'll be 21 000 way will be the transaction fee so then the question is well why would we ever bump it up why would we want to pay more
40:33
gas price why do i even have the option to pay more well and this comes down to block space we'll talk about this a little bit more when we get into how the blockchain actually works but the blockchain can only process so many transactions at a time and nodes and blockchain nodes can only process so many at a time so when i make a transaction a node has to decide why they want to include my transaction into the block and if there are a ton of people looking to make these transactions then the nodes are going to be highly
41:04
incentivized to pick the transactions that are going to give them a high price that are going to give them a lot of money for including that transaction so this is what's called eath gas station and it is a a gas estimator of the blockchain it currently says that if you want to get your transaction in right away it's going to cost you 31 way to do so if you want to get it in less than five minutes it's going to take you maybe about 21 gway so the gas prices of ethereum fluctuate
41:35
with how much people use it and the gas prices of all these blockchains fluctuate with how much people use it so this is an important concept so typically when you're setting your gas price in a transaction you can take a look-see at you know gas station and say okay if i want mine to go in right away i'm going to do asap if i want to go in fast maybe i'll do you know this fast amount standard i'll do this standard amount but it all depends on how many people are looking to work with this blockchain at the same time and and as you can see it fluctuates pretty quickly right it just went all the way up to 46
42:06
so maybe more people are using the blockchain now this is obviously for the eth main net and on the test net there's not going to be that same competition but we can still change it anyway so if i go ahead and do 100 for the gas price and i hit next and i hit confirm if i go to activity i now have this transaction in my metamask but i can go ahead and view this on etherscan as well and we can see this is what it looks like when it's still processing this transaction and now we can see that it's
42:36
passed and now if we look at the gas price we see it's a hundred gray and this is what we set it as when we were working with it before so gas prices 100 way here versus our first original transaction was just one way now if we look at our metamask we can see that the funds have indeed been subtracted from this account and they have been added to this account now there's one eighth in this account awesome and you can see the activity there's one ethan here so again the reason that these gas prices exist is because nodes can only put so many
43:09
transactions into a block so they're highly incentivized to input the transactions that are going to give them a higher fee so in times when a lot of people are looking to use a blockchain prices will get very high and when very few people are using a blockchain prices will be very low this ether scan tool is incredibly incredibly powerful and we'll be using it more and more as time goes on now here's something that's incredibly exciting with just this little bit of information you now know how to interact with blockchains and interact with the ethereum protocol so if you don't want to learn how to code
43:40
anything you can go and you can start interacting with ethereum and interact with protocols with just this much information however i know most of you guys are here to learn how to code so let's look under the hood of ethereum and what is actually going on with these transactions and with these gas and with these blockchains and what's really going on let's learn all the fundamentals of a blockchain now if you want to just go ahead and jump into the coding go ahead and grab a timestamp from the description however learning exactly how the blockchain works is going to make you an incredibly powerful developer so
44:11
let's take a look at that first so we're going to be going through this blockchain demo on this site right here now the creator of the site has a fantastic video and a fantastic walkthrough blockchain 101 it is right on their site so if you're looking for another explanation definitely check out his video it is absolutely fantastic but the first thing that we really need to do in order to understand blockchain in order to find really anything and everything that's going on here we first really need to understand this shot 256 hash or hashing just kind of in general let's first understand what a hash is a hash is a unique fixed length string
44:43
meant to identify any piece of data they are created by putting some piece of data into a hash function in this example the hashing algorithm used is sha256 now ethereum actually uses this this right here for its hashing algorithm which isn't quite um sha256 but is in kind of this shaw family but it's it's really just another way to hash things and uh the specific hash algorithm doesn't matter uh so much so uh this example uses sha-256 but you can imagine it's
45:14
the same as the ethereum hash they're just gonna you know result in a different hash so what's going to happen in this application here is whatever data or whatever information we put into this data section here as you can see below this hash changes so what's happening is this data is running through the sha 256 hash algorithm and it's outputting this unique hash so this hash is a unique fixed length string
45:44
that's going to identify like a blank data piece here right so if i put in you know my name like you know patrick collins this is the hash that's going to represent patrick collins right and you can see even when i put you know tons and tons of data in here the length of the string doesn't change right so it's always going to be the same amount we can put almost any amount of data in here there is an upper limit on the max size of the data but for all intents and purposes we
46:14
can pretty much put any length in here and you'll see too that you know every time i type in patrick collins this hash is always going to be this 7e5b right i'm going to delete i'm going to do patrick collins again you know seven e5b it's always this this unique hash is always going to be unique right it's always gonna be this fixed length string here so now we can take this idea right of putting this data in here and we can move on to uh this concept of a block so with this block concept we're going to take the exact same thing with this hash this this data section right but instead of
46:46
having everything just being this this singular data area right here we're going to split this data up into block nuns and data so all so what we're going to do is we're actually going to hash all three of these to get to get this hash right we're going to put all three of these we're going to say all three of these are combined uh together we're going to put every all three of them into this hashing algorithm uh to figure it out so if i type a bunch of stuff here we can see that block one with nuns you know this nonce and this data we're gonna get this hash and as
47:17
you can see actually the screen turns red this block turned red now what happens when i hit this mine button when i hit this mine button it's actually going to take some time it's going to think for a little bit and we can see that the nuns here actually changed right the nuns is different from what it was before and this hash now starts with four zeros okay and then it the the back turned green when we're talking about mining we're talking about miners solving some type of very difficult problem that
47:47
takes a lot of time to do now in this example here the problem that the miners had to solve was they had to find a nuns or or a value in this nun section that when hashed with at block number one with this data it would start with four zeros so the problem here the miners had to solve was to start with four zeros and the only way for them to really do that is kind of this brute force you know trying stuff so they tried one okay one didn't work okay two nope two didn't work three no four or five six okay five
48:18
well that started with one zero but it's not four and they have to keep trying all these numbers until they uh get to this one where you know let's hit mine again where it has four zeros at the top at the start now this specific problem changes blockchain to blockchain right ethereum has a different problem for miners to solve um bitcoin has different problems for minor itself but this concept is going to be the same so they have to take one block is going to be this uh this concept is going to be all this
48:49
data it's going to be the block number and it's going to be this nunce right and so this nunce is the solution um is is going to be the the number that they use to get like the solution to the problem right so if i go to one here you know when i do this again i'm gonna hit mine and the nuns has changed right it went from one to thirty three thousand one hundred and twenty eight because this is the nuns that allowed this hash to start with four zeros and so that's what's happening when uh blockchain miners are mining
49:21
they're going through this process this very computationally intensive process of trying to find a nuns that fulfills whatever the problem is so that's really it actually so that's a block and that's really what's happening when miners are mining they're just looking there's trial and error brute force trying to find this nut so so now that we know what a block is let's go to the next step and figure out okay well what's a block chain so here we have an example of what a block chain is going to look like right we have a combination you know we have back here in the block
49:51
section we have one what one block looks like now here we have multiple different blocks right each one of these represents a different block but we have an additional column here or we have additional uh variable here so like before you know we have block nuns and data right we have block nun's data but we also have this thing called previous right and so this is actually pointing to the previous hash of the last block so for example if we go to the the last block in this blockchain it says previous is 008 and if we look at the
50:23
hash of block number four it's zero zero zero zero eight e eight and then we look at its previous it's uh four zeros b9 we have four zeros b9 and so on all the way back to our first block which has previous of just all zeros right and so the block with the previous of all zeros is going to be known as the genesis block so you've probably heard that before the genesis block it's the first block in the blockchain where the previous hash points to a hash that doesn't actually exist now as you can imagine kind of the same as how this
50:53
block worked how the block nuns and data all go through the hashing algorithm in the blockchain the block nuns data and previous hash all go through this hashing algorithm to figure out you know what the hash is okay so if we go to over here you know for example if i type in you know patrick obviously this is now no longer valid right because this nuns uh combined with the block the data and the previous hash aren't gonna solve you know our problem of having four zeros at the start right so i'm going to go and fix that and and that's that's kind of an easy way to see
51:24
it being broken but but let's take a look if i break this block right here what happens if i if i break the data in here if i do like trick in here you can see that both of these are now red both of these are now invalid right because the block hashed with the nuns hash with the new data which is my name patrick has worked hashed with the previous block is now a brand new hash right and this block is still pointing to this previous hash right here right it's pointing to this
51:56
previous block and now it is wrong and it is messed up and now um and now it's nuns with this previous hash is also wrong right and this is where when we talk about uh blockchains being immutable this is exactly how it's immutable right because if i go back and i change anything you know if i've just typed a right here the entire blockchain is now invalidated because none of these are going to have nunses that solve this equation anymore so this is why blockchains are immutable
52:27
is because anytime you change one thing you ruin the rest of the blockchain okay so however though you know if if an a was here originally we can go ahead and mine these we can mine all these but as you can see you know this is going to start getting very computationally expensive because i have to go redo basically the entire blockchain and the farther and farther down the line you get the harder and harder it becomes to you know rehash and redo all these different blockchains here now this makes a lot of sense right so we have this blockchain it's really hard to change something in the past but if we
52:58
do we can just go ahead and remind it now if i'm the one who controls the blockchain right if i'm the one who controls this you know and i want to change something in the past well okay great all i got to do is change this data here and then you know mine each one of these and you know obviously it's going to be very computationally expensive but it's something that i can do right if i'm the one who owns the blockchain now here's where the decentralized nature or the distributed nature really uh makes it incredibly powerful so we're gonna go to the distributed tab here which i also refer to as the
53:29
decentralized tab here it's going to show us what a blockchain looks like in a decentralized manner so we have this exact same initial setup here we have distributed blockchain we have you know our first block chain which is kind of exactly as the one from here but we also have more than one so we have peer a peer b and peer c and when people are talking about peer to peer appear to be your transactions they're really talking uh this is kind of that concept that they're talking about right so we have a number of different peers who are running this blockchain technology they're all weighted equally right each
54:01
one of these peers or each one of these nodes each one of these entities running a blockchain has the exact same power as anybody else right so the way that we can tell very easily which blockchain is correct or which ones are correct are by looking at this end hash here right or by looking at where we are in the blockchain because again remember because again remember this this hash that this this in this last block here is going to encompass
54:31
all of the blocks from before right because this last hash is going to have the previous hash here which includes the previous hash here which this hash includes the previous hash here in which so this last hash is encompasses everything in here right and we can look we can look at the hash of pure c which is four zeros and then e4b we can look at the latest hash of peer b which is four zeros e4b and then pure a which is for zeros e4b so all of these peers all of these nodes all of these decentralized you know
55:02
these independent all these independent users running this blockchain software they're all matched up it's very easy for their nodes to look at each other and say hey great we are all matched up now what let's say that a decides that you know something happened on the blockchain that they didn't like and they wanted to go back and change something right so let's say they change here you know obviously the rest of their blockchain is invalidated and they have to spend a lot of computational power to catch up to speed so let's go ahead and humor it let's say that they they did they ended
55:33
up catching up uh they ended up catching up you know they ended up mining everything and now they have a valid blockchain right it solves the equation awesome however in block number three there's something new right this is here and it shouldn't have been here this is something that pier a put in by themselves all that happens now is we look at all the blockchains that are running the software and we're looking at all the hashes at hash at block number five so pier a has this new
56:05
hash now 009 bc but peer b has a different hash 0 e4b right so who's right is it is it pier a with their new stuff or is it peer b well that's where the decentralizator comes in because then we can look at pier c and pierce c also has e4b so pure b and pure c both say hey pure a you're wrong get out right and peer a will stop being able to participate in the mining rewards because they have essentially forked uh the blockchain and started
56:36
their own little blockchain right with their own history because they're the only ones with this this piece of data in block three whereas peer b and peer c have nothing in there so that really shows why uh in these blockchain worlds in this decentralized world there really is no central identity you know pier a you know might have been maliciously motivated to change you know this block number three however democracy rules right the majority rules in the blockchain peer b and pc both say hey you know that's cute and all pure a but
57:07
you're wrong right that that's not right now it might be a little abstract that you just look at data and you know us typing kind of random stuff in here and think okay yeah that's that's data right that makes sense you know just kind of random strings in here doesn't really do anything for us so if we actually go over to the token section here this is where everything really starts to make a lot of sense so we have the exact same setup here with peer a peer b peer c except the difference is instead of having kind of this this data section we have this tx this transaction section right and
57:38
this represents all the transactions that are happening in this block right so we're we're sending 25 dollars from darcy to bingle or to bingley uh force uh four dollars and 27 cents here uh 1922 right and it's the exact same thing so this all these transactions are going to get hashed in the exact same way uh that the data is going to get hashed and and this is why it's so powerful because again you know if i want to be malicious right if uh if i want to say hey i really wanted to give jane a lot more money from elizabeth so i'm
58:10
pure a and i go back and i change it to 100 well now you know not only do i does my whole blockchain get invalidated because that was so far so long ago but i'm not going to match any of these other chains right and so my blockchain is going to be excluded from the overall blockchain so and let's let's go ahead and fix this and it's the same thing if down here if i become malicious and i want to send you know i want uh miss audrey to have less money maybe i want to send a dollar and i go ahead and mind it the same thing here
58:41
this hash now this 2a1 is not going to match peer b's pre-b's hash of bba and it's not going to match pc's hash of bba as well so the two of them are going to say hey this your blockchain isn't valid it's not matching the majority you know you're out right so that's really how these blockchains work at a low level and it all goes back to this this understanding this hash idea and using it in this very sophisticated manner uh to kind of cryptographically prove
59:13
um you know where where stuff lies now the way the blockchain works is that instead of random stuff put in this data section it's actually going to be solidity code in here to finding ways to interact with different blocks and different protocols that are on chain or as we've said before different smart contracts now the next question that you might be asking is okay well how do i know how can i be sure that i'm the one uh you know let's say this is let's say i'm darcy right how can i be sure that i was that darcy was the one to actually
59:44
send this money here how do we know that darcy sent 25 to bingley well this is where we get into uh private keys and public keys and that's what we're going to go into now let's just do a quick recap of what we've learned in this section so far right we've learned that ethereum actually runs on this ketchup 256 but you know we used shot to v6 for this demo it doesn't really matter we're just talking about hashing algorithms so again a hash is a unique fixed length
01:00:14
string meant to identify any piece of datum a hash algorithm or a hash function is a function or algorithm that computes any type of data into a unique hash mining is the process of finding the solution to the blockchain problem in our example the problem was to find a hash that starts with four zeros whenever a node mines a block they get paid a little bit of that gas we were talking about earlier for doing something a block in a blockchain is basically a combination of a block nonce
01:00:45
transaction and previous hash to create this unique hash for this block and again depending on the blockchain implementation this might have a couple other fields or might have different fields but this is essentially what's going on blockchains are decentralized and distributed because many independent users are going to run this blockchain software and they will check and they will compare against each other to see which blockchains are acting honestly and which ones are acting maliciously in the blockchain world majority rules the nuns here is the answer used or the number used to
01:01:17
get this hash now nuns is kind of an overloaded term it's actually used for a number of different reasons in this case we're using it to solve this problem of getting you know four or five zeroes at the stop of the hash however in ethereum it'll also be often used as the number of transactions from a given address so now we're going to talk a little bit about signing these transactions and private keys and some other cryptography pieces right because in this blockchain demo here we can see we hover these these fantastic transactions right all these things went through but how do we know that it was darcy who was the one
01:01:49
to send 25 to bingley right how do we know that actually happened and this is where all those pieces that we just learned about uh in our our test net in our metamask account are really going to start to to come to life here a little bit here so here we have an example of public and private keys okay at the top we have this private key right that was that was randomly generated a private key is is you know as it kind of states is a key that you really want to keep secret because you're going to be using this
01:02:19
as kind of your secret password for all your transactions right i can really pick you know any any any private key anything that i want and with it uh this algorithm they're going to use an algorithm you know for ethereum in bitcoin they both use this elliptic curve digital signature algorithm it's a variant of just a digital signature algorithm and it's going to create this this public key right i'm really not going to go at all into kind of this digital signature algorithm but just know it does use some of these some of the hash
01:02:51
knowledge that we just learned combined with some other pieces to kind of get this this public here so i'm not going to go too deep into it but we have this private key that we create and we get this public key now this public key we want everybody to have access to right this is yeah whole world can see this this private key we really want it to be uh private we don't want people to see this we're going to use this private key as like a password to quote unquote digitally signed transactions and then people can verify them with this public key so let's let's
01:03:22
see what this actually looks like let's pick a random key a more secure key right because the longer it is the more secure it's going to be and if we go to signatures now right let's say we have this this message that we want right we'll say hi world right we want this to be the message what's going to happen is this private key that we've created we can use to sign this data right remember how in the blockchain demo you know we were kind of we were hashing stuff right we were we're using this uh shay256 hash to to get this hash well
01:03:53
we're doing something similar but instead of hashing we're we're using this digital signature algorithm to create this message signature now what's really powerful about how this uh this algorithm works is that you can create this message signature with your private key but somebody else can't derive your private key from the message signature and that's what makes this really really powerful however if we go to verify using this public key right and so this is the this is that o four zero three this is
01:04:23
that same public key using this using this public key anybody can verify let's go ahead and sign again anybody can verify that the signature is yours right so you have a public a private key just for you so you can sign things and a public key that anybody can verify something right so anybody can verify this and let's say somebody tries to fake a transaction from you they say hey you know this is this is this is their transaction all they have to do is verify
01:04:54
that this signature against your public key and very easily this whole thing turns red because it isn't verified right the the algorithm says hey that's wrong so we can go ahead and take that into transactions in this exact same way so if i want to send money you know if i want to send 400 from you know my address to another address using my private key i can sign that transaction and anybody else in the world can then
01:05:25
verify this transaction right and this is why when people say hide your keys you know protect your keys this is what we're talking about in our accounts here right if we go to uh settings and again the only reason that i'm showing you guys my mnemonic in my private key is because this is a uh this is a dumpster account i'm gonna throw this away at the end of this video or i'm just not gonna put any real money in it um but when we look at our our metamask here we have this mnemonic phrase which allows us to
01:05:59
easily get these different private keys right so demonic phrase combined uh with you know whatever account number will get us a private key so demonic phrase combined with one we're going to get this private key and this is when we look at account details export private key password confirm this is going to be the private key that we're going to use to sign our transactions right this if anybody else gets access to this private key they then can sign transactions for us and they can send transactions for us and that's why we want to keep these
01:06:33
private so it works the exact same way right so this is why it's so important to hide your private keys and hide your mnemonics now your ethereum address is actually a piece is actually a piece of your public key now to get our address in ethereum all we have to do is take this public key that we've created with our private key hash it using that same ethereum hashing algorithm and then take the last 20 bytes and that's how we'll actually derive to our um to our address here now
01:07:05
knowing the exact methodology of how to get the address doesn't really matter because it could change blockchain to blockchain and could even change an too but just know that that is essentially how kind of these addresses are derived right there's some derivative of the public key right because the public key is public and you know using the public key in kind of any public way is is totally fine but not the private key so that is how we sign our transactions note though this isn't how we send the transaction so so this is just going to sign it create a transaction for us to send
01:07:36
we'll learn later on how to send these transactions so that was a lot of information there too let's do a quick recap your public key is derived by using a digital signature algorithm on your private key right and you want to keep your private key private at all times because you're going to use your private key to sign transactions signing transactions with your private key you are the only one who can actually do this because you can't get the private key from a message signature however using your public key you can
01:08:08
anybody can very easily verify that a signature that's signed by you is in fact signed by you in our metamask our private keys are located in this account details section you just hit show private keys and type in your password and you'll get your private key here a quick note here is often times when using your private key somewhere they want it in hexadecimal form so if we're going to use our private key for something like brownie which we'll go into later we need to actually append in a 0x to the front but we'll get into
01:08:38
that later and the address of your account is derived from this so if you think about it your private key creates your public key which then can create your address and there's a little barrier or a big barrier here because your private key you want to keep private and your public key and your address can all be public information awesome so now that we know all the cryptography pieces and all the little nitty gritties of how the blockchain actually works and how our signatures work and how everything sticks together let's talk a little bit
01:09:08
about how this works in actuality and what's really going on now for a lot of this each different blockchain has slightly different algorithms and slightly different metrics and criteria for doing a lot of the stuff so when we're talking about these specific implementations keep in mind the exact algorithm might be a little bit different but the concepts are all still going to be exactly the same hashing and hash function is going to be the same no matter where you look a decentralized blockchain is going to be the same no matter where you look how it's actually implemented is going to be a little bit
01:09:40
different now traditionally when you run an application you know be it a website or something that connects to some server you are interacting with a centralized entity and unlike how we saw with the blockchain with multiple different peers it's going to be run by a single centralized group now it still could be run on many different servers but all those servers are still going to be controlled by the same centralized group blockchains as we saw run on a network of different independent nodes when we saw peer a peer b peer c those
01:10:11
were different examples of different independent users running the blockchain technology on their own node now when i use the term node i'm usually referring to a single instance of a decentralized system so when i say a single node when i'm talking about a blockchain i'm talking about one of those peer a's peer b's pcs running that blockchain software i'm talking about one server running this technology and again it's this network it's this combination of these nodes interacting with each other that creates this entire blockchain what makes this so potent too is that anybody
01:10:42
can join the network and that's why there's decentralized the barrier to entry is a little bit of hardware requirements you're getting the correct materials to run the software and then you running the software anybody can join these networks and participate and that's what makes it truly decentralized in fact you can go to github right now and run your own ethereum node in a few seconds now in the traditional world applications are run by centralized entities and if that entity goes down or is maliciously bribed or decides that they want to shut off they just can't
01:11:13
because they're the ones that control everything blockchains by contrast don't have this problem if one node or one entity that runs several nodes goes down since there are so many other independent nodes running that it doesn't matter the blockchain and the system will persist so long as there is at least one node always running and luckily for us most of the most popular chains like bitcoin and ethereum have thousands and thousands of nodes and as we showed in our demo if one node acts maliciously all the other nodes will
01:11:45
ignore that node and kick that out or even punish it in some systems because they can easily check everybody else's node and see okay this one is out of sync with the majority and yes majority rules when it comes to the blockchain each blockchain keeps a full list of every transaction and interaction that's happened on that blockchain and we saw if a node tries to act maliciously then all their hashes are going to be way out of whack and they're not going to match everybody else this gives blockchains this incredibly potent immutability trait where nothing can be changed or corrupted so in essence we
01:12:17
can think of a blockchain as a decentralized database and with ethereum it has an extra additional feature where it also can do computation in a decentralized manner now let's talk consensus proof of work and proof of stake because you've probably heard these before and they're really important to how these blockchains actually work we went through that blockchain example and we did that mining feature this is what's known as proof of work proof of work and proof of stake fall under this umbrella of consensus and consensus is a really
01:12:48
important topic when it comes to blockchains consensus is defined as the mechanism used to reach an agreement on the state or a single value on the blockchain especially in a decentralized system i briefly alluded to this consensus mechanism in our blockchain example when i said if one changes something and the other two don't then majority will rule and kick that one out this is part of that consensus mechanism now very roughly a consensus protocol in a blockchain or decentralized system can be broken down into two pieces a chain selection
01:13:18
algorithm and a civil resistance mechanism that mining piece that we were doing or or the proof of work algorithm is what's known as a civil resistance mechanism and this is what ethereum and bitcoin currently use please note that depending on when you're watching this if eth2 is out then it's no longer proof of work now proof of work is known as a civil resistance mechanism because it defines a way to figure out who is the block author which node is going to be the node who did the work to find that mine and be the author of that block so all the other nodes can verify that it's
01:13:50
accurate civil resistance is a blockchain's ability to defend against users creating a large number of pseudo-anonymous identities to gain a disproportionately advantageous influence over said system and in layman's terms it's basically a way for a blockchain to defend against somebody making a bunch of fake blockchains so that they can get more and more rewards now there are two types of the civil resistance mechanisms that we're going to talk about here namely proof of work and proof of stake let's talk about proof of work a little bit more in depth first in proof of work
01:14:20
this is silver resistant because a single node has to go through a very computationally expensive process called mining which we demonstrated earlier to figure out the answer to the blockchain's riddle of finding that correct nonsore or whatever the blockchain system has in place in proof of work this works because no matter how many pseudo-anonymous accounts you make each one still has to undergo this very computationally expensive activity of finding the answer to the proof-of-work problem or the proof-of-work riddle which again in our demonstration it was finding a nunce
01:14:51
with that first four zeros but again each blockchain might change the riddle work or change the problem to be a little bit different in fact some of these blockchains make this riddle intentionally hard or intentionally easy to change what's called the block time the block time is how long it takes between blocks being published and it's proportional to how hard these algorithms are so these problems actually can change depending on how long they want the block tone to be if the system wants the block time to be very very long they just make the problem very very hard if they want to be very short to make the problem a lot
01:15:23
easier we'll talk about civil attacks in a little bit and how they can affect the system but with proof of work it's a verifiable way to figure out who the block author is and be civil resistant now you need to combine this with a chain selection rule create this consensus now there are some consensus protocols that have more features but very very roughly these are the two pieces that we're going to look at the second piece is going to be a chain selection rule how do we know which blockchain is actually the real blockchain and the true blockchain now on bitcoin and ethereum they both use a form of
01:15:53
consensus called nakamoto consensus and this is a combination of proof of work and longest chain rule the decentralized network decides that whichever blockchain has the longest chain or the most number of blocks on it is going to be the chain that they use this makes a lot of sense because every additional block that a chain is behind it's going to take more and more computation for it to come up that's why when we saw in our transaction we actually saw confirmations the number of confirmations is the number of additional blocks added on after our
01:16:25
transaction went through in a block so if we see confirmations is two it means that the block that our transaction was in has two blocks ahead of it in the longest chain now i do want to point out that a lot of people use proof of work as a consensus protocol and i do want to say that this is a little bit inaccurate but sometimes people use it interchangeably proof of work is a piece of the overall consensus protocol which in bitcoin and ethereum one's current case is nakamoto consensus nakamoto consensus is a combination of proof of
01:16:56
work and its longest chain rule both equally and very very important now proof of work also tells us where these transaction fees and these block rewards go to remember how when we made this transaction we had to talk about gas and a transaction fee so who's getting paid who is getting this transaction and this transaction fee is going to the miners or the validators in a proof of work network they're called miners and in the proof of stake network they're called validators there are a little bit different and we'll get into that when we talk about proof of stake in this
01:17:27
proof of work system all these nodes are competing against each other to find the answer to the blockchain riddle remember in our example it was to find a hash that has four zeros at the start and again depending on the blockchain implementation that riddle is going to be a little bit different but all the nodes are trying as many as possible to try to get this answer first why because the first node to figure out the answer to the blockchain rule is going to get that transaction fee they're going to get paid from that now when a node gets paid they actually get paid in two different ways one is going to be with a
01:17:59
transaction fee and another piece is going to be the block reward remember how we talked about alternating the gas price or the way on our transaction well that's the transaction fee that we're going to pay to these blockchain nodes for including our transaction the block reward is given to these nodes from the protocol from the blockchain itself you've probably heard of the bitcoin having before the having is referring to this block reward getting cut in half and it's supposed to be cut in half roughly every four years this block reward increases the circulating amount
01:18:31
of whatever cryptocurrency that is being rewarded for example on ethereum the block reward is giving out ethereum and on bitcoin the block reward is giving out bitcoin so these nodes are competing against each other to be the first one to find this transaction to be the first one to find the answer to this problem so that they can be the ones to win both this block reward and your transaction fee some blockchains like bitcoin for example have a set time when they are no longer going to give out block rewards and the miners or the nodes are only going to get paid from transaction fees
01:19:02
now this gas fee again is paid by whoever initialized the transaction when we got our funds from the faucet there was some server and somebody else was paying the transaction fee for us however when we sent ether from one account to another our first account actually paid some transaction fee to send that ether in proof of stake there's also a gas fee but it's paid out to validators instead of miners and we'll talk about that in a little bit now let's talk about two types of attacks that can happen in these blockchain worlds let's talk about the
01:19:32
first one being the sybil attack the simple attack is when a user creates a whole bunch of pseudo-anonymous accounts to try to influence a network now obviously on bitcoin and ethereum this is really really difficult because the user needs to do all this work in proof of work or have a ton of collateral in proof of stake which again we'll talk about in a bit the other more prevalent attack is what's known as a 51 percent attack now as we saw as part of our consensus protocol these blockchains are going to agree that the longest chain is the one that they're going to go with so
01:20:03
long as it matches up with 51 percent of the rest of the network this means that if you have the longest chain and you have more than 51 percent of the rest of the network you can do what's called a fork in the network and bring the network onto your now longest chain now sybil attacks obviously are when a single node or a single entity tries to affect the decentrality of the network by pretending to be multiple different people although they're just the same person or entity and like i said it's really difficult to do in proof of work and proof of stake so you can see now that blockchains are very
01:20:33
democratic whichever blockchain has the most buy-in and is the longest is the blockchain that the whole system is going to corroborate when nodes produce a new block and add to the longest chain the other nodes will follow this longest chain that the rest of the network is agreeing with add those blocks to their chain and follow up so very small reorganizations are actually pretty common when a blockchain picks a block from a different longest chain puts it on and then has to swap it out for another block and continue with a different blockchain however if a group of nodes had enough nodes or enough power they could
01:21:05
essentially be 51 of the network and influence the network in whatever direction that they wanted this is what's known as a 51 attack and it's happened on blockchains like ethereum classic which is not ethereum this is why the bigger a blockchain is the more decentralized and the more secure it becomes so after you watch this video and you become a blockchain engineering expert i definitely recommend you run a note as well because you are going to increase the security of the network as a whole by running a node so proof of work is fantastic because it allows us to very easily protect against these
01:21:37
civil attacks and keep our blockchains decentralized and secure however it has some drawbacks as well proof of work costs a lot of electricity because every single node is running as fast as they can to win this race to get the rewards this leads to obviously an environmental impact now since proof of work and nakamoto consensus a lot of other protocols have taken this idea and gone in a different direction with a different civil resistance protocol a lot of them with the intention to be a lot more environmentally friendly and the most popular one right now is proof
01:22:07
of stake there are some chains that are already using this proof-of-stake protocol and that are live and thriving some of them are like avalanche solana polygon polkadot and terra and additionally ethereum has decided to upgrade to eth2 which will have this of stake algorithm as well it'll also have some other features which we'll talk about in a bit now as a quick aside all the tools that we're going to learn here are still going to work in eth2 so depending on when you watch this everything here is still valid so let's talk about proof of stake now again this
01:22:38
is a different civil resistance mechanism instead of solving this difficult problem proof of stake nodes put up some collateral that they're going to behave honestly aka they stake in the example of ethereum 2 nodes put up some ethereum as a stake that they're going to behave honestly in the network if they misbehave to the network they are going to be slashed or removed some of their stake obviously this is a very good civil resistance mechanism because if you try to create a whole bunch of anonymous accounts then each one of those accounts you have
01:23:08
to put up some stake and if you misbehave you're going to run the risk of losing all the money that you put up as collateral in this system miners are actually called validators because they're no longer binding anything they're actually just validating other nodes now unlike proof of work which every node is racing to be the first one to find the block in proof of stake nodes are actually randomly chosen to propose the new block and then the rest of the validators will validate if that node has proposed the block honestly as we saw with our cryptography lesson it's
01:23:38
usually very easy for other nodes to verify if a proposal or a transaction is honest now randomness is a really important topic when we're talking about blockchains because keep in mind these blockchains are deterministic systems they're walled gardens from the rest of the world and as you know a determinic system by definition can't have random numbers so how do we choose the random validators in the system well it changes from blockchain to blockchain and actually choosing the node will change blocks you to blockchain but in eth2
01:24:08
they're using what's called randow at least for the original implementation this is a decentralized autonomous organization that collectively chooses the random number and collectively chooses which node is going to run next we aren't going to dive too deep into this because there's a good chance that this might change in the future but we will go into randomness solutions in blockchain later on in this course now proof of stake obviously has some pros and cons as well pros are that again it is a great civil resistance mechanism and a great way to figure out who the author of a block should be the other pros are that it's way less
01:24:38
computationally expensive to figure out the new block because instead of every single node on the network trying to do this only one node needs to do this and then the rest of the nodes just need to validate it the cons are that it's usually considered a slightly less decentralized network due to the upfront staking costs it costs to participate now this gets into a little bit of a philosophical battle on how decentralized is decentralized enough and i think that's up to the community to decide and as we progress i think we'll learn more and more about how decentralized is decentralized enough
01:25:08
the general consensus amongst blockchain engineers though is that proof of stake is very very decentralized and very secure this massive environmental impact improvement is one of the two main reasons why eath is shifting to eth2 it reduces the environmental impact by up to 99 percent now these are the main pieces of proof of work and proof of stake but i did want to talk about another concept that's really important in these ecosystems and that is scalability when we were talking about gas prices we were saying that the gas prices can get really high if a lot of people want to send a transaction
01:25:40
because a block only has so much block space and the nodes can only add so many nodes so when a lot of people want to use a blockchain the gas price skyrockets this is not very scalable because if we want to add more and more people to these blockchains it's going to cost more and more to use the blockchains because more people are going to want to get into these blocks this means that there's kind of a ceiling to how many people can use the system because of the financial constraints that will get imposed as gas prices keep rising ethereum 2 is not only attacking the environmental impact of proof of work by
01:26:12
switching to proof of stake but they are also implementing this new methodology called sharding and sharding is a solution to this scalability problem a sharded blockchain really just means that it's going to be a blockchain of blockchains there is a main chain that's going to coordinate everything amongst several chains that hook into this main chain this means that there's more chains for people to make transactions on effectively increasing the amount of block space that there is sharding can greatly increase the number of
01:26:42
transactions on a blockchain layer 1. now there's another term that might be the first time you heard it a layer 1. we're going to talk about layer ones and layer twos in terms of scalability really quickly as well a layer one refers to any base layer blockchain implementation bitcoin's a layer one ethereum's a layer one avalanche is a layer one these are the base layer block chain solutions a layer two is any application that is added on top of a layer one added on top of a block chain some examples of layer twos are going to
01:27:13
be chain link arbitrarily or optimism arbitrary and optimism are very interesting because they are layer twos that also look to solve this scalability issue arbitrary and optimism are what's known as roll-ups and they roll up their transactions into a layer one like ethereum we're not going to go too deep into rollups and how they actually work but all you really need to know is that a rollup is kind of like a sharded chain they derive their security from the base layer from the layer one like ethereum and they bulk send their transactions onto the layer one they
01:27:45
solve some of the scalability issues by being another blockchain that people can make transactions on still on kind of this base ethereum layer now they're different from side chains because side chains derive their security from their own protocols roll ups derive their security from the base layers so arbitrary optimism for example is going to be just about as secure as ethereum there's some fantastic guys in there that go a little bit deeper into rollups and i've left a link in the description for you all right so we just talked about a lot of stuff so let's do
01:28:15
a quick recap before moving on ethereum and bitcoin are currently both proof-of-work blockchains that follow nakamoto consensus however ethereum is moving to ethereum two which will be a proof-of-stake sharded blockchain civil attacks are prevented due to protocols like proof of work and proof of stake 51 attacks grow increasingly harder with the size of blockchain so you should run a node consensus is the mechanism that allows a blockchain to agree upon what the state of the blockchain is sharding and rollups are solutions to scalability
01:28:46
issues on layer ones a layer one is any base blockchain implementation like bitcoin or ethereum a blockchain scalability problem is that there's not always enough block space for the amount of transactions that want to get in them this leads to very high gas prices and again gas prices are how much it costs to interact with a blockchain so we've learned a ton in this video so far everything that you went over is going to make you 10 times better as a developer because yes being a good developer means you understand the code
01:29:17
at a very technical level but if you can understand the overall architecture as well you can make the informed decisions about how to architect your design or how to build your software in however you want to do so so with all that being said it's finally time to jump into some solidity and jump into some code so let's do this now again in the description of this video there is a link to this github repository that's going to be the home base for all the code that we work with in this
01:29:48
tutorial we scroll down to this main section this readme there's a table of contents in here we can go to lesson one simple storage and we'll have links helpful tips the itinerary of what we're going to learn and everything else that you need to work with here all the code that we're going to be working with is located in this simple storage link that we can go ahead and click it's in its own different repository we can go ahead and click the file to see all the code that we're going to be working with so let's jump into it additionally back in our
01:30:20
full blockchain solidity course right at the top there's this resources for this course section if you have questions engaging in github discussions stack exchange ethereum and stack overflow are going to be great places to get help and get support i highly recommend making a stack overflow stack exchange ethereum and a github account so you can participate and engage with the community welcome to the remix ide or the remix
01:30:52
integrated development environment this is going to be where we're starting to teach you how to work with solidity and work with smart contracts and deploy to blockchains we're going to use remix to get us up to speed as it has a lot of nice features that allow us to really see and interact with blockchains and really see what's going on but eventually we're actually going to move off of remix to another platform but all the solidity that we're going to learn here obviously is going to apply everywhere as well when you come to remix there's a whole lot of different plugins like solidity learn eth soul hint linter and a whole bunch of other
01:31:23
plugins as well i'm going to go ahead and start by clicking the solidity plugin but we're not going to use any of these plugins for now but later on you can kind of go back and learn a little bit more about what these plugins do so let's start perusing let's start coding some things on the left hand side over here is where we're going to interact with everything so let's go ahead and click the files up here now you can always go back and peruse this a little bit more and in fact i highly encourage you to because that's how you're going to learn the most the quickest but for us we're actually just going to go ahead and start with our own brand new file
01:31:55
we're going to create a little application that can store information on the blockchain for us and this is our first project that we're going to do in solidity so we're actually going to create a new file and we're going to call it simple storage dot soul all solidity has an extension of dot soul at the end stands for solidity now let's take inventory of what we're going to be working with here this is the solidity compiler tab it compiles all the solidity code down to machine understandable code or
01:32:25
machine language here there's a whole bunch of different parameters we can choose when working with sliding we choose the compiler version we can choose the language which we're only going to be working with solidity the evm version don't worry about this for now so let's code our first solidity contract here now we are going to use something a little bit special here when we actually deploy these we're going to use a javascript virtual machine so we're going to use a virtual machine that's going to simulate actually deploying to a test net or a real
01:32:56
network we're not actually going to deploy on a real network we will in a little bit but just to get started we're going to work with a javascript vm which is kind of a fake environment for now okay testing locally and understanding how to test locally will make your coding experience a lot faster as you saw when we sent some transactions some of them actually took some a lot of time to actually deploy we don't want to have to spend that much time waiting around for our tests to actually finish so we're going to use a javascript vm to
01:33:27
kind of dummy it for now but let's just start coding and go from there so the first thing that you're going to need in any solidity program is the solidity version so that's always going to be at the top of your solidity code it's defined by doing pragma solidity and the version we're going to be using some version between 0.6.0 and 0.9.0 so we're saying we want to use anything between 0.6
01:34:00
and 0.9 and and as a force of habit i just automatically hit command s whenever i write anything so that's why you saw some of this pop up here we can hitting command s or control s depending on if your windows or not we'll hit this compile button and we'll compile everything for us now if we want a specific version of solidity we can also do 0.6.0 and if i go ahead and hit command s or compile our compiler will automatically convert to 0.6.0 however if i try to do 0.8.0 with my
01:34:34
solidity at 0.6.0 it's going to give us an error it's going to say the source file requires a different compiler version we're using 0.8.0 and this is 0.6.0 so we're going to go ahead and hit compile and it's going to automatically move down to 0.6.0 we can also do carrot 0.6.0 and this will allow us to work with really any version of 0.6 it'll work all the way up to 0.7 where if we hit command s or control s there it'll give us an error so this only
01:35:05
works with any version below 0.7 and above 0.6 we're going to be using version 0.6.6 however in future contracts that we work with we're actually going to work with different versions of solidity the reason we're going to be changing versions throughout this course is that solidity is a constantly updating language being good at switching between versions is going to make you an incredibly powerful smart contract engineer the next thing that we're going to do is we're going to define our contract so contract is a keyword in solidity
01:35:36
which stands for our smart contract that we're going to create you can think of a contract similar to a class in java or any other object-oriented programming language so we're going to give our contract a name here we're going to call it simple storage and we're going to add this little curly bracket to say this is the contents of our contract simple storage and i went ahead and hit command s and we can see it is compiling successfully you could hypothetically deploy this right now and this would be a valid contract so great job for making it this
01:36:08
far now in solidity there are many different types that we can work with let's go into some of the types of solidity we can have integers aka whole numbers they can be uint as in an unsigned integer meaning they're not positive or negative we can also have an int and we would define a variable by doing you in 256 favorite number equals 5. so we have an unsigned integer you went 256 means
01:36:38
this is an integer of size 256 bits so we can have this be upped this number be up to 256. you can also do uint favorite number equals 5 but if you want to be a little bit more precise a lot of protocols and a lot of smart contracts will do the whole name like you and 256. we can also have booleans booleans are true false so we can have boolean favorite bool equals true so this favorite bool would be true
01:37:11
it could also be false we can have strings string favorite string equals string a string is a string of text here right it's going to be some word or phrase or really any of these keystrokes here similar to the unsigned integer we can have an int 256 favorite int equals negative 5
01:37:42
so it could be positive or negative we can have an address which is going to be some type of ethereum address so we could do address favorite address equals and then we can even copy right from our metamask and just paste it right in here this is going to be a valid address here you'll also notice that we end all of our statements with a semicolon we can have a bytes object
01:38:14
size 32 bits favorite bytes for our example we're just going to use the word cat because cat is a string which can be converted down into a bytes object by 32 means that there's 32 bytes in this variable favored bytes we can also do bytes 2 bytes 3 bytes 5 etc with the maximum size of bytes 32 for example we can't do bytes 64. we're going to be talking about some other variables as well like arrays and mappings but let's just stick here for
01:38:45
now if you want to learn more about the different types and the different variables that you can use head over to the solidity documentation and there's a link in the github and the description to show you this section for now for our simple storage let's say we only want to store numbers we don't want to store anything else so we're just going to go ahead and delete everything and just have uint256 favorite number at the top now in solidity if i do this favorite number actually does get initialized even though i didn't have it
01:39:15
initialized to 5. if i leave it blank it gets initialized to the null value in this case it would be initialized to 0. so for now let's just not initialize it to anything that way it'll get automatically initialized to zero this means that when we deploy this contract as of right now favorite number will start off as zero if you get confused you can also make a comment on this you could say this will get initialized to zero this double slash here is the way to make comments in solidity and it won't
01:39:48
get executed as code so we can write whatever we want as long as it's preceded by two backslashes now let's go ahead and create our first function functions or methods are self-contained modules that will execute some task for us and in solidity it's the exact same thing they're defined by the keyword function let's make a function called store that will change the value of this favorite number here
01:40:18
so we're going to call store and we're going to allow it to be past a variable so we're going to allow it to be passed a variable of type unsigned integer 256 and we're going to call it underscore fave or it number we're going to make this a public function which we'll get to in a minute and all we're going to do is we're going to set favorite number equals to whatever variable we passed in favorite number so this in its simplest
01:40:50
form is how you can define a function now just to visualize what we're working on so far let's go ahead and deploy this contract so we can actually start to interact with it so if we hit this button this will bring us to the deploy tab and will allow us to deploy our smart contract here using our javascript vm it's given us a fake account with some ethereum in it it has 100 ethereum in it to start and same as before anytime we want to interact with the blockchain we have to pay a little bit of gas even in our fake
01:41:21
virtual machine here and we want to simulate that so you'll see it has some of the same parameters here as making a transaction like gas limit for example when we deploy a contract it's going to cost a little bit of ethereum or a little bit of gas to do so so let's go ahead and hit this deploy button and see what happens so once we deployed with this virtual machine a few things happened we have remix kicking out this section down here saying great job you've deployed something and if we scroll down it says
01:41:51
transactionsrecorded1 we can look at all the transactions we've recorded and we can see it says deployed contracts and we have a contract here that we've deployed now let's zoom out just a hair here so we can see everything a little bit better in this simple storage contract we see this big store button because there's one public function that we can actually interact with so we can add this number here and we'll hit store and you'll see again we have a completed transaction and if we look at our contract we'll have paid a little bit more gas
01:42:22
right we'll have paid a little bit more to interact with this function because again anytime we want to make a state change in the blockchain we have to pay a little bit of gas the reason metamask isn't popping up is because we're kind of doing it in this simulated environment so this is great however it looks like we can't actually see what our favorite number is we can't actually look at it so how do we actually make sure that we can view this favorite number well let's add another parameter to this as well if we add public
01:42:53
to our favorite number we recompile by hitting command s or hit the compile button we delete this contract and we redeploy and scroll down now we'll see two buttons pop up this blue button to show us favorite number which again is initialized to zero and we have the store function so let's talk a little bit about why this public variable allowed us to see this new button this new favorite number button this public keyword defines the
01:43:25
visibility of the variable or the function there are currently four different types of what's called visibility in solidity there's external public internal and private we're mostly going to be working with public for now but it's important to know how the rest of these work public functions can be called by anybody including variables so oddly enough variables are a function call to just look at them and return whatever that variable is an external
01:43:55
function means it can't be called by the same contract it has to be called by an external contract so if in this contract i had this be external i couldn't call the store function i couldn't call the store function inside this function because the function is external it means somebody outside of the contract has to call this function internal functions however can only be called by other functions inside of this contract
01:44:26
or in its derived contract and then private is the most restrictive as private functions and state variables are only visible for the contract they are defined in and not derived contracts now the reason that we didn't see favorite numbers show up in our original contract deployment is that if we don't give a state variable a visibility it will automatically get set to internal so if we want other people to be able to read it we have to change it to public now let's see how this interaction actually works if we hit the favorite number button
01:44:58
right now we'll get this call thing that shows up and it'll show us right here that the value of favorite number is zero now this function however is set so that whatever value we pass it is going to change the favorite number to whatever we pass it as so if we pass one two three hit store that transaction goes through and then hit favorite number we can see the value is now one two three now i will also be using transactions and smart contract interactions and
01:45:29
function calls a little bit interchangeably that's because on a blockchain whenever you call a function or whenever you make some state change to the blockchain you're actually also making a transaction that's what makes this whole thing so powerful and again that's why making a function call or deploying a contract costs a little bit of gas now the reason we can access this favorite number variable inside this function is because favorite number has this global or contract scope so even if we
01:45:59
made union 256 tests equals 4 or equals equals 4 we wouldn't be able to use this variable outside of this function right because it's self-contained it's self-contained inside this bracket and if i were to make another function store two perhaps public 2 doesn't know that this test variable exists functions only know about the variables that are in the same scope as them so favorite number is in this global scope
01:46:35
because the only bracket that's above them is simple storage and test is in this store scope because it has two brackets above it it has it's inside of the store function and inside of this contract simple storage store two isn't inside of this store function or this store scope so it can't access this test variable so let's go ahead and make this back to public and we'll compile again i'm hitting command s to compile but you can go ahead and click the button if you like and let's get rid of this now as you saw when we deploy this
01:47:07
there's this button here that we can click called favorite number we can also make a function called retrieve and make it a public function that is of type view and returns uint256 and all this is going to do is return favorite number so we're going to talk about views and returns here so i'm going to go ahead and compile i'm going to go ahead and delete delete this contract i'm gonna go ahead and
01:47:38
deploy it now and we can see now we have two functions or two blue buttons here we have retrieve and we have favorite number if i change favorite number by calling the store function favorite number and retrieve will both now say it's one two three so then the question might be well why is this one orange and these two are blue and the key relies in this view function or this view keyword there's two special
01:48:08
keywords that define functions that you actually don't have to make a transaction on and those keywords are view and pure a view function means that we want to read some state off the blockchain so we're just reading off the blockchain if we're reading off the blockchain and we're not actually making a state change then we don't need to make a transaction these blue buttons are blue because they are view functions public variables also have view functions that's why both of these are blue
01:48:39
this technically is a view function and when i click it i get to view and i get to read the state off the blockchain retrieve is the same way we could have this without a return but it wouldn't do anything pure functions are functions that purely do some type of math we'd have you in 256 favorite number public pure and just have favorite number plus favorite number so we're doing some type of math here but we're not actually saving state anywhere we're going to do this math but we're
01:49:10
not going to save this favorite number anywhere we're not going to save anything deploy this now this pure function we would have this retrieve function one two three it's blue as well because it's again not going to change the state of the blockchain so view functions and pure functions are both can have this blue color now the reason that nothing shows up at the bottom is because we didn't return anything all we're doing is we're saying add these two numbers together and that's it
01:49:42
in order for this function to give us something back we need to have it return something so if we go back to this retrieve to this retrieve function we have to define what we're going to return when we're defining this function so we're going to say this is a public function it's a view function because we're going to read some state and it's going to return a unit 256. so favorite number is of u 256 so that's what we're going to return our public variable favorite name is also a view function that returns a
01:50:16
un256 for now let's just remove that so we can work with this retrieve function so let's go ahead and deploy so now we see we don't have this favorite number button anymore because it is no longer a public function because again it gets initialized to internal so we can't actually view it now keep in mind later on we're going to talk about how everything on chain you actually can see and we'll talk about that a little bit later though so retrieve is going to do 0 we can call store and now retrieve is going to be 1 2 3.
01:50:48
now this application is great so far it allows a single person to store a favorite number and then go ahead and retrieve it later which is fantastic but what if we want a list of people or a group of people and store their favorite numbers or what if we want to associate a favorite number with a single person well what can we actually do now we have a whole number of different choices but the one that we're going to talk about is using a struct structs are ways to define new types and solidity they're almost like creating new objects as well
01:51:20
so we can create a struct called type people and allow it to start storing a favorite number associated with a certain people so inside of our struct we can have different types as well so we have a uin256 favorite number and we could also have a string name now we have a new type of type people that has a favorite number and a name inside of it now what we could do with destruct is we could say
01:51:53
people public person equals equals people and then inside we add the variables so we could say favorite number favorite number is 2 and name is patrick and of course the semicolon at the end and again i'm hitting command s to save but you can also go ahead and compile
01:52:24
in the compile tab so let's go ahead and delete this contract and see what this looks like now so now we've deployed this new contract and we have this person struct which at the zero index is the favorite number and the variable stored in the first index is going to be the name storing variables and solidity always works in this numeric index fashion in fact in contract simple storage uint 256 favorite number is at index 0. if we were to add another
01:52:55
object here like boolean favorite bool this would be at index one if we were to add bool favorite bool2 this would be at index two zero one two and it works the same in structs this is at index zero inside the struct this is at index one inside the struct so we can see the variables associated with this person we're gonna go ahead and delete this for now because instead of just creating one person we
01:53:25
actually want to create a whole list of people so how do we create a list of people let's delete that contract and what we can do is we can make what's called an array an array is a way of storing a list or a group of some object so as you're starting to see the way the syntax works for defining any type of variable is going to be the type of the variable the visibility of the variable like public or if you don't declare it it gets initialized to internal and then the name of the variable
01:53:57
it works the same with arrays so we'll make a people array the people array is the type we'll make it public and we'll call it people now if we deploy this contract we go and see we now have a people array but if we click this button you'll see that nothing shows up the reason is because it's an empty array to start with right we don't have anything inside of it now this type of array that we've created is what's known as a dynamic
01:54:28
array it's a dynamic array because it can change its size right now it's a size 0 and if we added something to it it's of size one you can also create arrays of a fixed size so if i were to do people one public people this array could only have a maximum of one person inside of it so we're going to work with a dynamic array though because we want to add an arbitrary number of people into here so let's go ahead and create a new function called add person where we can add a person to this array
01:54:59
so we'll do function add person string memory name i'll talk about this memory keyword in a minute uin256 favorite number and then we'll make this a public function and inside we'll do we'll add this person to our array the way to add a person to your arrays is using the push method so we're going to push
01:55:36
a new people or a new person and we're going to give it those variables again so we're going to give it so we're going to give it favorite number is this variable that we passed in here oops this needs a bracket and then we're going to give the name going to be this underscore name and then end bracket oops zoom out again here just so i can see stuff
01:56:09
this needs a semicolon and perfect now in that last clip we saw this little red box pop up whenever a little red box like this pops up after you compile it means you have a compile error this means that there's something wrong with your solidity code or your salinity syntax and it won't compile and deploy properly red is going to be this compile error now we're going to see a bunch of yellow warnings in the future if you get a little yellow pop-up these are okay to ignore but they usually have some helpful tips about something that might
01:56:40
be going wrong in your code so to summarize if it's red if it's red it's broken if it's yellow you might want to check it out but it could be okay so we can see we have our new function add person where we pass a string memory name and a unit 256 favorite number and then we create this people person this people object here and then we push it onto our people array and i'm going to show you another way we can actually create a people person is just by passing
01:57:10
favorite number and name and getting rid of this other bracket by passing favorite number and name because we know that the zeroth index of people is favorite number and the first index of people is name so we can also create a new person by adding it like this now let's talk about this memory keyword here now in solidity there's more or less two ways to store information you can store it in memory or in storage when you store an object in memory it
01:57:41
actually means that it'll only be stored during execution of the function or of the contract call if we hold it in storage that means that that data will persist even after the function executes a string in solidity is actually technically not a value type a string is actually an array of bytes a variable of type string is actually a special type of array that we can append text to so because it's technically an object we have to decide where we want to store in memory or in storage and since we only
01:58:13
need this name during the execution we can have it be string memory name and then when we create this new people object we will create a new copy of this name variable into storage memory means that after execution delete this variable and storage means keep it forever if this is a little bit confusing for you just know that for now when you use a parameter that's going to be a string for one of your functions you need to call it string memory so let's go ahead and deploy this contract and see what happens now
01:58:42
now we have this new function add person and since we are making a state change here we can see that this indeed is a orange button instead of being a blue button so we can add in here a string name we'll add patrick and we'll say his favorite number is 2. again right now if we look at people we see people 0 there's nothing in here if we retrieve there's nothing in here so we're going to add person patrick and now if we hit people of 0 we can see that the person at the zerowith index in
01:59:15
this people array is going to be string patrick if we try at one there's nothing in here let's add another person we'll add becca and her favorite number will be 24. let's add her now if we hit one we see favorite number 24 string name is becca and retrieve is so showing up blank because we haven't touched favorite number awesome this is great we can just keep adding people however there is kind of an issue here
01:59:45
what if i'm looking for a person what if i'm looking to find becca and find her favorite number in this array what if i know her name but i don't know her favorite number is there an easy way for me to find that favorite number without having to triage the entire array there's another data structure called a mapping so let's create this new data structure so this data structure is going to be of type mapping a mapping takes some type of key and spits out whatever variable it's mapped to so in this case if we're saying we want to use the name becca to find her favorite number we would say
02:00:17
the string becca is going to be mapped to the uint 256 favorite number and similar to all the other variable declarations the first part is going to be the typing so this is going to be a type mapping of string mapped to un256 we're going to give this public visibility and we'll call it name to favorite number and without adding any other functions that work with it if we deploy this we can see we have this
02:00:49
blue button because we're not making a state change name to favorite number and if we type in becca in here obviously nothing's going to happen because we haven't added this mapping in yet so in our ad person down here let's even have this ad person also add to the mapping so we can do name to favorite number and then the key is going to be this name so of becca we're going to say we're going to map the name becca to
02:01:21
the favorite number now we're going to go ahead and compile again i'm hitting command s deploy we can scroll down here if we look up backhand here we're going to get nothing however if we add becca and her favorite number being 24 this add person is going to add it both to the array and to this mapping now if we look up name to favorite number we see that becca returns 24. now one other thing i want to show you guys just because
02:01:56
compilers are going to yell you if you don't have them is typically at the top of these contracts you want to add an spx license identifier basically solidity and the ethereum community found out that trust in a smart contract can be better established if source code is available and in terms of legality and copyright it just makes life a lot easier if you add that license identifier right at the top of your solidity we're going to use the mit license identifier because it's the most open license out there it means hey
02:02:26
anybody can use this code and we don't care so you'll see a majority of salinity contracts have this spx license identifier mit at the top and compilers will yell you a lot less awesome we now have a contract that we've decided that we liked it's got mappings it enables us to actually store people and their favorite numbers this is fantastic we've done all of our testing in this javascript vm and we've decided you know what we want to deploy this to an actual test net or an actual mainnet how do we actually deploy this so that other
02:02:58
people can interact with this contract we are again going to use rink b because that's what we use to make our first transaction now again you will need some type of test ethereum in your test and wallet so again if you get lost you can always just google rink b faucet or ring b test net faucet and find a faucet or a better alternative would be to come to the link token contracts in the chain link documentation at docs.chain.link and
02:03:28
scroll down or just look up rink b this linked token contracts page has the most up-to-date faucets for any test net that it supports so for example we get a test and eth faucet right here which also just happens to be that exact faucet that we used earlier the only thing you need to change in remix is we need to change from javascript vm to injected web3 and metamask will actually pop up and say hey would you like to connect to this application anytime you're working with a web3
02:04:00
application or a web application that wants to access your funds or work with your metamask metamask will pop up and ask for authorization first this is really good so that we know which applications we're actually connected to so we're going to go ahead and say next connect and we can see here that we've even connected here and that our account on the main network has zero eth and remix even tells us hey you're on the main network so let's go ahead and switch to the rink b network
02:04:31
and we can now see we're on the rink b network and we have 17.74 eth on this ring b test network injected web 3 means we're taking our meta mask and injecting it into the source code of the browser here and that's the difference between injected web 3 and javascript vm web web3 provider is if we want to use our own blockchain node or our own web3 provider when we do injected web3 we're saying our web3 provider is our metamask which will work perfectly for what we're
02:05:02
trying to do so since we already have some tests on ethereum let's go ahead and deploy this and see what this would actually look like if we deployed to a mainnet the process is going to be exactly the same right the only difference was we would be on the main net instead of rank b so let's go ahead hit deploy we'll uncheck publish published ipfs hit deploy metamask will pop up asking if we want to do this transaction because remember we are deploying a contract we are changing the state of the blockchain so we have to pay a little bit of gas fee for it so we're going to go ahead and
02:05:33
hit confirm and we get a link to ring the ether scan similar exactly as we saw before when we made a transaction the difference here is that instead of sending ethereum to somebody we're actually making a transaction on the blockchain to create a contract after a short while it will show up here on etherscan with a success the number of block confirmations which again is the number of blocks appended to the block that included our transaction we see from which is our account here
02:06:05
and we see two is this new contract that we just created and we can even click it and we see that there's this unique transaction hash that created a new smart contract and same as working with the vm we have all the exact same functions in here and you can see if i hit retrieve these three functions since they're not making a state change you can just click them and no transactions will be made however what do you think is going to happen if i hit store if you guessed metamask will pop up you guess correctly
02:06:36
again we see the familiar pieces here we have a gas price gas limit go ahead and hit confirm we get another transaction here and once this transaction goes through we should be able to call our retrieve function and see a new number if we call it now nothing shows up because our original transaction hasn't succeeded but i bet if we look at it now okay it's still indexing but it looks like it's been included if we hit it now we do indeed see this value here
02:07:07
and we can do the same thing with adding a person we'll add becca we'll say her favorite number is 24. metamask will pop up we'll go ahead and confirm the transaction and if i look up becca and the name to favorite string right now it's going to show nothing because our transaction hasn't gone through yet but if we wait a little bit i should probably spell her name right we do see 24. and if we look at the zeroth index we also see becca's been added here as well now all the solidity code that we wrote and when we interacted with this
02:07:38
blockchain here all this solidity was compiled down to the evm also known as the ethereum virtual machine a lot of the blockchains out there today are what's called evm compatible and that means that all this solidity and all these functions that we're creating can still compile down to evm and deployed on their blockchain you'll find out a little later when we look to work on a non-ethereum based chain that we can still deploy our solidity smart contracts to these other chains as well but that's a term you'll hear more and
02:08:10
more the ethereum virtual machine or ebm now take a break give yourself a high five because you just deployed your first smart contract and you learned the basics the fundamentals of solidity so huge congratulations on how far you've gotten now in our second projects we're going to take the fundamentals a step further and start going into the more of the intricacies of solidity but just as a quick recap the first thing you always got to do in your smart contracts is name the solidity version then you'll have to name your contract a contract in solidity is like a class and
02:08:42
defines all the functions and parameters of your contract there's many different types in solidity like unsigned integer 256 boolean and bytes we can create structs in solidity we can create arrays in solidity we can create mappings in solidity we can create functions in solidity view functions don't make a state change memory and storage are two different ways to initialize where a variable is going to be saved all the solidity code that we're working with gets compiled down to the ethereum
02:09:12
virtual machine and last but not least congratulations on taking your first step in learning solidity let's move on to the next project all the code tips and links that we're going to be working with can be found in our course repository we can scroll down to lesson two storage factory click it here and we can see all the code we're going to be working with good luck all right so we've done it we've got our first contract out of the way we're understanding some of the basics of solidity now let's move onward let's
02:09:46
get a little bit more advanced with what we're going to do with our smart contracts and let's build what's called the factory pattern of smart contracts so we have our simple storage contract here which is great it allows us to store numbers and store favorite numbers associated with different people and this is great what if though i want to have a lot of these simple storage contracts deployed i want to give people the ability to generate and deploy their own lists based off of this contract
02:10:16
this is where the factory pattern comes into play so let's go ahead and create a new contract so in this contracts folder i'm going to do new file we're going to call this storage factory dot sol and now we'll have a storage factory.sol now the way that we're going to do this is that you need simple storage and storage factory in the same folder i have both of them in this contracts folder but if you have them outside or
02:10:47
in a different folder that's okay just make sure wherever they are they're in the exact same folder so let's figure out how to get a contract to actually deploy another contract we're going to add those basic pieces that we added in that simple storage.sol we'll add the spdx license identifier which will be mit we'll choose our solidity version which will be pragma solidity
02:11:17
and we'll say anything in the six range and then we'll create our contract we'll say contract storage factory and we'll create our brackets here and i'm going to do command s or compile whatever you want to do things are looking good here great so how can this contract deploy a simple storage contract well the first thing that we're going to need to do is actually import this simple storage into our storage factory contract we need to import it so that
02:11:51
our storage factory contract knows what a simple storage contract even looks like the way that we can import it is by doing the command import and then the file path that the simple storage is located so the file path for this is going to be at dot slash simple storage dot soul this means that simple storage is in the exact same directory as storage factory doing this line is equivalent to copying everything in this contract bit
02:12:22
coming over to storage factory and pasting it above you can even save and compile and have two contracts in the same file now what's interesting about having two contracts in the same file is that when you go to deploy you'll actually have a choice of which one you want to deploy and it's the same thing if i do that import statement so if i delete all this and i go back to import dot slash simple storage dot sol in our deploy tab still
02:12:53
you'll see that we still have our choice of which contract we actually want to deploy so this is how we actually import a contract or import any type of file that we want so that our contract knows what that contract looks like and can do so if we want this contract to then be able to deploy a simple storage contract we're of course going to have to create a function that can do that so we'll do function we'll call it create simple storage contract
02:13:25
we'll make this a public function we'll do our little open and close bracket in here the way we can generate a contract of simple storage type is by using a new keyword so let's create a simple storage variable we'll say a variable of type simple storage contract we'll name this variable simple storage with a lowercase s equals new simple storage
02:13:58
what this line is saying is we're saying we're going to create an object of type simple storage contract we're going to name it simple storage with a lowercase s and we're going to say this is going to be a new simple storage contract and we're saying this simple storage contract takes no input parameters of course if we deploy this contract as is by going to our deploy tab choosing the storage factory staying on a javascript vm deploying
02:14:29
scrolling down we have this function that doesn't return anything so we're creating new contracts but we can't really read where those contracts are being created we'd have to look on a block explorer like etherscan or something so let's make a way for us to keep track of all the different simple storage contracts that we deploy let's put them in a list or in an array so what we can do is we can say simple storage
02:15:00
array of visibility public and we'll call it simple storage array we'll initialize this symbol storage array and every time we deploy we create one of these new simple storage contracts we'll add it to our simple storage array so we'll do simple storage array dot push and we'll push this simple storage variable so again i'm compiling or hitting
02:15:32
command s delete that most recent contract we'll choose the storage factory and not the simple storage and we'll hit deploy now if we scroll down to our storage factory we have this blue button which stands for our simple storage array if we try to see what's at index 0 we get an error of course because we haven't added anything to it yet if i click this create simple storage contract orange button here
02:16:04
now i've created a transaction that's going to create a new simple storage contract and push it onto our simple storage array now if i try to access the zerowith index or the first index of this array i'm going to get this address here this is the address that this simple storage contract was deployed to so we've successfully deployed a contract to the blockchain from another contract and this of course is really exciting
02:16:34
now we can actually do more than just deploy the contracts we can actually deploy contracts from another contract and then call those functions as well so let's create a new function where we call this store function and we'll also create a function where we call the retrieve function from our storage factory so we'll do function storage factory store we're going to shorthand it by saying sf store we'll have it take unit 256
02:17:05
simple storage index and a uint256 underscore simple storage number we'll make this a public variable as well in our little brackets here and the reason i'm choosing a simple storage index is because we're going to choose which simple storage contract in our list that we want to interact with and then we're also going to pass a simple storage number to call
02:17:39
on the store function which of course we need to pass a favorite number to any time that you interact with a contract you need two things you need the address of the contract you want to interact with and you also need the abi for us we figured out that we're going to push and get this address from this simple storage array we can get the abi or the application binary interface from this import we'll explain the application binary interface a little bit more later
02:18:11
for now just know that in order for us to interact with this simple storage contract we can just do simple storage and then we'll pass this simple storage the address of that simple storage contract to get the address of that simple storage contract we'll say grab the address inside the simple storage array at index simple storage index this will return that contract that we want to
02:18:44
interact with so we could even say simple storage simple storage equals simple storage at that address in the array once we get this contract we can then call any and all of its functions so we could call simple storage dot store this simple storage number now if we compile this we go to our deploy tab deploy the
02:19:20
factory hit deploy open this up we can see we have a couple different functions here we of course have our create simple storage function which creates the contract and adds it to our array we now have this sf store which stores a number to one of those contracts on this array and then we have a lens into that simple storage contract so if i create a simple storage contract
02:19:50
i can now store on that zerowith contract on that first contract any number that i want like 55. of course i can't really see that 55 because we didn't add a retrieve functionality we didn't add a way to actually listen or read or retrieve that favor number that we got so let's add that now so we'll create a new function called sfget and this will take a uint 256
02:20:23
simple storage index and as a parameter and we'll choose one of these contracts on this array and return its favorite number calling the retrieve function on that contract so since we're just going to be reading state this can be a public view function that will return a uint256 to do this
02:20:55
we need to access that contract once again so we'll say simple storage simple storage equals simple storage at that address of simple storage array at index underscore simple storage index and we can return return simple storage
02:21:33
dot we call this retrieve function i'm just going to copy paste it so i don't spell it wrong simple storage we'll put the semicolon here too and here now if we compile this go to our deploy tab delete the most recent choose the storage factory and hit deploy we can see we now has have an sfget function so let's go ahead
02:22:06
create a simple storage contract we'll store a function on the zeroth contract we'll store 55 as its favorite number and we'll hit that and then for s of get we'll see if we can get the favorite number of the zero with contract and we do indeed get 55. awesome we can actually even refactor this code to be a little bit simpler here we don't need to save this simple storage contract is a variable here we can actually just call
02:22:38
retrieve on this whole section here paste retrieve at the end and just return like this the same goes for our sf store we can delete saving it as a variable we can copy this dot store paste it at the end here and delete this as well now we'll compile delete the most recent
02:23:11
we'll deploy the storage factory and if we go into it create a simple storage store the number 55 see what's at the zeroth index and we do indeed see 55. so this is really cool this is a way for us to actually deploy contracts and interact with contracts from another contract now to deploy a contract we do need all the functionality of that contract imported however to interact with the contract we don't need all of the
02:23:49
functionality we'll learn about interfaces in the next lesson which will allow us to actually interact with the contract without having all the functions defined and now i'm going to show you something really cool now i'm going to show you something really cool simple storage has got a lot of really cool functions and maybe i want all these functions inside my storage factory i want my storage factory to be able to create simple storage contracts and i want it to be a simple storage contract itself
02:24:19
well what i can do is my storage factory can actually inherit all the functions of simple storage without me having to copy paste all these functions and all these variables over to storage factory what i can do is i can do solidity's version of inheritance i can say contract storage factory is of type simple storage or is of contract simple storage and just by doing this line right here
02:24:51
my storage factory contract now will have all of the functions and variables of simple storage so it'll have a store function a retrieve function an add person function a people array a name to favorite number mapping it'll have everything because i will inherit it with this is syntax so if i go to my deploy tab now [Music] let's look at what our last storage factory was all we did to change this was add is
02:25:23
simple storage and we can see just the four functions that we originally added if i delete this now if i save and compile the storage factory let's go ahead and deploy storage factory if we open this up now we can see not only do we have all the functions originally defined in our storage factory but we additionally have all the functions from our simple storage and awesome you've completed the second lesson we've learned about some
02:25:57
incredibly powerful tools here we've learned how to import entire chunks of code from other files into our files we've learned how to do inheritance we've learned how to deploy contracts from our contract and then we've learned how to interact with different contracts from outside of our contract well done now is a great time to take a breath take a breather and review what you've learned the github repository associated with this course also has all the code for
02:26:30
this lesson so let's jump into it so we're back in remix now and we're going to go to contracts and same as before we're going to create a new file we're going to call this fundme.sol now same as last time we're actually going to add this spdx license identifier mit right at the top and then we're going to choose our solidity version so we're going to go pragma solidity and for this we're just going to do greater than equals to 0.6.6
02:27:00
and less than 0.9.0 and great this should look pretty familiar now we're going to do contract fund me and we're going to start working so what again do we want this contract to do we want this contract to be able to accept some type of payment so let's create a new function that can accept payment we'll call it fund so we'll do function fund public and we'll add a new keyword in here called payable when we define a
02:27:35
function as payable we're saying hey this function can be used to pay for things when you call a function every single function call has an associated value with it whenever you make a transaction you can always append a value this value is how much whey or gray or fini or ether you're going to send with your function call or your transaction as we mentioned before whey way and ether are just different ways to talk about how much ether you're
02:28:06
going to send so if we look at a way to ethereum converter one each is this much way one way is the smallest denomination of ether you can't break up ethereum into anything smaller than one way this is why when you're talking about how much something costs everything always defaults to whey or the smallest unit of measure in ethereum so again for us to test we're going to stick with the javascript vm for now if we hit deploy
02:28:37
we get a new contract and this button is now red it's red because it is a payable function so now if i hit this fun button [Music] i can add a value associated with it so what do we want to do with this funding what do we want to do when people send us something well let's keep track of who sent us some funding so what we can do is we can create a new mapping between addresses and value so let's do a mapping of address
02:29:09
to uin256 which will represent the value we'll make this a public mapping and we'll call it address to amount funded now in this fun function let's keep track of all the people who sent us money or all the addresses that sent us some value to do this we can use some keywords that go along with every transaction so we'll say address to amount funded
02:29:38
of message.sender equals or plus equals message.value message.sender and message.value are keywords in every contract call and every transaction message.sender is the sender of the function call and message.value is how much they sent so whenever we call fund now somebody can send some value because it's payable and we're going to save everything in this address to amount funded mapping
02:30:13
so if we deploy this now in our javascript vm we now have again a new view function called address to amount funded and we can even hit the drop down to see the full name now if i hit fund nothing's going to happen right because my address is going to be sending zero in order for me to send something i have to add some value along with my transaction so let's send for example one way which is going to be equal to 1
02:30:41
1 2 3 4 5 6 7 8 9 this much way so before i hit fund here if i copy this fake account which is up here and i put it in this address to amount funded it's going to return zero but now if i add 1 1 2 3 4 5 6 7 8 9 in here and we go ahead and hit fund now we've now just called this fun function with a value of one gray associated with it
02:31:13
so if i call this address to amount funded now with the same address i can now see how much we've funded this smart contract and we can even add more gray we'll add we'll add 11gway for example we'll call fund and if we call this now we can see that even more has been added when we send our funds to a contract this actually means that this contract wherever this is deployed now is the owner of this amount of ether
02:31:44
so this is fantastic we now have a way to fund our smart contracts now here's the thing in this smart contract in this funding that we're doing we want to create a minimum value for people to be able to fund our endeavors which whatever they may be we want to set some minimum value here and ether is great but for whatever reason we want to work in usd or maybe we want to work in some other token so how are we going to get the conversion rate from that currency to a currency that we can use in this smart contract well the first thing that we're going to
02:32:15
need to do to set this value is we're going to need to know what the eth to usd conversion rate is because if i want to accept ethereum as the token but i want it in its usd currency well then i'm going to need to know what that conversion rate is so how are we going to get this data into our smart contract where are we going to get this data from now remember how we talked about blockchains being deterministic systems and and oracles being the bridge between blockchains and the real world well this
02:32:46
is exactly where oracle's come in when we're talking about these systems you know these blockchains they can't connect to real-world events they can't connect to external systems they can't do external computation they're intentionally these deterministic systems these walled gardens so in order for us to make this connection we need a blockchain oracle we need some type of network here now just to get a little bit more technical for you if we look at a blockchain a blockchain can easily say one plus one equals two and every other node can
02:33:18
easily verify this however a blockchain can't easily say okay let's all grab the same random number because each node is going to get a different random number they also can't say hey let's make an api call because if one node calls the api at a different time another node calls it or specifically an http get there could potentially get very very different results and if another node tries to replay these transactions by calling these apis again
02:33:49
maybe 10 years in the future there's a good chance that that api is going to be depreciated and they could be hacked they could be malicious et cetera et cetera the other reason that blockchains intentionally can't make api calls is because then they would be making assumptions about the real world and layer ones typically don't want to have an opinion on any political or geopolitical issue whereas an oracle network on the other hand can make those assumptions the other main thing we need to talk about here is centralized oracles being main points of failures if
02:34:20
you or i say hey i'm just going to be the oracle i'm going to be the one to put this data on chain we now have this massive centralized point of failure we've done all this work to make our decentralized computation decentralized and on chain but we ruin all the decentrality by having a single point of failure remember one of the whole purposes of blockchain is so that not a single entity can flip a switch and restrict our freedom to interact with a centralized oracle a single entity can flip a switch and restrict our freedom
02:34:51
to interact with each other we also need to get data from many different decentralized sources or do any type of computation in a decentralized manner this is where chain link really shines chain link is a modular decentralized oracle infrastructure and oracle network that allows us to get data and do external computation in a highly civil resistant decentralized manner it can be as customizable as you want as you can run with one node or many nodes or do as many nodes as you like
02:35:23
now currently one of the most popular features of chain link is their data feeds or their price feeds we can even go check them out over at data dot chain dot link we can see a number of different price feeds and the networks that are providing the prices for these specific pricing powers we can see here by looking at the ui there is a whole number of decentralized different oracles returning data for different price feeds this one for
02:35:53
example is fusd and it's also exactly the price view that we're looking for having a decentralized network bring this data on chain and have it as a reference point of definitive truth allows users to all collaborate and use this common good and it will be cheaper more secure more efficient than anybody even running their own centralized oracle so these price feeds are incredibly powerful additionally they're being used by some of the top protocols in the defy system right now like synthetics which at the time of
02:36:25
recording is securing around 2 billion sushi swap for leveraging trades set protocol commodity money ave for understanding the price of an underlying collateral now this is an example of an out of the box decentralized solution that's already been packaged in the decentralized manner for you to consume and for you to use this makes going to production a thousand times easier than building everything yourself however if you want to make api calls and build your own decentralized network you absolutely can with the chainlink
02:36:57
api calls we're not going to go into that here because using the chainlink price feeds chainlink vrf keeper network and all these other pre-box decentralized services are going to make going live and going mainnet a lot easier you can always make a chain link http get call as well we're not going to go over this though because putting this into production is a little bit trickier and working with chainlink vrx if you ever want to try them out by themselves you can always head over to docs.chain.link and head over to get the latest price feed there's usually a remix button
02:37:28
actually that we can click and it will kick us out to a remix edition with all the code already ready to go for us if we just hit this just right here this will include all of our code which we'll go into in a second but let's go ahead and compile it we're going to deploy it to a real network here this one looks like it's actually for covin so we're going to go ahead and switch to coven looks like i don't have any covent ethereum so we're going to grab a covent faucet we can usually find different faucets in the chain link
02:37:59
documentation let's look up kovin here there is a coven faucet here it looks like in order for us to get some covent ethereum here we have to log in with github then we can add our address in here and get the ethereum in the interest of time i'm going to skip ahead for me doing that great it looks like i've got some covent test that now being able to switch between test nets is going to make you a lot more effective as an engineer as well because you're going to be able to understand how each network actually works so now we've compiled this
02:38:29
let's deploy this again metamask is going to pop up and let's go ahead and click to get the latest price and we can see that this function does indeed return the latest price of ethereum now you might be asking why does this number look so big well remember how we talked about whey and gray and ether well the reason that those exist is because decimals don't work in solidity so we actually have to return a value that's multiplied by 10 to some number so this value is actually 2614 times
02:39:05
10 raised to the eighth now the next question you might ask is well why did we work with this on a test net why can't we do this on a local network and the answer to this is because there is a network of nodes looking at this test net and delivering data onto this test stem when you spin up a local network or do a simulated vm there are no nodes actually doing that we'll learn later how to actually mock these interactions and mock a chain link node returning data onto our blockchain but for now let's head back over to the contract that we're working on so we can learn
02:39:35
how to implement this in any contract that we ever want to another contract called in this case called price feed has a function called latest round data which returns a lot of data it returns a round id which defines how many times this price feed has been updated it returns a price which is the actual conversion rate between the two assets it returns a started at which defines when this was last updated it returns a time stamp and it returns an answer in round don't worry about
02:40:07
answered in round for now if you want to dive a little bit deeper into what these rounds mean and what answered in round means you can definitely check out the chain link documentation and some of the faqs to learn more now how do we implement this data feed into our fundme application well the first thing we're actually going to need to do is we're going to need to import the chain link code so we're going to do import at chain link contracts source v 0.6 interfaces
02:40:39
slash aggregator v3 interface dot soul now let's talk about what this is actually doing oops looks like i spelt aggregator v3 interface wrong all right great now it's actually compiling so let's talk about what imports actually do as we know an import will take whatever code you're importing and stick it at the top of your project so when we import from at chainlink contracts we're actually importing from the at chainlink
02:41:12
contracts npm package we can look up at chain links contracts in npm and we can see and read more about this repository this links us back to the github which will tell us a little bit more about what's really going on if we follow that import path that we got from the documentation we'll end up on this file in front of me now we have what's called an interface you can see these contracts don't start with the contract keyword but they start with the interface
02:41:45
keyword they have the exact same pragma solidity at the top but the main difference is that you can see that their functions aren't completed they just have the function name and its return type now just to be a little bit more explicit here i'm actually going to go ahead and delete this import statement on the top and replace it with that interface code from github just to show you exactly what's going on however if you've already typed that at import syntax feel free to leave it in there and just remember that it's going to be the exact same as me copy pasting the
02:42:17
interface code in our code here solidity doesn't natively understand how to interact with another contract we have to tell solidity what functions can be called on another contract this is where interfaces are actually going to come in similar to structs what we can do with interfaces to find a new type so if we copy all this code from this section and place it at the top of our code here above where we're declaring a contract we can actually then
02:42:47
interact with contracts that have these functions if we go ahead and even compile this we can see this does indeed compile correctly remember how we said before we talked a little bit about abis well interfaces actually compile down to what's called the abi or the application binary interface the application binary interface tells solidity what functions can be called on another contract we need solidity to know what functions it can use and what functions it can call other contracts with and if that
02:43:23
was a little bit confusing just know any time you're going to interact with another contract in solidity or smart contract programming in general you're going to need that contracts abi we'll go into what these apis look like a little bit later anyways to simplify it interface compiles down to an api we always need an api to interact with the contract so how do we actually work with this contract here to interact with an interface contract it's going to work the exact same way is interacting with a struct or a variable let's define a new function called getversion and we're
02:43:55
going to call this version function on this other contract so we'll start out doing function get version public remember it needs to be a view since we're just going to be reading this state and even in the interface it even defines it as a view returns so we'll even grab this whole bit right here view returns unit 256. now the exact
02:44:25
same way we define variables and structs we define working with other contracts and interfaces so the first thing is we name the type which in this case is aggregator v3 interface then we'd name the visibility but since again we're inside of this contract we're going to skip it then let's give it a name we'll call it price feed since this aggregator v3 interface is going to be giving us a price fee then we can do equals and this is where we're going to initialize the contract so how do we actually choose
02:44:56
where to interact with this contract well we just type aggregator v3 interface and then we put in here the address of where this contract is located in order to find where this fusd price feed contract is located on the rink b chain we can look at the ethereum price feeds chain link documentation it has a ton of different price feeds and even more not price related data let's scroll down to rink b because again
02:45:27
on each different chain the contract address that has all this price feed information is going to be different let's scroll down and find fusd which is right here and we'll copy it and we'll paste it into here now what is this line saying it's saying that we have a contract that has these functions defined in the interface located at this address if that's true then we should be able to call
02:45:58
price feed dot version and we should be able to return it whoops looks like we forgot to add those here and we need a semicolon here i hit ctrl s or command s compiled it looks like we're compiling successfully and we do need to deploy this on a test n remember this address here is located on an actual test tent it's located on an actual network it's not going to be located on our simulated chain here so
02:46:32
we do need to deploy this to injected web 3. we do need to deploy our contract to rink b because the rink b chain has this address we'll learn later on how we actually can work with a simulated chain and work with these price feeds but that's much later in this course so let's go ahead and save we'll deploy make sure we're on injected web 3. now we can go ahead and hit deploy metamask is going to pop up per usual confirm we're going to get a transaction link to
02:47:04
etherscan showing us our contract and once it actually is confirmed we can see we have our contract right here now we have our familiar functions with one additional function we have our fund button which is red because it's payable we have our address to amount funded mapping which is blue because it's a view and we also have this get version button that's also blue if we go ahead and click it we can see that the version of our aggregator v3 interface is version 3. this is the third version of the
02:47:37
aggregator interfaces hence the name aggregator v3 interface so we just made a contract call to another contract from our contract using an interface this is why interfaces are so powerful because they're a minimalistic view into another contract so this is great we have a get version function but this still isn't the function that we want we want to call the get price function which if we look at our interface we can see there's a latest round data function that
02:48:08
returns an answer this is the function that we're going to want to call on this contract so let's go ahead and make a function that calls that instead so we're going to do function get price public view returns uint 256. uh oh this latest round data function though returns five variables so how do we actually work with that well let's find out to work with this contract we're gonna do
02:48:39
the exact same thing we're gonna do aggregator v3 interface because this is the type of the contract price feed equals aggregator v3 interface we'll do this same address in here because this is the fusd address then we'll do price feed dot latest round data now since this is going to return five different values we can actually have our contract also
02:49:11
return these five different values we can copy paste like this and literally do this these five values equal price feed that latest round data and you can see that even compiles correctly let's adjust the formatting a little bit here so it looks a little bit nicer a tuple is a list of objects of potentially different types this is the syntax for getting a tuple we can define several variables inside one of these tuples although our compiler is going to give us some warnings it's saying unused
02:49:42
local variable because we're not actually using these for anything we'll come back to this now we can go ahead and do return and we can pick one of these variables that we want to return answer is going to be the price so we're going to do return answer but uh-oh we're going to run into an error return type argument in 256 is not implicitly convertible to expected type answer is an int 256 and we want to return a uint 256 so how do we rectify this we can fix this by using what's
02:50:13
called typecasting if we just return answer we're going to be returning the wrong type however integers and solidity are really easy to cast into each other so we can just do uint 256 and wrap it around this answer and then compile and save that instead as you can see now our compiler is happy because we've changed this answer into a unit 256 awesome so now this get price should return the latest price of ethereum in
02:50:43
terms of usd let's go ahead and deploy this new contract with this new function so same thing we're going to come to our deploy section hit the deploy button up remember we got to go to the fundbean.sol let's deploy it confirm with metamask here and let's scroll down to our newly deployed contract as you can see we have our get version function which still works exactly the same but we have a new function too
02:51:14
called get price and this should return a uint 256 answer let's go ahead and click it amazing we've actually returned an answer now again if you're a little confused on why this number looks so big you have to remember that this actually has eight decimals we could call this decimal's function on the contract to learn that one two three four five six seven eight and we know that the current price of ethereum in terms of usd is two thousand four hundred and eighty-two awesome so now that we have the price we can actually get the
02:51:45
conversion rate but let's clean up this function a little bit before we go on up there as you can see one thing that the compiler is complaining about is we have a lot of unused local variables but latest round data returns five different variables so how do we actually return the five variables but make our compiler happy with us well we can actually just return blanks for each one of these sections with commas in between each other to say hey there is a variable here but we're not going to use it
02:52:16
this will also make our code look a lot cleaner because now this function's a lot smaller we say something's here we're ignoring it in 256 answer we're going to use some things here ignore it something here ignore it and ignore this too and we can even test this out by compiling it deploying it checking on rank b scrolling down hitting this and hitting get price and you can see
02:52:47
indeed it's the exact same as before awesome so now we're all done cleaning things up right not quite yet see the other thing that's really annoying here we have this massive chunk of code at the top that is probably a little redundant there's a good chance that a lot of our contracts are going to want to use this aggregator v3 interface so let's just go ahead and add that at chain link contract syntax back in because it's going to look a lot cleaner here and do at chain link contracts if you ever get a little bit confused with what you should be
02:53:19
importing to work with their contracts we can see right in the documentation at the top this at syntax is what this is going to use now you can also go ahead and browse that npm package of at chainlink contracts see what other applications are in there and what other files are in there or you can just peruse around the github now the third way we can actually do imports is we can import from contracts that are in the same file system as our contracts well awesome our contract is starting to look more and more put
02:53:50
together now one other thing that i usually like to do with these is i usually like to put everything into the gray way standard so as we saw this get price had eight decimal places however the smallest unit of measure aka whey if we look at it has 18. one two three four five six seven eight nine ten one two three four five six seven eight so typically in these i like to try to make everything have 18 decimals as well
02:54:21
you don't have to do this and it'll save some gas if you don't but i usually like to multiply everything so that everything has 18 decimal places so since i know this has 8 i can just do 1 2 three four five six seven eight nine ten and now this will return the price with eighteen decimal places instead of ten now we have the price of ethereum in us dollar which is fantastic so we could set the price of our funding function to anything that we want here for example let's say 50
02:54:53
we could convert whatever value that they send us to its us dollar equivalent and see if it's greater than or less than fifty dollars all we have to do is make a new function that converts that value that they send to its us dollar equivalent so we could do function get conver generate and it will take a u 256 f amount let's get this out of the way for the rest of this
02:55:24
it'll be a public view function since again we're not actually going to have it make any state change we'll do returns you want 256 and then in this function we can do uint 256 f price equals get price and we can call this get price function up here now we have the price in here what we want to do is we want to convert whatever value that they send as f amount let's say they send one way
02:55:57
or again that's going to be this much way what how do we convert this to fusd well we can now do unit 256 s amount in usd equals this f price times the f amount that they sent this is actually going to result in a much bigger number than we're looking for and then of course we're going to return it
02:56:29
let's test this out and see why we have to do one more thing here so let's again fund me we'll deploy rank b we'll scroll down and we have this new function called get conversion rate let's grab this one gray and put it in here whoa this seems like it's a really big number we're saying that one gray is equal to one two three four five six seven eight one two three four five six seven eight nine ten
02:57:00
this many dollars now i don't know about you but i don't think the price of even one ethereum is that many dollars maybe in the distant future but definitely not right now the reason that it's off is we have to then actually divide by this number the reason that we have to