Skip to content

Commit

Permalink
Merge pull request #15 from protofire/extend-csv
Browse files Browse the repository at this point in the history
Extend csv
  • Loading branch information
DenSmolonski authored Sep 18, 2024
2 parents 1b59098 + b4f4e21 commit cced4d3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
16 changes: 16 additions & 0 deletions apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/utility/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,20 @@ defmodule EthereumJSONRPC.Utility.Bech do
"0x" <> hex_address
end
end

@doc """
Encodes a hexadecimal value to a Bech32-encoded string with the prefix 'one1'.
## Examples
iex> EthereumJSONRPC.Utility.Bech.encode_bech_32("0x6162636465")
"one1abcde"
"""
def encode_bech_32(hex_value) do
hex_value = String.replace_prefix(hex_value, "0x", "")
binary = :binary.decode_hex(hex_value)
fiveBitBinary = Bech32.convertbits(binary, 8, 5, false)
if is_binary(fiveBitBinary), do: Bech32.encode_from_5bit("one", fiveBitBinary), else: "Invalid hex value"
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule Explorer.Chain.CSVExport.AddressTransactionCsvExporter do
alias Explorer.Market.MarketHistory
alias Explorer.Chain.{Address, DenormalizationHelper, Hash, Transaction, Wei}
alias Explorer.Chain.CSVExport.Helper

alias EthereumJSONRPC.Utility.Bech
@paging_options %PagingOptions{page_size: Helper.limit()}

@spec export(Hash.Address.t(), String.t(), String.t(), String.t() | nil, String.t() | nil) :: Enumerable.t()
Expand Down Expand Up @@ -48,10 +48,13 @@ defmodule Explorer.Chain.CSVExport.AddressTransactionCsvExporter do
"BlockNumber",
"UnixTimestamp",
"FromAddress",
"FromAddressOne",
"ToAddress",
"ToAddressOne",
"ContractAddress",
"Type",
"Value",
"Value in USD",
"Fee",
"Status",
"ErrCode",
Expand All @@ -70,21 +73,24 @@ defmodule Explorer.Chain.CSVExport.AddressTransactionCsvExporter do
Map.put(acc, date, price_at_date(date))
end
end)

transaction_lists =
transactions
|> Stream.map(fn transaction ->
{opening_price, closing_price} = date_to_prices[DateTime.to_date(Transaction.block_timestamp(transaction))]

price = exchange_rate.usd_value || closing_price || opening_price || Decimal.new("0")
total_value_usd = Wei.mult(transaction.value, price)
[
to_string(transaction.hash),
transaction.block_number,
Transaction.block_timestamp(transaction),
Address.checksum(transaction.from_address_hash),
Address.checksum(transaction.from_address_hash) |> to_string |> Bech.encode_bech_32,
Address.checksum(transaction.to_address_hash),
Address.checksum(transaction.to_address_hash) |> to_string |> Bech.encode_bech_32,
Address.checksum(transaction.created_contract_address_hash),
type(transaction, address_hash),
Wei.to(transaction.value, :wei),
Wei.to(total_value_usd, :wei),
fee(transaction),
transaction.status,
transaction.error,
Expand Down
5 changes: 5 additions & 0 deletions apps/explorer/lib/explorer/chain/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,11 @@ defmodule Explorer.Chain.Transaction do
],
boolean()
) :: [__MODULE__.t()]
@spec address_to_transactions_without_rewards(Explorer.Chain.Hash.t(), [
{:necessity_by_association, %{optional(atom()) => :optional | :required}}
| {:paging_options, Explorer.PagingOptions.t()}
| {:sorting, [{any(), any()} | {any(), any(), any()} | {any(), any(), any(), any()}]}
]) :: list()
def address_to_transactions_without_rewards(address_hash, options, old_ui? \\ true) do
paging_options = Keyword.get(options, :paging_options, Chain.default_paging_options())

Expand Down
3 changes: 2 additions & 1 deletion apps/explorer/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ defmodule Explorer.Mixfile do
{:cloak_ecto, "~> 1.2.0"},
{:redix, "~> 1.1"},
{:hammer_backend_redis, "~> 6.1"},
{:logger_json, "~> 5.1"}
{:logger_json, "~> 5.1"},
{:bech32, "~> 1.0"}
]
end

Expand Down

0 comments on commit cced4d3

Please sign in to comment.