Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a scheduler to measure the BOA balance of administrator accounts #398

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/relay/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,15 @@ scheduler:
- name: purchase
enable: true
expression: "*/1 * * * * *"
- name: delegatorApproval
- name: delegator_approval
enable: true
expression: "*/5 * * * * *"
- name: metrics
enable: true
expression: "*/5 * * * * *"
- name: metrics_certifier
enable: true
expression: "*/5 * * * * *"

relay:
certifiers:
Expand Down
7 changes: 6 additions & 1 deletion packages/relay/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { DefaultServer } from "./DefaultServer";
import { ApprovalScheduler } from "./scheduler/ApprovalScheduler";
import { CloseScheduler } from "./scheduler/CloseScheduler";
import { DelegatorApprovalScheduler } from "./scheduler/DelegatorApprovalScheduler";
import { MetricsCertifierScheduler } from "./scheduler/MetricsCertifierScheduler";
import { MetricsScheduler } from "./scheduler/MetricsScheduler";
import { Scheduler } from "./scheduler/Scheduler";
import { StorePurchaseScheduler } from "./scheduler/StorePurchaseScheduler";
Expand Down Expand Up @@ -49,14 +50,18 @@ async function main() {
if (scheduler && scheduler.enable) {
schedulers.push(new StorePurchaseScheduler(scheduler.expression));
}
scheduler = config.scheduler.getScheduler("delegatorApproval");
scheduler = config.scheduler.getScheduler("delegator_approval");
if (scheduler && scheduler.enable) {
schedulers.push(new DelegatorApprovalScheduler(scheduler.expression));
}
scheduler = config.scheduler.getScheduler("metrics");
if (scheduler && scheduler.enable) {
schedulers.push(new MetricsScheduler(scheduler.expression));
}
scheduler = config.scheduler.getScheduler("metrics_certifier");
if (scheduler && scheduler.enable) {
schedulers.push(new MetricsCertifierScheduler(scheduler.expression));
}
}

const contractManager = new ContractManager(config);
Expand Down
14 changes: 6 additions & 8 deletions packages/relay/src/routers/DefaultRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { WebService } from "../service/WebService";

import { Tspec, TspecDocsMiddleware } from "tspec";

import { BigNumber, Wallet } from "ethers";
import express from "express";

export class DefaultRouter {
Expand Down Expand Up @@ -57,13 +56,12 @@ export class DefaultRouter {
* @private
*/
private async getMetrics(req: express.Request, res: express.Response) {
res.set("Content-Type", this.metrics.contentType());
this.metrics.add("status", 1);
for (const elem of this.config.relay.certifiers) {
const wallet = new Wallet(elem, this.contractManager.sideChainProvider);
const balance = (await wallet.getBalance()).div(BigNumber.from(1_000_000_000)).toNumber();
this.metrics.gaugeLabels("certifier_balance", { address: wallet.address }, balance);
try {
res.set("Content-Type", this.metrics.contentType());
this.metrics.add("status", 1);
res.end(await this.metrics.metrics());
} catch (error: any) {
return res.status(500);
}
res.end(await this.metrics.metrics());
}
}
91 changes: 91 additions & 0 deletions packages/relay/src/scheduler/MetricsCertifierScheduler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import "@nomiclabs/hardhat-ethers";
import { BigNumber, Wallet } from "ethers";
import { Config } from "../common/Config";
import { logger } from "../common/Logger";
import { ContractManager } from "../contract/ContractManager";
import { Metrics } from "../metrics/Metrics";
import { GraphStorage } from "../storage/GraphStorage";
import { RelayStorage } from "../storage/RelayStorage";
import { Scheduler } from "./Scheduler";

/**
* Creates blocks at regular intervals and stores them in IPFS and databases.
*/
export class MetricsCertifierScheduler extends Scheduler {
private _config: Config | undefined;
private _contractManager: ContractManager | undefined;
private _metrics: Metrics | undefined;
private _storage: RelayStorage | undefined;
private _graph: GraphStorage | undefined;

constructor(expression: string) {
super(expression);
}

private get config(): Config {
if (this._config !== undefined) return this._config;
else {
logger.error("Config is not ready yet.");
process.exit(1);
}
}

private get metrics(): Metrics {
if (this._metrics !== undefined) return this._metrics;
else {
logger.error("Metrics is not ready yet.");
process.exit(1);
}
}

private get storage(): RelayStorage {
if (this._storage !== undefined) return this._storage;
else {
logger.error("Storage is not ready yet.");
process.exit(1);
}
}

private get contractManager(): ContractManager {
if (this._contractManager !== undefined) return this._contractManager;
else {
logger.error("ContractManager is not ready yet.");
process.exit(1);
}
}

private get graph(): GraphStorage {
if (this._graph !== undefined) return this._graph;
else {
logger.error("GraphStorage is not ready yet.");
process.exit(1);
}
}

public setOption(options: any) {
if (options) {
if (options.config && options.config instanceof Config) this._config = options.config;
if (options.contractManager && options.contractManager instanceof ContractManager)
this._contractManager = options.contractManager;
if (options.storage && options.storage instanceof RelayStorage) this._storage = options.storage;
if (options.graph && options.graph instanceof GraphStorage) this._graph = options.graph;
if (options.metrics && options.metrics instanceof Metrics) this._metrics = options.metrics;
}
}

public async onStart() {
//
}

protected async work() {
try {
for (const elem of this.config.relay.certifiers) {
const wallet = new Wallet(elem, this.contractManager.sideChainProvider);
const balance = (await wallet.getBalance()).div(BigNumber.from(1_000_000_000)).toNumber();
this.metrics.gaugeLabels("certifier_balance", { address: wallet.address }, balance);
}
} catch (error) {
logger.error(`Failed to execute the MetricsBOAScheduler: ${error}`);
}
}
}
Loading