Add Gitea tests Rename Remove oracle references Package for zx Reviewed-on: #235 Reviewed-by: dimitar.p.ivanov <dimitarivanov@qpq.swiss> Co-authored-by: Craig Everett <zxq9@zxq9.com> Co-committed-by: Craig Everett <zxq9@zxq9.com>
This commit was merged in pull request #235.
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @copyright (C) 2025, QPQ AG
|
||||
%%% @copyright (C) 2019, Aeternity Anstalt
|
||||
%%% @doc
|
||||
%%% Encode and decode data and function calls according to
|
||||
%%% Sophia-FATE-ABI
|
||||
%%% @end
|
||||
%%% Updated : 22 Jan 2025
|
||||
%%% Created : 11 Jun 2019
|
||||
%%%
|
||||
%%%-------------------------------------------------------------------
|
||||
-module(gmb_fate_abi).
|
||||
-vsn("3.4.1").
|
||||
|
||||
-export([ create_calldata/2
|
||||
, decode_calldata/2
|
||||
, get_function_hash_from_calldata/1
|
||||
, get_function_name_from_function_hash/2
|
||||
, get_function_type_from_function_hash/2
|
||||
, abi_version/0 ]).
|
||||
|
||||
-include("../include/gmb_fate_data.hrl").
|
||||
|
||||
%%%===================================================================
|
||||
%%% API
|
||||
%%%===================================================================
|
||||
|
||||
%% Shall match ?ABI_FATE_SOPHIA_1
|
||||
-spec abi_version() -> integer().
|
||||
abi_version() ->
|
||||
3.
|
||||
|
||||
-spec create_calldata(list(), [term()]) -> {ok, binary()}.
|
||||
create_calldata(FunName, Args) ->
|
||||
FunctionId = gmb_fate_code:symbol_identifier(list_to_binary(FunName)),
|
||||
{ok, gmb_fate_encoding:serialize(
|
||||
gmb_fate_data:make_tuple({FunctionId,
|
||||
gmb_fate_data:make_tuple(list_to_tuple(Args))}))}.
|
||||
|
||||
-spec decode_calldata(list(), binary()) -> {ok, term()} | {error, term()}.
|
||||
decode_calldata(FunName, Calldata) ->
|
||||
FunctionId = gmb_fate_code:symbol_identifier(list_to_binary(FunName)),
|
||||
try ?FATE_TUPLE_ELEMENTS(gmb_fate_encoding:deserialize(Calldata)) of
|
||||
[FunctionId, FateArgs] -> {ok, ?FATE_TUPLE_ELEMENTS(FateArgs)};
|
||||
_ -> {error, decode_error}
|
||||
catch _:_ ->
|
||||
{error, decode_error}
|
||||
end.
|
||||
|
||||
-spec get_function_name_from_function_hash(binary(), gmb_fate_code:fcode()) ->
|
||||
{ok, term()} | {error, term()}.
|
||||
get_function_name_from_function_hash(<<SymbolHash:4/binary, _:28/binary>>, FateCode) ->
|
||||
get_function_name_from_function_hash(SymbolHash, FateCode);
|
||||
get_function_name_from_function_hash(SymbolHash = <<_:4/binary>>, FateCode) ->
|
||||
Symbols = gmb_fate_code:symbols(FateCode),
|
||||
case maps:get(SymbolHash, Symbols, undefined) of
|
||||
undefined -> {error, no_function_matching_function_hash};
|
||||
Function -> {ok, Function}
|
||||
end.
|
||||
|
||||
-spec get_function_hash_from_calldata(binary()) ->
|
||||
{ok, binary()} | {error, term()}.
|
||||
get_function_hash_from_calldata(CallData) ->
|
||||
try ?FATE_TUPLE_ELEMENTS(gmb_fate_encoding:deserialize(CallData)) of
|
||||
[FunHash, _Args] -> {ok, FunHash};
|
||||
_ -> {error, bad_calldata}
|
||||
catch _:_ ->
|
||||
{error, bad_calldata}
|
||||
end.
|
||||
|
||||
-spec get_function_type_from_function_hash(binary(), gmb_fate_code:fcode()) ->
|
||||
{ok, term(), term()} | {error, term()}.
|
||||
get_function_type_from_function_hash(<<SymbolHash:4/binary, _:28/binary>>, FateCode) ->
|
||||
get_function_type_from_function_hash(SymbolHash, FateCode);
|
||||
get_function_type_from_function_hash(SymbolHash, FateCode) ->
|
||||
Functions = gmb_fate_code:functions(FateCode),
|
||||
case maps:get(SymbolHash, Functions, undefined) of
|
||||
undefined ->
|
||||
{error, no_function_matching_function_hash};
|
||||
{_Attrs, {ArgTypes, RetType}, _Code} ->
|
||||
{ok, ArgTypes, RetType}
|
||||
end.
|
||||
Reference in New Issue
Block a user