Track Ethereum Transactions and Balance Using Python

Tech With Tim · Beginner ·💻 AI-Assisted Coding ·4y ago

Key Takeaways

The video demonstrates how to track Ethereum transactions and balance using the Etherscan API and Python, covering topics such as API integration, data parsing, and graphing historical values. It provides a step-by-step guide on how to create an account on Etherscan, generate an API key, and use it to retrieve account transactions and balance, as well as graph the transactions using matplotlib.

Full Transcript

[Music] in this video i'll show you how to get the balance of any ethereum account as well as how to track that balance over time and then make a graph of that using the ether scan api now this api is completely free i'll show you all of the setup steps in this video and i do want to mention that this video is not designed for beginners so if you are a beginner programmer you can try to follow along but i am expecting that you have some familiarity with python because i'm not going to be explaining all of the basic syntax anyways let me show you a quick demo of what this program looks like so i have an address this will work for any ethereum address whether you own it or not you don't need the private key or anything like that and we're first going to get all of the account transactions we're going to put those on a nice graph and then we're going to look at the account balance so let me run this code it will take a second and then it should give us a graph all right so there we go we now have a graph that we can kind of zoom in on and look at all of the different transactions for so you can notice here that it's saying this account here one of the larger ones on ethereum has about 2 million ethereum currently just look at the bottom right hand corner of this graph and you'll see that and if i close this we see that actually prints out the exact balance which is 2 million i guess this is 3504.57 ethereum so quite a large number and there's a ton of other stuff you can do with this api i'm just going to show you these basic features but once you know how to use the api you can get all kinds of other data graph all kinds of awesome stuff and we'll hopefully this video will be helpful to you so with that said let's actually get into the tutorial after a quick word from our sponsor before we get started i need to thank back trace for sponsoring this video and tell you about the virtual game developer summit that they are hosting called break point 2. breakpoint 2 is a free event happening from 10 a.m to 6 p.m on february 16th and will be showcasing the tools and best practices developers rely on to ship quality games and apps there will be speakers from companies like unity aws epic games help shift and tons of other industry experts and these talks will discuss game quality testing pipelines performance issues and share valuable tips and insights from developer visionaries and leaders breakpoint 2 provides an opportunity for practitioners in the game development industry to learn from each other network and share best practices and ideas through technical talks don't miss out on this awesome opportunity by signing up today for free from the link in the description thanks again to backtrace for sponsoring this video alright so let's go ahead and get started as i mentioned we're going to use the ether scan api so the first thing we need to do is create an account on this website now i will mention there's a bunch of other ways to get account balances and transactions this is just the easiest way that i found it's also completely free so i figured we'd just use this for this video so go to this website i'll leave it in the description it's just etherscan.io and then you're going to want to sign in or create an account if you don't have one i already have an account so i'm just going to sign in and of course if you don't have one then just click to sign up okay so once you are into your account what you're going to want to do is go to your api keys which should be in the other tab here and then you're just going to create a new api key so i'm going to click on add and then just give it a name we could just do f balance uh if i could split balance correctly pi whatever name it whatever you want i'm going to create that then it will give you the api key you're just going to copy that key and now we have all we need to use the etherscan api that's literally how simple it is and now we can go back to our sublime text window which is what i'm going to use for this video feel free to use whatever editor you want and we can just say our api underscore key if we can put an underscore in here is equal to and then a string and we're just going to paste in this key now i'm going to delete this key after this video so it's fine that i show it to you great so now we have an api key we can use etherscan however before we can start sending requests to this api we need to install a few modules in python so what we need to do is open up our command prompt or terminal depending on your operating system if you're on mac it's going to be terminal and we're going to type in the following pip install and then we want the module called requests now this is going to allow us to send an http request to the api and then get some data and have a look at that inside of our code so i'm going to say pip install request i already have it satisfied for you guys most like you don't already have that and so you need to install that then we also need to install matte plot lib this is going to give us that nice graph that we saw okay so pip install matplotlib nice now for some reason that command did not work for you try pip 3 so pip 3 install and then matplotlib as well as request and if pip 3 is not working for you another command you can try is python hyphen m pip install and then request and matplotlib and if this one doesn't work if you try python 3 hyphen m pip install if none of those work for you i will leave two videos on the screen that show you how to fix the pip command one for mac and one for windows all right so hopefully at this point you've installed uh installed requests and you've installed matplotlib so we're gonna import those now i'm gonna say import and actually sorry i'm gonna say from and this is going to be request import and then we're going to import get we just need to use a get request to get some information and we're going to say from map plot lib import this is going to be pi plot as plt now this is just usually what we do and we want to access the graphing aspect of matplotlib all right so now that we've done all of these setup steps here i'm just going to go back to the etherscan website and show you some of the documentation for these apis just so you can understand how to read this and how to modify this code to access some of the other endpoints so in case you're unfamiliar an api is an application programming interface essentially it's some resource on the web that allows us to send requests to it to get some data and then use that data in whatever way we want so in this case we can send requests to the ether scan api we can ask for transactions related to a specific account or specific address we can ask for smart contract calls we can ask for the price of ethereum we can ask for all kinds of different stuff in this video i'm going to show you a very limited feature set of this api but what i do show you will let you use the other endpoints of this api so right now i'm just on the documentation i'll leave this link in the description and you can see that we have a bunch of different endpoints that we can hit we make this full screen related to accounts contracts transactions blocks logs whatever so i've clicked on accounts because this is the one that we want to use and we can see here that there's a bunch of different endpoints so we have one that gives us the ether balance for a single address and this endpoint requires that we send this type of request so requests formatted in this way we're gonna do https colon slash slash io slash api.etherscan.ios8 the module we're accessing is account the action is balance this is the address that we want to get the balance for then we have a tag which is going to be the latest balance as well as the api key which is our api key that we generated in kind of the first step in this video now it shows us what the request should look like so we need to pass an address and a tag and then it tells us the response that we're going to get so we're going to get a status a message and then a result and this result is going to be the amount of ethereum in whey which is the smallest unit of ethereum that we have now i'll show you how we can convert way to ether because obviously this looks like a massive amount but it's probably not even an entire ethereum anyways i'll show you that in a second now we also can get the ethereum balance for multiple addresses we can get a list of normal transactions we can get a list of external or sorry internal transactions all kinds of stuff i'm going to show you how to use all of this the point is that you can go to this documentation and very easily see how to call the api and what the response body is going to look like and of course if this is a little bit confusing don't worry we're going to go through all of it okay so the first thing we want to do is we just want to get the balance associated with the single address so what i'm going to do is just copy this entire string right here and paste this into my code and then we'll start modifying it to actually make kind of a get request url that we can send okay so let's go back to our code here i'm just going to paste this in oops so let's paste that i mean to run the code and for now i'm just going to put this inside of a comment so let's make this a multi-line comment just so we can reference it okay so this is the format of the request that we need to send and the base url is this okay so the base url is the part before the question mark all this stuff right here that you see is called query parameters and these are kind of the arguments we're sending to this uh api so this is the api url and these are the arguments that are going to give us the data that we want so i'm going to say that my base underscore url is equal to and then in a string i'm going to paste this right here etherscan.io api then what i want to do is write a function that's actually going to create a url for me based on my api key the tag the address all of this type of stuff okay so what i'm going to do is say define i'm going to say make api underscore url and then inside of here what i'm going to accept is a module a action like this and address and then something called quarks now we'll talk about what quarkx is in a second but for now just put it in there and then what we want to do is say that our url this will be a variable is equal to and then this is going to be the base url plus and then we're going to put in f string and i'll discuss how an f string works in a second we're going to say question question mark module equals and then this is going to be module we're going to say ampersand and then this is going to be action is equal to action we're going to do another ampersand and say that the address is equal to the address okay so let's explain what we're doing here the point is that i want to have multiple api calls right i'm not just going to be calling for the account balance i'm going to be calling for a bunch of stuff and all of these api calls are going to require very similar things we're going to need a module we're going to need an action we're going to need an address and we're also going to need an api key which is formatted like this i'm going to say api key is equal to api key now what this function is going to do for us is it's just going to make a url that we can send a get request to so we don't have to type out this entire thing every single time that we want to make a request so what we're doing is we're adding the module which is equal to whatever module we pass in here the action equal to whatever action right the address whatever address and then these keyword arguments are going to be any extra query parameters say like the tag that we might want to pass to a specific endpoint again oh you'll see how this works in a second but now what i'm going to do is i'm going to look at all of these quarks which are keyword arguments to this function and i'm going to add them to this url so let's just explain korg's quickly in case this is a bit confusing when i call a function these here are called our positional arguments which are required and we need to pass them to the function so the module would be something like account right and then our action would be address or sorry not address balance i'm just copying what's up here okay and then our address is going to be whatever it is so let's just copy this address right here let's put it in a variable called address is equal to that okay come on paste okay i guess i didn't copy it so let's copy and paste okay so we're going to put in the address and then if i do something like tag is equal to latest since this is not one of the positional arguments it's going to be passed in as a keyword argument now this star star quirks allows us to accept an unlimited number of keyword arguments so now i can go here and do something like x equals 2 or anything else that i want doesn't matter i can pass unlimited keyword arguments now inside of here they're going to come in as a dictionary so quarks is going to be equal to a dictionary that's going to have keys and values so if this is what i were to pass tag equals latest and x equals two then the dictionary is going to look like this we're gonna have tag associated with latest and x associated with two so hopefully that makes sense just allows us to accept an unlimited number of keyword arguments then we get them as a dictionary and we can access them inside of here this is a very dynamic way to write this function now what i'm going to do here is i'm going to say 4 key comma value in and this is going to be quarks dot items now dot items is going to give us the key value pair inside of a tuple for every single item in the dictionary so if we had latest and this was associated with tag or other way around i should have wrote it the opposite way we would have let me just do it correctly here tag and latest as one of the items okay because tag is the keyword argument and latest is the value so our key is going to be tag and our value is going to be latest hopefully that makes sense but that's what items gives us so what i'm going to do in here is i'm just going to look at the key and the value and i'm going to add them in this kind of format ampersand whatever the name of the key is equal to whatever the value is to my url so i'm going to say url plus equals and then i'm going to add an f string which is an ampersand and then i'm going to have inside of here the key is equal to the value and since i forgot to explain f strings what f strings are are something that are new in python version 3.6 you need to have 3.6 or above to use them and they allow you to embed expressions directly inside of curly braces so this way i don't have to do a bunch of concatenation i can just write curly brace module curly brace and it will take whatever module is and just place it right inside of this string so really easy okay so there we go we now have a function that can make an api url for us and it will add the api key and all the other stuff that we passed to so let's now try this out and see if it works i'm going to say get address url or get balance url oops is equal to make api url okay and we will just pass all of this and then i'm going to print out the get balance url and let's have a look at it okay so when i run this we see that we have a url now that has etherscan.io api we have question marks stating we're going to have query parameters now we have our module we have our action which is balance we have our address we have our api key and then we have our tag perfect so it just made a url for us that now we can use to actually get this data so i can actually just copy this url and i can put it directly inside of my browser and you'll notice here that when i do this i get the result which is the amount of ethereum i get okay and i get status 1. but i don't want to do this in my browser i want to do this from code so now how do we do that well let me get rid of this because we no longer need this and what i will do now is that i have this get balance url i'm going to send a get request which is kind of equivalent to me going to the url in my browser so what i'm going to do is say that my response is equal to and then this is going to be get and the reason i can use get is because i import it from requests here and i'm going to put right here the get balance url okay because this is the url that i want to hit now what i need to do if i want to get the data associated with this response is i need to say data is equal to response.json now json stands for javascript object notation and that's the format that is going to return my data now in python json is really just a dictionary so what this is going to do is give me a python dictionary containing all of the keys and values in that response so the status message um i guess if it was okay or not and then the value so the actual amount of ethereum for this address so let's now print this out i'm just going to print out data and let's see what we get when we actually send this request okay so i run this and then notice that i get status 1 message okay and then result and this is the amount of ethereum now all i really care about is the actual amount of ethereum so what i'm going to do here is say that i want to get the data at and then actually i forgot what this was was this value um actually i think it was results right yeah i think it was in results so let's print this out now and see what we get and notice that we actually get the result which is the amount of ethereum awesome so now we need to convert this to ether because this is in way which is the smallest unit of ethereum and 1 ethereum is equal to 10 to the exponent 18 away so what i'm going to do here is just make a variable at the top of my program i'm going to say ether underscore value is equal to and this is going to be 10 to the exponent 18 and all i'm going to do is i'm going to take my result and i'm going to divide it by the f value or the ether value but i need to first convert this to an integer because by default it's going to be a string okay so i'm going to take my result convert that to an int this is the amount of ethereum away i'm going to divide this by 10 to the exponent 18 and that will tell me how much ethereum i have not how much way i have for this bounce okay so let's go ahead and run this and notice here that this has a very large amount of ethereum so 353 318 ethereum and if we want to verify that that is indeed correct we can take this address right here okay again just a random address i'm using and i can punch this into ether scan so let's paste it in here and this is the amount of ethereum that we have looks like that's about one billion dollars nice this definitely is not my wallet in case you were one okay there we go so we now know how to get the balance for an ethereum account so since we've done this let's just put this in a function so we can use it really easily let's say define get account balance okay and then all we're going to take in here is an address and we will do all of this and then for the address yeah we can use address that's good let's see do we need anything else okay i'm just going to say value is equal to this so rather than printing this out i'm just going to put it in a variable and then i'm going to return the value so this way we can just print it outside of the function rather than directly inside so now if i wanted to call this i would just say you know f is equal to get account balance i'm going to pass my address which is a variable i'm just going to take this and put it a bit lower so it's easier to see okay and then i'm going to print out my f like that and we've just done the exact same thing as before but now we have a bit neater code okay there we go we get the bounce and now we can use this function for any single address that we want we just have to pass a different address as a parameter perfect okay hopefully that is clear that was step one hopefully you understand how to make an api call now again all we're doing is using the get method from request we're then getting the response we're looking at the json which is going to be the data associated with the response we're getting the key in the dictionary that we want which is result we're going to convert that to an int divide that by the ether value that gives us how much ethereum we actually have we return that from the function and then we print that out nice so now that we've done that what we want to do is get all of the transactions associated with an ethereum account and then put those on a graph so to do that i'm going to make a new function and i'm going to say get and we'll say transaction if we could spell this correctly i think that's right get transaction get transactions yeah that's fine okay and we'll pass an address so now let's go back to the website let's have a look at the transaction api and what we need for that okay so get ether balance no that's not what we want we want get a list of normal transactions so first let me just explain what is meant by normal so a normal transaction is a transaction that happens between two different ethereum addresses so me sending you money or you sending me money that's a normal transaction an internal transaction which you can see is right here is a transaction that occurs with a smart contract so we're going to want to get both the normal and the internal transactions so we can actually graph the historical value of an ethereum account i'll discuss more what i mean when we start doing all this but the point is we want both the normal transactions as well as the internal transactions because what we're going to do here is we're going to look at the very first transactions and start keeping track of how much ethereum is in the account based on all the different transactions that have occurred now there is actually an easier way to do this because all we're trying to do is graph the balance of the ethereum account over time however it's a pro api so i'll show you if i go all the way down here get historical ether balance of a single address by block number this is exactly what we want to do however it's pro and i don't want to pay for this so rather than us paying for it let's just do it ourselves and write some code that uses uh the free features okay so that's what we're doing so if you're wondering why i'm going through all this it's because i don't want to have to pay and i don't want you guys to have to pay either okay so let's first look at get a normal uh get a list of normal transactions by address so let's copy this into our code and let's do a similar thing to what we did before okay so let's just paste this in here this needs to be a multi-line string or multi-line comment okay so we want the module to be account we want the account uh the action to be transaction list we need the address we need a start block and end block a page an offset optionally a sort and then our api key so inside of here i'm going to say get underscore transactions underscore url is equal to and this is going to be the make api url and we're going to pass in the account we're going to pass in tx list as the module right for the address we'll just pass in the address then we're going to pass a start block this will be zero and n block this will be whatever this end block is here so this is just giving us the range we want to get the transactions for so you could filter this if you want to get the transactions between two different block numbers we just want all of them so we'll go 0 and then a block number that doesn't yet exist so we get all of them we need to pass a page and an offset now the page is going to give us now the page is here because we could get a ton of results and every request that we send can only give us a maximum of 10 000 transactions so if we had say 20 000 transactions that we'd have to send this request again with page equals 2 to get the second page of transactions now i don't think any of you are going to have accounts that have over 10 000 transactions so don't worry about it but just want to explain why that's there so we're going to say page is equal to 1. the offset is going to be how many transactions we actually want to get uh i believe that's what it does yeah i think that's how many transactions we want to get so let's just go with 10 000 which is the maximum and then we want to pass sort so if we pass sort ascending then that's going to give us the transaction that came very first when i say first meaning like the earliest possible date first and then the last transaction last which is what we want and then we had one other thing i think it was just the api key though let's see yeah just the api key which we already handled okay great so we've done all that so this should be good to go now and let's just see what happens if we send a get request to this url and just look at the raw data so i'm going to say that my response is equal to and i'm going to say get and then get transactions url and then let's just print out and let's make a variable here data is equal to response.json let's just print out the data and we'll print it actually yeah we'll just print out the data and have a look at what this is okay so let's go full screen let's not create a new file let's run this code well we need to call this function first okay so let's do that first so let's just get rid of this call let's say get transactions like that let's pass our address and let's see what happens okay so we get a bunch of data here which is going to be all of the transactions right there's a huge massive transactions here so let's just go up to the very top and we can see we have status message and then we have result and result is a list and these lists contain all of the different transactions okay this is a huge amount but let's start by just looking at one individual transaction and see what that looks like so to do that i'm going to say the data is equal to response.json at result that's going to be a list right so i can just access the first element in the list to look at one individual transaction so let's look at data zero here let's run this again and let's see if this is a little bit easier to comprehend okay so here we can see that we have a block number a timestamp a hash we have a nonce transaction index we have a from we have a two we have a value we have a gas the gas price we have a bunch of stuff here so what i want to do is i want to loop through all of these transactions and just print out a few details i want to print out the two the from the amount the value which is the amount of ethereum that was actually sent the amount of gas used the timestamp i want to print all that stuff so let's do that so i'm going to go here and i'm going to say four tx standings for a transaction in data now recall data is a list so we can just loop through it and i'm going to say that my 2 is equal to tx 2 okay my from not form my from is equal to tx and then from the value and this is going to be from adr just so we don't mirror the from word in python okay i'm going to say value is equal to and then tx at value i'm going to say the gas is equal to tx at gas used okay and is anything else we want to print here to from we probably want the time stamp so i'll say time is equal to tx at timestamp and then we can print out this data so i'm going to say print and we will say 2 colon 2 print we're going to say from colon from from underscore adr okay we're going to print this is going to be value which is the amount of ethereum that was sent in this transaction say this is value if i could type properly and then we'll print gas used this will be gas used or just gas okay and then print time colon and that time okay i know i went pretty quickly there but this is pretty straightforward code so let's run this now and see what we get now so i'm just going to print one thing to make kind of a separator between these different addresses here so let's just print this so we can kind of see a separator between them all all right so let's run this and what do we get key error timestamp okay the issue here sorry is that timestamp has a capital for an s here so let's fix that and now let's run this and see what we get all right so we get a bunch of data here uh let's have a look at some of these transactions starting from the very top all right so i'm at the top here now this one's a bit weird we don't have a to address i'm not going to talk about exactly why that happens that's getting deeper into ethereum anyways that we have a two we have a from we have a value we have the gas used and we have the time which is actually a time stamp so what i want to do now is convert this time to an actual date because this isn't very meaningful right i mean maybe some of you can understand what this time stamp means i definitely cannot and so i want to see it in like you know date or day month year right that's ideally what i want to see and then maybe the hour and the minute it occurred as well we're going through we can see all of the transactions for most of these transactions they're only sending zero or one ethereum and actually the one here i believe is in way so not even an ether they sent one way now that's just this account i guess a bunch of the transactions they're doing they're not actually sending data with them and they're not sending any ethereum i don't know why but that's what they're doing all right so now what i'm going to do is convert this timestamp to a date and the way we're going to do that is by using the datetime library so i'm going to say from time date time import date time and then i'm going to say that my time here is equal to and then this is going to be date time dot i think this is from timestamp i just have to look at my code here okay from timestamp we're going to pass this in and i think this should be good okay actually we need to convert this to an in sorry because this is going to be a string and now this should actually give us a proper date as opposed to a integer okay so let's try this here and let's see what we get and there we go so we get 2022 0201 and then the time that this occurred at perfect we are now getting the correct time all right so i don't really like using this address for this example because a lot of the transactions don't actually send any ethereum so let me find a different one then i'll be right back all right so i just found another address here that's going to be better to use because the transactions make a bit more sense than the other account we were using so let's just run this code now with this new one and see what transactions we're getting okay there we go so see the value is much higher now they're actually sending ethereum with a lot of the transactions they're doing now this is in way so what i want to do is convert this value to ether so let's do that we already know how to do that i'm just going to take the value i'm going to convert this to an int because by default these are going to be strings and i'm going to define divide this by the ether value okay let's run the code now and let's see what we get and notice now that we actually get the amount of ethereum and they're sending a very large amount of ethereum with a lot of their transactions this account is one of the whale accounts so that makes sense perfect okay so now what i want to do is i just want to do a better calculation for the gas because if i go back to here you're going to see that the response for getting the normal transactions we have gas as well as the gas priced and the gas used now what we actually care about here is the gas used and we want to multiply this by the gas price to determine how much this actually cost okay so we need to take the gas used and multiply it by the gas price that just gives us how much it actually costs to well do this transaction so let's fix that now because right now when i say gas used that's not exactly what we want we want to have i guess gas cost okay so i'm going to take my gas i'm going to convert this to an int and i'm just going to multiply this by the int of and this is going to be tx at and this is going to be gas price like that okay and then we're going to divide all of this by the ether value and i don't need parentheses because order of operations is is the same here with multiplication and division okay so let's try this now and let's see what we get and we see uh oh geez yeah it's gas price not gas price okay let's fix that all right now we actually get a more useful gas price here and you can see that the price of gas is super expensive on ethereum okay so there we go now we know the gas cost as well as the value and so this actually tells us how much it costs someone to submit a transaction so now that we have all of this data what i want to do is start making a graph that will give us the balance of an ethereum account over time now to do that we need to start from the very first transaction and at the very first transaction our account balance is zero so before we have any transactions in the account we have a zero balance then we're going to look at every single transaction we're going to determine if we're sending money or receiving money and then we're either going to subtract money from our account or add money to our account now the thing is when we are sending money what we are actually doing is we're paying a gas price as well as the value of our transaction so we need to add those two values together and subtract that from the current balance in our account to get an accurate account balance however when we're receiving money we're not paying the gas whoever sent it was paying the gas so we only care about the value and we're going to add that to our account hopefully that makes sense but the way we're going to do this is we're going to say current underscore balance is equal to zero and then we're going to increment or decrement this balance in this for loop based on every transaction and since our transactions are ordered by ascending what that means is that we can just go through all of them in the order we get them because the first transaction we get is the very first transaction that ever occurred in this account hopefully that makes a bit of sense but that's how we're going to do this we're going to loop through all the transactions and kind of increment or decrement the current balance we're going to store all of these results in a list so we're going to say balances is equal to that we're also going to say times is equal to this so we keep track of what time each balance occurred at and then we'll be able to graph that data okay so we have two from value gas time i don't want to print out this data anymore what i want to do is determine if we were the one who were sending money or receiving money now if we were the ones who were sending money the from address is going to be our address if we're the ones receiving money then the two address is going to be our address right that's the way the transactions work so i'm going to say money underscore in is equal to and then this is going to be the from address actually we'll just do this we'll say the to is equal to and then address okay now address is the parameter right here that we passed now what we do need to do is just convert the address to lower cases as well as the two address because we might have them in upper cases and that might mess it up so we're just going to convert both of them to lower cases to make sure that we're getting an accurate comparison here okay so this tells us if we have money in so we're going to say if money in then what we want to do is we want to take the value and we want to add that to the current balance so we're going to say current balance plus equals value otherwise if we're not having money in if money is coming out then what we want to do is say the current balance minus equals and this is going to be the value plus the gas okay because again if we are sending a transaction we're paying the gas we need to take the gas used and the gas price multiply them together divide them by the ether and then that tells us how much it costs us for gas we add that to the value which is how much ethereum we sent that gives us an accurate current balance okay there we go so that's what we're doing for current balance and then we're going to say balances like this oops come on can can i type properly here balances dot append and we're going to append whatever the current balance is and we're also going to say times dot append and we're going to append whatever the current time is because we're going to use one of these lists for the x-axis the other one for the y-axis i actually did them in the opposite order this is going to be x this is going to be y okay now we also have to remember here that we're only looking at the external or normal transactions we also want to look at the internal transactions we're going to add that to the equation here in one second so let's just run the code for right now but let's print out what the current balance is at the end just to see how far off we are when we're only looking at the normal transactions okay so let's run all this here and see what we get and we get a balance of 200 000 or sorry 2 million and then a 3504 ethereum which i think was almost exactly what the actual balance was uh when we got the account bounce now the issue is this might work for this specific account because it might not have any internal transactions but there will be a lot of accounts that have a ton of smart contract calls and we need to account for the value being sent there and all of those types of transactions so we need to add that in here and the way we're going to do that it's just going to go back to etherscan here is we're now going to get the list of internal transactions and we're going to combine that with the list of normal transactions and then loop through all of them in a similar way so this is what we have here we have tx list internal so the only change here is that we just have to pass tx list internal as opposed to account for the url that we want and we'll get a very similar response okay so let's check this out we're going to go now after we get this we're going to say get in terminal underscore tx underscore url and then this is going to be make api call and let's actually copy all of this okay so let's just paste this here and then rather than getting the tx list we want tx internal list i think that's what it was let's just check this here tx list internal okay so i did it wrong let's fix that okay tx list internal so again it's just the exact same thing as what we had up here except now we're doing txlist internal to get all of the internal transactions but we still need to send a request so we're going to say response to is equal to get and then this is going to be get internal tx url or sorry get and then pass here get internal tx url and in fact i shouldn't really be calling these get i'm just going to put transaction url and internal tx url just because that makes more sense and get balance url yeah i'll just make that balance url okay anyways now that we have that i'm going to say data2 is equal to response to dot json and then we're going to access the result as well so now we're going to get one list of results and another list of results they're both going to be ordered however what i want to do is combine them so i'm going to say data dot extend data2 this is going to take all of the elements and data too and simply add them to the current data list however now we're not going to have these sorted because we're going to have all of the normal transactions then all of the internal transactions so i want to sort all of them so that we have a correct list here so i'm going to say dot sort actually let's do this after let's go data dot sort and then we want to sort all of these by the key which is equal to lambda i'll explain this in a second so lambda x colon x at and then timestamp and then make sure we have a capital s here for timestamp okay so what we're doing is we're getting all of the regular transactions all the internal transactions right here we're then going to extend our data list by data2 so our normal by our internal now it's no longer going to be sorted because we have all of the new transactions we got at the end of this list so we need to resort this list we're going to say data.sort the key we're going to sort by is going to be every element's timestamp so that's what this is doing we're saying okay for every element which is going to be represented by x we're going to grab the timestamp and we're going to sort by that timestamp and we can convert this to an int just to make sure we're going to get a correct sorting here because by default this is a string although i think it will work even if we didn't convert it to an it okay so now we should get every single transaction and loop through all of them let's run this though and see if this is working all right so it looks like it is working we got two hundred thousand uh i got two million and then again three hundred three thousand five hundred four apparently i cannot read numbers however i will tell you that this will not work the way it is if we have any internal transactions now this account doesn't have any internal transactions i should probably find one that does however if we go and we look at the response here we can see that we only have gas and gas used we don't have a gas price so i just need to fix something in my code to account for the fact that my internal transactions don't have a gas price on them and we're just going to have to use the gas used as how much gas was used okay because the gas won't really have a price with the internal transactions i understand it's a little bit weird but we just have to account for the fact that we don't have that key in these responses so what i'm going to do here is say if and i'm just going to say gas price in and then tx then we'll calculate the gas using this but otherwise we'll just calculate the gas by being the gas used and then divided by the ether value okay divided by ether value so this will work the exact same way as before now again we're just going to account for the fact that the internal transactions don't have this gas price uh key okay so we're almost done we now have all of the data that we need we're getting an accurate current balance what we want to do now is actually graph this using matplotlib now this is super easy to do all we're going to do is say plt dot plot and we're going to plot as the x-axis are times and the y-axis are balances now i might be passing this in the wrong order we'll see in one second but i'm going to pass times and balances now since these are the same length this will work because we're going to match every balance with each time and then if i want to show this i just have to say plt.show okay so let's do plt.show let's run this and let's see what we get now all right there we go we get a nice graph the same one that i showed you in the demo at the very beginning and it looks like all this is working so our y-axis is going to be the ethereum amount in our account and then the y-axis is going to be the amount of time or the date right so if i go here you can see that we're at the correct balance uh just about 2 million ethereum in the account get an absurd amount of ethereum okay there you go so that is really all i need to show you for this video now hopefully this showed you how to work with the ether scan api and how you can easily ask for more information again we were calling different api endpoints very easily because of this nice function we made make api url you can reuse this function as many ways as you would like call all of the different types of endpoints get that data look at the data so on and so forth i showed you how to get the account balance as well as how to look at all of the transactions and then how to get the balance over time from those different transactions now again a bunch of other stuff you can do i hope you guys mess around with the api and make some cool stuff i will mention that i'm sure a lot of you want to actually get the ethereum value like the dollar amount in your account not the amount of ethereum now to do that for every single time that you have you just have to look up what the ethereum price is i believe you can do that from the ether scan api and then you would replace your balances with the actual balance multiplied by whatever the price of ethereum is and then that would give you the dollar amount of your account on every single day that a new transaction occurred now you also could do this over every single day you guys can kind of figure it out from here because i've given given you the building blocks to build a type of project like that anyways with that said i am going to wrap up the video here i hope you guys found this helpful if you did make sure to leave a like subscribe to the channel and i will see you in another one [Music]

