Contract factories and bytecode introspection #796
@ -327,12 +327,12 @@ to_fcode(Env, [{Contract, Attrs, Con = {con, _, Name}, Decls}|Rest])
|
||||
when ?IS_CONTRACT_HEAD(Contract) ->
|
||||
case Contract =:= contract_interface of
|
||||
false ->
|
||||
|
||||
#{ builtins := Builtins } = Env,
|
||||
ConEnv = Env#{ context => {main_contract, Name},
|
||||
builtins => Builtins#{[Name, "state"] => {get_state, none},
|
||||
[Name, "put"] => {set_state, 1},
|
||||
[Name, "Chain", "event"] => {chain_event, 1}} },
|
||||
#{ functions := PrevFuns } = ConEnv,
|
||||
#{ functions := Funs } = Env1 =
|
||||
decls_to_fcode(ConEnv, Decls),
|
||||
StateType = lookup_type(Env1, [Name, "state"], [], {tuple, []}),
|
||||
@ -350,7 +350,8 @@ to_fcode(Env, [{Contract, Attrs, Con = {con, _, Name}, Decls}|Rest])
|
||||
contract_main -> Rest = [], {Env1, ConFcode};
|
||||
contract_child ->
|
||||
Env2 = add_child_con(Env1, Name, ConFcode),
|
||||
to_fcode(Env2, Rest)
|
||||
Env3 = Env2#{ functions := PrevFuns },
|
||||
to_fcode(Env3, Rest)
|
||||
end;
|
||||
true ->
|
||||
Env1 = decls_to_fcode(Env#{ context => {abstract_contract, Con} }, Decls),
|
||||
|
@ -26,14 +26,14 @@ run_test(Test) ->
|
||||
simple_compile_test_() ->
|
||||
[ {"Testing the " ++ ContractName ++ " contract with the " ++ atom_to_list(Backend) ++ " backend",
|
||||
fun() ->
|
||||
case compile(Backend, ContractName, [pp_assembler]) of
|
||||
case compile(Backend, ContractName) of
|
||||
#{byte_code := ByteCode,
|
||||
contract_source := _,
|
||||
type_info := _} when Backend == aevm ->
|
||||
?assertMatch(Code when is_binary(Code), ByteCode);
|
||||
#{fate_code := Code} when Backend == fate ->
|
||||
Code1 = aeb_fate_code:deserialize(aeb_fate_code:serialize(Code)),
|
||||
?assertMatch({X, X}, {Code1, Code}), error(xd);
|
||||
?assertMatch({X, X}, {Code1, Code});
|
||||
ErrBin ->
|
||||
io:format("\n~s", [ErrBin]),
|
||||
error(ErrBin)
|
||||
|
28
test/contracts/create.aes
Normal file
28
test/contracts/create.aes
Normal file
@ -0,0 +1,28 @@
|
||||
contract IntegerAdder =
|
||||
entrypoint init() = ()
|
||||
entrypoint addIntegers(x, y) = x + y
|
||||
|
||||
contract IntegerAdderHolder =
|
||||
type state = IntegerAdder
|
||||
stateful entrypoint init() = Chain.create() : IntegerAdder
|
||||
entrypoint get() = state
|
||||
|
||||
contract IntegerAdderFactory =
|
||||
entrypoint init() = ()
|
||||
stateful entrypoint new() =
|
||||
let i = Chain.create() : IntegerAdderHolder
|
||||
i.get()
|
||||
|
||||
payable contract ValueAdder =
|
||||
entrypoint init() = ()
|
||||
stateful entrypoint addValue(x) =
|
||||
let integerAdderFactory = Chain.create()
|
||||
let adder = integerAdderFactory.new()
|
||||
adder.addIntegers(x, Contract.balance)
|
||||
|
||||
main contract EnterpriseContract =
|
||||
entrypoint init() = ()
|
||||
stateful payable entrypoint increaseByThree(x) =
|
||||
require(Call.value >= 3, "Price for addition = 3AEtto, insufficient funds")
|
||||
let threeAdder = Chain.create(value = 3)
|
||||
threeAdder.addValue(x)
|
Loading…
x
Reference in New Issue
Block a user