forked from arkadiko-dao/keeper-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan-vaults.js
114 lines (102 loc) · 3.88 KB
/
scan-vaults.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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 getCollateralizationRatio(vaultId) {
const vaultTx = await tx.callReadOnlyFunction({
contractAddress: CONTRACT_ADDRESS,
contractName: "arkadiko-freddie-v1-1",
functionName: "calculate-current-collateral-to-debt-ratio",
functionArgs: [
tx.uintCV(vaultId),
tx.contractPrincipalCV(CONTRACT_ADDRESS, 'arkadiko-collateral-types-v1-1'),
tx.contractPrincipalCV(CONTRACT_ADDRESS, 'arkadiko-oracle-v1-1'),
tx.falseCV()
],
senderAddress: CONTRACT_ADDRESS,
network
});
return tx.cvToJSON(vaultTx).value.value;
}
async function getLiquidationRatio(collateralType) {
const vaultTx = await tx.callReadOnlyFunction({
contractAddress: CONTRACT_ADDRESS,
contractName: "arkadiko-collateral-types-v1-1",
functionName: "get-liquidation-ratio",
functionArgs: [tx.stringAsciiCV(collateralType)],
senderAddress: CONTRACT_ADDRESS,
network
});
return tx.cvToJSON(vaultTx).value.value;
}
async function liquidateVault(vaultId, nonce) {
const txOptions = {
contractAddress: CONTRACT_ADDRESS,
contractName: "arkadiko-liquidator-v1-1",
functionName: "notify-risky-vault",
functionArgs: [
tx.contractPrincipalCV(CONTRACT_ADDRESS, 'arkadiko-freddie-v1-1'),
tx.contractPrincipalCV(CONTRACT_ADDRESS, 'arkadiko-auction-engine-v1-1'),
tx.uintCV(vaultId),
tx.contractPrincipalCV(CONTRACT_ADDRESS, 'arkadiko-collateral-types-v1-1'),
tx.contractPrincipalCV(CONTRACT_ADDRESS, 'arkadiko-oracle-v1-1'),
],
senderKey: process.env.STACKS_PRIVATE_KEY,
postConditionMode: 1,
nonce: new BN(nonce),
network
};
const transaction = await tx.makeContractCall(txOptions);
console.log('Nonce =', nonce);
const result = tx.broadcastTransaction(transaction, network);
return await utils.processing(result, transaction.txid(), 0);
}
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;
const vaultIds = Array.from(Array(lastId).keys());
for (let index = 1900; index <= lastId; index++) {
vault = await getVaultById(index);
if (!vault['is-liquidated']['value'] && vault['debt']['value'] > 0) {
// console.log(vault);
console.log('Querying vault', index);
const collRatio = await getCollateralizationRatio(index);
const liqRatio = await getLiquidationRatio(vault['collateral-type']['value']);
if (collRatio < liqRatio) {
console.log('Vault', index, 'is in danger... need to liquidate - collateralization ratio:', collRatio, ', liquidation ratio:', liqRatio, 'and debt of', vault['debt']['value'] / 1000000, 'with collateral', vault['collateral']['value'] / 1000000);
// await liquidateVault(index, nonce);
nonce += 1;
} else {
console.log('collateralization:', collRatio, ' and liquidation ratio', liqRatio);
}
}
await new Promise(r => setTimeout(r, 2000));
}
}
iterateAndCheck();