-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcalculate-total-debt.js
52 lines (46 loc) · 1.45 KB
/
calculate-total-debt.js
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
require('dotenv').config();
const CONTRACT_ADDRESS = process.env.CONTRACT_ADDRESS;
const tx = require('@stacks/transactions');
const utils = require('./utils');
const network = utils.resolveNetwork();
const BN = require('bn.js');
async function getLastVaultId() {
const lastVaultTx = await tx.callReadOnlyFunction({
contractAddress: CONTRACT_ADDRESS,
contractName: "arkadiko-vault-data-v1-1",
functionName: "get-last-vault-id",
functionArgs: [],
senderAddress: CONTRACT_ADDRESS,
network
});
return tx.cvToJSON(lastVaultTx).value;
}
async function getVaultById(vaultId) {
const vaultTx = await tx.callReadOnlyFunction({
contractAddress: CONTRACT_ADDRESS,
contractName: "arkadiko-freddie-v1-1",
functionName: "get-vault-by-id",
functionArgs: [tx.uintCV(vaultId)],
senderAddress: CONTRACT_ADDRESS,
network
});
return tx.cvToJSON(vaultTx).value;
}
async function iterateAndCheck() {
let nonce = await utils.getNonce(CONTRACT_ADDRESS);
const lastId = await getLastVaultId();
console.log('Last Vault ID is', lastId, ', iterating vaults');
let vault;
let debt = 0;
const vaultIds = Array.from(Array(lastId).keys());
for (let index = 1; index <= lastId; index++) {
vault = await getVaultById(index);
if (!vault['is-liquidated']['value']) {
// console.log(vault);
console.log('Querying vault', index);
debt += vault['debt']['value'];
}
console.log(debt);
}
}
iterateAndCheck();