Skip to content

Commit

Permalink
Merge pull request #1257 from edenia/feat/limit-evm-history
Browse files Browse the repository at this point in the history
Feat/limit evm history
  • Loading branch information
xavier506 committed Aug 23, 2023
2 parents 4ebf1c7 + 5a1f787 commit 1abd70d
Show file tree
Hide file tree
Showing 22 changed files with 439 additions and 17 deletions.
41 changes: 36 additions & 5 deletions hapi-evm/src/models/block/queries.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import moment from 'moment'
import { gql } from 'graphql-request'

import { coreUtil } from '../../utils'
Expand All @@ -22,52 +23,68 @@ interface BlockInsertOneResponse {
}
}

interface BlockDeleteResponse {
delete_evm_block: {
affected_rows: number
}
}

const internal_get = async <T>(
type: OperationType = Operation.query,
table: TableType,
parameters: string,
// TODO: not only accept where but also additional content
// such as limit, order, etc
where: object,
order: object | null,
attributes: string,
operation?: string
): Promise<T> => {
const query = gql`
${type} (${parameters}) {
${table}${operation ? `_${operation}` : ''}(where: $where) {
${table}${
operation ? `_${operation}` : ''
}(where: $where, order_by: $order) {
${attributes}
}
}
`

return await coreUtil.hasura.default.request<T>(query, {
where
where,
order
})
}

export const exist = async (hashOrNumber: string | number) => {
const result = await internal_get<BlockAggregateResponse>(
'query',
'evm_block',
'$where: evm_block_bool_exp!',
'$where: evm_block_bool_exp!, $order: [evm_block_order_by!]',
{
[typeof hashOrNumber === 'string' ? 'hash' : 'number']: {
_eq: hashOrNumber
}
},
null,
'aggregate { count }',
'aggregate'
)

return result.evm_block_aggregate.aggregate.count > 0
}

