Hello everyone.
Forgive my ignorance, but I am relatively new to this area. I would like to know if I can track Harmony pools using web3.py. I have read that Harmony is compatible with Ethereum, but I don’t know if this is possible.
If not, what is the best way to download data from Harmony chain using Python code?
Thanks in advance.
1 Like
Maffaz
November 18, 2021, 4:45pm
2
You can use the Harmony Python SDK
(Install from GIT as the pip package is missing dependencies)
Alternatively you can connect to the RPC’s Harmony Node API with your own code and it is compatible with ETH RPC calls.
json-rpc | Ethereum Wiki
Because I don’t need the full package, I made this function to call the RPC that we use to gather data from the chain.
ts = int(harmony_data["timestamp"], 16)
date_ts = datetime.fromtimestamp(ts)
return date_ts, date_ts.strftime("%d-%m-%y")
def months_between_dates(date_from: datetime) -> int:
today = datetime.today()
return (today.year - date_from.year) * 12 + today.month - date_from.month
def rpc_v2(result: list, method: str, params: list) -> dict:
d = {
"jsonrpc": "2.0",
"method": method,
"params": params,
"id": 1,
}
try:
r = post(harmony_api, json=d)
data = r.json()["result"]
except KeyError:
It also has connections to Smartstake and Prometheus as well…
Feel free to contact me if you need any direction…
Severin
November 19, 2021, 2:40am
3
Hey I created this exact use case as example for harmony:. It’s in JavaScript, but it should work quite similar in python:
const Web3 = require('web3')
const moment = require('moment-timezone')
const IUniswapV2PairAbi = require("./IUniswapV2Pair.json").abi
const IUniswapV2Factory = require("./IUniswapV2Factory.json").abi
const UniswapV2Router02 = require("./IUniswapV2Router02.json").abi
// WEB3 CONFIG
const web3 = new Web3("https://api.s0.t.hmny.io")
/**
* Setup factory contracts
*/
const VIPER_FACTORY_ADDRESS = "0x7D02c116b98d0965ba7B642ace0183ad8b8D2196"
const viperFactoryContract = new web3.eth.Contract(IUniswapV2Factory, VIPER_FACTORY_ADDRESS)
const LOOT_FACTORY_ADDRESS = "0x021AeF70c404aa9d70b71C615F17aB3a4038851A"
const lootFactoryContract = new web3.eth.Contract(IUniswapV2Factory, LOOT_FACTORY_ADDRESS)
/**
This file has been truncated. show original
Note: The abi is like a description of the structure of a contract, it differs depending on what contract is used.
1 Like
Maffaz
November 19, 2021, 8:45am
5
You could use the web3 library for Python and pretty much copy paste this code… It is basically the same…
1 Like