1
+ /*
2
+ This file contains all the methods to interact with
3
+ models from the API. They check for parameters,
4
+ make sure the required resources exist and can
5
+ be accessed by the user.
6
+ APIs should use methods from this file and not interact
7
+ with the models directly.
8
+ Background jobs do not need to use methods here and
9
+ can interact with the models directly, as they are not
10
+ exposed to the user.
11
+ */
1
12
const Sequelize = require ( 'sequelize' ) ;
2
13
const { getDemoUserId, getMaxBlockForSyncReset } = require ( './env' ) ;
3
14
const models = require ( '../models' ) ;
@@ -21,6 +32,145 @@ const ExplorerFaucet = models.ExplorerFaucet;
21
32
const ExplorerV2Dex = models . ExplorerV2Dex ;
22
33
const V2DexPair = models . V2DexPair ;
23
34
35
+ /*
36
+ This method is used to get the latest biggest gas spenders for a workspace
37
+ for a given interval (now - intervalInHours).
38
+
39
+ @param {number } workspaceId - The ID of the workspace
40
+ @param {number } intervalInHours - The interval in hours to get the gas spenders for
41
+ @param {number } limit - The limit of gas spenders to return
42
+ @returns {array } - The gas spenders
43
+ - from: The address of the gas spender
44
+ - gasUsed: The total gas used by the gas spender
45
+ - gasCost: Cost of total gas used
46
+ - percentUsed: The percentage of total gas used by the gas spender
47
+ */
48
+ const getLatestGasSpenders = async ( workspaceId , intervalInHours = 24 , limit = 50 ) => {
49
+ if ( ! workspaceId )
50
+ throw new Error ( 'Missing parameter' ) ;
51
+
52
+ const workspace = await Workspace . findByPk ( workspaceId ) ;
53
+ if ( ! workspace )
54
+ throw new Error ( 'Could not find workspace' ) ;
55
+
56
+ return workspace . getLatestGasSpenders ( intervalInHours , limit ) ;
57
+ } ;
58
+
59
+ /*
60
+ This method is used to get the latest biggest gas consumers for a workspace
61
+ for a given interval (now - intervalInHours).
62
+
63
+ @param {number } workspaceId - The ID of the workspace
64
+ @param {number } intervalInHours - The interval in hours to get the gas consumers for
65
+ @param {number } limit - The limit of gas consumers to return
66
+ @returns {array } - The gas consumers
67
+ - to: The address of the gas consumer
68
+ - gasUsed: The total gas used by the gas consumer
69
+ - gasCost: Cost of total gas used
70
+ */
71
+ const getLatestGasConsumers = async ( workspaceId , intervalInHours = 24 , limit = 50 ) => {
72
+ if ( ! workspaceId )
73
+ throw new Error ( 'Missing parameter' ) ;
74
+
75
+ const workspace = await Workspace . findByPk ( workspaceId ) ;
76
+ if ( ! workspace )
77
+ throw new Error ( 'Could not find workspace' ) ;
78
+
79
+ return workspace . getLatestGasConsumers ( intervalInHours , limit ) ;
80
+ } ;
81
+
82
+ /*
83
+ This method is used to get the gas utilization ratio history for a workspace.
84
+
85
+ @param {number } workspaceId - The ID of the workspace
86
+ @param {string } from - The start date of the gas utilization ratio history
87
+ @param {string } to - The end date of the gas utilization ratio history
88
+ @returns {array } - The gas utilization ratio history
89
+ - day: The day of the gas utilization ratio history
90
+ - gasUtilizationRatio: The average gas utilization ratio for the day
91
+ */
92
+ const getGasUtilizationRatioHistory = async ( workspaceId , from , to ) => {
93
+ if ( ! workspaceId || ! from || ! to )
94
+ throw new Error ( 'Missing parameter' ) ;
95
+
96
+ const workspace = await Workspace . findByPk ( workspaceId ) ;
97
+ if ( ! workspace )
98
+ throw new Error ( 'Could not find workspace' ) ;
99
+
100
+ return workspace . getGasUtilizationRatioHistory ( from , to ) ;
101
+ } ;
102
+
103
+ /*
104
+ This method is used to get the gas limit history for a workspace.
105
+
106
+ @param {number } workspaceId - The ID of the workspace
107
+ @param {string } from - The start date of the gas limit history
108
+ @param {string } to - The end date of the gas limit history
109
+ @returns {array } - The gas limit history
110
+ - day: The day of the gas limit history
111
+ - gasLimit: The average gas limit for the day
112
+ */
113
+ const getGasLimitHistory = async ( workspaceId , from , to ) => {
114
+ if ( ! workspaceId || ! from || ! to )
115
+ throw new Error ( 'Missing parameter' ) ;
116
+
117
+ const workspace = await Workspace . findByPk ( workspaceId ) ;
118
+ if ( ! workspace )
119
+ throw new Error ( 'Could not find workspace' ) ;
120
+
121
+ return workspace . getGasLimitHistory ( from , to ) ;
122
+ } ;
123
+
124
+ /*
125
+ This method is used to get the gas price history for a workspace.
126
+
127
+ @param {number } workspaceId - The ID of the workspace
128
+ @param {string } from - The start date of the gas price history
129
+ @param {string } to - The end date of the gas price history
130
+ @returns {array } - The gas price history
131
+ - day: The day of the gas price history
132
+ - minSlow: The minimum slow gas price
133
+ - slow: The average slow gas price
134
+ - maxSlow: The maximum slow gas price
135
+ - minAverage: The minimum average gas price
136
+ - average: The average average gas price
137
+ - maxAverage: The maximum average gas price
138
+ - minFast: The minimum fast gas price
139
+ - fast: The average fast gas price
140
+ - maxFast: The maximum fast gas price
141
+ */
142
+ const getGasPriceHistory = async ( workspaceId , from , to ) => {
143
+ if ( ! workspaceId || ! from || ! to )
144
+ throw new Error ( 'Missing parameter' ) ;
145
+
146
+ const workspace = await Workspace . findByPk ( workspaceId ) ;
147
+ if ( ! workspace )
148
+ throw new Error ( 'Could not find workspace' ) ;
149
+
150
+ return workspace . getGasPriceHistory ( from , to ) ;
151
+ } ;
152
+
153
+ /*
154
+ This method is used to get the latest gas stats for a workspace.
155
+
156
+ @param {number } workspaceId - The ID of the workspace
157
+ @param {number } intervalInMinutes - The interval in minutes to get the gas stats for
158
+ @returns {object } - The gas stats object
159
+ - averageBlockSize: The average block size in transactions
160
+ - averageUtilization: The average quantity of gas used per block
161
+ - averageBlockTime: The average block time in seconds
162
+ - latestBlockNumber: The number of the latest block used for this calculation
163
+ - baseFeePerGas: The base fee per gas for the latest block
164
+ - priorityFeePerGas: The three levels of priority fee per gas for the latest block (slow, average, fast)
165
+ */
166
+ const getLatestGasStats = async ( workspaceId , intervalInMinutes = 1 ) => {
167
+ const workspace = await Workspace . findByPk ( workspaceId ) ;
168
+ if ( ! workspace )
169
+ throw new Error ( 'Could not find workspace' ) ;
170
+
171
+ return workspace . getLatestGasStats ( intervalInMinutes ) ;
172
+ } ;
173
+
24
174
const createUserStripeSubscription = ( userId , stripeSubscription , stripePlan ) => {
25
175
if ( ! userId || ! stripeSubscription || ! stripePlan )
26
176
throw new Error ( 'Missing parameter' ) ;
@@ -2495,5 +2645,11 @@ module.exports = {
2495
2645
deleteV2Dex : deleteV2Dex ,
2496
2646
getV2DexPairCount : getV2DexPairCount ,
2497
2647
getUserStripeSubscription : getUserStripeSubscription ,
2498
- createUserStripeSubscription : createUserStripeSubscription
2648
+ createUserStripeSubscription : createUserStripeSubscription ,
2649
+ getLatestGasStats : getLatestGasStats ,
2650
+ getGasPriceHistory : getGasPriceHistory ,
2651
+ getGasLimitHistory : getGasLimitHistory ,
2652
+ getGasUtilizationRatioHistory : getGasUtilizationRatioHistory ,
2653
+ getLatestGasConsumers : getLatestGasConsumers ,
2654
+ getLatestGasSpenders : getLatestGasSpenders
2499
2655
} ;
0 commit comments