Add functions for contracts to Explorer.Chain

Fix chain functions
pull/6324/head
sl1depengwyn 2 years ago
parent 25e4a0e7f7
commit 628d601c14
  1. 109
      apps/explorer/lib/explorer/chain.ex

@ -4359,6 +4359,14 @@ defmodule Explorer.Chain do
|> limit(^paging_options.page_size) |> limit(^paging_options.page_size)
end end
defp handle_verified_contracts_paging_options(query, nil), do: query
defp handle_verified_contracts_paging_options(query, paging_options) do
query
|> page_verified_contracts(paging_options)
|> limit(^paging_options.page_size)
end
defp handle_token_transfer_paging_options(query, nil), do: query defp handle_token_transfer_paging_options(query, nil), do: query
defp handle_token_transfer_paging_options(query, paging_options) do defp handle_token_transfer_paging_options(query, paging_options) do
@ -4606,6 +4614,12 @@ defmodule Explorer.Chain do
) )
end end
defp page_verified_contracts(query, %PagingOptions{key: nil}), do: query
defp page_verified_contracts(query, %PagingOptions{key: {inserted_at}}) do
where(query, [contract], contract.inserted_at < ^inserted_at)
end
@doc """ @doc """
Ensures the following conditions are true: Ensures the following conditions are true:
@ -6479,4 +6493,99 @@ defmodule Explorer.Chain do
def filter_token_creation_dynamic(dynamic) do def filter_token_creation_dynamic(dynamic) do
dynamic([tx, created_token: created_token], ^dynamic or (is_nil(tx.to_address_hash) and not is_nil(created_token))) dynamic([tx, created_token: created_token], ^dynamic or (is_nil(tx.to_address_hash) and not is_nil(created_token)))
end end
def verified_contracts(options \\ []) do
necessity_by_association = Keyword.get(options, :necessity_by_association, %{})
paging_options = Keyword.get(options, :paging_options, @default_paging_options)
filter = Keyword.get(options, :filter, nil)
search_string = Keyword.get(options, :search, nil)
query = from(contract in SmartContract, select: contract, order_by: [desc: :id])
query
|> filter_contracts(filter)
|> search_contracts(search_string)
|> handle_verified_contracts_paging_options(paging_options)
|> join_associations(necessity_by_association)
|> Repo.all()
end
defp search_contracts(basic_query, nil), do: basic_query
defp search_contracts(basic_query, search_string) do
from(contract in basic_query,
where:
ilike(contract.name, ^"%#{search_string}%") or
ilike(fragment("'0x' || encode(?, 'hex')", contract.address_hash), ^"%#{search_string}%")
)
end
defp filter_contracts(basic_query, :solidity) do
basic_query
|> where(is_vyper_contract: ^false)
end
defp filter_contracts(basic_query, :vyper) do
basic_query
|> where(is_vyper_contract: ^true)
end
defp filter_contracts(basic_query, _), do: basic_query
def count_verified_contracts do
Repo.aggregate(SmartContract, :count)
end
def count_new_verified_contracts do
query =
from(contract in SmartContract,
select: contract.inserted_at,
where: fragment("NOW() - ? at time zone 'UTC' <= interval '24 hours'", contract.inserted_at)
)
query
|> Repo.aggregate(:count)
end
def count_contracts do
query =
from(address in Address,
select: address,
where: not is_nil(address.contract_code)
)
query
|> Repo.aggregate(:count)
end
def count_new_contracts do
query =
from(tx in Transaction,
select: tx,
where:
tx.status == ^:ok and
fragment("NOW() - ? at time zone 'UTC' <= interval '24 hours'", tx.created_contract_code_indexed_at)
)
query
|> Repo.aggregate(:count)
end
def count_verified_contracts_from_cache do
ContractsCounter.fetch("verified")
end
def count_new_verified_contracts_from_cache do
ContractsCounter.fetch("new_verified")
end
def count_contracts_from_cache do
ContractsCounter.fetch("all")
end
def count_new_contracts_from_cache do
ContractsCounter.fetch("new")
end
end end

Loading…
Cancel
Save