Blockchain Python programming tutorial [FULL COURSE] Web3.py

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today I want to give you an introduction to blockchain development with Python all right you don't know anything about blockchain or Python necessarily to follow along this video I'm gonna teach you everything from scratch right I've got several different examples then we're gonna walk through step by step they're gonna teach you Python and blockchain at the same time all right I've created some other videos about Python and blockchain on my channel before it I put them all together so you can see them alright and there's a table of contents down below that you can use to skip around each part in the video ok there's me step 11 examples that are jam-packed full of value for you free hey I'm Greg Grayson adapt university be sure to like the video subscribe to the channel that really ups this video get found so that more people can learn how to build blockchain technology and if you're serious about coming a blockchain developer you need you join my free training on my website / deputy reversing calm for it slash bootcamp first I want to introduce you to this resource you know this is you know web 3 read the docs at i/o where you can find the Python version and you know reference this in addition to all the content that I created for this tutorial series this is a great documentation reference where you can find all of the you know examples for anything you might ever want to learn how to do with the web new library so let's talk about actually how aetherium works and how a blockchain works and how we can interact with it with this library so basically you know the etherium blockchains a network of nodes which basically means that it's a bunch of computers that all talk to one another and all of the nodes on the network share a copy of all the data all the blocks that make up the blockchain or on each computer and they basically you know participate in maintaining the network right and we can talk to a single node on the network right here and act on behalf of the entire network so essentially we can read data from the blockchain we can find all the transactions we can you read information from smart contracts we can you know do all that kind of stuff just by talking to a single node but we need some kind of interface to you know talk to that node and get data from the blockchain and you know make transactions and stuff like that and that's exactly where the web 3 library comes in it's gonna give us a way to talk to an aetherium node with python and you know get data back send data create transactions all that kind of stuff ok and there's a lot more I could say here you know basically we're gonna talk to an aetherium know with RPC which is basically just a protocol for you're talking to it theory um nodes and you can see that right here and that's what you know web three handles under the hood so you have to build that stuff yourself you just get it for free ok and so what we're gonna do is use the web 3 library of Python to talk to aetherium nodes and we're gonna basically just get a URL for our ready to go a theory of node so that we can talk to the blockchain directly without having to run a No our selves it's really nice we can you know kind of get up and running fast without having to download all the blockchain data and keep it in sync as stuff like that so that being said the next thing that I want to do is head over to a Pheo to get a link to an aetherium node and that's what we're gonna use to create our connection to the blockchain it's furious basically as aetherium as a node service which gives us access to the network right and so basically if once you sign up you know we're actually have a new convention here you're gonna see a way to create a project so sign up create a project get an endpoint like this don't use my endpoint I'll probably delete this after I create the video anyway but copy that and keep it handy because that's what we're gonna need in order to connect to our theory emmm node all right so let's jump in and actually start writing some Python code and talk to the blockchain with web 3 alright so in order to get started the web 3 on Python you're gonna want to you know have Python installed have a virtual environment set up all that kind of stuff so once you've got all that ready to go we can install web 3 like this we can just say you know pip install what 3 all right so I've already got it and I also created a requirements txt file in this directory so I'll show you more about that later in this video when we actually turn this into a code project but for now we're just gonna install everything manually you run it in the terminal on the console all that kind of stuff so what we'll do now is actually take the you know inferior URL that you got from registration step and just make sure you got that handy I've got mine here I'm just gonna put it here so now I'm gonna actually open the console on say Python alright and the first thing I'm gonna do is actually import web 3 like this I'll just say from web 3 import capital web 3 ok lowercase web 3 capital o 3 alright so that worked and what I'm gonna do is take this in fury or L copy it I'm gonna say if your URL you yes all right and now I'm going to create a new web 3 connection like this will say web 3 equals I'm going to do this say web capital of 3 web 3.8 gtp provider I'm going to pass in the inferior URL oops all right oops sorry about that I mistyped that I just left the three off of web 3 so yeah what 3 equals you know capital web 3 and we're going to pass in the HTTP provider like this and then pass in our in Furio RL all right so now I'm actually just gonna see if it's connected and we could do they like this we can say web 3 is connected all right it's true so we are actually talking to aetherium and we can further verify this with a really easy step what we're gonna do is just get the latest block on aetherium right so the theorem is a block chain made up of bundles of Records called blocks which was chained together to make the block chain each blocks got a number and we can read the latest block number with web 3 like this we could say web 3 we do the e th or a through 8th namespace say block number all right and that's the current block number and it might change if I do it again I'm stay the same but you could like whenever you do this you know your number will be different and if I do this probably 10 minutes from now it might be also be different so I'll show you the next thing which is actually you know interact with the counts on aetherium right so let me pull up the diagram that I had a minute ago where I kind of showed you how theorem works so whenever we you know talk to the network everyone who uses the blockchain has a unique address so this is a very different paradigm from the regular web like you know if you go to a web application and you sign up you get a username and a password and you're a user of the application well when you use the blockchain you actually are a user of the entire network and when you are a user of the network you get an address right and that address is gonna correspond to your account which has some sort of balance right and that you know balance sometimes is called your account sometimes it's called a wallet that's gonna have this balance and that's gonna tell you you know how much ether or aetherium cryptocurrency that you have and also you can store tokens in it and things like that and you can use it to interact with smart contracts but basically we're gonna think about it like an account and a wallet right now that actually stores some aetherium cryptocurrency and with web 3 we can actually like you know find stuff out about the account like see how much aetherium is in it so I'm going to use my real account with my meta mask wallet so I've got from both met amass this is basically just a wallet for you know talking to aetherium and I have a real accountant here you know this is the theory of made network I've got some real ether I've got almost three eise in here and I want to read information about this account with web 3 Python so what I'm gonna do is actually get the account balance so we can do it like this so I'm gonna go to the terminal here I'm gonna say web 3 CU f not get balanced alright get balanced I'm just gonna paste in my meta mask address that I copied from the clipboard okay say balance all right so we can see this number here we actually got a number back so that looks like a lot that's not actually ether I wish it was ether be great if I had that much ether normo wallet that's actually way so way it's kind of like a penny on aetherium it's the smallest you can subdivide ether it's actually 218 decimal places but we can actually convert that to ether like this with a utility inside web 3 we can say web 3 from way and pass in the balance and convert it to ether all right there we go and we see that number matches actually what's in my meta mask account right all right so that's how you do this in the console and I'm actually going to turn this into a project so I'm gonna like open this in sublime text and I created a new project inside of here we're going to you know use this for the rest of the tutorial series will actually write the code inside of a script and will run the script instead of doing everything inside a console over again you'll have to you know open it in and load you know your provider every time basically we're going to put all the code inside here so I'll just show you you know a quick example of that we can just run the script like this you can say you know import do well do the same set we didn't side the console will say import web 3 from web 3 then we can import our if your a URL like this and now we can instantiate our with your connection just like we did in the console web 3 equals web 3 capital of 3 HTTP provider and it'll pass in the afure URL and then we can print the block number well actually let's make sure it's connected first will say web 3 is connected and we'll print the block number okay and then we will put the balance say balance calls web 3 heath get balance and i'm gonna copy that meta mask address again one more time I don't remember it off the top of my head so do like this okay and then we'll print web 3 that from way say balance I'll say ether okay now let's hope I do all that right let's gonna run the script say Python at Pui alright yeah it worked so it's connected there's the block number and there's my ether bounce so that's a good example project again I've got a virtual environment set up I've got these requirements and a file here so that we can load web 3 every time into our project so if you're not familiar with smart contracts they're basically just programs that run on the blockchain in this tutorial I'm gonna show you an example of a smart contract we're gonna get all the data from it and you know pull it down into our project and read some information about it and I'm gonna show you how to do all of that in Python so here's the smart contract we're gonna use in our example today so this is oMG it's a token on aetherium so it's a cryptocurrency that's powered by smart contracts and that's because if theory I'm allowed you to create your own token without creating your own blockchain with smart contracts it's basically just a program that governs this currency it's just money in it you know it governs you know people's balances it allows them to transfer tokens and things like that and we're gonna read some information about - the token again this is a real you know live cryptocurrency on the blockchain that is you know worth a lot of money and we can interact with it and play around there today in Python that's what I'm excited to show you so let's see what we need to do in order to interact with this token smart contract in Python so first we want to you know go to our project we set this project up in the first video so check back in that video if you have already this is pretty simple to set up you didn't esses airily have to have gone through that first video you could just you know jump in as long as you have this framework ready to go so basically we want to do is you know pull in web 3 first but in addition to that we also want to import JSON alright and we want to get our connection to the blockchain so we did that in the last video where we got an inferior Eyre Aria and inferior URL to an aetherium node so go check that out in the previous video of you Emma already basically just got to sign up and get a link that looks like this and now we want to actually talk to the blockchain want to connect to it so we need a connection like this we say web 3 equals a Capitol web 3 and we say Capitol web 3 HTTP provider in fury RL I'm gonna pass that in and you know we're gonna run this as a script and we can just check everything's working we said web 3 is connected I believe that's how it works oops sorry and we'll say print I will run this script Pacific everything is working properly I will just say Python say app tap py all right so we're connected that's good so I'm gonna clear this out so what do we need to do we need to actually connect this smart contract ok and we need a couple of pieces of information about it what we need is an ABI and we need an address sorry my text editor is going nuts here so what are these two pieces of information well the ABI is basically just gonna be a JSON array that describes what the smart contract looks like okay and the address is actually gonna be the address of the deployed smart contract on the blockchain and with these two pieces of information we can actually reconstruct the smart contract in Python to interact with it and you know get data from the blockchain about it so let's take a look here you know this is either scan where we can see the token so we need to find the address and we can see that right here this is the smart contract address we'll just copy this and we go back to your project we'll paste it in alright and now we need the ABI so you know what is them where is it alright I've actually got the code pulled up here so I'm gonna scroll down and find ABI so this is what the API looks like it's basically this big JSON array that describes the functions on the smart contract we're gonna want all of these in order to you know basically be able to talk to our smart contract in Python so just take all this and copy it I'm gonna copy like this okay copy clipboard and now we're gonna paste that into our project like this we'll just minimize this and say ABI is this oh sorry I had to use single quotes alright so now we actually want to do is do this I'm gonna say take JSON and then loads and then do that okay so now we're gonna have the ABI the way we want it okay so now with YouTube this is information we can reconstruct our contract with web 3 and Python because a contract is equal to web 3.8 eh a contract we could say address and address is gonna be a dress and the ABI is gonna be a bi okay and we can just print the contract make sure this is working and we'll just run the app again okay so we see that no errors so far so good so now we can do is actually call the smart contract functions we want to see how many tokens there are so we can actually go back to either scan and pull up the information you know we can see that this total supply is like a hundred and forty some big large number I think million if I look at it at first glance so we can go back to our code and actually read that number from the blockchain like this we could say total supply is equal to contract functions and then total supply and in order to actually get this it's not enough to just to call the function we actually have to do additionally this call okay so in what three there's their call functions and then their send functions and we can talk about the send functions in another video but basically call just means we're reading data from the blockchain and a send function means agree writing data that's gonna require some extra parameters like signing the transaction and paying gas fees and stuff like that so we'll get to that another video but this is just how you read it from the smart contractors with this extra call function at the end right so now I'm going to print total supply and we can run our app again all right I've got a value back so boom we've actually read some information from the smart contract from the blockchain and we can see that number looks somewhat like this but it's got a lot more like looks like the decimals in the wrong place so why is that well that's because the value we got back is actually the way amount and I talked about this in the last video so when you're dealing with crypto currencies they have a decimal resolution which basically means like it can be divided by so many decimals you can actually see it here on either scan this is 18 decimals that basically means that you can have 18 decimal places after the decimal point that you know show you how far you can subdivide the cryptocurrency so it's almost like having a really small penny so if a dollar can be divided by 100 in in the US you know this oMG token can be divided by you know 18 decimal places all right so we want to do is actually convert that to the real value so we can do it like this we can say web 3 that utils say total supply sorry wait so utils from way so we're gonna treat this like ether because ether also has 18 decimal places and so does this token so we can just say æther even though it's not either is a token that's okay you're just saying use 18 decimal places okay so let's try that and now you should see a number Oh web 3 utils sorry this is different language okay try this so there's also a Python version of sorry not Python is also a javascript version of web 3 which I use a lot more than the Python version and some things are just different uh-huh so I find myself doing the wrong thing in the wrong language that's the problem of knowing a lot of programming languages is sometimes you just mix and match things by accident so anyways here is the value that looks a lot more like what we see in an ether scan here okay when are the commas obviously and you could you know add some formatting if you wanted to but this at least puts the decimal in the correct place alright so now we can get some more information about this let's say contract look at the token name tract functions say name I'll print this alright alright because the oMG token was returned now let's print the symbol all right boom OMG and then let's let's actually read some information let's do something besides just you know calling a function that has no arguments let's actually get a function that requires arguments okay and we can kind of go back to ether scan and look to see what's available to us we can go back to the code you know you could read through the solidity source code if you wanted to and like you know kind of figure out all the functions or you could just go to this read contract tab right here and we could see some functions here that have arguments so we see balance of is a function on the smart contract and we would pass in you know an address to see what the balances so here's a fun little experiment we can go to the holders tab and find a really rich holder of OMG someone who has a bunch of tokens we can actually see the like you know somebody has you know 19 percent of all the oMG tokens out there so we can copy that address like this and say contract functions balance of and pass in that you know pretty rich account because that call actually let's instead of doing the balance by itself set balance and then we'll do the same thing we did up top where say web 3 dot balance and the ether all right so let's run an app oh so I put the Breathitt s try it again oh one more time oh sorry let's try it again all right there we go now we see that this count actually has a balance returned to us all right so that's an example of how you can you know read smart contract information from a real smart contract with Python with the web 3 library so that's how you basically use Python to build you know anything that can talk to the etherium blockchain whether it's a blockchain based application or whether you're scraping data from the blockchain for data mining or whatever you're trying to do with the blockchain python is a really great tool for interacting with it alright so in this video we're gonna continue on with our series and you know the last video we talked about how to you know read information from smart contracts and interact with them and things like that so this video is gonna be all about sending transactions on aetherium so essentially I'm gonna show you how to send a cryptocurrency on aetherium with python and we're gonna kind of go in-depth on how that works so let's kind of just jump right in so if you go to ether scan you know I showed you this in the last video but if you look at like these things on the home page called they're called transactions right so the blockchain is made up of transactions right that's pretty much all it is on the at the smallest level from all the records right so basically you know all the history and the blockchain is made up of transactions the current state of the blockchain is basically just you know sum of all the transactions it's like that's how you build the blockchain state that's how you build the state of accounts how you build a state of smart contracts things like that is just a summing up all the transactions basically so whenever you you send cryptocurrency on the blockchain it creates a transaction whenever you you know call a smart contract function or you send ether or whatever right like you're creating a transaction ok and I'm going to show you how to create one of those in this video with Python with web 3 and what we're gonna do is basically just show you how to transition is built because there's several different components that actually have to go into creating a transaction when you're writing Python and we're gonna do it in real time and I'll watch I'll walk you through it and we'll see you know how it happens on the blockchain some minimize this and what I'm going to do instead of you know talking to the main aetherium blockchain I don't want you to have to risk like losing money by watching this video and fall in las tutorial and I also don't want you to have any security risks by like you know throwing private keys on screen that are potentially sensitive so I'm gonna actually use ganache which is a personal blockchain it's a development block chain that won't require us to risk any funds getting lost it won't require us you know to really do anything risky from a security and point so basically we're gonna have an in-memory blockchain that's gonna have a theory on cryptocurrency you know that's not worth any money so if you're my ganache you can just download it here from the truffle framework website just click download and install it's pretty easy to set up and once you've got ganache running you know it'll just look like this basically you'll have this right here and this is going to show you you know list of addresses accounts that are gonna have you know balances you can see 100 you know ether on each account and we can you know see their address we can see their private keys which is what we want in this video you don't want to show anyone your private keys for a real account it's like your password on the blockchain and that's really risky because that can you can lose you can lose money that way so that's we're gonna use in this tutorial so first we're gonna connect to our personal blockchain a lot like we did in our previous tutorials but instead of using accurate URL we're gonna use a ganache URL we're takin osh URL is equal to I'm just going to copy the one from ganache right here this is just local host port 75 40 files at HTTP colon and I'll paste in that address and we're going to you know instantiate our web 3 connection just like we did in the previous videos will say web 3 equals o AB 3 web 3 D HTTP provider I'll pass in the ganache URL all right and now we're going to print web three is connected just to see if this worked I'm gonna clear out all this in the last example and we're going to run the app all right so we're connected now we can actually just try to run a transaction or sorry just read some more information just double check so we have three th block number I think that it work okay cool all right so let's send some cryptocurrency all right we need a couple different variable set up in order to do this first you need two accounts the first account must be this count 1 paste in some code here oops sorry does that count - goes another account and then we also want a private key all right I'll explain why we need all these things in a moment first let's just copy the first account from the list ok paste it in oops and then let's copy the second account all right and now we want the first accounts private key you want to show this and ganache like this okay copy it and then paste it here it's really this private key belongs to this first account and what we're gonna do is send cryptocurrency it's an aetherium from this account to this account but in order to do that we need a private key to tell web 3 that it's okay that we send a cryptocurrency from account 1 to account two right so let's talk about it council ii quickly you know counts are basically like kind of like your user name on a blockchain your public key is kinda like a user name that's kind what your account is the private key is kind of like a passwords this is sensitive data you don't want to share this with anybody for a real account but you know our private key is basically what allows us to authorize transactions it's technically called signing transactions so we can sign transactions I am web 3 with our private key to let you know the blockchain know that we're awesome that the transactions sent that it's okay to send cryptocurrency from this first account to this second account okay so we need to use this private key whenever we're signing the transaction only to sign up before it gets sent off so here are the steps that we need to execute we need to basically build a transaction right and these transactions that I showed you sort of on the network they have an anatomy that we're going to go through and build and code in Python and then we're going to sign the transaction and then we're going to send the transaction and then we'll get a transaction hash and that'll show us it was successful and we can watch that transaction as it went through and actually there's a preliminary step here we're going to get the knots so explain what that is all right so it's like you just build the transaction first we're basically just gonna build a dictionary that contains all of the transaction information certainly change the spacing got my text editor set up incorrectly so first we need some values here we need a nonce which I'll fetch in a second we need the account that we're going to send the transaction to it's actually account to all right we need the from account well from account actually gonna be infer when I we sign it sorry it's we need to value this is going to be the actual amount of ether we're going to send and this is going to be let's just said one aether and again if you saw the last videos we talked about way you know ways the smallest denomination of ether like it's to eat there without any decimal places kinda like ëthere eum's penny so we can say web three two way and we can say one ether okay and we also need to specify a gas limit like this so gas on aetherium basically is some amount of cryptocurrency you have to pay whenever you're sending a transaction that's because you know theorem is a proof-of-work block chain that has miners on it and the miners need to be compensated for running the network and the miners are the ones who actually you know mind the transactions that get included in the blocks that get written to the blockchain so basically we we specify a limit for how much gas were willing to pay this is this is not actually a theorem this is just units of gas which is in kind of an abstract concepts than itself it's not tied to you know not tied to any cryptocurrency you think about it like gallons or liters of gas in your car it's just a unit and then you multiply that by gas price okay and then so it's like saying you know if I want to fill my car up with a hundred gallons of gas that just be that would be a hundred gas and that would say you know I pay you know a dollar per gallon right and that's how you get the actual total amount so say web three two-way let's just say 50 away so way or gee way is just a larger denomination of way.when smaller than aetherium though okay so that's pretty much all we need in order to build the transaction this is sort of the anatomy of the transaction object or dictionary in this case but we need to get the nuts so what does the knots do well the knots basically prevents you from sending a transaction twice on aetherium it's kind of based on your accounts transaction history and the number of transactions you've created already so we can get the nonce like this we say web 3.8 to get transaction count let me say I count one alright well build the transaction alright and that should give us everything we need to complete this transaction definition in this dictionary it's a nonce alright and then we're gonna sign the transaction like this they signed T X I'm actually gonna remove some is code comments so that we can have more space at the bottom of the file here and then we'll say web three ETH account sign transaction I will say T X and then private key so we need to basically sign the transaction by passing in the actual transaction we built here and then when you pass in the private key to let us know that you know account number one is authorizing this and it's gonna know it's account one it can rebuild the entire account from the private key we don't actually have to have pass in like you know this at all we just needed that for the transaction count now we can actually send the transaction we'll get the hash back like this we'll say we have three send raw transaction ok signed and they'll say raw transaction okay so then we can get the result she's gonna be a hash and we can print the result all right so let's try this I might have made some mistakes but let's just run the app and see what happens all right no it actually worked first time awesome so if you look at ganache you can see that one ether was actually transferred from the first account to the second so how is that that's awesome so boom that worked but if you go back to our our terminal here you can see this looks kind of funny so we can actually convert this to hex we can say web 3 to hex all right and then run it again boom there's a transaction hash and we can see it actually in a you know more readable format so if you go to transaction so we can see that same value here alright awesome so you can do the same kind of thing on the main net or a test Network if you wanted to you could just you know take this transaction hash and you know look at it on ether scam but yeah there you go guys that's how you build a transaction in Python and send a cryptocurrency with web 3 and Python again you could do this on you know the main aetherium Network but for the sake of this tutorial I didn't want to expose any private keys I want to make this as easy as possible and yeah I don't want your risk like losing your money right this is serious business if you've got you know any amount of funds you know be very careful alright so in this video we're going to interact with smart contracts on the blockchain and we're actually going to you know call their functions we're going to write transaction to the blockchain with smart contracts and we already did a video this series where we kind of just read some information from smart contracts but now we actually want to do more we want to create transactions that are triggered on the blockchain with smart contracts that's gonna work a little differently than just you know reading information from them so in order to do that we first need a smart contract and we need a blockchain to put it on so I walk you through how to set up ganache in some of the previous tutorials so check that out if you haven't already you know actually just a personal blockchain that can be run for development purposes locally so I've got ganache setup here on my machine running on port 75 45 and now that we've got a blockchain set up we want to deploy a smart contract to it and instead of you know going through the hassle of setting up a truffle project and all that stuff I got plenty of tutorials on my channel that show you how to do that instead of that we're just gonna use remix so remix is a great tool I use all the time remixed aetherium org and I just paste it in some code here you know you could put any smart contract in here that you wanted to this is the basic hello world canonical greeter smart contract that everyone uses in the tutorials that basically just allows you to you know send a message and get a message it's the basic readwrite contract that just shows you the basics of the solidity programming language okay and sort of just how you learn your ABCs so I've pasted this in to remix important thing to note here is I'm using solidity cuz you're about 4.2 one and I also have the compiler versions set to zero for that to run and I'm going to compile it like this start compile alright and then I'm going to run it sorry I've got some old information here and I'm going to choose web 3 provider as my current environment okay you should do the same and this is how you're going to connect to ganache so once you choose that it'll say are you want are you you want to connect to an etherium no click okay and then you want to do localhost and whatever pour ganache is running on mine is seventy five forty five right here all right so I'm gonna choose seventy five forty five okay and now it's connected to ganache and we can upload the smart contract so boom it worked we can actually verify that on ganache by looking at the logs or the transactions alright we could see a contract was created okay so now we have a smart contract and we want to interact with it on the blockchain so we need a couple things we need the smart contract address which I can copy here here let's open it and see actually go back to compile let's hold on let's just copy the address like this ok boom address copied so we can go to our text editor really fast and paste that value in there I'm going to go to sublime text and actually haven't opened it yet so I'll open it sorry this opened on my other monitor alright so go to app type UI and I paste in this is the address that was deployed its address alright and now we need the contract ABI so I talked about this in my previous tutorial in this series and ABI basically is just JSON array that describes how the smart contract works will say ABI equals JSON loads and we're gonna pass it in here and in order to do that we need to import JSON okay we're gonna pass in the ABI array right there so let's fetch that go back to remix and go to compile and click details and don't copy this ABI because it's gonna give you a big JSON object that's gonna span multiple lines we just want a single line so I'm going to just let's see how they give you a nice easy way to do this let's copy the web 3 version so I'm trying to copy this line of JavaScript and we'll just format it inside of our editor somebody that I'm gonna remove all this junk go to the end of the line and copy out the JSON array alright so here's the a beyond the address now we can actually instantiate the contract like this Oh important note here's a gotcha when working with Python you have to do this in order to format the address will say web 3 to check some address this is going to basically format this the correct way and check some format so basically like ganache and and sorry my phone just deigned ganache and remix both use like a combination of either uppercase or lowercase and we got to do it and check some format now gonna stance you at the contract like this was a contract equals web 3/8 contract like a passing the keyword arguments address equals address and ABI equals ABI all right and now we can read the greeting I'll say contract functions a/c grete so we can call grete and if you remember from the previous videos it's not simply enough just to call the function we actually have to say call all right let's print this return value okay let's run the app see what happens oops add an error we actually went JSON loads my fault let's try it again all right we have a new error so named web three is not defined all right so now what we want to do on my fault we actually have it we haven't created a web three connection yet so if we go to some of the previous examples like from I'll see whenever we wrote transactions I'm just going to copy this out of the examples directory so if you clone this project from github I'll have all the examples here on the side so if I go to app type you I'll put that back in there all right so that's how we connect to ganache so my fault guys so let's try it again all right there we go there's the actual original greeting all right so now that's how you you know read smart contract functions that's nothing new we did that in the previous videos but now what we want to do is actually write data to this smart contract and we want to do is update the greeting that we saw in remix so basically we want to call this function right here set greeting okay so we can do that in our project like this will say contract functions set greeting that's the function name on the smart contract and we'll say let's say hello that's the new greeting okay and so now we'll say transact all right so instead of call it's just going to read the value and there's transact which is gonna create a transaction on the blockchain you know to do that alright so if you're coming from web 3 J as a JavaScript background this is similar to their send function so we can actually return a transaction hash here and oh well I'm here I just realized we need to set a default account in order to do this so don't forget whenever we you know are creating transaction of the bloch and we gotta sign the transactions and we need to tell it which account we're gonna be sending this from ok so we can do that like this at web 3e --the default account equals web 3e 'the accounts 0 all right so what that's gonna do is actually get this first account from the list and ganache and make that the default account and unlike you know working with echo HD while provider something this is already unlocked so actually your hty provider is also unlocked but basically we don't have to provide a private key like we did in the transaction video earlier in this series basically this can out this account it's already unlocked the ganache and we don't have to give it a private key and so we're gonna be able to sign transactions for us without giving too much information all right so now we can actually do this so let's print TX hash and let's run the script see what happens I might have made a mistake we'll find out all right yep I did make a mistake so it has no attribute functions so sorry function plural so I forgot the S let's try it again alright see what happens all right so we got in a transaction hash that's good so now let's actually display the new greeting so what we can do is wait for a transaction receipt so whenever you're creating your transaction the blockchain it's got a life cycle first you're going to get a transaction hash back almost instantly and then you have to wait for the transaction to be mined and also confirm and this is also like there's all sorts of steps that goes through and sometimes people use some term synonymously but basically what has to happen is you want to get a receipt back before you can say ok the transaction was successful so what we can do is say web three-eighth wait for transaction receipt and you pass in the actual hash go start hash all right and that will you know stop executing any code until we get there so now we can say print once it's once it's actually got the receipt we can say updated greeting and we can say yes as a format and pass in contract functions Griet call so now will actually print the new greeting once it's changed so I'll try again it's gonna take a second to load all right there we go so we updated the greeting and here's the new one all right so it's actually changed something else just to make sure it actually did work new greeting all right start it again let's make sure that we didn't just write it twice okay there we go so there's the old greeting and there's a new greeting so we know that our script worked awesome in this video we're gonna deploy smart contracts to the blockchain with Python and in order to do that you need a blockchain so I've got ganache running here I showed you how to set this up in previous videos so check those out if you haven't already and we also need a smart contract to deploy so I'm gonna use remix which we've also used in other videos so you basically can just go to remix theorem to org I've got the greeter contract in here this is sort of the hello world contract the canonical hello world contract of the etherium world we can basically just get instead of value in this case it's a greeting and the contract is a greeter okay so what we want to do is deploy this particular smart contract to this blockchain right here so we want to make sure we're connected to that blockchain first we're going to go to sublime text and connect to that blockchain just like we did in the previous tutorials or we get the URL this is ganache running on localhost port seventy five forty five thats what one 27001 is port seventy five forty five and we can instantiate our web three connection like this okay so this will connect the file to the blockchain we can actually just check that we can say web three is connected now run the script see what happens all right it's working okay so now let's actually get the information that we need so in order to deploy the smart contracts we need an ABI all right that's going to be a JSON array and we need some byte code oopsies when I paste that in here so there's a few different ways to do this right and the cool thing about web 3/4 Python is you can you know you can compile smart contracts you could actually take this solidity source code and compile it down and then get all the necessary information from that to deploy the smart contract to the blockchain so I would show you how to do that but there's a lot of failure points namely you have to have solidity installed in your computer in order to get the solidity the compiler you have to get a Python version of the solidity compiler it's an interface and importing your project and sometimes those things got a sink because this little D programming language is changing all the time and not everything can keep up with it so instead of just creating a video that 90% of people are just gonna fail to complete and that's not that's not your fault it's just because the tools are so bad I'm just going to show you to do it with the ABI and the bike code from remix so I'm gonna go in here and just compile this so we'll go to the details and I'll get the ABI inside of here so I'm going to copy this line of JavaScript and we'll just format it inside of our project we'll just get the JSON array remove this junk say ABI is this alright and now we're actually going to load the JSON JSON I've got just an imported here JSON loads and they do that alright and now we want the bytecode which we're gonna get from here so let's scroll down and go to this object right here and copy it and paste it instead of our string okay this is gonna be the bytecode that we need in order to deploy the smart contract okay so with these two pieces of information or what we need in order to deploy it and also we need an account to deploy it from which will be this one because we need some aetherium cryptocurrency in order to you know create transactions and deploy smart contracts to the blockchain so I'm going to set the default account web three-eighth default count as equal to Webb three eath accounts zero which gave me the first account in the list which is going to be this one right here all right so now it's actually instantiate this contract will say greeter equals web three eath contract say ABI equals ABI and then bytecode equals bytecode all right and now let's point like this so basically whenever you run the smart contract constructor function this is what the constructor function looks like and slowed up to 0.4 to 1 it deploys it to the blockchain or said another way this is the function that gets called with the smart contracts to deploy to the blockchain so we deploy the smart contract in web 3 for Python just by calling that function so we say I can get the transaction hash from that CTX ash equals greeter constructor and I'll call that and just like the last video where I showed you how to call the smart contract functions we must transact with the blockchain by calling transact on the constructor function all right now let's print the transaction hash just to see this work I might have made a mistake we're gonna find out all right so let's Python app py oops sorry typo all right so let's look at just a little copy error so did work that time all right so let's continue on so we've actually deployed the smart contract to the blockchain and we can see evidence of that here transactions we can see that we do have a successful transaction the contract was created all right so now let's go back to our project and let's interact with the contract that we just created so just like we did in the last video we can wait for the transaction to be mine okay say TX receipt see eath sorry what sorry web three-eighth wait for transaction receipt and paste in the TX hash all right and we can consult log print TX receipt so it happens when I program in too many different languages especially with the same library you start just doing things in the wrong language so my apologies all right let's see here if this is going to run okay so there's the receipt now we can read some information from their receipts to interact with the contract we can instantiate the contract and went through like this contract equals a 3/8 contract I'm going to paste in the address this will be the transaction receipt contract address and then we'll say the ABI is equal to the ABI you paste it in above from remix and now we can basically call the functions so they can say print contract functions greet call see we get all right there's the greeting and just like the last video we can update the greeting we can say TX ash pull over right in this case contract functions set greeting [Laughter] and will transact all right and then we'll same thing will wait for the transaction to finish and then we will print a contract functions greet call all right let's run it and see if it actually changes there we go we could see the new greeting printed out at the console alright guys that is it that's how you deploy smart contracts to the blockchain with web 3 and Python so like I said there's other ways to do this you know I can leave you with an exercise if you're feeling brave you can actually try your hand at compiling the source code for the smart contracts I'll show you how to do that right now just for your own sake I'm not gonna do it on-screen just because it's kind of treacherous with all the dependencies out there but I'll point you to the reference of how you can do yourself if you want to do it on your own time alright so here it is so basically what you need is a solidity source code this is the same as a greeting contract we just used and remix so what you have to do is you can get this Liddy's source code and compile it with the solidity compiler right but here's the thing you need to install a solidity compiler alright so this can be kind of tricky because the solidity compiler keeps like updating all the time and sometimes the version gets out of sync with the Python version or the Python interface for the solidity compiler and you can find yourself with facing a lot of problems you know you can you can basically enforce which version of the solidity compiled you want to use but a lot of times that involves uninstalling it from your machine and installing an older version that you have to find in some branch somewhere and github and it can be a real hassle so that's why I didn't do it on-screen but if you want to try that yourself you can follow the documentation here at web 3 that redox i/o in this video we're gonna talk about how to interact the blockchain and actually look at the data from the blocks themselves so in order to do that I'm gonna kind of pull up ether scan here and we're gonna talk about some of this data that you see on the home page you know you see all this information about the latest blocks the transactions and things like that and we're gonna you know actually read some of this information with web 3 with the Python programming language so you cuz he's me information here like the latest block that's actually the number for the blocks so if you don't know what a block is first and foremost you know the blockchain excuse me the blockchain is made up of transactions right the transactions are basically just like records in a traditional database but they can't be changed right and all these transactions are grouped together into these bundles of records that are called blocks which are chained together to make up the blockchain and it each block has a number and we can see the latest block number here right and we could see the block time right we can see that's thirteen point two seconds so the block time is the amount of time it actually takes for a transaction to get included in a block so if I were going to send you know basically I was gonna send a transaction on the etherium network here take this amount of time for it to get included in the block and you can also see some other information like transactions per second that's actually the sort of central bottleneck for the blockchain and it can only process a 5.6 transaction per second at its current capability so anyway I don't want to get into every single detail about everything you see here on ether scam but I want to kind of give you a high-level overview of you know you know what this information is and how we can fetch it from web 3 so like with Python you can essentially rebuild this entire thing just with the web 3 library itself you know you could display this piece of information you can display all these blocks you can display all these transactions right so like if you were gonna go click on this block in this latest block you can see a lot of information right you can see it's got a 165 transactions in it all right you can see who is mine by this is actually a mining pool the reward so it was to ether and all kinds of information right we can read the difficulty total difficulty the size on the gas used and this is all information that we can get back with web 3 and Python so let's go ahead and try to do some of that so first I'm gonna minimize this and I'm going to open this project in sublime text so we've been working out of this app dot py file here and so I'm gonna use this I'm going to basically get our and Fira connection just like we did our other examples I'm just gonna paste this in here we're gonna connect to the main net and we're going to read information from the blockchain like we saw in ether scan so if you don't have an inferior API key yet go ahead and grab one of those we did that in the first video I believe so go back and check out that information if you haven't known that just yet so we get the latest block from the blockchain like this or at least the latest block number we want to display this piece of information right here right this year so we can do that in Python like this with web 3 we say well do print say web 3 eath say block number okay so let's say that I go to my terminal I'm gonna run this will say Python app w-why all right so that number looks the same so see your let's compare them to I'm gonna open either scan again sorry second it's pretty close eight three and eight - so as a small discrepancy and that could be for a couple reasons let me explain why it could be the fact that just in between the time that we ran this and you know going to fetch the website that it could have changed write a new block number could have been added but you could sometimes also nodes have discrepancies they're not you know in sync with one another and that's something you'll find in developing for a blockchain so next we can also get the actual information from that block we can say print web 3/8 get block we can pass in latest block number if you want to let's do this say let's do this say latest all right and then we'll oops print latest all right and then we'll say get block latest okay let's try that all right we're on this script again and we can see all that information was logged to the screen now you can see a lot of stuff is hard to read but if you look at some of the information right we can see the difficulty see we saw that on ether scam she the gas limit the gas used we can see the hash we could see the miner address right this is all information that she could parse yourself - you know rebuild this page right here that we saw in ether scans sorry I'm pulling it up yeah all this information that we saw right here you could basically rebuild from this information that you got back we went through right here right okay so let's go back to our truck sublime text text editor and now we can actually you know get the latest ten blocks we could basically count down from the block number and count down by ten so we can do that Python like this we can say 4i which either stand for integer and we'll do a range say zero all the way to ten we can say print web three ETH I get okay say I'll say block number what if this will work let me just try this I don't know if this is gonna work let's give it a try sorry my spacing set the two I don't know what is doing that my text editor alright this may bomb let's just see okay I didn't like that very much let's just actually read all the information from the blog so let's say get block try that see this works all right there we go it's actually working you can see all this data that was logged out to the screen basically it's the same information we saw a minute ago but it's you know doing it for the latest tim blocks we're just taking the range we're counting down from the latest oops sorry it's gonna be minus 10 hit say - i that was dumb let's try it again there we go so that should actually be the integer with inside this range so sorry about that guys all right so now let's actually do the next thing which is we're gonna get a it's a data from a specific block by its transaction okay so if we see this block right here we can basically find the block hash so basically if you look on here you can see the block hash which by clicking see more you can copy this and I'll go back to text editor and I'll paste in the hash here so hash equals this got put in quotation marks and then we can say a print web three-eighth get transaction by block and we could say the hash and then we can actually specify which transaction we want to see okay so let's do that all right let's comments on this other stuff so that we don't just see bunch of noise in the console all right there we go so now you can see some information about the transaction right we can see the gas the gas price we can see all kinds of information the input so this is an actual transaction object right so if we were to open this up we can see 144 transactions and you know one of these is the same that we're just looking at right you can see all this information we can see you know the transaction hash the block the timestamp you know who is from whose to was four right so it looks like they were actually sending some ERC 20 tokens right so that's an example of how you can you know rebuild ether scam with Python basically you can read all the information you want to from the blockchain like this can be really useful if you're doing any kind of data scraping you're doing any kind of data analysis and you're building it is a centralized application you're building a back-end for a DAP that has to you know pull some information in from the blockchain this is a really powerful tool
Info
Channel: Dapp University
Views: 63,241
Rating: 4.9425716 out of 5
Keywords: ethereum developer, ethereum solidity, dapp ethereum, ethereum app, ethereum development, ethereum dapps, ethereum application, ethereum tutorial, ethereum mist, decentralized applications, ethereum web3, dapp, ethereum contracts, solidity, programming ethereum, ethereum programming language, ethereum coding, ethereum contract, ethereum code, ethereum virtual machine, ico
Id: pZSegEXtgAE
Channel Id: undefined
Length: 69min 34sec (4174 seconds)
Published: Wed Oct 23 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.