Skip to content

Commit

Permalink
Merge pull request #2757 from santiment/extend-top-transfers
Browse files Browse the repository at this point in the history
Extend top transfers
  • Loading branch information
IvanIvanoff committed Jun 11, 2021
2 parents 17f61d1 + c28e0a9 commit b71332c
Show file tree
Hide file tree
Showing 20 changed files with 698 additions and 365 deletions.
2 changes: 1 addition & 1 deletion .iex.exs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ alias SanbaseWeb.Graphql.Resolvers.{
PriceResolver,
ProjectBalanceResolver,
ProjectResolver,
ProjectTransactionsResolver,
ProjectTransfersResolver,
TechIndicatorsResolver,
TwitterResolver,
UserListResolver,
Expand Down
2 changes: 1 addition & 1 deletion lib/sanbase/model/project/list/selector/list_selector.ex
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ defmodule Sanbase.Model.Project.ListSelector do
case Process.get(@cycle_detection_key) do
nil ->
Process.put(@cycle_detection_key, %{
iterations_left: 5,
iterations_left: 20,
args_seen_so_far: MapSet.new([args]),
original_args: args
})
Expand Down
26 changes: 13 additions & 13 deletions lib/sanbase/transfers/btc_transfers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ defmodule Sanbase.Transfers.BtcTransfers do
datetime: Datetime.t()
}

