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.txSkeletonThe partial transaction supplied as input to the contract.
context : Zen.Types.contextThe blockchain context of the transaction, given by
blockNumber(unsigned 32 bit integer), andtimestamp- which is the UNIX Epoch time (unsigned 64 bit integer) of the block creation.
contractId : Zen.Types.contractIdThe contract identifier.
(version, hash)
command : stringString 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.senderThe sender identity. Can be any of the following:
Contractof Zen.Types.contractIdA contract, given by its ID PKof Zen.Types.publicKeyPublic key AnonymousAn anonymous sender
messageBody : option Zen.Types.dataThe transaction may carry a message which can be any of the following:
Byteof FStar.UInt8.t8 bit unsigned integer U32of FStar.UInt32.t32 bit unsigned integer U64of FStar.UInt64.t64 bit unsigned integer I64of FStar.Int64.t64 bit signed integer ByteArrayof Zen.Array.t FStar.UInt8.tByte array Stringof stringString Hashof Zen.Types.hash256-bit hash value Lockof Zen.Types.lockLock Signatureof Zen.Types.signatureSignature PublicKeyof Zen.Types.publicKeyPublic key Collectionof Zen.Types.dataCollectionData collection
wallet : Zen.Types.walletContains 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.dataThe contract current state. Can be either
Somedata (as the message body) orNone.
Output¶
The output of the contract is of the record type Zen.Types.contractReturn which has 3 fields:
state : Zen.Types.stateUpdateState update. Can be any of the following:DeleteDelete the current state, resetting it to NoneNoChangeKeeping the current state as it is, with no change. Updateof Zen.Types.dataChange the state to be the new given data.tx : Zen.Types.txSkeletonThe generated transaction structure.message : option Zen.Types.messageAn optional message for invoking another contract. This is a record type which has 3 fields:recipient: Zen.Types.contractIdThe recipient contract. command: stringThe command given to the recipient contract. body: option Zen.Types.dataThe message body of given to the recipient contract.