Skip to content

Commit

Permalink
Merge pull request #358 from pshenmic/feat/transactions-gas-history
Browse files Browse the repository at this point in the history
Transactions gas history series
  • Loading branch information
owl352 authored Dec 8, 2024
2 parents 9941ac1 + 7c5daee commit 3168bf1
Show file tree
Hide file tree
Showing 6 changed files with 399 additions and 0 deletions.
35 changes: 35 additions & 0 deletions packages/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Reference:
* [Transactions By Identity](#transactions-by-identity)
* [Transfers by Identity](#transfers-by-identity)
* [Transactions history](#transactions-history)
* [Transactions gas history](#transactions-gas-history)
* [Rate](#rate)
* [Decode Raw Transaction](#decode-raw-transaction)

Expand Down Expand Up @@ -1033,6 +1034,40 @@ Response codes:
400: Invalid input, check start/end values
500: Internal Server Error
```
### Transactions Gas history
Return a series data for the used gas of transactions chart

* `start` lower interval threshold in ISO string ( _optional_ )
* `end` upper interval threshold in ISO string ( _optional_ )
* `intervalsCount` intervals count in response ( _optional_ )

```
GET /transactions/gas/history?start=2024-01-01T00:00:00&end=2025-01-01T00:00:00
[
{
timestamp: "2024-04-22T08:45:20.911Z",
"data": {
"gas": 772831320,
"blockHeight": 64060,
"blockHash": "4A1F6B5238032DDAC55009A28797D909DB4288D5B5EC14B86DEC6EA8F25EC71A"
}
},
{
timestamp: "2024-04-22T08:50:20.911Z",
"data": {
"gas": 14108752440,
"blockHeight": 64333,
"blockHash": "507659D9BE2FF76A031F4219061F3D2D39475A7FA4B24F25AEFDB34CD4DF2A57"
}
}, ...
]
```
Response codes:
```
200: OK
400: Invalid input, check start/end values
500: Internal Server Error
```
### Rate
Return a rate DASH to USD
```
Expand Down
46 changes: 46 additions & 0 deletions packages/api/src/controllers/TransactionsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,52 @@ class TransactionsController {
response.send(timeSeries)
}

getGasHistory = async (request, response) => {
const {
start = new Date().getTime() - 3600000,
end = new Date().getTime(),
timespan = null,
intervalsCount = null
} = request.query

if (start > end) {
return response.status(400).send({ message: 'start timestamp cannot be more than end timestamp' })
}

let timespanStart = null
let timespanEnd = null

const timespanInterval = {
'1h': { offset: 3600000, step: 'PT5M' },
'24h': { offset: 86400000, step: 'PT2H' },
'3d': { offset: 259200000, step: 'PT6H' },
'1w': { offset: 604800000, step: 'PT14H' }
}[timespan]

if (timespanInterval) {
timespanStart = new Date().getTime() - timespanInterval.offset
timespanEnd = new Date().getTime()
}

const intervalInMs =
Math.ceil(
(new Date(timespanEnd ?? end).getTime() - new Date(timespanStart ?? start).getTime()) / Number(intervalsCount ?? NaN) / 1000
) * 1000

const interval = intervalsCount
? iso8601duration(intervalInMs)
: (timespanInterval?.step ?? calculateInterval(new Date(start), new Date(end)))

const timeSeries = await this.transactionsDAO.getGasHistorySeries(
new Date(timespanStart ?? start),
new Date(timespanEnd ?? end),
interval,
isNaN(intervalInMs) ? Intervals[interval] : intervalInMs
)

response.send(timeSeries)
}

decode = async (request, reply) => {
const { base64 } = request.body

Expand Down
55 changes: 55 additions & 0 deletions packages/api/src/dao/TransactionsDAO.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ module.exports = class TransactionsDAO {
.count('*')
.as('tx_count')
)
.select(
this.knex('state_transitions')
.leftJoin('blocks', 'state_transitions.block_hash', 'blocks.hash')
.whereRaw('blocks.timestamp > date_from and blocks.timestamp <= date_to')
.sum('gas_used')
.as('gas_used')
)
.select(
this.knex('blocks')
.whereRaw('blocks.timestamp > date_from and blocks.timestamp <= date_to')
Expand Down Expand Up @@ -183,6 +190,54 @@ module.exports = class TransactionsDAO {
.map(({ timestamp, data }) => new SeriesData(timestamp, data))
}

getGasHistorySeries = async (start, end, interval, intervalInMs) => {
const startSql = `'${new Date(start.getTime() + intervalInMs).toISOString()}'::timestamptz`

const endSql = `'${new Date(end.getTime()).toISOString()}'::timestamptz`

const ranges = this.knex
.from(this.knex.raw(`generate_series(${startSql}, ${endSql}, '${interval}'::interval) date_to`))
.select('date_to', this.knex.raw(`LAG(date_to, 1, '${start.toISOString()}'::timestamptz) over (order by date_to asc) date_from`))

const rows = await this.knex.with('ranges', ranges)
.select('date_from')
.select(
this.knex('state_transitions')
.leftJoin('blocks', 'state_transitions.block_hash', 'blocks.hash')
.whereRaw('blocks.timestamp > date_from and blocks.timestamp <= date_to')
.sum('gas_used')
.as('gas')
)
.select(
this.knex('blocks')
.whereRaw('blocks.timestamp > date_from and blocks.timestamp <= date_to')
.select('hash')
.orderBy('blocks.height')
.limit(1)
.as('block_hash')
)
.select(
this.knex('blocks')
.whereRaw('blocks.timestamp > date_from and blocks.timestamp <= date_to')
.select('height')
.orderBy('height')
.limit(1)
.as('block_height')
)
.from('ranges')

return rows
.map(row => ({
timestamp: row.date_from,
data: {
gas: parseInt(row.gas),
blockHeight: row.block_height,
blockHash: row.block_hash
}
}))
.map(({ timestamp, data }) => new SeriesData(timestamp, data))
}

getCollectedFees = async (timespan) => {
const interval = {
'1h': { offset: '1 hour', step: '5 minute' },
Expand Down
8 changes: 8 additions & 0 deletions packages/api/src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,14 @@ module.exports = ({
querystring: { $ref: 'timeInterval#' }
}
},
{
path: '/transactions/gas/history',
method: 'GET',
handler: transactionsController.getGasHistory,
schema: {
querystring: { $ref: 'timeInterval#' }
}
},
{
path: '/validators',
method: 'GET',
Expand Down
Loading

0 comments on commit 3168bf1

Please sign in to comment.