Current section
Files
Jump to
Current section
Files
src/gleeth@ethereum@contract.erl
-module(gleeth@ethereum@contract).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/gleeth/ethereum/contract.gleam").
-export([parse_parameter/1, generate_function_selector/2, encode_parameters/1, build_call_data/1]).
-export_type([param_type/0, parameter/0, contract_call/0]).
-type param_type() :: u_int256 | address | string | bool | bytes32.
-type parameter() :: {parameter, param_type(), binary()}.
-type contract_call() :: {contract_call, binary(), list(parameter())}.
-file("src/gleeth/ethereum/contract.gleam", 117).
-spec param_type_to_abi_type(param_type()) -> gleeth@ethereum@abi@types:abi_type().
param_type_to_abi_type(Param_type) ->
case Param_type of
u_int256 ->
{uint, 256};
address ->
address;
string ->
string;
bool ->
bool;
bytes32 ->
{fixed_bytes, 32}
end.
-file("src/gleeth/ethereum/contract.gleam", 148).
-spec parse_uint_value(binary()) -> {ok, gleeth@ethereum@abi@types:abi_value()} |
{error, gleeth@ethereum@abi@types:abi_error()}.
parse_uint_value(Value) ->
case gleam_stdlib:parse_int(Value) of
{ok, N} ->
{ok, {uint_value, N}};
{error, _} ->
case gleeth@utils@hex:to_int(Value) of
{ok, N@1} ->
{ok, {uint_value, N@1}};
{error, _} ->
{error,
{encode_error,
<<"Cannot parse uint value: "/utf8, Value/binary>>}}
end
end.
-file("src/gleeth/ethereum/contract.gleam", 164).
-spec parse_bool_value(binary()) -> {ok, gleeth@ethereum@abi@types:abi_value()} |
{error, gleeth@ethereum@abi@types:abi_error()}.
parse_bool_value(Value) ->
case string:lowercase(Value) of
<<"true"/utf8>> ->
{ok, {bool_value, true}};
<<"1"/utf8>> ->
{ok, {bool_value, true}};
<<"false"/utf8>> ->
{ok, {bool_value, false}};
<<"0"/utf8>> ->
{ok, {bool_value, false}};
_ ->
{error,
{encode_error,
<<"Boolean must be 'true', 'false', '1', or '0'"/utf8>>}}
end.
-file("src/gleeth/ethereum/contract.gleam", 204).
-spec make_zero_bytes_acc(integer(), bitstring()) -> bitstring().
make_zero_bytes_acc(N, Acc) ->
case N =< 0 of
true ->
Acc;
false ->
make_zero_bytes_acc(N - 1, <<Acc/bitstring, 0:8>>)
end.
-file("src/gleeth/ethereum/contract.gleam", 197).
-spec make_zero_bytes(integer()) -> bitstring().
make_zero_bytes(N) ->
case N =< 0 of
true ->
<<>>;
false ->
make_zero_bytes_acc(N, <<>>)
end.
-file("src/gleeth/ethereum/contract.gleam", 177).
-spec parse_bytes32_value(binary()) -> {ok,
gleeth@ethereum@abi@types:abi_value()} |
{error, gleeth@ethereum@abi@types:abi_error()}.
parse_bytes32_value(Value) ->
case gleeth@utils@hex:decode(Value) of
{ok, Bytes} ->
Size = erlang:byte_size(Bytes),
case Size =< 32 of
true ->
Padding = make_zero_bytes(32 - Size),
{ok,
{fixed_bytes_value,
gleam_stdlib:bit_array_concat([Bytes, Padding])}};
false ->
{error, {encode_error, <<"bytes32 value too long"/utf8>>}}
end;
{error, _} ->
{error,
{encode_error,
<<"Invalid hex for bytes32: "/utf8, Value/binary>>}}
end.
-file("src/gleeth/ethereum/contract.gleam", 135).
-spec string_to_abi_value(param_type(), binary()) -> {ok,
gleeth@ethereum@abi@types:abi_value()} |
{error, gleeth@ethereum@abi@types:abi_error()}.
string_to_abi_value(Param_type, Value) ->
case Param_type of
u_int256 ->
parse_uint_value(Value);
address ->
{ok, {address_value, Value}};
string ->
{ok, {string_value, Value}};
bool ->
parse_bool_value(Value);
bytes32 ->
parse_bytes32_value(Value)
end.
-file("src/gleeth/ethereum/contract.gleam", 127).
-spec param_to_abi_pair(parameter()) -> {ok,
{gleeth@ethereum@abi@types:abi_type(),
gleeth@ethereum@abi@types:abi_value()}} |
{error, gleeth@ethereum@abi@types:abi_error()}.
param_to_abi_pair(Param) ->
Abi_type = param_type_to_abi_type(erlang:element(2, Param)),
gleam@result:'try'(
string_to_abi_value(erlang:element(2, Param), erlang:element(3, Param)),
fun(Abi_value) -> {ok, {Abi_type, Abi_value}} end
).
-file("src/gleeth/ethereum/contract.gleam", 211).
-spec parse_param_type(binary()) -> {ok, param_type()} |
{error, gleeth@rpc@types:gleeth_error()}.
parse_param_type(Type_str) ->
case string:lowercase(Type_str) of
<<"uint256"/utf8>> ->
{ok, u_int256};
<<"uint"/utf8>> ->
{ok, u_int256};
<<"address"/utf8>> ->
{ok, address};
<<"string"/utf8>> ->
{ok, string};
<<"bool"/utf8>> ->
{ok, bool};
<<"boolean"/utf8>> ->
{ok, bool};
<<"bytes32"/utf8>> ->
{ok, bytes32};
_ ->
{error,
{parse_error,
<<"Unsupported parameter type: "/utf8, Type_str/binary>>}}
end.
-file("src/gleeth/ethereum/contract.gleam", 101).
-spec parse_parameter(binary()) -> {ok, parameter()} |
{error, gleeth@rpc@types:gleeth_error()}.
parse_parameter(Param_str) ->
case gleam@string:split(Param_str, <<":"/utf8>>) of
[Type_str, Value] ->
gleam@result:'try'(
parse_param_type(Type_str),
fun(Param_type) -> {ok, {parameter, Param_type, Value}} end
);
_ ->
{error,
{parse_error,
<<"Parameter must be in format 'type:value'"/utf8>>}}
end.
-file("src/gleeth/ethereum/contract.gleam", 224).
-spec abi_error_to_gleeth_error(gleeth@ethereum@abi@types:abi_error()) -> gleeth@rpc@types:gleeth_error().
abi_error_to_gleeth_error(Err) ->
{abi_err, Err}.
-file("src/gleeth/ethereum/contract.gleam", 31).
-spec generate_function_selector(binary(), list(param_type())) -> {ok, binary()} |
{error, gleeth@rpc@types:gleeth_error()}.
generate_function_selector(Function_name, Param_types) ->
Abi_types = gleam@list:map(Param_types, fun param_type_to_abi_type/1),
case gleeth@ethereum@abi@encode:function_selector(Function_name, Abi_types) of
{ok, Selector} ->
{ok, gleeth@utils@hex:encode(Selector)};
{error, Err} ->
{error, abi_error_to_gleeth_error(Err)}
end.
-file("src/gleeth/ethereum/contract.gleam", 43).
-spec encode_parameters(list(parameter())) -> {ok, binary()} |
{error, gleeth@rpc@types:gleeth_error()}.
encode_parameters(Parameters) ->
case Parameters of
[] ->
{ok, <<""/utf8>>};
Params ->
gleam@result:'try'(
begin
_pipe = gleam@list:try_map(Params, fun param_to_abi_pair/1),
gleam@result:map_error(
_pipe,
fun(E) -> abi_error_to_gleeth_error(E) end
)
end,
fun(Abi_pairs) ->
case gleeth@ethereum@abi@encode:encode(Abi_pairs) of
{ok, Encoded} ->
{ok,
string:lowercase(
gleam_stdlib:base16_encode(Encoded)
)};
{error, Err} ->
{error, abi_error_to_gleeth_error(Err)}
end
end
)
end.
-file("src/gleeth/ethereum/contract.gleam", 62).
-spec build_call_data(contract_call()) -> {ok, binary()} |
{error, gleeth@rpc@types:gleeth_error()}.
build_call_data(Contract_call) ->
Abi_type_list = gleam@list:map(
erlang:element(3, Contract_call),
fun(P) -> param_type_to_abi_type(erlang:element(2, P)) end
),
gleam@result:'try'(
begin
_pipe = gleam@list:try_map(
erlang:element(3, Contract_call),
fun param_to_abi_pair/1
),
gleam@result:map_error(
_pipe,
fun(E) -> abi_error_to_gleeth_error(E) end
)
end,
fun(Abi_pairs) ->
case gleeth@ethereum@abi@encode:function_selector(
erlang:element(2, Contract_call),
Abi_type_list
) of
{ok, Selector} ->
case Abi_pairs of
[] ->
{ok, gleeth@utils@hex:encode(Selector)};
_ ->
case gleeth@ethereum@abi@encode:encode(Abi_pairs) of
{ok, Encoded} ->
Selector_hex = string:lowercase(
gleam_stdlib:base16_encode(Selector)
),
Params_hex = string:lowercase(
gleam_stdlib:base16_encode(Encoded)
),
{ok,
<<<<"0x"/utf8, Selector_hex/binary>>/binary,
Params_hex/binary>>};
{error, Err} ->
{error, abi_error_to_gleeth_error(Err)}
end
end;
{error, Err@1} ->
{error, abi_error_to_gleeth_error(Err@1)}
end
end
).