Current section
Files
Jump to
Current section
Files
src/gleeth@deploy.erl
-module(gleeth@deploy).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/gleeth/deploy.gleam").
-export([deploy/5, deploy_with_args/6]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" Contract deployment helpers.\n"
"\n"
" Build deployment transactions from bytecode and optional constructor\n"
" arguments, broadcast them, and extract the deployed contract address\n"
" from the receipt.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" // Deploy with no constructor args\n"
" let assert Ok(address) =\n"
" deploy.deploy(provider, wallet, bytecode, \"0x100000\", 1)\n"
"\n"
" // Deploy with constructor args\n"
" let assert Ok(address) =\n"
" deploy.deploy_with_args(\n"
" provider, wallet, bytecode,\n"
" [#(types.Uint(256), types.UintValue(1_000_000))],\n"
" \"0x200000\", 1,\n"
" )\n"
" ```\n"
).
-file("src/gleeth/deploy.gleam", 67).
?DOC(
" Build a deployment transaction, sign it, broadcast, wait for receipt,\n"
" and return the contract address.\n"
).
-spec deploy_raw(
gleeth@provider:provider(),
gleeth@crypto@wallet:wallet(),
binary(),
binary(),
integer()
) -> {ok, binary()} | {error, gleeth@rpc@types:gleeth_error()}.
deploy_raw(Provider, W, Data, Gas_limit, Chain_id) ->
Sender = gleeth@crypto@wallet:get_address(W),
gleam@result:'try'(
gleeth@rpc@methods:get_transaction_count(
Provider,
Sender,
<<"pending"/utf8>>
),
fun(Nonce) ->
gleam@result:'try'(
gleeth@rpc@methods:get_gas_price(Provider),
fun(Gas_price) ->
gleam@result:'try'(
begin
_pipe = gleeth@crypto@transaction:create_legacy_transaction(
<<""/utf8>>,
<<"0x0"/utf8>>,
Gas_limit,
Gas_price,
Nonce,
Data,
Chain_id
),
gleam@result:map_error(
_pipe,
fun(Field@0) -> {transaction_err, Field@0} end
)
end,
fun(Tx) ->
gleam@result:'try'(
begin
_pipe@1 = gleeth@crypto@transaction:sign_transaction(
Tx,
W
),
gleam@result:map_error(
_pipe@1,
fun(Field@0) -> {transaction_err, Field@0} end
)
end,
fun(Signed) ->
gleam@result:'try'(
gleeth@rpc@methods:send_raw_transaction(
Provider,
erlang:element(12, Signed)
),
fun(Tx_hash) ->
gleam@result:'try'(
gleeth@rpc@methods:wait_for_receipt(
Provider,
Tx_hash
),
fun(Receipt) ->
case erlang:element(
10,
Receipt
) of
<<""/utf8>> ->
{error,
{parse_error,
<<"No contract address in receipt"/utf8>>}};
Address ->
{ok, Address}
end
end
)
end
)
end
)
end
)
end
)
end
).
-file("src/gleeth/deploy.gleam", 36).
?DOC(
" Deploy a contract with no constructor arguments.\n"
" Returns the deployed contract address.\n"
).
-spec deploy(
gleeth@provider:provider(),
gleeth@crypto@wallet:wallet(),
binary(),
binary(),
integer()
) -> {ok, binary()} | {error, gleeth@rpc@types:gleeth_error()}.
deploy(Provider, W, Bytecode, Gas_limit, Chain_id) ->
deploy_raw(Provider, W, Bytecode, Gas_limit, Chain_id).
-file("src/gleeth/deploy.gleam", 111).
?DOC(" Append hex data to bytecode, handling 0x prefixes.\n").
-spec append_hex(binary(), binary()) -> binary().
append_hex(Bytecode, Args_hex) ->
Base = case gleam_stdlib:string_starts_with(Bytecode, <<"0x"/utf8>>) of
true ->
Bytecode;
false ->
<<"0x"/utf8, Bytecode/binary>>
end,
Args = case gleam_stdlib:string_starts_with(Args_hex, <<"0x"/utf8>>) of
true ->
gleam@string:drop_start(Args_hex, 2);
false ->
Args_hex
end,
<<Base/binary, Args/binary>>.
-file("src/gleeth/deploy.gleam", 48).
?DOC(
" Deploy a contract with ABI-encoded constructor arguments appended to the bytecode.\n"
" Returns the deployed contract address.\n"
).
-spec deploy_with_args(
gleeth@provider:provider(),
gleeth@crypto@wallet:wallet(),
binary(),
list({gleeth@ethereum@abi@types:abi_type(),
gleeth@ethereum@abi@types:abi_value()}),
binary(),
integer()
) -> {ok, binary()} | {error, gleeth@rpc@types:gleeth_error()}.
deploy_with_args(Provider, W, Bytecode, Constructor_args, Gas_limit, Chain_id) ->
gleam@result:'try'(
begin
_pipe = gleeth@ethereum@abi@encode:encode(Constructor_args),
gleam@result:map_error(
_pipe,
fun(Field@0) -> {abi_err, Field@0} end
)
end,
fun(Encoded_args) ->
Args_hex = string:lowercase(
gleam_stdlib:base16_encode(Encoded_args)
),
Full_data = append_hex(Bytecode, Args_hex),
deploy_raw(Provider, W, Full_data, Gas_limit, Chain_id)
end
).