-
Notifications
You must be signed in to change notification settings - Fork 51
/
addressBalanceMonitor.ts
43 lines (33 loc) · 1.39 KB
/
addressBalanceMonitor.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
import { activeChain } from "@akashnetwork/database/chainDefinitions";
import { MonitoredValue } from "@akashnetwork/database/dbSchemas/base/monitoredValue";
import axios from "axios";
export class AddressBalanceMonitor {
async run() {
const monitoredValues = await MonitoredValue.findAll({
where: {
tracker: "AddressBalanceMonitor"
}
});
await Promise.allSettled(monitoredValues.map(x => this.updateValue(x)));
console.log("Refreshed balances for " + monitoredValues.length + " addresses.");
}
async updateValue(monitoredValue: MonitoredValue) {
const balance = await this.getBalance(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();
}
async getBalance(address: string): Promise<number> {
const response = await axios.get(`https://rest.cosmos.directory/${activeChain.cosmosDirectoryId}/cosmos/bank/v1beta1/balances/${address}`, {
timeout: 15_000
});
const balance = response.data.balances.find(x => x.denom === activeChain.denom || x.denom === activeChain.udenom);
if (!balance) {
return null;
}
return balance.denom === activeChain.udenom ? parseInt(balance.amount) : parseInt(balance.amount) * 1_000_000;
}
}