Spaces:
Running
Running
File size: 5,611 Bytes
1477b95 15781db 7c193af 15781db 7c193af 15781db 7c193af 15781db 7c193af 15781db 7c193af 15781db 7c193af 15781db 7c193af 15781db 7c193af 15781db 7c193af 15781db 7c193af 15781db 7c193af 15781db 7c193af 15781db 7c193af 15781db 7c193af 15781db 7c193af 15781db 7c193af 15781db 7c193af 15781db |
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 |
<!-- livebook:{"app_settings":{"access_type":"public","output_type":"rich","show_source":true,"slug":"helius-transaction-render"}} -->
# Helius Transaction Render
```elixir
Mix.install([
{:req, "~> 0.3.4"},
{:jason, "~> 1.4.0"},
{:kino, "~> 0.12.3"}
])
```
## Code Setup
This section includes all the Elixir code to fetch and render the given transaction
You don't need to edit any of it
```elixir
# Define transaction fetching logic
defmodule HeliusFetch do
def fetch_transaction(signature, api_key) do
transactions_url = "https://api.helius.xyz/v0/transactions"
response =
Req.post!(
transactions_url,
params: ["api-key": api_key],
json: %{transactions: [signature]}
)
case response do
%Req.Response{status: 200, body: body} ->
{:ok, List.first(body)}
%Req.Response{body: body} ->
{:error, body}
end
end
end
```
```elixir
import Kino.Shorts
```
````elixir
# Define transaction rendering logic
defmodule TransactionRender do
defp truncate(string, length) do
start = String.slice(string, 0, length)
last = String.slice(string, 0 - length, length)
start <> "..." <> last
end
defp render_summary(transaction) do
source = transaction["source"]
type = transaction["type"]
description = transaction["description"]
fee_payer = transaction["feePayer"]
markdown("""
**Source**: #{source}
**Type**: #{type}
**Description**: #{description}
**Fee Payer**: [#{truncate(fee_payer, 8)}](https://explorer.solana.com/address/#{fee_payer})
""")
end
defp render_event(name, event) do
markdown("""
### #{name}
```json
#{Jason.encode!(event, pretty: true)}
```
""")
end
defp render_events(transaction) do
events = transaction["events"]
case events do
%{} ->
text("No events")
_ ->
events
|> Enum.map(fn {name, event} -> render_event(name, event) end)
|> Kino.Layout.grid()
end
end
defp native_transfer_diagram_line(transfer) do
from =
case transfer["fromUserAccount"] do
"" -> "none"
address -> truncate(address, 4)
end
to =
case transfer["toUserAccount"] do
"" -> "none"
address -> truncate(address, 4)
end
amount = (transfer["amount"] / 1_000_000_000) |> Float.round(4)
label = "#{amount} SOL"
# Mermaid diagram line starting with 2 spaces
" #{from}-...->|#{label}|#{to}"
end
defp render_native_transfers(transaction) do
native_transfers = transaction["nativeTransfers"]
case native_transfers do
[] ->
text("No native transfers")
_ ->
diagram_lines =
native_transfers
|> Enum.filter(fn transfer -> transfer["amount"] > 0 end)
|> Enum.map(fn transfer -> native_transfer_diagram_line(transfer) end)
|> Enum.join("\n")
diagram = """
flowchart LR
#{diagram_lines}
"""
mermaid(diagram)
end
end
defp token_transfer_diagram_line(transfer) do
from =
case transfer["fromUserAccount"] do
"" -> "none"
address -> truncate(address, 4)
end
to =
case transfer["toUserAccount"] do
"" -> "none"
address -> truncate(address, 4)
end
token_amount = transfer["tokenAmount"]
mint = truncate(transfer["mint"], 4)
label = "#{token_amount} #{mint}"
" #{from}-...->|#{label}|#{to}"
end
defp render_token_transfers(transaction) do
token_transfers = transaction["tokenTransfers"]
case token_transfers do
[] ->
text("No token transfers")
_ ->
diagram_lines =
token_transfers
|> Enum.map(fn transfer -> token_transfer_diagram_line(transfer) end)
|> Enum.join("\n")
diagram = """
flowchart LR
#{diagram_lines}
"""
mermaid(diagram)
end
end
def render(transaction) do
Kino.Layout.tabs(
Summary: render_summary(transaction),
Tree: tree(transaction),
Events: render_events(transaction),
"Native Transfers": render_native_transfers(transaction),
"Token Transfers": render_token_transfers(transaction)
)
end
end
````
## Fetch a transaction
```elixir
form =
Kino.Control.form(
[
signature:
Kino.Input.text("Transaction Signature",
default:
"5r4xyeKUJkagGvNGpzKd7rE2LuxPJfhZfiub7JrqS28gFHY2Z18H557srUCPbiJQErW2XA4xBoZGQpLjDE8wyFs4"
),
api_key: Kino.Input.password("Helius API Key")
],
submit: "Fetch"
)
form |> Kino.render()
frame = frame()
```
This next code block does all the magic
You just need to evaluate it :)
```elixir
Kino.listen(form, fn event ->
signature_length = byte_size(event.data.signature)
api_key_length = byte_size(event.data.api_key)
origin = event.origin
case {signature_length, api_key_length} do
{0, _} ->
Kino.Frame.render(
frame,
Kino.Markdown.new("**No transaction signature given**"),
to: origin
)
{_, 0} ->
Kino.Frame.render(
frame,
Kino.Markdown.new("**No Helius API key given**"),
to: origin
)
_ ->
case HeliusFetch.fetch_transaction(event.data.signature, event.data.api_key) do
{:ok, transaction} ->
Kino.Frame.render(frame, TransactionRender.render(transaction), to: origin)
{:error, error} ->
Kino.Frame.render(frame, markdown("*Error*: #{inspect(error)}"), to: origin)
end
end
end)
```
|