A Simple Example

Let us build a contract which counts how many time it was called. We will allow clients to specify that they do not want to be counted by passing a boolean ghost: if it is true, the contract will not count the transfer.

Based on this description, we already have the storage and parameter types:

storage nat;
parameter bool;

The code of this contract should perform the following steps:

  • destroy the parameter/storage pair
  • branch on the ghost parameter: this consumes the parameter, meaning the storage is now on the top of the stack
    • do nothing if ghost is true: the storage is unchanged
    • add 1 to the storage otherwise
  • pair an empty list of operations with the new storage

The complete description of the contract, simpleExample.tz, is thus

storage nat;
parameter bool;
code {
    UNPAIR;        # Unpair parameter and storage.
    IF {           # Asked not to count: storage is unchanged, nothing to do.
    } {
        PUSH nat 1;
        ADD
    };
    NIL operation; # We don't want to perform any operations.
    PAIR           # Aggregate the operation list and the new storage.
};