Skip to content

Commit

Permalink
Add a scheduler to measure the BOA balance of administrator accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKim20 committed Sep 12, 2024
1 parent df3dd0d commit 22bb0d3
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 8 deletions.
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
6 changes: 0 additions & 6 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 @@ -60,11 +59,6 @@ export class DefaultRouter {
try {
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);
}
res.end(await this.metrics.metrics());
} catch (error: any) {
return res.status(500);
Expand Down
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}`);
}
}
}

0 comments on commit 22bb0d3

Please sign in to comment.