hugobarauna commited on
Commit
7c193af
1 Parent(s): 15781db

Update public-apps/helius_transaction_render.livemd

Browse files
public-apps/helius_transaction_render.livemd CHANGED
@@ -22,16 +22,26 @@ defmodule HeliusFetch do
22
  def fetch_transaction(signature, api_key) do
23
  transactions_url = "https://api.helius.xyz/v0/transactions"
24
 
25
- Req.post!(
26
- transactions_url,
27
- params: ["api-key": api_key],
28
- json: %{transactions: [signature]}
29
- ).body
30
- |> List.first()
 
 
 
 
 
 
 
 
31
  end
32
  end
 
33
 
34
- Kino.nothing()
 
35
  ```
36
 
37
  ````elixir
@@ -49,7 +59,7 @@ defmodule TransactionRender do
49
  description = transaction["description"]
50
  fee_payer = transaction["feePayer"]
51
 
52
- Kino.Markdown.new("""
53
  **Source**: #{source}
54
 
55
  **Type**: #{type}
@@ -61,7 +71,7 @@ defmodule TransactionRender do
61
  end
62
 
63
  defp render_event(name, event) do
64
- Kino.Markdown.new("""
65
  ### #{name}
66
 
67
  ```json
@@ -75,7 +85,7 @@ defmodule TransactionRender do
75
 
76
  case events do
77
  %{} ->
78
- Kino.Text.new("No events")
79
 
80
  _ ->
81
  events
@@ -108,7 +118,7 @@ defmodule TransactionRender do
108
 
109
  case native_transfers do
110
  [] ->
111
- Kino.Text.new("No native transfers")
112
 
113
  _ ->
114
  diagram_lines =
@@ -122,7 +132,7 @@ defmodule TransactionRender do
122
  #{diagram_lines}
123
  """
124
 
125
- Kino.Mermaid.new(diagram)
126
  end
127
  end
128
 
@@ -150,7 +160,7 @@ defmodule TransactionRender do
150
 
151
  case token_transfers do
152
  [] ->
153
- Kino.Text.new("No token transfers")
154
 
155
  _ ->
156
  diagram_lines =
@@ -163,14 +173,14 @@ defmodule TransactionRender do
163
  #{diagram_lines}
164
  """
165
 
166
- Kino.Mermaid.new(diagram)
167
  end
168
  end
169
 
170
  def render(transaction) do
171
  Kino.Layout.tabs(
172
  Summary: render_summary(transaction),
173
- Tree: Kino.Tree.new(transaction),
174
  Events: render_events(transaction),
175
  "Native Transfers": render_native_transfers(transaction),
176
  "Token Transfers": render_token_transfers(transaction)
@@ -179,15 +189,17 @@ defmodule TransactionRender do
179
  end
180
  ````
181
 
182
- <!-- livebook:{"branch_parent_index":0} -->
183
-
184
  ## Fetch a transaction
185
 
186
  ```elixir
187
  form =
188
  Kino.Control.form(
189
  [
190
- signature: Kino.Input.text("Transaction Signature"),
 
 
 
 
191
  api_key: Kino.Input.password("Helius API Key")
192
  ],
193
  submit: "Fetch"
@@ -195,7 +207,7 @@ form =
195
 
196
  form |> Kino.render()
197
 
198
- frame = Kino.Frame.new()
199
  ```
200
 
201
  This next code block does all the magic
@@ -206,23 +218,31 @@ You just need to evaluate it :)
206
  Kino.listen(form, fn event ->
207
  signature_length = byte_size(event.data.signature)
208
  api_key_length = byte_size(event.data.api_key)
 
209
 
210
  case {signature_length, api_key_length} do
211
  {0, _} ->
212
  Kino.Frame.render(
213
  frame,
214
- Kino.Markdown.new("**No transaction signature given**")
 
215
  )
216
 
217
  {_, 0} ->
218
  Kino.Frame.render(
219
  frame,
220
- Kino.Markdown.new("**No Helius API key given**")
 
221
  )
222
 
223
  _ ->
224
- transaction = HeliusFetch.fetch_transaction(event.data.signature, event.data.api_key)
225
- Kino.Frame.render(frame, TransactionRender.render(transaction))
 
 
 
 
 
226
  end
227
  end)
