Skip to content

Commit

Permalink
fix(hapi-evm): limit get result
Browse files Browse the repository at this point in the history
  • Loading branch information
Torresmorah committed Nov 9, 2023
1 parent 9adacca commit 1971f50
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
12 changes: 8 additions & 4 deletions hapi-evm/src/models/block/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,36 +38,39 @@ const internal_get = async <T>(
// such as limit, order, etc
where: object,
order: object | null,
limit: number | null,
attributes: string,
operation?: string
): Promise<T> => {
const query = gql`
${type} (${parameters}) {
${table}${
operation ? `_${operation}` : ''
}(where: $where, order_by: $order) {
}(where: $where, order_by: $order, limit: $limit) {
${attributes}
}
}
`

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

export const exist = async (hashOrNumber: string | number) => {
const result = await internal_get<BlockAggregateResponse>(
'query',
'evm_block',
'$where: evm_block_bool_exp!, $order: [evm_block_order_by!]',
'$where: evm_block_bool_exp!, $order: [evm_block_order_by!], $limit: Int',
{
[typeof hashOrNumber === 'string' ? 'hash' : 'number']: {
_eq: hashOrNumber
}
},
null,
null,
'aggregate { count }',
'aggregate'
)
Expand All @@ -83,9 +86,10 @@ const get = async (
const result = await internal_get<BlockResponse>(
'query',
'evm_block',
'$where: evm_block_bool_exp!, $order: [evm_block_order_by!]',
'$where: evm_block_bool_exp!, $order: [evm_block_order_by!], $limit: Int',
where,
order,
!many ? 1 : null,
'hash, gas_used, transactions, number, timestamp'
)

Expand Down
16 changes: 11 additions & 5 deletions hapi-evm/src/models/transaction/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,33 @@ const internal_get = async <T>(
// TODO: not only accept where but also additional content
// such as limit, order, etc
where: object,
limit: number | null,
attributes: string,
operation?: string
): Promise<T> => {
const gqlObj = gql`
${type} (${parameters}) {
${table}${operation ? `_${operation}` : ''}(where: $where) {
${table}${
operation ? `_${operation}` : ''
}(where: $where, limit: $limit) {
${attributes}
}
}
`

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

export const exist = async (hash: string) => {
const result = await internal_get<TransactionAggregateResponse>(
Operation.query,
'evm_transaction',
'$where: evm_transaction_bool_exp!',
'$where: evm_transaction_bool_exp!, $limit: Int',
{ hash: { _eq: hash } },
null,
'aggregate { count }',
'aggregate'
)
Expand All @@ -63,9 +68,10 @@ const get = async (where: object, many = false) => {
const result = await internal_get<TransactionResponse>(
'query',
'evm_transaction',
'$where: evm_transaction_bool_exp!',
'$where: evm_transaction_bool_exp!, $limit: Int',
where,
'hash, gas_used, transactions, number, timestamp'
!many ? 1 : null,
'hash, block_hash, block_number, gas, gas_price'
)

return many ? result.evm_transaction : result.evm_transaction[0]
Expand Down

0 comments on commit 1971f50

Please sign in to comment.