Contract Structure

Contract Structure

Each contract must contain 2 functions: main and cf.

The main function is run whenever a contract is used within a transaction.

The function will run both at the validation and at the generation of the transaction.

The structure of the main function is:

main
    ( txSkel      : Zen.Types.txSkeleton  )
    ( context     : Zen.Types.context     )
    ( contractId  : Zen.Types.contractId  )
    ( command     : string                )
    ( sender      : Zen.Types.sender      )
    ( messageBody : option Zen.Types.data )
    ( wallet      : Zen.Types.wallet      )
    ( state       : option Zen.Types.data )
    : Zen.Types.contractResult `Zen.Cost.t` n

where n must be an expression which evaluates to a natural number (nat).

Parameters

  • txSkel : Zen.Types.txSkeleton : The partial transaction supplied as input to the contract.

  • context : Zen.Types.context : The blockchain context of the transaction, given by blockNumber (unsigned 32 bit integer), and timestamp - which is the UNIX Epoch time (unsigned 64 bit integer) of the block creation.

  • contractId : Zen.Types.contractId : The contract identifier. (version, hash)

  • command : string : String that the contract may use. Contains a command which tells the contract what to do. For example:

match command with
| "redeem" -> redeem txSkeleton contractId returnAddress wallet
| "buy"    -> buy txSkeleton contractId returnAddress
  • sender : Zen.Types.sender : The sender identity. Can be any of the following:

    • Contract of Zen.Types.contractId: a contract, given by its ID

    • PK of Zen.Types.publicKey: a Public key

    • Anonymous: an anonymous sender

  • messageBody : option Zen.Types.data : The transaction may carry a message which can be any of the following:

    • Byte: of FStar.UInt8.t: 8 bit unsigned integer

    • U32 of FStar.UInt32.t: 32 bit unsigned integer

    • U64 of FStar.UInt64.t: 64 bit unsigned integer

    • I64 of FStar.Int64.t: 64 bit signed integer

    • ByteArray of Zen.Array.t FStar.UInt8.t: array of 8 bit unsigned integer

    • String of string: FStar.prims

    • Hash of Zen.Types.hash: 256-bit hash value

    • Lock of Zen.Types.lock: Lock

    • Signature of Zen.Types.signature: Signature

    • PublicKey of Zen.Types.publicKey: Public key

    • Collection of Zen.Types.dataCollection: Data collection

  • wallet : Zen.Types.wallet : Contains all the transaction inputs that were previously locked to the contract. In order for a contract to spend its own funds they need to come from contract wallet.

  • state : option Zen.Types.data : The contract current state. Can be either Some data (as the message body) or None.

Output

The output of the contract is of the record type Zen.Types.contractReturn which has 3 fields:

  • state : Zen.Types.stateUpdate State update. Can be any of the following:

    • Delete - Delete the current state, resetting it to None

    • NoChange - Keeping the current state as it is, with no change.

    • Update of Zen.Types.data - Change the state to be the new given data.

  • tx : Zen.Types.txSkeleton The generated transaction structure.

  • message : option Zen.Types.message An optional message for invoking another contract. This is a record type which has 3 fields:

    • recipient: Zen.Types.contractId - The recipient contract.

    • command: string - The command given to the recipient contract.

    • body: option Zen.Types.data - The message body of given to the recipient contract.

Last updated