WEB3. py: Interact with the Blockchain using Python -2022- | Dave Slater | Skillshare
Drawer
Search

Playback Speed


  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

WEB3. py: Interact with the Blockchain using Python -2022-

teacher avatar Dave Slater, Develop the World that you dream of

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

    • 1.

      Web3.py Intro

      0:53

    • 2.

      Install web3.py and Chainstack

      8:01

    • 3.

      Connect to the blockchain

      33:24

    • 4.

      Interact with a token smart contract

      20:09

    • 5.

      Send transactions with Web3.py

      20:55

    • 6.

      Swap Tokens on a Decentralized Exchange

      20:32

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.

64

Students

--

Project

About This Class

Who is this class for?

This class is for everyone who has some python experience and wants to learn how to use it to access the blockchain, you will learn how to call smart contract functions and retrieve information, as well as make swaps and transactions! At the end of this course, you will be able to basically build trading bots in Python! (We don't cover the strategies, but how to make transactions and swaps happen).


Web 3 and the blockchain have been expanding a lot, developers need more ways to interact with the environment. And here is where the web3 library for python comes into play!
Python is one of the most versatile programming languages and it integrates very well with almost any backend functionality.


In this course, we’ll see how we can use python with the web3.py library to interact with the blockchain. We will see many functionalities, connect to a network, take information from the blockchain, interact with smart contracts, call functions and get token information, as well as send transactions and even swap tokens on decentralized exchanges! At the end of this course, you’ll be comfortable making web3 applications in python!

Basic Python knowledge are recommended!

You will Learn:

GitHub Repository

Chainstack platform

Music: https://www.bensound.com

Meet Your Teacher

Teacher Profile Image

Dave Slater

Develop the World that you dream of

Teacher

Hello, I'm Dave. I am a developer advocate, tech enthusiast, pilot, and flight instructor! I have an extensive teaching experience from my flight instructor background. During my programming journey, I learned so much from the community, and I want to do my part to give something back!
Here I will focus on the Solidity language to create smart contracts and python, especially python, to interact with the blockchain.


I have worked on many projects and helped many teams launch their DApps.


What can you expect from me?
- Clear and step-by-step explanations.
- Support and interaction if you have doubts or questions.
- Enjoyable classes that will help you during your coding journey!


I structure my courses to be beginner friendly; if you have zero... See full profile

Level: Beginner

Class Ratings

Expectations Met?
    Exceeded!
  • 0%
  • Yes
  • 0%
  • Somewhat
  • 0%
  • Not really
  • 0%

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.

Transcripts

1. Web3.py Intro: It had been expanding along lately. And developers always in more ways to interact with the environment. So here's where the website library for Python coming into play. You know, Python is one of the most versatile programming languages. It integrates very well with almost any backend functionality. In this course, we'll see how we can use the Python with the Web three dot py library to interact with the blockchain. We will see many functionalities like connect to a network, taking information from the blockchain, interactive smart contract code function and get targeted information. You'll even be able to send transaction and soft tokens on decentralized exchanges. You're basically we'll be able to build crypto trading. But at the end of this course, you'll be very comfortable using the word three applications with Python. 2. Install web3.py and Chainstack: Hi and welcome to the first lesson, Web three, we'll pipe. Today's gonna be more of an introductory lesson where we're talking about what the library is, how we install it, and how we work in installed through the notes. Width three, the pie is the Python library to interact with the web three Environmental, with the blockchain environment. It's very versatile because using Python we can do very many things. It's mainly derived from the JavaScript web three library, which was the original one. So basically what we can do with these libraries use all the backend functionality to access the blockchain access smart contracts and things like that. The first thing we're going to have to do is install it. If you already have installed it, it's good. The documentation here we can find all the information that we need. So if we go and Quickstart, He's going to tell us how to install it. The main way to install libraries in Python is using pip. And if you don't have pip install or recommend to go find another. There are many videos on YouTube there show how to do it. It's a very simple process. And then you will just stop in your command prompt. Pip install Web three, press Enter and should install it. Now, if you're using Visual Visual Studio Code or Visual Studio to code in your environment. It might give you problems because it has some weird other things that Visual Studio changes. So I had quite a bit of trouble to install it in the beginning, but I found a way to do itself. If you have Visual Studio and gives the error during installation where you want to do, it's golf, find Visual Studio 2022 and download the entire suit suite. Because if you look through the error's gonna tell you that it needs to have the C plus plus Developer Tools. Downloading this package and then start the installation. It's going to install the C plus plus Bu to the Union. But then as an optional, you'll want to also add the Windows ten. Windows ten is decay. I'm sorry. You want to serve on throughout this one here. Because otherwise still not going to work. Once you install this install, the package is going to take some time. But once you install the package, then you restart the computer and it's going to work. So at that point you can install it with pip. And then once it's installed in solution is complete, you'll want to do pip freeze to make sure that the package was installed correctly. So here you can see we have Web three right here. Very good. The second step is going to be creating an OT connection. In this course, we're going to work mostly will be a C. This is the same process for any chain that you'll want to use. So there are two ways to do it. The first way is to use the public free waypoints. Waypoints, endpoints, sorry. If we go on dogs of bunnies.org, then running nodes are perceived is going to give us the RPC endpoints. So those are the public endpoints that we can use. These will be the recommended ones for my net. And if we scroll down, we have the web sockets. The test that as well. Those are free and those are not maintained really. These work with, these might also know work. And many functionalities of the Web, three libraries don't work in this one here. For example, funding pending transactions or finding the latest transactions. Sometimes these functionalities of the library don't work with those free public ones. So what I wanted, what I will recommend you to do is use change. That change is a company that provide these type of services where you can create a node and you can create a decently and is maintain in all these functionalities, work is a very great platform. You can do it for free as well. You can just go and change psych.com, do start for free. And you can have a developer account which is free during the registration is going to ask for a card, but it's totally free. Nothing is charged with the developer plan. You can create one node on any other change that they support, the support Ethereum, the support polygon, avalanche, BSE, phantom, all those. And this is free up to three million, three million requests per month. So just think that if you make one request per second 247 for an entire month, you're not going to reach the 3 million. These are really, really good way to do it and I really recommend it because it's much more stable. Now, once you're signed up and you enter in your platform, you can create a project. And now I will show you how to create the node on create your project, you just do get started. It's very simple and we'll call it Web three is going to be a public chain. We won't really need a description. Then once it's created, we can go in and create a note. As you can see, he's asked you to join a network, will do get started. From here, we can create a node. We can, we can select the blockchain. We have all of those available. We'll do violence mark chain, which is the one that we're gonna use in this course and we'll do it on the main net. Next here we can leave everything standard for the Cloud provider. I will take the clauses one through you. Always best. You can call it whatever you want, call it Web three. Next, C says that the estimated cost is $0 a month up 3 million requests, which is enough. We'll do join network. We'll take a few seconds to a few minutes to create a node. But then once it's created, it's there and we can access it. We're doing this on BSC in this case, you can use, you can do it on a theory or manly thing, for example, another common provider for these type of services for Ethereum is in food and fewer and fewer. I will not recommend it right now because it doesn't have the functionality with those applications. Are we gonna make, for example, we're going to make a part where we receive every three pending transactions. If you use an in foreign art is now going to work. That's why I really recommend change tack that it works with all these things. Now let's just wait for a moment to create the you can try it. Refresh the page, see if they didn't already know yet. I guess we can just stop this video here since it's just creating the node that don't have anything else to add about this thing right now. Then in the next video we're gonna see how we can create the connection to the blockchain using Python. And we're going to revisit the notes. So I will recommend you create it now. I'll see you in the next video. 3. Connect to the blockchain: Hi and welcome to disperse coding lesson out the Web three dot py library. So today we're gonna start finally recording and starting using Python to access the blockchain. We, in this course we're going to use an axis, the blindness meiner Smart chain, sorry. We're gonna use it to us as the BSC. But you can do this on any other network that you can find. List networks that are compatible with the Ethereum Virtual Machine. Here I'm coding in Visual Code, which is at the moment, is there free? My favorite IDE is great. It has all the usual systems plus you can test your code in a built-in terminal without having to switch back and forth. He's greater really recommended. By now you should have Web three Python library installed. And if you don't have it yet, you should watch the previous video where I'll show you how to do it and even with to install it with the errors that gives because of visual code. The first thing that we'll want to do here is to import the library will go from Web three. We import Web three with a capital B. Now the library is important and we can use all the functions that they have. So what we're gonna do in these videos, we're going to connect to the smart chain. And we're going to start retrieving information from it. We're going to look at how to retrieve the latest block number. For example, we can, we're going to retrieve balances from addresses. And we can even retrieve pending transactions. And then once we're through the printing transaction, we can look through them and pull out the details. And so we're going to do all these things today. Now, the first thing that we want to do is connect to the blockchain, right? The first thing that we need to do, otherwise, we can't access his right. To access the blockchain. We're gonna need to have a node. And how I mentioned in the other video, we should have, we should use chain stack because it provides free nodes that we can have and all the functionalities working with Web three in Python, we'll create first and know the URL. We can just call it an all URL. Here we're going to insert our URL. So to find it, we'll have to go and change stack. So in the previous video, show how to create a node. And so now here is running to get the URL to access the blockchain will go on. The name here is going to give us the details. You can see I just built it so it doesn't have any it doesn't have any requests or anything like that. If we scroll down, we can look at the data and here we have the access point and the credentials. If we copy the HTTPS, well, we have two ways we can do it with HTTPS and width. Web sockets. Web sockets are a little bit more efficient, so I will recommend to use those, but in this case we'll just use the HTTPS is a little more straightforward and has, unless you copied the link, will go back to the program and we'll paste it in here. So now we have our node link setup. Then here we can create the connection, will create another variable that we can call Web three. I usually call it a Web three. You can see called you can see called the W3 or any other thing like that. So with three and we're going to establish the connection for establishing the first time, we'll type Web three with a capital W. Web three dot http provider. Http provider. When I hear three delta http provider, and then we'll pass through the node URL that we have. Whereas all know the URL. That's it. Now. Now we have the connection midwives I didn't immediately. Interesting. Anyway. Now here with this we can create the connection to the blockchain. What we're gonna do next is that we can verify if the connection is successful. There is a function that went three, there were three library gives us, is connected. We will take our connection. Remember we create our variable connection here. And so we'll take the variable connection with three. We do dot is connected. Now if you run it like this, this is just going to retrieve us and bring Boolean expression back. This is going to say true or false. If we print three is connected, should just give us a true or false. Understand why. If you tried to run in Dennis, I'm going to work. Look what I did. Then the dissolving by mistake. Sorry about that. Mistakes happen all the time. If we save it and now we'll print, run it, then it should just say true. Takes a moment run. True. This one tells us that the connection is established, so our node words, and right now this script is connected to the abundance marching. Now, whenever I build programs and see this through this course, I like, I really liked to put user-friendly functions in it. What I'd like to do is for example, in this checking their connection, I like to print out an actual statement. So we can do, we can use an if statement. If Web three is connected, then we print connection, successful, success. Just like that. And of course these are all not necessary that the system, the program is going to work anyway, but I'd like to add them because it just makes it a little bit nicer, a little bit more user-friendly. And a D&D becomes a little bit more satisfying to write programs that actually give you a back meaningful information that other people can read. What we'll do is connect the print successful if he's not connected, print connection failed. Connection failed. My mistake. Just like that. Now if you tried to run the script again, here it says connection successful. So now we have an actual meaningful message that tells us, Hey, we are connected to the blockchain, works very well. Now, after we were connected like this, we can start ticket information. So if you go look on the documentation from the Web three Library, you'll find the examples and you will find many things that you can retrieve. Today, we're just going to see a few example, some basic stuff so we can get into the mode of how it works and then you can explore the documentation to have more details and especially to be able to find maybe things that you want to build. The first thing that we're gonna do is retrieve the latest block. There is a function butene that allows us to take and print the latest block. All we have to do is called the e'th dot block number command. And so what we're gonna do, we're gonna do print three dots, three dots, dot block malware. This one here. Now, one thing that I want to point out, there's all these commands, all are named, eat. Every measure unit that we're going to use is gonna be Ether, even though we are working on the button and smart Chen and it will be the same if you're working on avalanche or Phantom or any other. The EVA compatible change. And this is just because the main code, the source code comes from Ethereum. That's why it's like that. Now if we print this one, should give us the last block number or the latest product. Let's see if we cannot successful in here a principle that says block number. If we want to make it a little bit more user-friendly here I'm going to add a string, says latest block, latest block. Now, or we can have another, a little bit more user-friendly situation here. This is how we can get the block, the actual block number. Now, next thing that we can do, it's called balances. We can retrieve the balance from an address. So we'll make a variable called balance. Will use the Web three, get balanced. Command will do Web three, dot, dot get balance, like this one here. In the parenthesis. We're going to paste the address that we want to look up. What we can do is we can go on the scan. I love Zeus, find the run home address from, I don't know, from the latest transactions for example, one of those Let's see. This one has some balancing, has about $20 in B&B. Let's copy the address. By the way, every violence and other number that will receive from here is always going to be a B&B till we get a little bit later into the course. And we're gonna start using actual smart contracts. Because obviously they're not in narrative token here is B&B. If we were working on have a lunch for example, then it will return them out and see now works and Ethereum and so on. Now we have this balance variable and we can just print it like this, right? So I want to show you something now if I print the balance, we said that we have about in disasters there is about $18 worth and B&B there is 0.046 something B&B. If we just print it like this, looking at what number is going to give us back. It's going to give us this humongous number. That's because all these tokens have 18 decimals. What this does, this function here does is it retrieves the balance in way, which is the number with 18 decimals. So it will be the number at the power of ten to the 80. Luckily, to be able to read it fine. There is a good, interesting, an interesting command in the library that allows us to convert from way to the regular number. So to do that, we'll just have to use the From way method. Since his Python, we can just do it only in one sentence what I like to, or in one line, but I like to break things up here so it makes it a little bit more clear. Let's put it, let's create another variable called normal number. We're going to use the front way method will be Web three from way. Confusing because it didn't give me the suggestion is because it's not equipped three phases with three from way. And then inside here, we're going to have to specify what we want to convert. So in this case, we want to take the nominal that there is imbalance. So we'll just put balance. And then we want to put it in which measure you on and we want to, we want to convert it. In this case, we're going to convert it in ether. And that's because that is the main number though we read, we read one, BMD, A1, A3, and so on. And so that's what we want to read. Now. Remember is this is derived from the theory on virtual machine and so everything is called Aethon ether, but this is going to return the balance of DMV. Remember that because we're connected to balance market. Now if we print normal number, we should get the 0.046, something that we had in that address. Here we go. Now we are printing a number that we can see. So what we just saw that we understand what we just did here is that we took a program through Empire, reconnected to the blockchain and we retrieve some information off of it. It's pretty cool. Now, let's do something a little bit more elaborate. We use the same principle, but we're gonna retrieve some transaction hash. This is something that can be very useful if you're trying to build some type of bot, for example, the ones to look at the transactions will retrieve pending transactions. In this case. What we want to do is we're gonna use a filter with three lone wolf through library has a filter method. What we're going to do is let's call it pending transaction filter. Pending transaction filter, like that. And we'll apply the filter method totally worth three dot dot filter in here. Then we can put whatever we want to take. We want we can put the pending transaction, we can pull the latest transactions. If you go through the documentation in the web through library is going to tell you the options. We're gonna depend in this time. So we can just retrieve the opinion transactions. And then after we retrieve them and we're going to have to put them in a list. And so we're gonna do pending transactions. We're going to call it brand in transaction. Here we're gonna take the filter. So Pending transaction filter apply to the GED new entries, method.gov new entries. Just like that. Now this one here is a list. What this is gonna do is gonna return us a list of the older last pending transactions when you run it. Now in my in my even know have anything because it might be that there are no pending transaction is the same exact instant that we run in. But let's try. If we do print pending transaction, it should print a list. Let's try. We'll run it. Connects and here we go. So now this is a list of, these are lists of transaction hashes like this. One here is a transaction hash. So if we take, let's say we take the first one here. We will put it in the BS scan. Here we go. We have the details from the transaction. And here it was indexing and it's just succeeded very well. And then from here we can see from what others it will send to where it was sent and what happened with the transaction and everything they value and so on. Now, from here, we are only taking the hash. That's a list. So how could we do to printed in a nicer way that we can actually use for something. Just use some regular Python here. And we can just loop through the list. We can do a for-loop. We can just do for hash independent transactions. We print the hash. Now if I just print it like this, I want to show you how it's gonna look. Gonna look a little bit on readable. If we just print it like that. It's actually a no print it and it's indirectly because it takes it in bytes. So we have to use an extra command here. And we'll have to print the exit that we had to convert from binary to hex. So we'll do Web three dot hex. We'll take our hash there. And so now it should print a regular, just a regular hash. Here we go. We have an entire list of all the last, the latest pending hashes. And by the way, I just realized that the previous one that didn't print anything else probably because there wasn't anything to print the moment. We can look it up again in a second. See here we have the pending transaction just like we checked before. Now we have a system that can retrieve assaulted last bit in the latest pending transactions. Let me check here. If I remove this, I just print the regular hash. Save this time there is something to print a one. I wanted to see how it looks because it might be important later on. Okay, so here now we found something. You can see we have the hash, but it's all broken up in pieces. If you look at here, there is a B in France. So when there is a B in front of me, this is bites. And we cannot read this. We can just take this one and put it in your scan. It's not gonna we're not gonna work. That's what this one does, is that takes it from the bytes and puts it back in x in hex. So the system can see, I wanted you to see how it looks because maybe later on you're doing something else and you print and you're like, Oh, why is it doing this thing? I don't want it this way. That's how it looked. So now you know, you will know how to use it. We can just convert from bytes x if you see something that looks like that. Now you know how to convert in a mighty useful. Now this one gives us an entire list of all the hashes that we can find independent transactions. But what if we want to look at the details of these hash? What if we wanted to see what the transaction is doing? We can use the get transaction method. We can create another variable that we can call details. We'll do Web three dot, dot, get transaction, transaction. Then we can place the transaction hash in. Let's say we already have a previous one. We can just paste this one for a moment. I'm just going to comment this out. So it's not going to print a huge list again. Comment, hoping through. Let's see how it looks when we tried to print the details are from the grid Get transaction method. Didn't print anything. Obviously. Why did it do that? Transaction and it wasn't of course it didn't print anything. I didn't tell him to print anything. Sorry about that. We print the details. Say I keep making mistakes. If we print details, you should print out the details from this transaction. Here we go. This is how it will look like. If we have basically a crisis dictionary, or sorry, it's not yet a dictionary. Web three object called active attribute date. And if you look in high inside it has the hash of the block hash. It says the block number. It says the address they will send from the gas. The hash is the actual transaction hash. Import everything and value. So value here, it's the column how many BMD or rather at how, how, how equal to b and be able to transfer how many equal to VMB or transferred during the transaction. We could just look through this and extract the data. So let's say that we want to find the one I cannot then monitor this transaction and only wants to take the transactions that I'll go over a certain value, then we could do that. We could just extract all the data and print this one here. Print, writer, print all the data that is just above a certain amount. How can we decode that? Because if we tried to just loop, so this is not going to work if we just do a for-loop from inside details. So let's say that in details, we've printed the data. It's not going to work because that's not a Python object. Router. It prints it, but we don't really have a way to print the key value pair. So we can just take all the keys. We don't have yet a way to get the value of the pairs. What we can do is we can transform that into a dictionary. We are not going to print details anymore. We're just going to create something else. Let's call it just a dictionary. This dictionary is gonna be a dict of details. So now with this, we can access the actual key value pairs. In this case, we can just create a for-loop where we do. We can basically access the tuple like that key for key and value in the dictionary. Then we have to look into the items or the dictionary for key and value in the dictionary items, then we'll print the key and value. So now we should be able to have all the data, though it's actually associated to the key. If you run it. I messed something up here. Object is not a terrible. Why is it doing that? Here? We have to be a little bit more creativity or it's not gonna work right? That we have to tell them what to get out of there. Or rather we have to we have to tell them what to get. We saw in one of the previous ones. We have the two from hash and so on. So let's say in this case we just want to get some of the important information. Let's say we only want to get the address that it comes from or it goes to and the volume it. Now what we want to do is I will have to create a list here, Let's call it list of information. These are just gonna be equal to the keys that we want to get, right? So we'll do from, Sorry about this, I kind of messed it up here too. Then we want to print the value. We'll just print where it comes from, where it goes and the value. Now in here, we can iterate through the list. Let us call it forward in info. In this case, we'll do if the key is equal to one over the world, then we're going to print. Now this should work. Now this should give us a list of all the things that we wanted to know from that transaction. How is that possible? This was a big mistake. I forgot the parenthesis. That's why it was any trouble. We will have to get to this point anyway. And here we go. Now we can see that there are other ways. We said we have the transaction hash has a from address a to address and the value. Now here remember that the value is in a way, so we will have to convert it. If we weren't already normally, then we will have to convert it like this. And be able to do that. We will have to get a little bit more creative. Because we will have to print the value separately. Because if we just try to, I believe that if we just try to use this, if we just tried to use this on the Val here, believe is not gonna work because not all of them are way the other things that we're looking for. The addresses. So this should probably be whenever. Indeed it does because we are trying to do the wrong thing. So for now, I just to show how that works, I'm just going to remove the other ones and we're just gonna look for the value. Now if we print it like this, is only going to print the value. In this case it should work. It should convert it away. All right, or to the theorem. The ether number indeed transferred these many B&B in this case. Just wanted to show you, if we remove the list here, we're just print the key and value pairs. I just wanted to show you how it looks. If we only iterate through them without putting out, without specifying what we want to take. Then it's going to print all of the data. Here. You can see it at all. These hashes are printed in the bytes way. If we want to extract those, then we will have to reiterate through them and convert them in. Very well. This is kind of the beginning lesson where I'll show you how to connect to the software, how to retrieve some information, how to get pending transaction interest to them. Sorry about the confusion with the items here. I put all these two parentheses, but now you should have a decent understanding of how this part works. And in the next one, we're just going to move on into retrieve more information. 4. Interact with a token smart contract : Hi and welcome back to Web three for the blockchain. So in this video, we're gonna show how we can use the Web three library in Python to interact with those tokens smart contract. What we're gonna do is that we're going to start retrieving information about a specific token. So this one is going to this script. It's gonna start just like the last one, because the first thing that we want to do is to connect the blockchain. So here I just deleted, I just restarted everything with the new in the new file, just with a new connection. And the only thing that you want to notice here is that we're also importing JSON because we're going to work with JSON file from the ABI. So once we connect, the first thing that we need for actually talking to a token smart contract is the EBI. We can call it a BI, you can call it token API, you can call it how you like. The EV AI is just a connection. So basically represents all the functions available in the smart contract. And we use this JSON file to be able to communicate with it. Json is just a language that we use to have different application communicate with each other. How do we find the ABI? How do we find the address? So these are the two things that we need. We need the contract address and a quarter ABI. As pretty much anything else in this environment, we will find it on the BS scan. Remember we are working on BAC right now. If we go back to be a scan, we can find the token. Let's work. Today. We can just work with the cake token. Just type cake, and we'll find the pancake soft token, which is symbol cake. Now on its page, we're going to find the contract. Here. If we click on the contract, now, we can say the contract address from here and paste it into the file. So we'll paste it here in the address variable, and here we have it. And now we can find the ABI. So to find the RBI, we'll just go in contract where there is a code we can scroll down and we want to see the entire smart contract here. This is the solidity code. And then if we script scrolling and we had contracted BI. Here you can see the JSON where it just lists all the variables and all the the functions of variable. You didn't really, don't worry if you don't understand what disease that's not really the need right now, so we can just copy it. Go paste it in the ABI, in the BI variable here. And it's a pretty long file, but that's okay. Now with these two, we can actually have access to the smart contract and interact with it. So the first thing that we need to do, we have to basically reconstruct the contract in Python. Recall the smart contract. To do that, we're going to have to use the contract method. And we can just create a variable. Now we can call contract. We'll do Web three. Dot contract. Keeps misspelling things today. 3d, not contract. Inside the contract. We're going to pass two parameters, will have the address, which in this case is address variable, the ABI, which in this case is our behind variable. And that's it. Now we have the contract reconstructed in Python. If we print this, if we try to print it and running, we should see that the connection succeeds. And here we go in a prints this thing here that says, hey, we have a contract object at these coordinates. This means that now we have a contract in Python and we can access it. What can we do? Well, we can do multiple things. We can call the total supply. We can recall the function name, the symbol name, and the balances. If we go on the bs can back here. Here we have the contract section. If we go and read contract. Going to highlight all the functions that we can call. For example, here we have the allowance function for when you approve and allow the contract to access your funds. And we have the balance of function where we can put an address and query for the balance of the directors and so on. We can record the decimals, the name, Seymour an august, things. We can read them through this or we can do it in Python. That's what we're gonna do now. For example, the first thing that we're going to do, we can, we can recall the total supply. Let's find the total supply of the stock. So we'll do, we create a variable called total supply. In this case, we're going to, we're going to access the total supply function, which is going to show what the total supplies will access the contract. This is the name of the variable that we created. So if you call this contract variable in a different way than you have to match, can match here. We'll do contract dot functions because we are, we are trying to access the functions. And then here we can put the function that we want to x-axis. So in this case, total supply. Then we put the parenthesis. If we just leave it like this, we are actually not calling the function yet. Make sure that we do not call the method that Python has to actually call the function. Now, we are calling the total supply function. If we print it. Guess what's gonna happen? We are going to have a number, though. We are going to have one of those huge, incomprehensible numbers. Because again, here we are calling the total supply, but it is in way. Say, we had this huge number and kind of resembles what we have here. If we go back to the previous page, it should show us the total supply sees kinda resembles this, but all the commas and everything they own the wrong place. We had to convert it. Let me just make this smaller. So now we can just, we had to convert the total supply. How can we do that? We'll just switch it with a metal just like we did before. So here in front of total supply, we call Web three dot from way. Then here's gonna be the total supply. Will convert it in ether, just like we did in the previous one. Then let's not forget all the parenthesis. Now if we print it, we should have a more reasonable number. And here we go. The decimal point in this isn't the right place. It's still a big number because the total supplies was 700000000.700 million tokens. Right now. It's still a pretty big number, but at least the decimal isn't the right place. Then if you want to make it a little bit more user-friendly, then we can print a string in front of it, like we did before. We can just call it total supply. Now I should give us in a little bit more user-friendly wages is not going to just pin out a random numbers. Say, Hey, here's the total supply there. Nice. What else can we do? We are accessing the smart contract over token. We can also access the data from the talk and we can access name and symbol, for example. And it's going to work in the same way. Let's do the name, how the token is going to work in the same way, we'll use the contract dot function's name, in which is the function, the name.com. Then we can just copy this, paste it, and then we'll modify this is gonna be the symbol. We'll call this symbol like this. Now if we print those two, still print name. Then we'll print name. Then we'll print consumer print. Let's try to run it. There we go. We were able to call the pancake soft token name and the cake token name. Now, one interesting thing is that the ABI here for these tokens is always the same. All the ERC20 standard token and the depth 20 standard token from the BSC. They all have the same structure. If we have these MBI, just by switching the address, we're gonna be able to just retrieve all this information for any token here. So let's modify this variable. Instead of having hard-coded address, let's create an input where we allow the user to input the address. So let's say right there, address, token others. Like the space looks nice. Now, the contract here, and in all these functions, instead of hard-coding and taking that specific cake token orders, then we can have whatever we want. We're just going to place it in there. Let's find another token here. Let's do, for example, they have shared on here. Yeah, here it is. Let's find sheep. Here's the contract address. We can copy it. And if you look in the supply here while I don't even know how to read this, how many those are, but we're gonna be able to retrieve this name numbers and so on. So here, when we run the program, the program should connect and then ask, hey, pulled the address in here. Here it is, right and talking address. So we can paste it. If we do enter, then it's going to retrieve total supply talking name and symbol. And you can do this for any token. Let's do, I don't know what are tokens of the heavier Pantone. Let's try font on the BSC. We run the program, will paste the address. Sometime VS code does this when you are trying to do control V, It does this. How you fix this is that you kill the terminal. You'll restart it like this. And at some point is going to restart. My computer is so slow lately. Alright, so we can paste the address in here. Here we have supplier, name and symbol. Very nice, Very nice. So here's how we can retrieve the information. So how would you use this? Well, you could create an app that allows users to input the address they want and retrieve information from it, just like we did now. Or you can associate these two above. There may be scans for a specific transaction of specific tokens and so on. The last thing that we're gonna check in here, it's how to find the balance of different addresses. Now we access, we have a way to access contract. Here. I'm gonna change this back. I'm just gonna hard-code this again. We can keep that frontal matters in there. Now let's find how we can find that balance of Phantom in this case into an address or rather. Let's do, we'll create a variable here called wallet. Others will have an address somewhere in here. So we can just find a random one. From here, we can do at holders. Let's find an artist that has a lot of the log. This one here has many of them. Has 6% of the supply. Went on the wrong. Alright, so we'll copy the address. We just paste it in here. So now we have an address. Then we're gonna use the balance of function for this. For the balance, we can just create a variable called balance. We'll call the contract the function's dot balance off. This is the function that are going to access the address and check the, check the violence. So to pass the address as a parameter, we want to pass it through to check some address method. We'll do Web three. In this case, Web three is with a capital W dot to check some address these over here. This is a method that we can use to pass the addresses and the library likes it better because it's more secure. Sometime if you tried to pass an address and you don't use this is gonna get a traceback and it's gonna tell you that he wants it as two checks and Madras and you'll see that error. That's what you need to do. And now we can paste the wallet arteries actually we don't have to face, we already have it will just wallet address in here. Then remember that after this, we need to call the function like this. And then we can print. But remember that this one here is going to be printed in way, again. To be able to convert it, we can just do the usual with three From way. Then we pass the parameters and we want to take it from the balance variable. We want it in ether as you measure unit. Now when we run the program, we should get a few things we should add the connection is successful. We should get a total supplier, the token, we should get named symbol, and then the balance of this token in this random address, a wave function will be a scan. So let's run it. Successful supply and every hearing. And now we're it here. Now we know how to call for balances from addresses. And you can use this if you want to create a bot, for example, let's say that you want to create a bot to place limit orders or just to place transaction and make swaps on our listener exchange. Well, this will be a good way for displaying to the user the balance so they know how many coins like swap and so on. This concludes this lesson. This is a little bit shorter, just to see the specific or how to interact with the smart contract, especially the smart contract of a token. In the next one, we're gonna see how to send transactions. So we're going to just send tokens between wallets. Alright. See you in the next video. 5. Send transactions with Web3.py: Hi and welcome to the transactions lessons in Web three with Python. In the previous lesson, we saw how we can connect to a blockchain and take data from aid, especially just regular data from the blockchain or two specific token. There. We're gonna see how we can send the transaction. So we can just send token between wallets. Here I have a couple of different things from the last videos. First of all, I'm importing an extra object here. The private key. To send transactions, you have to sign it. Usually if you do it manually, you Sonny from MetaMask send a transaction and then MetaMask shows up and asks you to sign it. In this case, we're going to the computer, the software is gonna assign it automatically and we need a private key for that. Since I just don't want to show it in the video, I put it in a different script and I'm important it as a private key, keyword here. Variable. It's not gonna change anything for you. You can just take your private key from your wallet and just paste it in there when you do it by yourself. The second thing is that I changed the endpoint and the node. This one here, I'm doing it on the test net. We can send B&B back and forth without having to try to be real, actual money to get to the new endpoint. You want to go on the blindness dogs. And then where you had the RPCs. If you scroll scroll down, then you had the test net and I just took the first one from here. That's the first part. That's the same where we connect to an endpoint. And then is this, the transaction is basically divided in two parts. First, we'll set up the addresses. Then we build the transaction. Then we sign it. If we sign it and then send it. Right away with this. The first thing that we're gonna do is specify the different accounts in a different analyses that we're going to use. We can just call it a count one account two, or sender and receiver. Let's call it sender and receiver. So it's a little bit more mnemonic. Will create the variable sender, which is going to be an address and account, and the receiver, which is also gonna be an address. Then we'll have the private keys, which in my case, it's just gonna be the private key variables that are imported from the beginning. In your case is gonna be your keyword or you can just put it between the course of your key, we're sorry, your private key that you can put between the keywords, the course, and just like that. So we have the sender and the receiver gonna take the addresses and we want to get in for my MetaMask. So what we want to do is that we want to we have these arteries here which has one testing and B&B. You see we are on the test net. We're gonna take it from there. We're going to send it to another address. We're going to send a half B&B to another address. Just switch that account and put it here under receiver. Just in case you don't know how to get your private key. Just do it from Matthew mask. Wear whatever account that you want to get the private key frame on the three dots, though account details, then you'll have it here. You'll click Export private key for your password. And then you're gonna have the private key make sure to use on shoulder on to anybody because they're one with the private key, then anybody could just send, makes, make transactions, send money away from your accounting, or want to do that. Now we have the three variables. The next thing that we're gonna get is the nonce, nonce new ones. These basically it's something on your wallet that keeps track of the transaction. So if we don't have it isn't going to work. We want to make sure that we establish it. So the, the, the script knows. Is to send a transaction only once. And you can keep track of that. What we're gonna do, we'll create a variable called lawns. We'll use a method called Get transaction counts. So we'll do Web three, dot dot transaction count, which is done here, this one here, good transaction count like that. We'll have to do it for the sender, will do it for the center like this. By the way, all the codes in these, in all these videos, we want the lead hub and the GitHub link is in the description of the class. I'll put it probably in the discussion as well. So if you want to have the complete code pre-made already, you can just look it up there. But I will recommend to just coat it with me because it's better for practicing. Now the next step once we have this, is we need to build the transaction. You will see the end of this video. You'll see how quick and easy disease and you have to do it only once and then you can reuse this. The transaction is built this way. Has, it's basically a dictionary, has a few different pairs in it. The first one that we're going to use as the knowns. Leave on a take it from the norms variable that we made like that. So now the transit descript knows to send it only to only one time. Now we have two which specifies which others to send it to. In this case, we'll put receiver. Then we have to specify how much we want to send with the keyword value. Now, if we just put, we said we wanted to send a half B&B. If do we just put this, then is now going to work because this is gonna send away. We'll have to convert it to the actual B&B value and we'll do it in the usual way, Web three from way like this. And then in here we can specify the amount. So in here we'll put 0.5 is going to be 0.5 ether, just like that. Then we have to specify the gas. This here will be basically the gas limit, how much gas we're willing to spend. I think these many ways should be enough. Then there are ways to estimate how much gas you could spend and you could use. There are scripts that you could design to do that. Why did you take basically depending trends of the last transactions that went through and then you take all the value they had and how much they paid, and then you can make an estimate although those but in this case it was just gonna say like this. And then we'll have gas price, which is basically a how many units of gas you're going to send. This one also has to be said, has to be transformed from way. So we'll do width three. Sorry, this has to be has to be converted to weigh. We'll do two-way. Then we can put our units in here. I'm gonna send 747 gray with it. Which are bigger, is a little bit of bigger measure or unit, right? Like that. So now let me just check the reference that I have here. I think we were able to build a transaction like this. Very well. Now that the transaction is built, we had to sign it. Sign transaction. We can put, we can create a variable called signed transaction. To sign it will use assign transaction methods. So it's Web three dot dot account in this case because we are extracting it from an account. And then we'll do sign transaction, which is not the one we nano scoreboard is this one here. Signed transaction in here will have to pass two parameters. The first parameter is a transaction that we send. The second one is our private key. Just like that. And remember my private key is in a different file, but your private key will be in there. Now that we had the transaction, we can send it. How we send it, we can do we can call it. Let's just call it a transaction hash because out of this one here is going to come out come the transaction hash. Then, then we can use to verify the transaction, will call it a transaction ash and will be Web three. This one is going to be called send raw transactions. Send raw transaction. This one here. Without the underscores. The parameter is going to be the sign transaction like this, which is the one that we created before dot brought transaction. I'm trying to type fast. We're looking at the keyboard moon, am I doing? Bro, transaction? Transaction should be good. Then this one is going to provide providers with the transaction hash. What we can do is that we can also print the transaction ash. We can't do that. Printed in them. Remember we had to convert it from bytes two x, three x. In here, we'll pass the transaction hash. Like that. Let's make, let's put a little note here. Maybe. It looks a little bit nicer when it prints like that. This is pretty much the simple system that we have to send transactions. Then we could make it, we could improve it and add the functionality like looking for maybe out of variables. It's Python, so you can do pretty much whatever you like. Let's see if it works. They should work. I tested in just a second ago. So what's going to happen once we press the button? The Run button is going to connect to the test and it's going to take a half B&B from these address and send it to this address here. And then it should give us a transaction hash and then we convert five stripe. Going to run it happens. We are just trying to run a different script that I do it again. Running this script here, if you see it has the previous video script. That is trying to run. The wrong thing. I did. I put Q wave, he said, Why is it running this thing? Interesting. Let's try it again now that we corrected the mistake. Here is saying that the value is just tested on second ago. Let's see if I tried to send on it because it seems like it doesn't like the decimal. Let's see if there's some sort of block in my system. First of all, because it's also running another script running in because I connected it here. That's why. Here we go. We will say that mistake that I made instead of two way, I pulled from way. Sorry about that. These must be to wait because we are converting it from our readable number two, the system number. Alright, let's see. Now it should work. Transaction under priced. It means that it doesn't have enough here. Let's try to put ten gla, increase the gas limit as well. So the transaction under price, sometimes you'll see, that's why it's usually good to have, maybe it'll put some more gas or having a script that calculates the Gaspar you. Now it gives insufficient funds for gas to the price. So all these little arrows that we are receiving as good to see, there are little c So we can troubleshoot in case you're gonna see them later. What's going on here is any new account, I have exactly one BNB and I'm trying to send one BnB plus the gas, which is not gonna work. Let's try to put the half BNB and now it should work. Here we go. This time it worked. We had to do some troubleshooting, which is okay, that's part of the normal life, programming life. Now if we take these hash, we go checking our, well, first of all, we can check if the transaction happened. Fundamental mask. Now you can see from this Indies accounts I had 1.2, now I have 7.71. These other account I have the half minus the gas that are used. So the transaction went through. And we can also check it here on the scan. And it says that it can find it. Sometimes it happen that it can't find it because maybe it's slow in index in N. And now never mind is not because of that. It's because I'm using the different isn't a test. This one here is not the test and of course it's not going to find it. We'll have to locate through the BS kind of the test and we don't have to go look it up Now because we already verified in the transaction happened. Here. We verified it. A transaction happened. Just to recap, what's going on here when I run my script. First, it gives me all this. Because what I did is that I just pulled that. I just put my product key in the script that they created in the previous video, then I just linked it to it. So whenever I'm running, this script is also running the other one. That's why it's showing these and I got me confused at first. And then we saw a whole bunch of errors that we made I put here was a good example to see the difference between the two and from way. So 2-way is when we want to convert the numbers that we can read through the numbers of the blockchain can read. And from way is when you want to have a number from the blockchain with 18 decimals, convert it to redouble numbers for us, then you might have to tweak the gas prices a little bit. We saw that, we saw those errors and so on. So this was pretty instructive. I hope you enjoyed it. I'm sorry. There were so many back-and-forth over the troubleshooting, but that's just a normal programming day. Or next video is going to be the last one I'm going to show how to make swap from Swap transactions on a decentralized exchange. So in this case, we're going to use pancake syrup and we're going to make a swap between B&B in cake or B&B in another token. See you in the next video. 6. Swap Tokens on a Decentralized Exchange: Hi and welcome to the last lesson in this course, we wept three applications for the blockchain. In the previous lesson, we saw how we can connect to the blockchain, how we can retrieve data from it, how we can send transaction two different wallets. Today we are going to see how we can interact with the decentralized exchange and make a swap. There are many different functions or we can call from decentralized exchange today we're just going to focus on the swap B&B in this case because we're going to use packing swap, these two, swap B&B for other tokens. We are going to interact with the smart contract and the decentralized exchange. The first thing that we're gonna have to place here, besides the usual, here I have the usual collection pattern. We're gonna have to set up the address of the smart contract address of the pancake swap or whatever out of decentralized exchange you need the ABI. So I will start from that. We'll have a variable called pancake address. I misspelled it. The pancake ABI. These are the first two things that we need. How do we get those? We'll basically Google pancakes of router address and it's going to give you the documentation or you can just go directly to a pancake. So documentation, if you go on the router V2, then you will have information. And here we had the contract to address the pancakes soap router. The router is the contract that manages the transactions for the swaps. And then if we go and paste it here, it's going to bring us know pancakes syrup router V2. Here we'll have the address. And we got to here. We'll just paste it. For the ABI will do the same way we did with the token. We go to contract. The code was crawling. We find the IBI so we can just call it. Now while we're here, we can go look at the functions who do read contract. We can see all the functions we can call. For example, we had get a mountain amount out. Those two can be used to retrieve prices. For example, if you make a function to call this function, then we can basically compare to talk as I can get the price. Let's say you want to have the pricing BU is the of what B&B is right now, then that's what you would do. Here. We'll use the getter among out. And you will basically put the quantity that you want to swap and it's one token. You don't know that we want to solve a while. The quantity they want to get, the price of, it'd be one. And then you will get the address of where you want the comparison. So BAB with the n is going to give you the price at the moment. There will be, it will be this one here, get amounts out. And then you can do coats and everything. Those are the ones that we can read. Now, the ones that we can write, those here, and these are the actual function that we're going to use. For example, see you can add the liquidity, remove liquidity. The ones that we're gonna use. Here. We're going to use this one here. Soap is exempt token or no, I think we're gonna have to use soap except for tokens. This one here, There's a function that we're going to use today is basically going to take the amount of B&B is that we input and it's gonna swap that amount for whatever token that we want. So those are the functions that we can access. And if we go back here, then we're going to put the ABI. Now we have a way to access the smart contract or the decentralized exchange right? Now. There are other things that we wanted that are variables that we want to add. Is this equity before the address we want to do is we can call it a swap address. Because this is the Arizona we're gonna use to make the swap. The private key. Let's call it private key. And actually, I'm just going to be like this for now. We'll put the address in here. One thing that I want to notice that I put it back on the main net because the pancake swab doesn't really have a test net platform. There is some testing it pancakes of the summer made, but it doesn't work as well. In this case, we're actually not going to be able to send a real transactions because I don't actually have any funds on those wallets. But the code works. I just tested it previously, is the same except called. So that's how, what do you need? In this case, you will need to put also your private key and I'll just put random stuff. That's at least we have it. Then the next thing that we're going to do is that we need to specify what token we want to buy. We make a variable token to buy. Here we had to put the address. Let's say that we want to solve. B&b is for cake like we practiced last time. Go and search for the Kcat address. Here we had the contract address, cop it. We place it in the tokens y-variable. Now, compared to the regular transaction that we made the last time, we have to add the token that we want to spend. The previous video. We just send a B&B from a place to another. In this case, we're spending a BnB, we want to spend B&B, slop it to another coin, will have to actually have another variable we can call it spend. This is gonna be the address that we want to the other, so the token that we want to use, and I just realized here in the token to buy, Let's wrap it in the checksum address function. We are sure that it works. So we have three dot to check some address. Rapid in here. We'll do the same with the spin token to check some others. Now, for the spin token, we are said that we're going to spend BMI, we are actually going to spend B&B. But when you do these swaps, use actually spend wrapped B&B. That's how we're going to use it at the same thing for ethereum, you're going to swell up. If you, if you do this around on unit swap, then you're going to swap wrapped Ethereum and so on. So we'll take the rough B&B address. I will put it in here. This the arteries of the talk, we're going to swap and then the rest is going to be fairly similar to the previous lesson. We'll build a transaction and so on. So now we'll create the nonces again. Three dot. The transaction count is one here. This is gonna be the transaction count of the address that we want to swap, that we want to use to use the swap. So now we can create the actual swap transaction. This one here we're gonna call that swap exact for tokens. So what we're gonna do, we're going to create the contract first. Here. Here we, we made the router contract where we basically did just like we did before when we access the contract in the token, we just created a router contract. We went three dot contract and then just the pancake address and the ABI. So now we have the connection to the contract. At this point. We can actually create a transaction. Here is where we build that will be the swap side that if you notice in the beginning, I don't know why this bandages. I import the time too, because here we want to put a time reference in tune the transaction so it doesn't timeout. We can start building the transit, the swap. We have a variable called start. Here. We'll just use time like this. This is basically gonna start a timer when the transaction is initiated, a timeout. And you'll see later during the function is going to render transaction is going to show that. Then we start creating the packet transaction. We can call it pancake be two transaction like this. We'll just do it through the router contract that we created earlier here. Product contract. The functions. We said we were going to swap, called the swap exact for tokens. Now this is the function that is going to, this is the function that is going to swap the exact amount of BNB that we replace for the equivalent of the tokens, in this case the cake token that we placed in here. The first thing that we need to put is the minimum amount of tokens that were going to live over into received. And we're just going to place zeros. So the transaction is gonna always gonna go through. We could put, I don't know that for each B&B though we swap, we want to a receive at least a certain amount of cakes. And the transaction is not going to go through unless they're, requisite is met. And then in the second step, we basically have an array of the addresses that we use. So we first have to spend address, which is the address of the wrap and BNB that we're going to use the token to buy. These are right here has this addresses of the token that we want to spend, a token that we want to buy, then the address that is actually going to make the swap, which is our swap address here. Then this point here will need to use the time. So we have to make sure that it is an integer. But then we'll do times the time plus 10 thousand milliseconds. So these numbers they will put here is in milliseconds. So what happens here is that the transaction is gonna start here. And then if it's not done within 10 thousand milliseconds, then it's going to timeout. Is a transaction doesn't get stuck in there forever. After this. We can do build transaction is parenthesis looks a little bit. Transaction in here. We can actually build a transaction like we did before in the previous swap, not know software transfer. Here we have the transaction in this case, the first key is gonna be from. Again, it's just like a dictionary. Here we have this swap partners because we are sending it from there. Then the value. We'll just have to do it like before, where we have to convert the numbers that we can read, the numbers and the system can read. We'll do three dot two way this time. Then we replace the quantity that we want to swap. So let's say we want to swap one entire B&B. Then that's what we're going to put in here. And the unit is going to be ether. Then the next one, it will be the gas that we use. Here. We can put much we did before like this. I think there's just a gasoline basically. Remember that then during the actual transaction we might have to tweak it a little bit. And then we'll have the gas price. The same principle with three, two-way. We'll put ten GHz way this time. So we know that we should work. Then at the end here we'll place the nonce. This makes the transaction happened only once, and we want to make sure of that. Now let's just check quickly that we put everything we pulled them from. We put the value gas, gas price at the non-square. Good. Now the transaction here is built. Just say that it looks like we have an extra parenthesis here that we don't need. Redo needed was an extra one. Okay, very good. Now, the transaction is built software that we just have to sign a transaction just like before. Sign transaction. We're just use the same that we used before in the previous video with three dot dot count sign transaction. In this case, we put the name of this transaction that we called pancake V2 transaction, the private key. Just like that. And I just realized that I put it in the entered in here. Gonna give us an error. Then we will receive the transaction hash. Will send in here is how we're going to send the raw transaction. Transaction. One here. Here we'll have the signed transaction, transaction like this. Let me scroll down so we can see better. This is going to return our transaction hash. And then we can print the transaction ash like before, three dot e to x. Then we'll print the transaction hash. Like that. Very well. If we tried to save it, it works good. So this is how we make the swap. Now I'm not going to make it work because I don't have fans in here just going to give errors and if I have anything in there, so it's gonna try to waste gas. I don't want to do that. But this is how we make the slump from B&B to another token. Now if you want to do the opposite, then we'll call it a different transaction. So let's say we wanted to swell to swap. We want to sell at talking and swap it to B&B. Then we'll have to use a different transaction, sorry, a different function called swap except tokens for that you can find over there in the, in the contract. On the, actually on the GitHub. There is also how we will be this function. That's it. This is the end of the course where we went through basically the fundamentals of how we can interact through Web three with Python. Well, thank you for following, and I'll see you in the next course.