228
  ```
 
22
  def fetch_transaction(signature, api_key) do
23
  transactions_url = "https://api.helius.xyz/v0/transactions"
24
 
25
+ response =
26
+ Req.post!(
27
+ transactions_url,
28
+ params: ["api-key": api_key],
29
+ json: %{transactions: [signature]}
30
+ )
31
+
32
+ case response do
33
+ %Req.Response{status: 200, body: body} ->
34
+ {:ok, List.first(body)}
35
+
36
+ %Req.Response{body: body} ->
37
+ {:error, body}
38
+ end
39
  end
40
  end
41
+ ```
42
 
43
+ ```elixir
44
+ import Kino.Shorts
45
  ```
46
 
47
  ````elixir
 
59
  description = transaction["description"]
60
  fee_payer = transaction["feePayer"]
61
 
62
+ markdown("""
63
  **Source**: #{source}
64
 
65
  **Type**: #{type}
 
71
  end
72
 
73
  defp render_event(name, event) do
74
+ markdown("""
75
  ### #{name}
76
 
77
  ```json
 
85
 
86
  case events do
87
  %{} ->
88
+ text("No events")
89
 
90
  _ ->
91
  events
 
118
 
119
  case native_transfers do
120
  [] ->
121
+ text("No native transfers")
122
 
123
  _ ->
124
  diagram_lines =
 
132
  #{diagram_lines}
133
  """
134
 
135
+ mermaid(diagram)
136
  end
137
  end
138
 
 
160
 
161
  case token_transfers do
162
  [] ->
163
+ text("No token transfers")
164
 
165
  _ ->
166
  diagram_lines =
 
173
  #{diagram_lines}
174
  """
175
 
176
+ mermaid(diagram)
177
  end
178
  end
179
 
180
  def render(transaction) do
181
  Kino.Layout.tabs(
182
  Summary: render_summary(transaction),
183
+ Tree: tree(transaction),
184
  Events: render_events(transaction),
185
  "Native Transfers": render_native_transfers(transaction),
186
  "Token Transfers": render_token_transfers(transaction)
 
189
  end
190
  ````
191
 
 
 
192
  ## Fetch a transaction
193
 
194
  ```elixir
195
  form =
196
  Kino.Control.form(
197
  [
198
+ signature:
199
+ Kino.Input.text("Transaction Signature",
200
+ default:
201
+ "5r4xyeKUJkagGvNGpzKd7rE2LuxPJfhZfiub7JrqS28gFHY2Z18H557srUCPbiJQErW2XA4xBoZGQpLjDE8wyFs4"
202
+ ),
203
  api_key: Kino.Input.password("Helius API Key")
204
  ],
205
  submit: "Fetch"
 
207
 
208
  form |> Kino.render()
209
 
210
+ frame = frame()
211
  ```
212
 
213
  This next code block does all the magic
 
218
  Kino.listen(form, fn event ->
219
  signature_length = byte_size(event.data.signature)
220
  api_key_length = byte_size(event.data.api_key)
221
+ origin = event.origin
222
 
223
  case {signature_length, api_key_length} do
224
  {0, _} ->
225
  Kino.Frame.render(
226
  frame,
227
+ Kino.Markdown.new("**No transaction signature given**"),
228
+ to: origin
229
  )
230
 
231
  {_, 0} ->
232
  Kino.Frame.render(
233
  frame,
234
+ Kino.Markdown.new("**No Helius API key given**"),
235
+ to: origin
236
  )
237
 
238
  _ ->
239
+ case HeliusFetch.fetch_transaction(event.data.signature, event.data.api_key) do
240
+ {:ok, transaction} ->
241
+ Kino.Frame.render(frame, TransactionRender.render(transaction), to: origin)
242
+
243
+ {:error, error} ->
244
+ Kino.Frame.render(frame, markdown("*Error*: #{inspect(error)}"), to: origin)
245
+ end
246
  end
247
  end)
248
  ```