forked from akash-network/console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploymentBalanceMonitor.ts
56 lines (44 loc) · 1.92 KB
/
deploymentBalanceMonitor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { activeChain } from "@shared/chainDefinitions";
import { MonitoredValue } from "@shared/dbSchemas/base/monitoredValue";
import axios from "axios";
import * as Sentry from "@sentry/node";
export class DeploymentBalanceMonitor {
async run() {
const monitoredValues = await MonitoredValue.findAll({
where: {
tracker: "DeploymentBalanceMonitor"
}
});
await Promise.allSettled(monitoredValues.map((x) => this.updateValue(x)));
console.log("Refreshed balances for " + monitoredValues.length + " deployments.");
}
async updateValue(monitoredValue: MonitoredValue) {
try {
const balance = await this.getDeploymentBalance(monitoredValue.target);
if (balance === null) {
throw new Error("Unable to get balance for " + monitoredValue.target);
}
monitoredValue.value = balance.toString();
monitoredValue.lastUpdateDate = new Date();
await monitoredValue.save();
} catch (err) {
console.error(err);
Sentry.captureException(err, { tags: { target: monitoredValue.target } });
}
}
async getDeploymentBalance(target: string): Promise<number> {
const [owner, dseq] = target.split("/");
const response = await axios.get(`https://rest.cosmos.directory/akash/akash/deployment/v1beta3/deployments/info?id.owner=${owner}&id.dseq=${dseq}`, {
timeout: 15_000
});
const balance = response.data.escrow_account.balance;
const funds = response.data.escrow_account.funds;
const isAktDenom = balance.denom === activeChain.denom || balance.denom === activeChain.udenom;
if (!balance || !funds || !isAktDenom) {
return null;
}
const balanceAmount = balance.denom === activeChain.udenom ? parseInt(balance.amount) : parseInt(balance.amount) * 1_000_000;
const fundsAmount = funds.denom === activeChain.udenom ? parseInt(funds.amount) : parseInt(funds.amount) * 1_000_000;
return balanceAmount + fundsAmount;
}
}