Packages
forge_sdk
0.40.0
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.0.7
1.0.6
1.0.4
1.0.4-p1
1.0.4-p0
1.0.3
1.0.2
1.0.2-p1
1.0.1
1.0.1-p1
1.0.0
0.40.6
0.40.5
0.40.4
0.40.3
0.40.2
0.40.1
0.40.0
0.39.1
0.39.0
0.38.6
0.38.5
0.38.4
0.38.3
0.38.2
0.38.1
0.38.0
0.37.5
0.37.4
0.37.3
0.37.2
0.37.1
0.37.0
0.34.0
0.33.2
0.33.1
0.33.0
0.32.2
0.32.1
0.32.0
0.31.1
0.31.0
0.30.0
0.29.1
0.29.0
0.28.3
0.28.2
0.28.1
0.28.0
0.27.4
0.27.3
0.27.2
0.27.1
0.27.0
0.26.6
0.26.5
0.26.4
0.26.3
0.26.1
0.26.0
Elixir / Erlang version of the SDK for Forge framework.
Current section
Files
Jump to
Current section
Files
lib/forge_sdk/util/loader.ex
defmodule ForgeSdk.Loader do
@moduledoc """
Load type urls and various protobufs in sdk
"""
require Logger
alias ForgeAbi.ForgeState
alias ForgeSdk.Rpc
@spec update_type_url(ForgeState.t()) :: :ok
def update_type_url(forge_state) do
forge_state
|> get_tx_protocols()
|> Enum.each(fn %{code: code, type_urls: type_urls} -> update_type_url(code, type_urls) end)
end
@spec update_type_url(list(), list()) :: :ok
def update_type_url(code, type_urls) do
load_code(code)
load_type_urls(type_urls)
end
@spec get_tx_protocols(ForgeState.t(), String.t()) :: [map()]
def get_tx_protocols(forge_state, address \\ "") do
forge_state
|> Map.get(:protocols, [])
|> Enum.filter(fn %{address: protocol_address} ->
case address === "" do
true -> true
false -> protocol_address === address
end
end)
|> Task.async_stream(fn %{address: address} -> get_one_tx_protocol(address) end)
|> Enum.map(fn {:ok, res} -> res end)
end
defp get_one_tx_protocol(address) do
%{itx: itx, context: context} = Rpc.get_protocol_state(address: address)
Map.put(itx, :installed_at, context.genesis_time)
end
defp load_code(code) do
code
|> Enum.each(fn %{binary: binary} ->
{:ok, {mod, _}} = :beam_lib.md5(binary)
name = Atom.to_string(mod)
if need_load?(name) do
# purge old code
purge_result = :code.soft_purge(mod)
load_result = :code.load_binary(mod, '', binary)
Logger.info(
"#{name} - Purged old code: #{inspect(purge_result)}, and loaded new code: #{
inspect(load_result)
}"
)
end
end)
end
defp need_load?(name) do
String.starts_with?(name, "Elixir.ForgeAbi") or String.ends_with?(name, ".Rpc") or
String.ends_with?(name, ".Helper")
end
defp load_type_urls(urls) do
type_urls =
Enum.map(urls, fn %{url: url, module: module} ->
mod = Module.concat("Elixir", module)
{url, mod}
end)
ForgeAbi.add_type_urls(type_urls)
end
end