@spec top_transactions(
@spec top_transfers(
%DateTime{},
%DateTime{},
non_neg_integer(),
non_neg_integer(),
list(String.t())
) ::
{:ok, list(transaction)} | {:error, String.t()}
def top_transactions(from, to, page, page_size, excluded_addresses \\ []) do
{query, args} = top_transactions_query(from, to, page, page_size, excluded_addresses)
def top_transfers(from, to, page, page_size, excluded_addresses \\ []) do
{query, args} = top_transfers_query(from, to, page, page_size, excluded_addresses)

Sanbase.ClickhouseRepo.query_transform(
query,
Expand All @@ -35,7 +35,7 @@ defmodule Sanbase.Transfers.BtcTransfers do
)
end

@spec top_wallet_transactions(
@spec top_wallet_transfers(
list(String.t()),
DateTime.t(),
DateTime.t(),
Expand All @@ -44,10 +44,10 @@ defmodule Sanbase.Transfers.BtcTransfers do
String.t()
) ::
{:ok, nil} | {:ok, list(map())} | {:error, String.t()}
def top_wallet_transactions([], _from, _to, _page, _page_size, _type), do: {:ok, []}
def top_wallet_transfers([], _from, _to, _page, _page_size, _type), do: {:ok, []}

def top_wallet_transactions(wallets, from, to, page, page_size, type) do
{query, args} = top_wallet_transactions_query(wallets, from, to, page, page_size, type)
def top_wallet_transfers(wallets, from, to, page, page_size, type) do
{query, args} = top_wallet_transfers_query(wallets, from, to, page, page_size, type)

ClickhouseRepo.query_transform(query, args, fn
[timestamp, address, trx_hash, balance, old_balance, abs_value] ->
Expand All @@ -70,7 +70,7 @@ defmodule Sanbase.Transfers.BtcTransfers do

# Private functions

defp top_wallet_transactions_query(wallets, from, to, page, page_size, type) do
defp top_wallet_transfers_query(wallets, from, to, page, page_size, type) do
query = """
SELECT
toUnixTimestamp(dt),
Expand All @@ -81,7 +81,7 @@ defmodule Sanbase.Transfers.BtcTransfers do
abs(balance - oldBalance) AS absValue
FROM btc_balances FINAL
PREWHERE
#{top_wallet_transactions_address_clause(type, arg_position: 1, trailing_and: true)}
#{top_wallet_transfers_address_clause(type, arg_position: 1, trailing_and: true)}
dt >= toDateTime(?2) AND
dt <= toDateTime(?3)
ORDER BY absValue DESC
Expand All @@ -101,23 +101,23 @@ defmodule Sanbase.Transfers.BtcTransfers do
{query, args}
end

defp top_wallet_transactions_address_clause(:in, opts) do
defp top_wallet_transfers_address_clause(:in, opts) do
arg_position = Keyword.fetch!(opts, :arg_position)
trailing_and = Keyword.fetch!(opts, :trailing_and)

str = "address IN (?#{arg_position}) AND balance > oldBalance"
if trailing_and, do: str <> " AND", else: str
end

defp top_wallet_transactions_address_clause(:out, opts) do
defp top_wallet_transfers_address_clause(:out, opts) do
arg_position = Keyword.fetch!(opts, :arg_position)
trailing_and = Keyword.fetch!(opts, :trailing_and)

str = "address IN (?#{arg_position}) AND balance < oldBalance"
if trailing_and, do: str <> " AND", else: str
end

defp top_wallet_transactions_address_clause(:all, opts) do
defp top_wallet_transfers_address_clause(:all, opts) do
arg_position = Keyword.fetch!(opts, :arg_position)
trailing_and = Keyword.fetch!(opts, :trailing_and)

Expand All @@ -128,7 +128,7 @@ defmodule Sanbase.Transfers.BtcTransfers do
if trailing_and, do: str <> " AND", else: str
end

defp top_transactions_query(from, to, page, page_size, excluded_addresses) do
defp top_transfers_query(from, to, page, page_size, excluded_addresses) do
to_unix = DateTime.to_unix(to)
from_unix = DateTime.to_unix(from)
offset = (page - 1) * page_size
Expand Down
26 changes: 13 additions & 13 deletions lib/sanbase/transfers/erc20_transfers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ defmodule Sanbase.Transfers.Erc20Transfers do
raise "Should not try to change eth daily active addresses"
end

@spec top_wallet_transactions(
@spec top_wallet_transfers(
String.t(),
list(String.t()),
DateTime.t(),
Expand All @@ -60,11 +60,11 @@ defmodule Sanbase.Transfers.Erc20Transfers do
String.t()
) ::
{:ok, list(map())} | {:error, String.t()}
def top_wallet_transactions(_contract, [], _from, _to, _page, _page_size, _type), do: {:ok, []}
def top_wallet_transfers(_contract, [], _from, _to, _page, _page_size, _type), do: {:ok, []}

def top_wallet_transactions(contract, wallets, from, to, decimals, page, page_size, type) do
def top_wallet_transfers(contract, wallets, from, to, decimals, page, page_size, type) do
{query, args} =
top_wallet_transactions_query(contract, wallets, from, to, decimals, page, page_size, type)
top_wallet_transfers_query(contract, wallets, from, to, decimals, page, page_size, type)

ClickhouseRepo.query_transform(query, args, fn
[timestamp, from_address, to_address, trx_hash, trx_value] ->
Expand All @@ -83,7 +83,7 @@ defmodule Sanbase.Transfers.Erc20Transfers do
If the top transactions for SAN token are needed, the SAN contract address must be
provided as a first argument.
"""
@spec top_transactions(
@spec top_transfers(
String.t(),
%DateTime{},
%DateTime{},
Expand All @@ -93,11 +93,11 @@ defmodule Sanbase.Transfers.Erc20Transfers do
list(String.t())
) ::
{:ok, list(t)} | {:error, String.t()}
def top_transactions(contract, from, to, decimals, page, page_size, excluded_addresses \\ []) do
def top_transfers(contract, from, to, decimals, page, page_size, excluded_addresses \\ []) do
decimals = Sanbase.Math.ipow(10, decimals)

{query, args} =
top_transactions_query(
top_transfers_query(
contract,
from,
to,
Expand Down Expand Up @@ -205,7 +205,7 @@ defmodule Sanbase.Transfers.Erc20Transfers do

# Private functions

defp top_wallet_transactions_query(wallets, contract, from, to, decimals, page, page_size, type) do
defp top_wallet_transfers_query(wallets, contract, from, to, decimals, page, page_size, type) do
query = """
SELECT
toUnixTimestamp(dt),
Expand All @@ -215,7 +215,7 @@ defmodule Sanbase.Transfers.Erc20Transfers do
value / ?7
FROM erc20_transfers FINAL
PREWHERE
#{top_wallet_transactions_address_clause(type, arg_position: 1, trailing_and: true)}
#{top_wallet_transfers_address_clause(type, arg_position: 1, trailing_and: true)}
assetRefId = cityHash64('ETH_' || ?2) AND
dt >= toDateTime(?3) AND
dt <= toDateTime(?4) AND
Expand All @@ -239,23 +239,23 @@ defmodule Sanbase.Transfers.Erc20Transfers do
{query, args}
end

defp top_wallet_transactions_address_clause(:in, opts) do
defp top_wallet_transfers_address_clause(:in, opts) do
arg_position = Keyword.fetch!(opts, :arg_position)
trailing_and = Keyword.fetch!(opts, :trailing_and)

str = "from NOT IN (?#{arg_position}) AND to IN (?#{arg_position})"
if trailing_and, do: str <> " AND", else: str
end

defp top_wallet_transactions_address_clause(:out, opts) do
defp top_wallet_transfers_address_clause(:out, opts) do
arg_position = Keyword.fetch!(opts, :arg_position)
trailing_and = Keyword.fetch!(opts, :trailing_and)

str = "from IN (?#{arg_position}) AND to NOT IN (?#{arg_position})"
if trailing_and, do: str <> " AND", else: str
end

defp top_wallet_transactions_address_clause(:all, opts) do
defp top_wallet_transfers_address_clause(:all, opts) do
arg_position = Keyword.fetch!(opts, :arg_position)
trailing_and = Keyword.fetch!(opts, :trailing_and)

Expand All @@ -269,7 +269,7 @@ defmodule Sanbase.Transfers.Erc20Transfers do
if trailing_and, do: str <> " AND", else: str
end

defp top_transactions_query(contract, from, to, page, page_size, decimals, excluded_addresses) do
defp top_transfers_query(contract, from, to, page, page_size, decimals, excluded_addresses) do
query = """
SELECT
toUnixTimestamp(dt) AS datetime,
Expand Down
26 changes: 13 additions & 13 deletions lib/sanbase/transfers/eth_transfers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ defmodule Sanbase.Transfers.EthTransfers do
The `type` argument control wheteher only incoming, outgoing or all transactions
are included.
"""
@spec top_wallet_transactions(
@spec top_wallet_transfers(
list(String.t()),
DateTime.t(),
DateTime.t(),
Expand All @@ -60,10 +60,10 @@ defmodule Sanbase.Transfers.EthTransfers do
String.t()
) ::
{:ok, nil} | {:ok, list(map())} | {:error, String.t()}
def top_wallet_transactions([], _from, _to, _page, _page_size, _type), do: {:ok, []}
def top_wallet_transfers([], _from, _to, _page, _page_size, _type), do: {:ok, []}

def top_wallet_transactions(wallets, from, to, page, page_size, type) do
{query, args} = top_wallet_transactions_query(wallets, from, to, page, page_size, type)
def top_wallet_transfers(wallets, from, to, page, page_size, type) do
{query, args} = top_wallet_transfers_query(wallets, from, to, page, page_size, type)

ClickhouseRepo.query_transform(query, args, fn
[timestamp, from_address, to_address, trx_hash, trx_value] ->
Expand All @@ -77,10 +77,10 @@ defmodule Sanbase.Transfers.EthTransfers do
end)
end

@spec top_transactions(%DateTime{}, %DateTime{}, non_neg_integer(), non_neg_integer()) ::
@spec top_transfers(%DateTime{}, %DateTime{}, non_neg_integer(), non_neg_integer()) ::
{:ok, list(t)} | {:error, String.t()}
def top_transactions(from, to, page, page_size) do
{query, args} = top_transactions_query(from, to, page, page_size)
def top_transfers(from, to, page, page_size) do
{query, args} = top_transfers_query(from, to, page, page_size)

ClickhouseRepo.query_transform(query, args, fn
[timestamp, from_address, to_address, trx_hash, trx_value] ->
Expand Down Expand Up @@ -117,7 +117,7 @@ defmodule Sanbase.Transfers.EthTransfers do

# Private functions

defp top_wallet_transactions_query(wallets, from, to, page, page_size, type) do
defp top_wallet_transfers_query(wallets, from, to, page, page_size, type) do
query = """
SELECT
toUnixTimestamp(dt),
Expand All @@ -127,7 +127,7 @@ defmodule Sanbase.Transfers.EthTransfers do
value / #{@eth_decimals}
FROM #{@table} FINAL
PREWHERE
#{top_wallet_transactions_address_clause(type, arg_position: 1, trailing_and: true)}
#{top_wallet_transfers_address_clause(type, arg_position: 1, trailing_and: true)}
dt >= toDateTime(?2) AND
dt <= toDateTime(?3) AND
type = 'call'
Expand All @@ -148,23 +148,23 @@ defmodule Sanbase.Transfers.EthTransfers do
{query, args}
end

defp top_wallet_transactions_address_clause(:in, opts) do
defp top_wallet_transfers_address_clause(:in, opts) do
arg_position = Keyword.fetch!(opts, :arg_position)
trailing_and = Keyword.fetch!(opts, :trailing_and)

str = "from NOT IN (?#{arg_position}) AND to IN (?#{arg_position})"
if trailing_and, do: str <> " AND", else: str
end

defp top_wallet_transactions_address_clause(:out, opts) do
defp top_wallet_transfers_address_clause(:out, opts) do
arg_position = Keyword.fetch!(opts, :arg_position)
trailing_and = Keyword.fetch!(opts, :trailing_and)

str = "from IN (?#{arg_position}) AND to NOT IN (?#{arg_position})"
if trailing_and, do: str <> " AND", else: str
end

defp top_wallet_transactions_address_clause(:all, opts) do
defp top_wallet_transfers_address_clause(:all, opts) do
arg_position = Keyword.fetch!(opts, :arg_position)
trailing_and = Keyword.fetch!(opts, :trailing_and)

Expand All @@ -178,7 +178,7 @@ defmodule Sanbase.Transfers.EthTransfers do
if trailing_and, do: str <> " AND", else: str
end

defp top_transactions_query(from, to, page, page_size) do
defp top_transfers_query(from, to, page, page_size) do
from_unix = DateTime.to_unix(from)
to_unix = DateTime.to_unix(to)

Expand Down
18 changes: 9 additions & 9 deletions lib/sanbase/transfers/transfers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ defmodule Sanbase.Transfers do

alias Sanbase.Transfers.{EthTransfers, Erc20Transfers, BtcTransfers}

def top_wallet_transactions(slug, address, from, to, page, page_size, transaction_type) do
def top_wallet_transfers(slug, address, from, to, page, page_size, transaction_type) do
case Project.contract_info_infrastructure_by_slug(slug) do
{:ok, "BTC", _, _} ->
List.wrap(address)
|> BtcTransfers.top_wallet_transactions(from, to, page, page_size, transaction_type)
|> BtcTransfers.top_wallet_transfers(from, to, page, page_size, transaction_type)

{:ok, "ETH", _, _} ->
List.wrap(address)
|> Enum.map(&Sanbase.BlockchainAddress.to_internal_format/1)
|> EthTransfers.top_wallet_transactions(from, to, page, page_size, transaction_type)
|> EthTransfers.top_wallet_transfers(from, to, page, page_size, transaction_type)

{:ok, contract, decimals, "ETH"} ->
List.wrap(address)
|> Enum.map(&Sanbase.BlockchainAddress.to_internal_format/1)
|> Erc20Transfers.top_wallet_transactions(
|> Erc20Transfers.top_wallet_transfers(
contract,
from,
to,
Expand All @@ -39,21 +39,21 @@ defmodule Sanbase.Transfers do
end
end

def top_transactions(slug, from, to, page, page_size, opts \\ [])
def top_transfers(slug, from, to, page, page_size, opts \\ [])

def top_transactions(slug, from, to, page, page_size, opts) do
def top_transfers(slug, from, to, page, page_size, opts) do
excluded_addresses = Keyword.get(opts, :excluded_addresses, [])

case Project.contract_info_infrastructure_by_slug(slug) do
{:ok, "ETH", _, _} ->
EthTransfers.top_transactions(from, to, page, page_size)
EthTransfers.top_transfers(from, to, page, page_size)

{:ok, "BTC", _, _} ->
excluded_addresses = Keyword.get(opts, :excluded_addresses, [])
BtcTransfers.top_transactions(from, to, page, page_size, excluded_addresses)
BtcTransfers.top_transfers(from, to, page, page_size, excluded_addresses)

{:ok, contract, decimals, "ETH"} ->
Erc20Transfers.top_transactions(
Erc20Transfers.top_transfers(
contract,
from,
to,
Expand Down
Loading

0 comments on commit b71332c

Please sign in to comment.