Packages
forge_sdk
0.40.4
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/rpc/tx/helper.ex
defmodule ForgeSdk.Tx.Builder.Helper do
@moduledoc """
Helper function for building tx rpc.
"""
alias ForgeAbi.{RequestSendTx, Transaction}
alias ForgeSdk.Wallet.Util, as: WalletUtil
# credo:disable-for-lines:40
def build(itx, opts) do
type_url = ForgeAbi.get_type_url(itx.__struct__)
any = ForgeAbi.encode_any!(itx, type_url)
wallet = opts[:wallet]
delegatee = opts[:delegatee]
sign? = Keyword.get(opts, :sign, true)
conn = ForgeSdk.Util.get_conn(opts[:conn] || "")
if wallet === nil do
raise "wallet shall be provided in opts"
end
if wallet.sk === "" and sign? do
raise "Tx requires signature but no sk found"
end
nonce =
case opts[:nonce] do
v when is_integer(v) -> v
_ -> Enum.random(1..10_000_000_000)
end
"fg:t:" <> type = type_url
gas = Map.get(conn.gas, type, 0)
case sign? do
true ->
case do_create_tx(any, nonce, gas, wallet, delegatee, conn.chain_id) do
{:error, _} = error ->
error
tx ->
case Keyword.get(opts, :send, :broadcast) do
:broadcast -> send_tx(RequestSendTx.new(tx: tx), conn)
:commit -> send_tx(RequestSendTx.new(tx: tx, commit: true), conn)
:nosend -> tx
end
end
false ->
create_unsigned_tx(any, nonce, gas, wallet, delegatee, conn)
end
end
# private functions
defp create_unsigned_tx(any, nonce, gas, wallet, nil, conn) do
Transaction.new(
itx: any,
from: wallet.address,
nonce: nonce,
gas: gas,
chain_id: conn.chain_id,
pk: wallet.pk
)
end
defp create_unsigned_tx(any, nonce, gas, wallet, delegatee, conn) do
Transaction.new(
itx: any,
delegator: wallet.address,
from: delegatee,
nonce: nonce,
gas: gas,
chain_id: conn.chain_id,
pk: wallet.pk
)
end
defp do_create_tx(any, nonce, gas, wallet, delegatee, chain_id) do
tx =
case delegatee do
nil ->
Transaction.new(
itx: any,
from: wallet.address,
nonce: nonce,
gas: gas,
chain_id: chain_id,
pk: wallet.pk
)
_ ->
Transaction.new(
itx: any,
from: delegatee,
delegator: wallet.address,
nonce: nonce,
gas: gas,
chain_id: chain_id,
pk: wallet.pk
)
end
tx = %Transaction{tx | signature: <<>>}
signature = WalletUtil.sign!(wallet, Transaction.encode(tx))
%Transaction{tx | signature: signature}
end
defp send_tx(req, conn) do
case ForgeSdk.send_tx(req, conn.name) do
{:error, _} = error -> error
res -> res
end
end
end