Skip to content

Commit

Permalink
support for fluxmon contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
ijonas committed Jul 16, 2023
1 parent 4dbdbd9 commit 00139a3
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 59 deletions.
11 changes: 11 additions & 0 deletions cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,14 @@ def load_config():

with open(cfg_path, "r") as f:
return load(f, Loader=Loader)


def network(networks, network_code):
for netw in networks:
if netw["code"] == network_code:
return netw
return None


flux_monitor_abi = [{"inputs": [], "name":"allocatedFunds", "outputs":[{"internalType": "uint128", "name": "", "type": "uint128"}], "stateMutability": "view", "type": "function"}, {"inputs": [], "name":"availableFunds", "outputs":[{"internalType": "uint128", "name": "", "type": "uint128"}], "stateMutability": "view", "type": "function"}, {"inputs": [], "name":"decimals", "outputs":[{"internalType": "uint8", "name": "", "type": "uint8"}], "stateMutability": "view", "type": "function"}, {"inputs": [
], "name":"latestAnswer", "outputs":[{"internalType": "int256", "name": "", "type": "int256"}], "stateMutability": "view", "type": "function"}, {"inputs": [], "name":"latestRound", "outputs":[{"internalType": "uint256", "name": "", "type": "uint256"}], "stateMutability": "view", "type": "function"}, {"inputs": [{"internalType": "address", "name": "_oracle", "type": "address"}], "name": "withdrawablePayment", "outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}], "stateMutability": "view", "type": "function"}]
60 changes: 45 additions & 15 deletions contract_metrics.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,51 @@
from web3 import Web3
from cfg import network, flux_monitor_abi

from metrics import metrics

def analyse_contracts(cfg, contracts):

def analyse_fluxmon_contracts(cfg, contracts):
print(f"Analysing {len(contracts)} contracts...")
for contract in contracts:
rpc_for_contract = cfg[contract["network"]]
w3 = Web3(Web3.HTTPProvider(rpc_for_contract))
print(
f"Analysing contract {contract['code']} on {contract['network']}")
chain = network(cfg['general']['networks'], contract['network'])
if network is not None:
w3 = Web3(Web3.HTTPProvider(chain["rpc"]))

if w3.is_connected():
contract_instance = w3.eth.contract(
contract["address"], abi=flux_monitor_abi)
decimals = contract_instance.functions.decimals().call()
allocated_funds = contract_instance.functions.allocatedFunds().call() / 10 ** 18
available_funds = contract_instance.functions.availableFunds().call() / 10 ** 18
latest_answer = contract_instance.functions.latestAnswer().call() / 10 ** 18
latest_round = contract_instance.functions.latestRound().call()
withdrawable_payment = (
contract_instance.functions.withdrawablePayment(
contract['oracle_address']).call()
) / 10 ** 18

metrics["fluxmon_decimals"].labels(
contract["project"], contract["code"]
).set(decimals)
metrics["fluxmon_allocated_funds"].labels(
contract["project"], contract["code"]
).set(allocated_funds)
metrics["fluxmon_available_funds"].labels(
contract["project"], contract["code"]
).set(available_funds)
metrics["fluxmon_latest_answer"].labels(
contract["project"], contract["code"]
).set(latest_answer)
metrics["fluxmon_latest_round"].labels(
contract["project"], contract["code"]
).set(latest_round)
metrics["fluxmon_withdrawable_payment"].labels(
contract["project"], contract["code"]
).set(withdrawable_payment)

if w3.is_connected():
print(f"Connected to {contract['network']}")
contract_instance = w3.eth.contract(contract["address"], abi=cfg["abi"])
allocated_funds = contract_instance.functions.allocatedFunds().call()
available_funds = contract_instance.functions.availableFunds().call()
decimals = contract_instance.functions.decimals().call()
latest_answer = contract_instance.functions.latest_answer().call()
latest_round = contract_instance.functions.latest_round().call()
withdrawable_payment = (
contract_instance.functions.withdrawablePayment().call()
)
else:
print(f"Failed to connect to {contract['network']}")
else:
print(f"Failed to connect to {contract['network']}")
print(f"Network {contract['network']} not found in config")
43 changes: 3 additions & 40 deletions data_feed_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,7 @@

import pyjq
import requests
from prometheus_client import Counter, Gauge, Info

metrics = {
"data_feed": Gauge("data_feed", "Numeric data feed", ["project", "code"]),
"data_feed_response_time_ms": Gauge(
"data_feed_response_time",
"Response time of data feed in milliseconds",
["project", "code"],
),
"data_feed_last_error": Info(
"data_feed_last_error", "Most recent error detected", ["project", "code"]
),
"data_feed_error_count": Counter(
"data_feed_error_count",
"Number of errors whilst retrieving data feed",
["project", "code"],
),
"data_feed_update_count": Counter(
"data_feed_update_count",
"Number of times data feed retrieved",
["project", "code"],
),
}


def init_metrics(data_feed_sources):
for data_feed_source in data_feed_sources["data_source_metrics"]:
metrics["data_feed"].labels(
data_feed_source["project"], data_feed_source["code"]
).set(0)
metrics["data_feed_response_time_ms"].labels(
data_feed_source["project"], data_feed_source["code"]
).set(0)
metrics["data_feed_last_error"].labels(
data_feed_source["project"], data_feed_source["code"]
).info({"status": "", "timestamp": "", "error": ""})
print(
f"Initialised metrics for {data_feed_source['project']}-{data_feed_source['code']}",
)
from metrics import metrics


def analyse_data_feeds(data_feed_sources):
Expand All @@ -66,7 +28,8 @@ def analyse_data_feeds(data_feed_sources):
).inc()
else:
data = response.json()
duration = (current_datetime - request_datetime).total_seconds() * 1000
duration = (current_datetime -
request_datetime).total_seconds() * 1000
metrics["data_feed_response_time_ms"].labels(
data_feed_source["project"], data_feed_source["code"]
).set(duration)
Expand Down
11 changes: 7 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
from prometheus_client import start_http_server

from cfg import load_config
from data_feed_metrics import analyse_data_feeds, init_metrics
from contract_metrics import analyse_fluxmon_contracts
from metrics import init_metrics
from data_feed_metrics import analyse_data_feeds

if __name__ == "__main__":
start_http_server(8000)
data_feed_sources = load_config()
init_metrics(data_feed_sources)
config = load_config()
init_metrics(config)
while True:
analyse_data_feeds(data_feed_sources)
analyse_data_feeds(config)
analyse_fluxmon_contracts(config, config['fluxmon_contracts'])
time.sleep(60)
73 changes: 73 additions & 0 deletions metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from prometheus_client import Counter, Gauge, Info

metrics = {
"data_feed": Gauge("data_feed", "Numeric data feed", ["project", "code"]),
"data_feed_response_time_ms": Gauge(
"data_feed_response_time",
"Response time of data feed in milliseconds",
["project", "code"],
),
"data_feed_last_error": Info(
"data_feed_last_error", "Most recent error detected", [
"project", "code"]
),
"data_feed_error_count": Counter(
"data_feed_error_count",
"Number of errors whilst retrieving data feed",
["project", "code"],
),
"data_feed_update_count": Counter(
"data_feed_update_count",
"Number of times data feed retrieved",
["project", "code"],
),

"fluxmon_decimals": Gauge("fluxmon_decimals", "Decimals of reported value", ["project", "code"]),
"fluxmon_allocated_funds": Gauge(
"fluxmon_allocated_funds",
"Allocated funds",
["project", "code"],
),
"fluxmon_available_funds": Gauge(
"fluxmon_available_funds",
"Available funds",
["project", "code"],
),
"fluxmon_latest_answer": Gauge(
"fluxmon_latest_answer",
"Lastest answer",
["project", "code"],
),
"fluxmon_latest_round": Gauge(
"fluxmon_latest_round",
"Lastest round",
["project", "code"],
),
"fluxmon_withdrawable_payment": Gauge(
"fluxmon_withdrawable_payment",
"Withdrawable payment",
["project", "code"],
),

}


def init_metrics(cfg):
for data_feed_source in cfg["data_source_metrics"]:
metrics["data_feed"].labels(
data_feed_source["project"], data_feed_source["code"]
).set(0)
metrics["data_feed_response_time_ms"].labels(
data_feed_source["project"], data_feed_source["code"]
).set(0)
metrics["data_feed_last_error"].labels(
data_feed_source["project"], data_feed_source["code"]
).info({"status": "", "timestamp": "", "error": ""})
for fluxmon_contract in cfg["fluxmon_contracts"]:
for metric in ["fluxmon_decimals", "fluxmon_allocated_funds", "fluxmon_available_funds", "fluxmon_latest_answer", "fluxmon_latest_round", "fluxmon_withdrawable_payment"]:
metrics[metric].labels(
fluxmon_contract["project"], fluxmon_contract["code"]
).set(0)
print(
f"Initialised metrics for {data_feed_source['project']}-{data_feed_source['code']}",
)

0 comments on commit 00139a3

Please sign in to comment.