Published on

[NeoX Tutorial] Interact with the chain in Python

Authors

Description

On this article we will see how to interact with NeoX blockchain in Python to retrieve on-chain data.

As an example we will make a simple script allowing us to get access to reserves of a pool on ForTheWin exchange.

This can be useful if you want for example to compute the price of your token to create a price bot.

Getting NeoX documentation

First we will need to access the documentation to get the information about NeoX testnet or mainnet chain.

What is interesting for us at this point is to get the RPC endpoint URL of the chain we want to use.

Getting contract ABI

We then need the ABI of the contract we want to interact with. For this we will pick the Block Explorer URL of the chain we are working on on the same documentation page.

For every verified contract we just need to look for the contract hash or name on the explorer and the contract page the ABI is available under the Contract ABI section.

Here is an exemple link for the Carrot Swap Router contract https://xexplorer.neo.org/address/0x82b56Dd9c7FD5A977255BA51B96c3D97fa1Af9A9?tab=contract

For the example we want to realize It's a bit more complicated as ForTheWin Swap contract is not verified. In this case you can always ask the developer to provide the ABI. I asked to ED and he sent the ABI to me which is available here.

Getting contract hash

Last information we need to find are the hashes of the contracts. For our example we need the FTW Swap contract hash and hashes for the two tokens of the pair we want to retrieve reserves for.

Python code

Now we have all the information we need and we can switch to the coding step. Let's start by installing the web3 Python module we will use for interacting with NeoX using the command pip install web3 and then make the script.

The code will contains the following parts. The import of the web Python module:

from web3 import Web3

Then the definition of the variable we retrieved previously and we will use for our call:

FTW_SWAP_HASH = "0xE1e1cC68841D1fF13bdd8C8fB36E6f91995788f4"
FTW_SWAP_ABI = open("ftw_abi.json").read() #The ABI file
NDMEME_HASH = "0xE816deE05cf6D0F2a57EB4C489241D8326B5d106"
WGAS_HASH = "0xdE41591ED1f8ED1484aC2CD8ca0876428de60EfF"
RPC_ENDPOINT = "https://mainnet-1.rpc.banelabs.org/"

Now we can instantiate a Web3 object that will help connecting to NeoX chain:

w3 = Web3(Web3.HTTPProvider(RPC_ENDPOINT))

Once we have this object we will create the FTW Swap contract object:

contract = w3.eth.contract(FTW_SWAP_HASH, abi=FTW_SWAP_ABI)

Having this contract object we are now able to simply call all methods like this:

reserves = contract.functions.getReserves(NDMEME_HASH, WGAS_HASH).call()

Putting everything together we have the following final script.

from web3 import Web3

FTW_SWAP_HASH = "0xE1e1cC68841D1fF13bdd8C8fB36E6f91995788f4"
FTW_SWAP_ABI = open("ftw_abi.json").read() #The ABI file
NDMEME_HASH = "0xE816deE05cf6D0F2a57EB4C489241D8326B5d106"
WGAS_HASH = "0xdE41591ED1f8ED1484aC2CD8ca0876428de60EfF"

w3 = Web3(Web3.HTTPProvider(RPC_ENDPOINT))

contract = w3.eth.contract(FTW_SWAP_HASH, abi=FTW_SWAP_ABI)
reserves = contract.functions.getReserves(NDMEME_HASH, WGAS_HASH).call()

That's as simple as this to get the reserves of this NDMEME-WGAS pool.

Let's go further

Now that we have those information we can use Flamingo Finance API to get the GAS price and we will then be able to compute NDMEME price. For doing this you will just need to add those steps.

For this we will use Python requests module to make calls to this Flamingo API so we will add the following import:

import requests

Now for getting the GAS price we will add a variable with Flamingo API endpoint for getting last price:

FLAMINGO_PRICE_API = "https://neo-api.b-cdn.net/flamingo/live-data/prices/latest"

This endpoint is returning price for all tokens on Flamingo platform so we will filter to get only the GAS one:

res = requests.get(FLAMINGO_PRICE_API)
gas_price = [x['usd_price'] for x in res.json() if x['symbol'] == 'GAS'][0]

Adding everything to what we previously did end up with the following script:

import requests

from web3 import Web3

FTW_SWAP_HASH = "0xE1e1cC68841D1fF13bdd8C8fB36E6f91995788f4"
FTW_SWAP_ABI = open("ftw_abi.json").read()
NDMEME_HASH = "0xE816deE05cf6D0F2a57EB4C489241D8326B5d106"
WGAS_HASH = "0xdE41591ED1f8ED1484aC2CD8ca0876428de60EfF"
FLAMINGO_PRICE_API = "https://neo-api.b-cdn.net/flamingo/live-data/prices/latest"

w3 = Web3(Web3.HTTPProvider('https://mainnet-1.rpc.banelabs.org/'))

contract = w3.eth.contract(FTW_SWAP_HASH, abi=FTW_SWAP_ABI)
reserves = contract.functions.getReserves(NDMEME_HASH, WGAS_HASH).call()
ndmeme_reserve = reserves[2] / 10 ** 18
wgas_reserve = reserves[4] / 10 ** 18

res = requests.get(FLAMINGO_PRICE_API)
gas_price = [x['usd_price'] for x in res.json() if x['symbol'] == 'GAS'][0]

ndmeme_price = gas_price * wgas_reserve / ndmeme_reserve

Conclusion

You now have an example of Python script to interact with NeoX blockchain. Feel free to modify It to your own need and to reach me on Discord if you need more help.