HEX and Web3

Intro

HEX crypto has it all when it comes to game theory, trustless design, performance and community, but few understand how to talk to the blockchain and programatically talk to the Ethereum (and PulseChain) contract to pull down stats and data key for understanding various technicals around the system.

We’ll be using the Web3 Python library to interact with the blockchain and poke around with HEX to understand more about what’s happening in the space. Combining this code with Anvil’s web services, we can create a basic HEX stats website to show you what’s possible.

Getting Started

Infura provides public Ethereum nodes for development that we can get access to via the generated API key so we can talk to the blockchain. We need an Infura rpc node to use + API Key.

Go to infura.io and register an account. Once complete, go to the Infura app dashboard and create a new key. You should see Ethereum L1 Blockchain listed (not Celo) with a mainnet URL and API key you can copy. Save these both for later as we’ll use them in the testing code.

Now we can move on to coding and testing the app locally. If you have Visual Studio Code, Python and Web3 module installed, we can build and test a local app that we can convert to code for running on the Anvil platform afterwards.

HEX

But first, you’ll need to do a few things to get all the information and setup to make an app.

Since we’re interacting with the HEX smart contract, there’s a few things we need to find and define in the code.

1) HEX contract address

https://etherscan.io/token/0x2b591e99afe9f32eaa6214f7b7629768c40eeb39

2) HEX contract ABI (Application Binary Interface)

This is basically a list of the contract’s functions and parameters in JSON, so we know how to talk to it. It can also be found on Etherscan in the code area and can conveniently be copied and pasted into the app with MINOR yet IMPORTANT changes.

This is Python, so we need to find all the false and turn them into False. Same with true, find + replace all true to True. Now we’re good to go.

If you need future reference, learn more here.

You can also throw the ABI into a JSON Beautifier so it’s easier to read and find stuff.

We see here that we can pull out interesting data such as the t-share rate and stake penalties, etc.

Once we have these things complete and researched, we can build the app.

Test Code

You can check out the full code on github here, but let’s take a look at the output.

$ ./gethexstats.py
+ successfully connected to the blockchain
+ ready to talk to the contract

name:             HEX
decimals:         8
totalSupply:      582456927210
shareRate:        25211
stakeSharesTotal: 10293836

We connected to the Ethereum network via Infura and loaded the HEX’s ABI in order to interact with the contract. Then we called various functions and parsed global info to pull out the name and decimals as well as current total supply, share rate and total t-shares.

Now let’s move on to Anvil, minimize our code a bit and use the web service to do even more cool stuff.

Anvil

We’re going to make this console app work in Anvil and utilize it’s fun web features.

In case you haven’t heard of it, Anvil.works is a platform for building and deploying webapps in Python. You can create a website that does complex backend calls with it, which makes it a perfect candidate for blockchain related projects that you want to make public to share data online.

It’s free to try and cheap enough to pay for if you want to keep using it and its advanced features, such as using the Web3 module. Just go to the website and create an account. Then, go to the Build page and create a new blank app. Choose any theme you like.

Once you have the editor up with the new app, go to SERVER CODE and choose ServerModule1. That’s where we’ll code within the Anvil template provided and display our data on the web. We’ll also add a little code to CLIENT CODE in Form1 to call our functions defined on the server side.

You can grab both the Server Module and Client Code Form1 code @ Github here!

In the top left corner, click the >_Output button so that we can view output. Also click the drop down on the top right side where Basic Python 3 is the default and select Full Python 3. This ensures we can use the Web3 module for our app. If it gives you an error, its probably because you need to upgrade and pay for an Anvil package to do this.

We can now deploy it with the Run (play) button at the top center of the page and check the Output.

We can queried Dexscreener’s API for current price info via Anvil, as we’d get a 403 unauthorized on console as we’re not whitelisted, but apparently the Anvil web service is allowed.

But the fun part is that Anvil allows us to display the data on the web. We can add a textbox to the Client Code Form and then set it to display the data returned from our ServerModule getHexStats() function. This is of course a very basic way to display data, as you can use tables and other nice formatting to make it into a nice, real website. This is just showing you how powerful this way of interacting and pulling data down from the blockchain and providing it via web services can really be.

Once you’re ready, you can publish the app by clicking the button in the top right corner. You can even set a custom URL, eg. https://gethexstats.anvil.app and send people over to use your app.

Conclusion

So there you have it. We used Python Web3 library to write a console app that interacts with the HEX contract, adapted the code for Anvil and published a basic HEX Stats website.

Special thanks to Dip Catcher from the MaximusDAO project (maker of HEX derivatives)and CryptoSloth for help getting started with Anvil and generally being awesome people.

All of this code is freely available to try, test, modify on the github repo.

Previous
Previous

Alex Brogan Interview

Next
Next

The Big Pumpamental