Skip to main content
Version: Next

Contract of module/namespace

For technical reasons, contracts are not namespaces, and namespaces are not contracts. In order to use a namespace that contains functions with the type of an entrypoint, and that are decorated as @entry, as a contract, for example for testing, we need the built-in function contract_of. It is built-in (and a keyword) because it takes a module as parameter, and the type system of LIGO has also a predefined type for its return value: module_contract<param, storage>, but not the full type.

type storage = int;
type @return = [list<operation>, storage];
namespace C {
@entry
const decrement = (param: int, storage: storage) : @return =>
[[], storage - param];
@entry
const increment = (param: int, storage: storage) : @return =>
[[], storage + param];
@entry
const reset = (_unit: unit, _storage: storage) : @return =>
[[], 0];
}
const test_initial_storage = () : unit => {
const init_storage = 42;
const fee = 0mutez;
const orig = Test.Next.originate (contract_of(C), init_storage, fee);
const new_storage = Test.Next.Typed_address.get_storage(orig.taddr);
assert(new_storage == init_storage);
}