-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1257 from edenia/feat/limit-evm-history
Feat/limit evm history
- Loading branch information
Showing
22 changed files
with
439 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * as interfaces from './interfaces' | ||
export * as queries from './queries' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, {}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * as interfaces from './interfaces' | ||
export * as queries from './queries' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.