1
+ import Table from 'cli-table'
2
+ import axios from 'axios'
1
3
import * as dotenv from 'dotenv'
2
- import { Wallet } from 'ethers'
4
+ import { BigNumber , Wallet , utils } from 'ethers'
3
5
import { extendEnvironment , task } from 'hardhat/config'
4
6
5
7
import { getAddressBook } from './cli/address-book'
@@ -9,6 +11,8 @@ import { getContractAt } from './cli/network'
9
11
import { migrate } from './cli/commands/migrate'
10
12
import { verify } from './cli/commands/verify'
11
13
14
+ const { formatEther, parseEther } = utils
15
+
12
16
dotenv . config ( )
13
17
14
18
// Plugins
@@ -119,6 +123,140 @@ task('print-fn-hashes', 'Print function hashes for a contract')
119
123
}
120
124
} )
121
125
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
+
122
260
const config = {
123
261
paths : {
124
262
sources : './contracts' ,
0 commit comments