Contract as a first class module value
We can consider type 'storage contract
for a contract whose storage type is 'storage
.
module type CONTRACT = sig type storage end
type 'storage contract = (module CONTRACT with type storage = 'storage)
module C = struct
type storage = int
let [@entry] main () (i : storage) = [], i
end
let c : int contract = (module C)
then, we can have a function:
val Contract.create : 'storage contract -> key_hash option -> tz -> 'storage -> operation * address
We can write a contract which create a contract as follows:
open SCaml
(* type 'a contract must be defined in SCaml *)
module M = struct
(* special type checking for storage and the entries must be performed *)
type storage = unit
let [@entry] main () () = [], ()
end
let [@entry] main () () =
let op, ad =
(* The first arg of Contract.create must always have the form (module X) *)
(* If the storage type is known by the last arg, (module M) does not need any type constraint *)
Contract.create (module M) None (Tz 0.0) ()
in
[op], ()
Edited by Jun Furuse