Contracts

Syntax
Contract :
   contract IDENTIFIER {
  ContractMember*
  _}

ContractMember:
   Visibility?
   (
         ContractField
      | Function
      | Struct
      | Enum
   )

Visibility :
   pub?

ContractField :
   IDENTIFIER : Type

A contract is a piece of EVM Code associated with an Account. See Appendix A. in the Yellow Paper for more info. In Fe, a contract is denoted using the contract keyword. A contract definition adds a new contract type to the module. This contract type may be used for calling existing contracts with the same interface or initializing new contracts with the create methods.

An example of a contract:

struct Signed {
    pub book_msg: String<100>
}

contract GuestBook {
    messages: Map<address, String<100>>

    pub fn sign(mut self, mut ctx: Context, book_msg: String<100>) {
        self.messages[ctx.msg_sender()] = book_msg
        ctx.emit(Signed(book_msg: book_msg))
    }

    pub fn get_msg(self, addr: address) -> String<100> {
        return self.messages[addr].to_mem()
    }
}

The __init__ function

The __init__ function is a special contract function that can only be called at contract deployment time. It is mostly used to set initial values to storage variables upon deployment.

pub contract Example {

    admin: address

    pub fn __init__(mut self, admin: address)  {
        self.admin = admin
    }
}

It is not possible to call __init__ at runtime.