-
Notifications
You must be signed in to change notification settings - Fork 55
/
addressBalanceMonitor.ts
44 lines (34 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
44
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 [targetAddress, targetToken] = monitoredValue.target.split("|");
const balance = await this.getBalance(targetAddress, targetToken);
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, denom?: 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 === (denom || activeChain.udenom));
if (!balance) {
return null;
}
return parseInt(balance.amount);
}
}