From 6c172c4783e94abf5522c265aebec054675aa74b Mon Sep 17 00:00:00 2001 From: Craig Everett Date: Thu, 12 Feb 2026 17:44:56 +0900 Subject: [PATCH] Adjusting a few calls. --- src/hz.erl | 31 ++++++++++++++++++++++++------- src/hz_aaci.erl | 19 ++++++++++--------- src/hz_sophia.erl | 8 +++++++- 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/src/hz.erl b/src/hz.erl index 4734e74..fc50119 100644 --- a/src/hz.erl +++ b/src/hz.erl @@ -1098,7 +1098,7 @@ contract_create_built(CreatorID, Compiled, InitArgs) -> contract_create_built(CreatorID, Nonce, Amount, TTL, Gas, GasPrice, Compiled, InitArgs) -> - AACI = hz_aaci:prepare_aaci(maps:get(aci, Compiled)), + AACI = hz_aaci:prepare(maps:get(aci, Compiled)), case encode_call_data(AACI, "init", InitArgs) of {ok, CallData} -> assemble_calldata(CreatorID, Nonce, Amount, TTL, Gas, GasPrice, @@ -1192,7 +1192,7 @@ read_aci(Path) -> AACI :: aaci() | {aaci, Label :: term()}, ConID :: unicode:chardata(), Fun :: string(), - Args :: [string()], + Args :: [string()] | {erlang, [term()]}, Result :: {ok, CallTX} | {error, Reason}, CallTX :: binary(), Reason :: term(). @@ -1227,7 +1227,7 @@ contract_call(CallerID, AACI, ConID, Fun, Args) -> AACI :: aaci() | {aaci, Label :: term()}, ConID :: unicode:chardata(), Fun :: string(), - Args :: [string()], + Args :: [string()] | {erlang, [term()]}, Result :: {ok, CallTX} | {error, Reason}, CallTX :: binary(), Reason :: term(). @@ -1265,7 +1265,7 @@ contract_call(CallerID, Gas, AACI, ConID, Fun, Args) -> AACI :: aaci() | {aaci, Label :: term()}, ConID :: unicode:chardata(), Fun :: string(), - Args :: [string()], + Args :: [string()] | {erlang, [term()]}, Result :: {ok, CallTX} | {error, Reason}, CallTX :: binary(), Reason :: term(). @@ -1437,7 +1437,7 @@ contract_call4(PK, Nonce, Gas, GasPrice, Amount, TTL, CK, CallData) -> prepare_contract(File) -> case so_compiler:file(File, [{aci, json}]) of - {ok, #{aci := ACI}} -> {ok, hz_aaci:prepare_aaci(ACI)}; + {ok, #{aci := ACI}} -> {ok, hz_aaci:prepare(ACI)}; Error -> Error end. @@ -1517,12 +1517,29 @@ encode_call_data({aaci, Label}, Fun, Args) -> error -> {error, aaci_not_found} end. -encode_call_data2(ArgDef, Fun, Args) -> +encode_call_data2(ArgDef, Fun, {erlang, Args}) -> case hz_aaci:erlang_args_to_fate(ArgDef, Args) of {ok, Coerced} -> gmb_fate_abi:create_calldata(Fun, Coerced); - Errors -> Errors + Errors -> Errors + end; +encode_call_data2(ArgDef, Fun, SophiaArgs) -> + case convert(ArgDef, SophiaArgs) of + {ok, Args} -> gmb_fate_abi:create_calldata(Fun, Args); + Errors -> Errors end. +convert(Defs, Args) -> convert(Defs, Args, 1, [], []). + +convert([{Name, Def} | Defs], [Arg | Args], Nth, Terms, Errors) -> + case hz_sophia:parse_literal(Def, Arg) of + {ok, Term} -> convert(Defs, Args, Nth + 1, [Term | Terms], Errors); + {error, Reason} -> convert(Defs, Args, Nth + 1, Terms, [{Nth, Name, Reason} | Errors]) + end; +convert([], [], _, Terms, []) -> + {ok, lists:reverse(Terms)}; +convert([], [], _, _, Errors) -> + {error, Errors}. + sign_tx(Unsigned, SecKey) -> case network_id() of diff --git a/src/hz_aaci.erl b/src/hz_aaci.erl index f9da33f..90ec5bc 100644 --- a/src/hz_aaci.erl +++ b/src/hz_aaci.erl @@ -16,8 +16,8 @@ -license("GPL-3.0-or-later"). % Contract call and serialization interface functions --export([prepare_contract/1, - prepare_aaci/1, +-export([prepare_from_file/1, + prepare/1, erlang_to_fate/2, fate_to_erlang/2, erlang_args_to_fate/2, @@ -33,21 +33,22 @@ %%% ACI/AACI --spec prepare_contract(File) -> {ok, AACI} | {error, Reason} - when File :: file:filename(), +-spec prepare_from_file(Path) -> {ok, AACI} | {error, Reason} + when Path :: file:filename(), AACI :: aaci(), Reason :: term(). %% @doc %% Compile a contract and extract the function spec meta for use in future formation %% of calldata -prepare_contract(File) -> - case so_compiler:file(File, [{aci, json}]) of - {ok, #{aci := ACI}} -> {ok, prepare_aaci(ACI)}; +prepare_from_file(Path) -> + case so_compiler:file(Path, [{aci, json}]) of + {ok, #{aci := ACI}} -> {ok, prepare(ACI)}; Error -> Error end. -prepare_aaci(ACI) -> + +prepare(ACI) -> % We want to take the types represented by the ACI, things like N1.T(N2.T), % and dereference them down to concrete types like % {tuple, [integer, string]}. Our type dereferencing algorithms @@ -1009,7 +1010,7 @@ coerce_hash_test() -> aaci_from_string(String) -> case so_compiler:from_string(String, [{aci, json}]) of - {ok, #{aci := ACI}} -> {ok, prepare_aaci(ACI)}; + {ok, #{aci := ACI}} -> {ok, prepare(ACI)}; Error -> Error end. diff --git a/src/hz_sophia.erl b/src/hz_sophia.erl index 4ac67f2..a2e00ee 100644 --- a/src/hz_sophia.erl +++ b/src/hz_sophia.erl @@ -8,6 +8,12 @@ -include_lib("eunit/include/eunit.hrl"). + +-spec parse_literal(String) -> Result + when String :: string(), + Result :: {ok, gmb_fate_data:fate_type()} + | {error, Reason :: term()}. + parse_literal(String) -> parse_literal(unknown_type(), String). @@ -821,7 +827,7 @@ compile_entrypoint_value_and_type(Source, Entrypoint) -> FATE = extract_return_value(Code), % Generate the AACI, and get the AACI type info for the correct entrypoint. - AACI = hz_aaci:prepare_aaci(ACI), + AACI = hz_aaci:prepare(ACI), {ok, {_, Type}} = hz_aaci:get_function_signature(AACI, "f"), {FATE, Type}.