Original Description

Have you ever wanted to track your Ethereum transactions? Well you've come to the right video! In this video, I am going to show you how to get the balance of any Ethereum account, as well as how to track that balance over time and then make a graph of that using the Etherscan API! I will mention this is not meant for beginner programmers, you're welcome to follow along, but I'm going to assume you are familiar with Python because I'm not going to explain all of the basic syntax. I hope you enjoy and find this video helpful! 💻Join BackTrace for their virtual game development summit on February 16th from 10am - 5pm EST! https://hubs.la/Q011RLGz0 Breakpoint is an open virtual event, free of charge, that provides an opportunity for practitioners in the game development industry to learn from each other, network, share best practices and ideas through technical talks. 💻 ProgrammingExpert is the best platform to learn how to code and become a software engineer as fast as possible! Check it out here: https://programmingexpert.io/tim and use code "tim" for a discount! 📄 Resources 📄 Etherscan Website: https://etherscan.io/myapikey Etherscan API Docs: https://docs.etherscan.io/api-endpoints/accounts Code In This Video: https://github.com/techwithtim/Ethereum-Wallet-Tracker Fix Pip (Windows): https://www.youtube.com/watch?v=AdUZArA-kZw&t=204s Fix Pip (Mac): https://www .youtube.com/watch?v=E-WhAS6qzsU ⭐️ Timestamps ⭐️ 00:00 | Ethereum Wallet Tracker 01:36 | BackTrace Event 02:30 | Setup & Installation 05:30 | Understanding Etherscan API 07:52 | Ethereum Wallet Balance 19:00 | Ethereum Wallet Transactions 31:08 | Graphing Wallet Data ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️ 👕 Merchandise: https://teespring.com/stores/tech-with-tim-merch-shop 📸 Instagram: https://www.instagram.com/tech_with_tim 📱 Twitter: https://twitter.com/TechWithTimm ⭐ Discord: https://discord.gg/twt 📝 LinkedIn: https://www.linkedin.com/in/tim-ruscica-82631b179/ 🌎 Website: https://techwithtim.net ���
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tech With Tim · Tech With Tim · 0 of 60

← Previous Next →
1 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This video teaches how to track Ethereum transactions and balance using the Etherscan API and Python, and provides a step-by-step guide on how to integrate the API with Python and graph historical values. It covers topics such as API integration, data parsing, and graphing, and provides a comprehensive overview of how to use the Etherscan API to retrieve account transactions and balance.

Key Takeaways
  1. Create an account on Etherscan and generate an API key
  2. Use the API key to retrieve account transactions and balance
  3. Graph the transactions using matplotlib
  4. Integrate the Etherscan API with Python
  5. Parse JSON response from the API
  6. Convert result from wei to ether
  7. Calculate gas cost and update balance
💡 The Etherscan API provides a comprehensive set of endpoints for retrieving Ethereum transactions and balance, and can be easily integrated with Python using the requests library.

Related Reads

Chapters (7)

| Ethereum Wallet Tracker
1:36 | BackTrace Event
2:30 | Setup & Installation
5:30 | Understanding Etherscan API
7:52 | Ethereum Wallet Balance
19:00 | Ethereum Wallet Transactions
31:08 | Graphing Wallet Data
Up next
Vibe coding was big last year, but now the conversation is shifting.
Zubair Trabzada | AI Workshop
Watch →