-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
address_transaction_controller.ex
275 lines (231 loc) · 8.41 KB
/
address_transaction_controller.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
defmodule BlockScoutWeb.AddressTransactionController do
@moduledoc """
Display all the Transactions that terminate at this Address.
"""
use BlockScoutWeb, :controller
import BlockScoutWeb.Account.AuthController, only: [current_user: 1]
import BlockScoutWeb.Chain, only: [current_filter: 1, paging_options: 1, next_page_params: 3, split_list_by_page: 1]
import BlockScoutWeb.Models.GetAddressTags, only: [get_address_tags: 2]
alias BlockScoutWeb.{AccessHelper, Controller, TransactionView}
alias Explorer.{Chain, Market}
alias Explorer.Chain.CSVExport.{
AddressInternalTransactionCsvExporter,
AddressLogCsvExporter,
AddressTokenTransferCsvExporter,
AddressTransactionCsvExporter
}
alias Explorer.Chain.Wei
alias Indexer.Fetcher.CoinBalanceOnDemand
alias Phoenix.View
alias Plug.Conn
@transaction_necessity_by_association [
necessity_by_association: %{
[created_contract_address: :names] => :optional,
[from_address: :names] => :optional,
[to_address: :names] => :optional,
:block => :optional,
[created_contract_address: :smart_contract] => :optional,
[from_address: :smart_contract] => :optional,
[to_address: :smart_contract] => :optional
}
]
{:ok, burn_address_hash} = Chain.string_to_address_hash("0x0000000000000000000000000000000000000000")
@burn_address_hash burn_address_hash
def index(conn, %{"address_id" => address_hash_string, "type" => "JSON"} = params) do
address_options = [necessity_by_association: %{:names => :optional, :smart_contract => :optional}]
with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string),
{:ok, address} <- Chain.hash_to_address(address_hash, address_options, false),
{:ok, false} <- AccessHelper.restricted_access?(address_hash_string, params) do
options =
@transaction_necessity_by_association
|> Keyword.merge(paging_options(params))
|> Keyword.merge(current_filter(params))
results_plus_one = Chain.address_to_transactions_with_rewards(address_hash, options)
{results, next_page} = split_list_by_page(results_plus_one)
next_page_url =
case next_page_params(next_page, results, params) do
nil ->
nil
next_page_params ->
address_transaction_path(
conn,
:index,
address,
Map.delete(next_page_params, "type")
)
end
items_json =
Enum.map(results, fn result ->
case result do
{%Chain.Block.Reward{} = emission_reward, %Chain.Block.Reward{} = validator_reward} ->
View.render_to_string(
TransactionView,
"_emission_reward_tile.html",
current_address: address,
emission_funds: emission_reward,
validator: validator_reward
)
%Chain.Transaction{} = transaction ->
View.render_to_string(
TransactionView,
"_tile.html",
conn: conn,
current_address: address,
transaction: transaction,
burn_address_hash: @burn_address_hash
)
end
end)
json(conn, %{items: items_json, next_page_path: next_page_url})
else
:error ->
unprocessable_entity(conn)
{:restricted_access, _} ->
not_found(conn)
{:error, :not_found} ->
case Chain.Hash.Address.validate(address_hash_string) do
{:ok, _} ->
json(conn, %{items: [], next_page_path: ""})
_ ->
not_found(conn)
end
end
end
def index(conn, %{"address_id" => address_hash_string} = params) do
with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string),
{:ok, address} <- Chain.hash_to_address(address_hash),
{:ok, false} <- AccessHelper.restricted_access?(address_hash_string, params) do
render(
conn,
"index.html",
address: address,
coin_balance_status: CoinBalanceOnDemand.trigger_fetch(address),
exchange_rate: Market.get_coin_exchange_rate(),
filter: params["filter"],
counters_path: address_path(conn, :address_counters, %{"id" => address_hash_string}),
current_path: Controller.current_full_path(conn),
tags: get_address_tags(address_hash, current_user(conn))
)
else
:error ->
unprocessable_entity(conn)
{:restricted_access, _} ->
not_found(conn)
{:error, :not_found} ->
{:ok, address_hash} = Chain.string_to_address_hash(address_hash_string)
address = %Chain.Address{
hash: address_hash,
smart_contract: nil,
token: nil,
fetched_coin_balance: %Wei{value: Decimal.new(0)}
}
case Chain.Hash.Address.validate(address_hash_string) do
{:ok, _} ->
render(
conn,
"index.html",
address: address,
coin_balance_status: nil,
exchange_rate: Market.get_coin_exchange_rate(),
filter: params["filter"],
counters_path: address_path(conn, :address_counters, %{"id" => address_hash_string}),
current_path: Controller.current_full_path(conn),
tags: get_address_tags(address_hash, current_user(conn))
)
_ ->
not_found(conn)
end
end
end
defp captcha_helper do
:block_scout_web
|> Application.get_env(:captcha_helper)
end
defp put_resp_params(conn) do
conn
|> put_resp_content_type("application/csv")
|> put_resp_header("content-disposition", "attachment;")
|> put_resp_cookie("csv-downloaded", "true", max_age: 86_400, http_only: false)
|> send_chunked(200)
end
defp items_csv(
conn,
%{
"address_id" => address_hash_string,
"from_period" => from_period,
"to_period" => to_period,
"recaptcha_response" => recaptcha_response
} = params,
csv_export_module
)
when is_binary(address_hash_string) do
filter_type = Map.get(params, "filter_type")
filter_value = Map.get(params, "filter_value")
with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string),
{:ok, address} <- Chain.hash_to_address(address_hash),
{:recaptcha, true} <- {:recaptcha, captcha_helper().recaptcha_passed?(recaptcha_response)} do
address
|> csv_export_module.export(from_period, to_period, filter_type, filter_value)
|> Enum.reduce_while(put_resp_params(conn), fn chunk, conn ->
case Conn.chunk(conn, chunk) do
{:ok, conn} ->
{:cont, conn}
{:error, :closed} ->
{:halt, conn}
end
end)
else
:error ->
unprocessable_entity(conn)
{:error, :not_found} ->
not_found(conn)
{:recaptcha, false} ->
not_found(conn)
end
end
defp items_csv(
conn,
%{
"address_id" => address_hash_string,
"from_period" => from_period,
"to_period" => to_period
},
csv_export_module
)
when is_binary(address_hash_string) do
with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string),
{:ok, address} <- Chain.hash_to_address(address_hash),
true <- Application.get_env(:block_scout_web, :recaptcha)[:is_disabled] do
address
|> csv_export_module.export(from_period, to_period)
|> Enum.reduce_while(put_resp_params(conn), fn chunk, conn ->
case Conn.chunk(conn, chunk) do
{:ok, conn} ->
{:cont, conn}
{:error, :closed} ->
{:halt, conn}
end
end)
else
:error ->
unprocessable_entity(conn)
{:error, :not_found} ->
not_found(conn)
false ->
not_found(conn)
end
end
defp items_csv(conn, _, _), do: not_found(conn)
def token_transfers_csv(conn, params) do
items_csv(conn, params, AddressTokenTransferCsvExporter)
end
def transactions_csv(conn, params) do
items_csv(conn, params, AddressTransactionCsvExporter)
end
def internal_transactions_csv(conn, params) do
items_csv(conn, params, AddressInternalTransactionCsvExporter)
end
def logs_csv(conn, params) do
items_csv(conn, params, AddressLogCsvExporter)
end
end