const get = async (where: object, many = false) => {
const get = async (
where: object,
order: object | null = null,
many = false
) => {
const result = await internal_get<BlockResponse>(
'query',
'evm_block',
'$where: evm_block_bool_exp!',
'$where: evm_block_bool_exp!, $order: [evm_block_order_by!]',
where,
order,
'hash, gas_used, transactions, number, timestamp'
)

Expand Down Expand Up @@ -112,6 +129,20 @@ export const add_or_modify = async (block: CappedBlock) => {
return data
}

export const deleteOldBlocks = async () => {
const mutation = gql`
mutation ($date: timestamptz) {
delete_evm_block(where: { timestamp: { _lt: $date } }) {
affected_rows
}
}
`

await coreUtil.hasura.default.request<BlockDeleteResponse>(mutation, {
date: moment().subtract(1, 'years').format('YYYY-MM-DD')
})
}

export default {
exist,
get,
Expand Down
2 changes: 2 additions & 0 deletions hapi-evm/src/models/historical-stats/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * as interfaces from './interfaces'
export * as queries from './queries'
17 changes: 17 additions & 0 deletions hapi-evm/src/models/historical-stats/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export interface HistoricalStats {
id?: string
total_transactions?: number
total_incoming_token?: number
total_outgoing_token?: number
tps_all_time_high?: {
blocks: string[]
transactions_count: number
gas_used: number
}
}

export interface HistoricalStatsIncInput {
total_transactions?: number
total_incoming_token?: number
total_outgoing_token?: number
}
138 changes: 138 additions & 0 deletions hapi-evm/src/models/historical-stats/queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { gql } from 'graphql-request'

import { coreUtil } from '../../utils'
import { HistoricalStats, HistoricalStatsIncInput } from './interfaces'

interface HistoricalStatsResponse {
evm_historical_stats: HistoricalStats[]
}

interface HistoricalStatsOneResponse {
insert_evm_historical_stats_one: {
id: string
}
}

const defaultHistoricalStats = {
id: '00000000-0000-0000-0000-000000000000',
total_transactions: 0,
total_incoming_token: 0,
total_outgoing_token: 0,
tps_all_time_high: {
blocks: [],
transactions_count: 0,
gas_used: 0
}
}

const save = async (payload: HistoricalStats) => {
const mutation = gql`
mutation ($payload: evm_historical_stats_insert_input!) {
insert_evm_historical_stats_one(object: $payload) {
id
}
}
`

const data =
await coreUtil.hasura.default.request<HistoricalStatsOneResponse>(
mutation,
{
payload
}
)

return data.insert_evm_historical_stats_one
}

const update = async (
id: string,
inc: HistoricalStatsIncInput,
payload: HistoricalStats
) => {
const mutation = gql`
mutation (
$id: uuid!
$inc: evm_historical_stats_inc_input
$payload: evm_historical_stats_set_input
) {
update_evm_historical_stats_by_pk(
_inc: $inc
pk_columns: { id: $id }
_set: $payload
) {
id
}
}
`

await coreUtil.hasura.default.request(mutation, {
id,
inc,
payload
})
}

export const getState = async () => {
const query = gql`
query {
evm_historical_stats(
where: { id: { _neq: "00000000-0000-0000-0000-000000000000" } }
limit: 1
) {
id
total_transactions
total_incoming_token
total_outgoing_token
tps_all_time_high
}
}
`
const data = await coreUtil.hasura.default.request<HistoricalStatsResponse>(
query
)

if (!data.evm_historical_stats.length) {
return defaultHistoricalStats
}

const state = data.evm_historical_stats[0]

return {
id: state.id || defaultHistoricalStats.id,
total_transactions:
state.total_transactions || defaultHistoricalStats.total_transactions,
total_incoming_token:
state.total_incoming_token || defaultHistoricalStats.total_incoming_token,
total_outgoing_token:
state.total_outgoing_token || defaultHistoricalStats.total_outgoing_token,
tps_all_time_high:
state.tps_all_time_high || defaultHistoricalStats.tps_all_time_high
}
}

export const saveOrUpdate = async (payload: HistoricalStats): Promise<void> => {
const currentState = await getState()

if (currentState === defaultHistoricalStats) {
await save(payload)

return
}

await update(currentState.id, {}, payload)
}

export const saveOrIncrement = async (
payload: HistoricalStatsIncInput
): Promise<void> => {
const currentState = await getState()

if (currentState === defaultHistoricalStats) {
await save(payload)

return
}

await update(currentState.id, payload, {})
}
2 changes: 2 additions & 0 deletions hapi-evm/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ export * as hyperionStateModel from './hyperion-state'
export * as transferModel from './transfer'
export * as paramModel from './param'
export * as historyPayloadModel from './history-payload'
export * as historicalStatsModel from './historical-stats'
export * as StatsModel from './stats'
2 changes: 2 additions & 0 deletions hapi-evm/src/models/stats/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * as interfaces from './interfaces'
export * as queries from './queries'
5 changes: 5 additions & 0 deletions hapi-evm/src/models/stats/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Stats {
ath_blocks: string
ath_transactions_count: number
ath_gas_used: number
}
24 changes: 24 additions & 0 deletions hapi-evm/src/models/stats/queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { gql } from 'graphql-request'

import { coreUtil } from '../../utils'
import { Stats } from './interfaces'

interface StatsResponse {
evm_stats: Stats[]
}

export const getPartialATH = async () => {
const query = gql`
query {
evm_stats(limit: 1) {
ath_blocks
ath_transactions_count
ath_gas_used
}
}
`
const data = await coreUtil.hasura.default.request<StatsResponse>(query)
const state = data.evm_stats[0]

return state
}
33 changes: 32 additions & 1 deletion hapi-evm/src/models/transfer/queries.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import moment from 'moment'
import { gql } from 'graphql-request'

import { coreUtil } from '../../utils'
import { Transfer } from './interfaces'
import { Transfer, Type } from './interfaces'
import { historicalStatsModel } from '..'

// interface TransferResponse {
// evm_transfer: Transfer[]
Expand All @@ -13,6 +15,12 @@ interface TransferInsertOneResponse {
}
}

interface TransferDeleteResponse {
delete_evm_transfer: {
affected_rows: number
}
}

export const save = async (payload: Transfer) => {
const mutation = gql`
mutation ($payload: evm_transfer_insert_input!) {
Expand All @@ -22,6 +30,15 @@ export const save = async (payload: Transfer) => {
}
`

await historicalStatsModel.queries.saveOrIncrement({
total_incoming_token: Number(payload.type === Type.incoming),
total_outgoing_token: Number(payload.type === Type.outgoing)
})

if (moment(payload.timestamp).isBefore(moment().subtract(1, 'years'))) {
return
}

const data = await coreUtil.hasura.default.request<TransferInsertOneResponse>(
mutation,
{
Expand All @@ -31,3 +48,17 @@ export const save = async (payload: Transfer) => {

return data.insert_evm_transfer_one
}

export const deleteOldTransfers = async () => {
const mutation = gql`
mutation ($date: timestamptz) {
delete_evm_transfer(where: { timestamp: { _lt: $date } }) {
affected_rows
}
}
`

await coreUtil.hasura.default.request<TransferDeleteResponse>(mutation, {
date: moment().subtract(1, 'years').format('YYYY-MM-DD')
})
}
Loading

0 comments on commit 1abd70d

Please sign in to comment.