forked from akash-network/console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploymentBalanceMonitor.ts
More file actions
58 lines (45 loc) · 1.83 KB
/
deploymentBalanceMonitor.ts
File metadata and controls
58 lines (45 loc) · 1.83 KB
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
57
58
import { activeChain } from "@akashnetwork/database/chainDefinitions";
import { MonitoredValue } from "@akashnetwork/database/dbSchemas/base";
import * as Sentry from "@sentry/node";
import axios from "axios";
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 | null> {
const [owner, dseq] = target.split("/");
const response = await axios.get(`https://rest.cosmos.directory/akash/akash/deployment/v1beta4/deployments/info?id.owner=${owner}&id.dseq=${dseq}`, {
timeout: 15_000
});
const escrowState = response?.data?.escrow_account?.state;
if (!escrowState?.funds) {
return null;
}
const funds = escrowState.funds.find((f: { denom: string; amount: string }) => f.denom === activeChain.denom || f.denom === activeChain.udenom);
if (!funds) {
return null;
}
const fundsAmount = funds.denom === activeChain.udenom ? parseInt(funds.amount) : parseInt(funds.amount) * 1_000_000;
return fundsAmount;
}
}