Skip to content

Commit edb007d

Browse files
committed
cli: simple monitoring tasks
1 parent 9d86990 commit edb007d

File tree

3 files changed

+194
-15
lines changed

3 files changed

+194
-15
lines changed

hardhat.config.ts

+139-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import Table from 'cli-table'
2+
import axios from 'axios'
13
import * as dotenv from 'dotenv'
2-
import { Wallet } from 'ethers'
4+
import { BigNumber, Wallet, utils } from 'ethers'
35
import { extendEnvironment, task } from 'hardhat/config'
46

57
import { getAddressBook } from './cli/address-book'
@@ -9,6 +11,8 @@ import { getContractAt } from './cli/network'
911
import { migrate } from './cli/commands/migrate'
1012
import { verify } from './cli/commands/verify'
1113

14+
const { formatEther, parseEther } = utils
15+
1216
dotenv.config()
1317

1418
// Plugins
@@ -119,6 +123,140 @@ task('print-fn-hashes', 'Print function hashes for a contract')
119123
}
120124
})
121125

126+
task('list-rebates', 'List rebate pools')
127+
.addParam('addressBook', cliOpts.addressBook.description, cliOpts.addressBook.default)
128+
.setAction(async (taskArgs, hre) => {
129+
const accounts = await hre.ethers.getSigners()
130+
const { contracts } = await loadEnv(taskArgs, (accounts[0] as unknown) as Wallet)
131+
const { formatEther } = hre.ethers.utils
132+
133+
const table = new Table({
134+
head: ['Epoch', 'Total Fees', 'Claimed Amount', 'Unclaimed Allocs'],
135+
colWidths: [10, 40, 40, 20],
136+
})
137+
138+
const currentEpoch = await contracts.EpochManager.currentEpoch()
139+
for (let i = 0; i < 5; i++) {
140+
const epoch = currentEpoch.sub(i)
141+
const rebatePool = await contracts.Staking.rebates(epoch)
142+
table.push([
143+
epoch,
144+
formatEther(rebatePool.fees),
145+
formatEther(rebatePool.claimedRewards),
146+
rebatePool.unclaimedAllocationsCount,
147+
])
148+
}
149+
console.log(table.toString())
150+
})
151+
152+
task('list-allos', 'List allocations')
153+
.addParam('addressBook', cliOpts.addressBook.description, cliOpts.addressBook.default)
154+
.setAction(async (taskArgs, hre) => {
155+
const accounts = await hre.ethers.getSigners()
156+
const { contracts } = await loadEnv(taskArgs, (accounts[0] as unknown) as Wallet)
157+
158+
const query = `{
159+
allocations(where: { status: "Active" }, first: 1000) {
160+
id
161+
allocatedTokens
162+
subgraphDeployment { id }
163+
createdAt
164+
createdAtEpoch
165+
indexer { id stakedTokens }
166+
}
167+
}
168+
`
169+
const url = 'https://api.thegraph.com/subgraphs/name/graphprotocol/graph-network-mainnet'
170+
const res = await axios.post(url, { query })
171+
const allos = res.data.data.allocations
172+
173+
const table = new Table({
174+
head: ['ID', 'Indexer', 'SID', 'Allocated', 'IdxRewards', 'IdxCut', 'Cooldown', 'Epoch'],
175+
colWidths: [20, 20, 10, 20, 20, 10, 10, 10],
176+
})
177+
178+
const currentBlock = await hre.ethers.provider.send('eth_blockNumber', [])
179+
180+
let totalIndexingRewards = hre.ethers.BigNumber.from(0)
181+
let totalAllocated = hre.ethers.BigNumber.from(0)
182+
for (const allo of allos) {
183+
const pool = await contracts.Staking.delegationPools(allo.indexer.id)
184+
const r = await contracts.RewardsManager.getRewards(allo.id)
185+
table.push([
186+
allo.id,
187+
allo.indexer.id,
188+
allo.subgraphDeployment.id,
189+
formatEther(allo.allocatedTokens),
190+
formatEther(r),
191+
pool.indexingRewardCut / 10000,
192+
pool.updatedAtBlock.add(pool.cooldownBlocks).toNumber() - currentBlock,
193+
allo.createdAtEpoch,
194+
])
195+
196+
totalIndexingRewards = totalIndexingRewards.add(r)
197+
totalAllocated = totalAllocated.add(allo.allocatedTokens)
198+
}
199+
console.log(table.toString())
200+
console.log('total entries: ', allos.length)
201+
console.log('total pending idx-rewards: ', hre.ethers.utils.formatEther(totalIndexingRewards))
202+
console.log('total allocated: ', hre.ethers.utils.formatEther(totalAllocated))
203+
})
204+
205+
task('list-indexers', 'List indexers')
206+
.addParam('addressBook', cliOpts.addressBook.description, cliOpts.addressBook.default)
207+
.setAction(async (taskArgs, hre) => {
208+
const accounts = await hre.ethers.getSigners()
209+
const { contracts } = await loadEnv(taskArgs, (accounts[0] as unknown) as Wallet)
210+
211+
const query = `{
212+
indexers(where: {stakedTokens_gt: "0"}, first: 1000) {
213+
id
214+
stakedTokens
215+
delegatedTokens
216+
allocatedTokens
217+
allocationCount
218+
}
219+
}`
220+
const url = 'https://api.thegraph.com/subgraphs/name/graphprotocol/graph-network-mainnet'
221+
const res = await axios.post(url, { query })
222+
const indexers = res.data.data.indexers
223+
224+
const table = new Table({
225+
head: ['ID', 'Stake', 'Delegated', 'Capacity Ratio', 'Allocated', 'Used', 'N'],
226+
colWidths: [20, 20, 20, 20, 20, 10, 5],
227+
})
228+
229+
let totalStaked = hre.ethers.BigNumber.from(0)
230+
let totalDelegated = hre.ethers.BigNumber.from(0)
231+
let totalAllocated = hre.ethers.BigNumber.from(0)
232+
for (const indexer of indexers) {
233+
const t = indexer.stakedTokens / 1e18 + indexer.delegatedTokens / 1e18
234+
const b = indexer.allocatedTokens / 1e18 / t
235+
const maxCapacity = indexer.stakedTokens / 1e18 + (indexer.stakedTokens / 1e18) * 16
236+
const capacityRatio =
237+
(indexer.stakedTokens / 1e18 + indexer.delegatedTokens / 1e18) / maxCapacity
238+
239+
table.push([
240+
indexer.id,
241+
formatEther(indexer.stakedTokens),
242+
formatEther(indexer.delegatedTokens),
243+
capacityRatio.toFixed(2),
244+
formatEther(indexer.allocatedTokens),
245+
b.toFixed(2),
246+
indexer.allocationCount,
247+
])
248+
totalStaked = totalStaked.add(indexer.stakedTokens)
249+
totalDelegated = totalDelegated.add(indexer.delegatedTokens)
250+
totalAllocated = totalAllocated.add(indexer.allocatedTokens)
251+
}
252+
253+
console.log(table.toString())
254+
console.log('# indexers: ', indexers.length)
255+
console.log('total staked: ', formatEther(totalStaked))
256+
console.log('total delegated: ', formatEther(totalDelegated))
257+
console.log('total allocated: ', formatEther(totalAllocated))
258+
})
259+
122260
const config = {
123261
paths: {
124262
sources: './contracts',

package-lock.json

+53-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@
3636
"@types/yargs": "^15.0.5",
3737
"@typescript-eslint/eslint-plugin": "^3.10.1",
3838
"@typescript-eslint/parser": "^3.10.1",
39+
"axios": "^0.21.0",
3940
"bignumber.js": "^9.0.0",
4041
"chai": "^4.2.0",
41-
"cli-table": "^0.3.1",
42+
"cli-table": "^0.3.4",
4243
"consola": "^2.14.0",
4344
"dotenv": "^8.2.0",
4445
"eslint": "^7.7.0",

0 commit comments

Comments
 (0)