I’m not sure if this is the right place to ask, but if not I would appreciate being pointed to a better place. So I’m trying to get historical supply data for ERC20 tokens like WBTC by querying the blockchain directly. Looking at the contract, every mint and burn emits a Transfer event either from or to the 0 address. So, I’ve queried for all of these events and I believe I have them all. I’ve summed them up, subtracting transfer events from burns. But, the total supply that I get by summing the events is incorrect by about 90 WBTC. I’m doing the same analysis for other tokens, and in most cases the sum I’m getting is slightly off or really off. In one case so far, HBTC, the sum of the events is perfect with the current total supply. I’m using the exact same querying and summing algorithm for all of the tokens I’m working with. Does anyone have insight on what I might be doing wrong? What is the best way to get historical supply data (not from a GUI or third-party service, but directly from the blockchain)?
Welcome to the community .
Assuming a token emits a Transfer event for mint, transfer and burn and appropriately updates the total supply (if minting and burning) then I would have thought that the sum of the transfer events should match the total supply.
You could use tools such as Etherscan to help track down where the issue might be in either the specific token or your process (I appreciate that you want to be able to calculate directly yourself): https://etherscan.io/tokencheck-tool
I’d be interested to see if anyone in the community can advise on this.
This is a really interesting project. Unfortunately I don’t know exactly what could be going wrong here.
Even if you’re using the same process for all those tokens, the implementation for each contract is likely slightly different. You might want to look at the source code to see if you spot something that could be throwing things off. I took a look at WBTC and didn’t see anything though.
Where are you getting the events from? That’s another potential source of error, and maybe there’s some events you’re missing.
Oh okay I think I have an idea. For some contracts it may be possible for users to manually trigger a transfer to
0x0 event without reducing the total supply, and in those scenarios looking at the
Transfer event wouldn’t be enough. This isn’t the case for our implementation though, which WBTC seems to use…
Thanks for all of the insights everyone. The biggest problem was something in my code. I believe I have pulled all of the transfer events from and to the 0 address correctly, but my code had an assumption after receiving the events that there would only be one event per block or something like that. So, I fixed that and things are looking quit a bit better. But, some of the tokens I’m looking at still aren’t perfect. WBTC works perfectly by summing up the transfer events, but for example imBTC is off by 4 BTC. I think the transfers to the 0 address which don’t reduce the supply might be the culprit. I will probably look into using the Mint/Minted and Burn/Burned events to see if those are more accurate. I’ll update here, thanks again.
Glad to hear you are getting closer. Please ask all the questions that you need.
If your project is open source, it would be great to share a link to the repo along with any write up with the community when you are ready.