-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathall-keeps.js
157 lines (142 loc) · 5.49 KB
/
all-keeps.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const fs = require('fs')
const ethers = require('ethers')
const BondedECDSAKeepFactory = require("@keep-network/keep-ecdsa/artifacts/BondedECDSAKeepFactory.json")
const Deposit = require("@keep-network/tbtc/artifacts/Deposit.json")
const DepositFactory = require("@keep-network/tbtc/artifacts/DepositFactory.json")
const FeeRebateToken = require("@keep-network/tbtc/artifacts/FeeRebateToken.json")
const TBTCDepositToken = require("@keep-network/tbtc/artifacts/TBTCDepositToken.json")
const VendingMachine = require("@keep-network/tbtc/artifacts/VendingMachine.json")
const states = [
"START",
"AWAITING_SIGNER_SETUP",
"AWAITING_BTC_FUNDING_PROOF",
"FAILED_SETUP",
"ACTIVE",
"AWAITING_WITHDRAWAL_SIGNATURE",
"AWAITING_WITHDRAWAL_PROOF",
"REDEEMED",
"COURTESY_CALL",
"FRAUD_LIQUIDATION_IN_PROGRESS",
"LIQUIDATION_IN_PROGRESS",
"LIQUIDATED",
];
function printUsage() {
console.log('Usage: node all-keeps gather')
console.log('Usage: node all-keeps stats')
console.log('Usage: node all-keeps getchurners')
console.log('Usage: node all-keeps getcsv')
}
async function main() {
if (process.argv.length < 3) {
printUsage()
return
}
const action = process.argv[2]
if (action === 'stats') {
await stats()
} else if (action === 'gather') {
await gather()
} else if (action === 'getchurners') {
await getChurners()
} else if (action === 'getcsv') {
await getCSV()
} else {
printUsage()
return
}
}
async function stats() {
let data = fs.readFileSync('data/allKeeps.json');
let keeps = JSON.parse(data);
const activeKeeps = keeps.filter(k => k.state === "ACTIVE")
console.log(`Total keeps: ${keeps.length}`)
console.log(`Total keeps active: ${activeKeeps.length}`)
console.log(`BTC held in active keeps: ${activeKeeps.map(k => k.lotSize).reduce((a,b) => a + b, 0) / 100000000}`)
console.log(`Lowest collateralization: ${Math.min.apply(Math, activeKeeps.map(k => k.collateralizationRate))}`)
}
async function getCSV() {
let data = fs.readFileSync('data/allKeeps.json');
let keeps = JSON.parse(data);
console.log('blockNum,cloneAddress,lotSize,state,signer1,signer2,signer3')
for (let keep of keeps) {
console.log(`${keep.blockNum},${keep.cloneAddress},${keep.lotSize/100000000},${keep.state},${keep.signers[0]},${keep.signers[1]},${keep.signers[2]}`)
}
}
async function getChurners() {
let data = fs.readFileSync('data/allKeeps.json');
let keeps = JSON.parse(data);
const redeemedBySigner = {}
const activeBySigner = {}
for (let keep of keeps) {
for (let signer of keep.signers) {
if (!activeBySigner[signer]) {
activeBySigner[signer] = 0
}
if (!redeemedBySigner[signer]) {
redeemedBySigner[signer] = 0
}
if (keep.state === 'ACTIVE' ||
keep.state === 'AWAITING_SIGNER_SETUP' ||
keep.state === `AWAITING_BTC_FUNDING_PROOF`) {
activeBySigner[signer] += 1
} else if (keep.state === 'REDEEMED' ||
keep.state === 'FAILED_SETUP' ||
keep.state === 'AWAITING_WITHDRAWAL_SIGNATURE' ||
keep.state === 'AWAITING_WITHDRAWAL_PROOF') {
redeemedBySigner[signer] += 1
}
}
}
for (let signer in activeBySigner) {
console.log(`${signer} ${activeBySigner[signer]}/${redeemedBySigner[signer]}`)
}
}
async function gather() {
const ip = new ethers.providers.InfuraProvider('homestead', "414a548bc7434bbfb7a135b694b15aa4")
const bondedECDSAKeepFactory = new ethers.Contract("0xa7d9e842efb252389d613da88eda3731512e40bd", BondedECDSAKeepFactory.abi, ip)
const depositFactory = new ethers.Contract(DepositFactory.networks["1"].address, DepositFactory.abi, ip)
const frt = new ethers.Contract(FeeRebateToken.networks["1"].address, FeeRebateToken.abi, ip)
const tdt = new ethers.Contract(TBTCDepositToken.networks["1"].address, TBTCDepositToken.abi, ip)
// Assumption: these are the same length, and in the same order
const createdKeeps = await bondedECDSAKeepFactory.queryFilter(bondedECDSAKeepFactory.filters.BondedECDSAKeepCreated());
const depositClones = await depositFactory.queryFilter(depositFactory.filters.DepositCloneCreated());
let index = 0;
let allKeeps = []
for (let keep of createdKeeps) {
const depositCloneAddress = depositClones[index].args.depositCloneAddress
const d = new ethers.Contract(depositCloneAddress, Deposit.abi, ip)
const [frtExists, collateralizationRate, stateIndex, lotSize, tdtOwner] = await Promise.all([frt.exists(depositCloneAddress), d.collateralizationPercentage(), d.currentState(), d.lotSizeSatoshis(), tdt.ownerOf(depositCloneAddress)])
const state = states[stateIndex]
let tdtRedeemable = false
if (frtExists) {
if (tdtOwner == VendingMachine.networks["1"].address) {
tdtRedeemable = true
}
}
index++
const newEntry = {
keepAddress: keep.args.keepAddress,
blockNum: keep.blockNumber,
cloneAddress: depositCloneAddress,
lotSize: lotSize.toNumber(),
frtExists: frtExists,
redeemable: tdtRedeemable,
tdtOwner: tdtOwner,
collateralizationRate: collateralizationRate.toNumber(),
state: state,
signers: keep.args.members
}
console.log(newEntry)
allKeeps.push(newEntry)
// Load existing data
let data = JSON.stringify(allKeeps)
fs.writeFileSync("data/allKeeps.json", data)
}
console.log(createdKeeps[0])
console.log(depositClones[0])
console.log(createdKeeps.length)
console.log(depositClones.length)
}
main().catch(err => {
console.error(err);
})