Concept 6 of 8

Actions and modes

How the type of a contract's action says which operations of the chain it may run, so that a view cannot write, and a read can run wherever a write may.

A contract's handler is an action: a value that describes what to do with the chain, which the compiler turns into code. An action does not reach the chain's operations by itself. It receives them as a record, and the type of that record depends on the handler's mode: Pure, View, Write or Payable. An action cannot use an operation that it is not given, so the type checker checks the mode of every handler.

The operations that each mode gives an action, and a view that tries to write
1 / 5

Step 1 of 5: Select an access record

let E = std:EVM/Effect
let peek
    : forall {extra : (Type -> Type) -> Row Type} (m : Type -> Type)
      -> E.Access E.Mode.View Natural Bool extra m
      -> E.Mutation Natural Bool m
    = \{extra} m access -> access.write
in  peek
Your result
Complete example · with imports
Full example and walkthrough
Rejected
let E = std:EVM/Effect
let peek
    : forall {extra : (Type -> Type) -> Row Type} (m : Type -> Type)
      -> E.Access E.Mode.View Natural Bool extra m
      -> E.Mutation Natural Bool m
    = \{extra} m access -> access.write
in  peek
Your result
Complete example · with imports

Error E0006

error[E0006]: the record has no field `write`
  ┌─ example.omg:6:35
  │
6 │     = \{extra} m access -> access.write
  │                                   ^^^^^ unknown field
  │
  = its fields are: `read`
  1. Select an access record. E.Access is the type of the record of operations that an action receives. It depends on the mode, one of the alternatives of E.Mode: Pure, View, Write and Payable. (lines 4: E.Access)
  2. Leave extra operations open. The caller supplies extra. Since peek must work for any such row, it cannot rely on extra containing a write operation. (lines 3: extra : (Type -> Type) -> Row Type)
  3. Choose a read-only view. A view receives read: storage loads, static calls and the results of calls. (lines 4: E.Mode.View)
  4. Ask for a mutation. The result type says peek returns a record of write operations, E.Mutation. A Write or Payable handler receives that record. Its argument only grants View access. (lines 5: E.Mutation)
  5. A view cannot write. peek selects access.write from its argument. View access promises no write field, so the type checker refuses the program with E0006. A view can still run wherever read is given, as the next section shows. (lines 6: access.write)

A read where a write is allowed

An action is polymorphic in extra, the operations that it does not name. A view therefore works where a write is expected, with write in its extra, without a conversion:

Checked
let C =
      std:EVM/Contract.withSoliditySignals { Events = <>, Errors = <> }
let State = { count : Word 256 }
let storage = C.storage State
let inWrite : storage.Bindings -> C.Action State C.Mode.Write (Word 256)
    = \state ->
        let current : C.Action State C.Mode.View (Word 256) = state.count.read
        in  current
in  inWrite
Your result
Complete example · with imports

The compiler accepts this program

The converse does not hold: a write action cannot take its write field from an arbitrary extra, so it is refused where a view is expected. lib/EVM/Effect.omg defines the modes and the records of operations.

Handlers in a contract

Each method of examples/evm/Counter.omg declares its mode. C.writing gives a handler the operations of a write:

           let methods =
                 C.methods
                   { increment =
                       C.define
                         (C.writing { Args = {}, Result = {} })
                         (\_ ->
                            do write {
                              previous <- state.count.read;
                              state.count.write (wrapping.add previous (Word/fromNatural 256 1))
                            })
Your result
Complete file: examples/evm/Counter.omg · with imports

The compiler accepts this program

C.reading gives read the operations of a view, so the handler can only read the counter:

                   , read =
                       C.define
                         (C.on
                            (C.Routes.Both
                               { native = C.Name.Field, solidity = C.Name.Rename ("count", refl) })
                            (C.reading { Args = {}, Result = Word 256 }))
                         (\_ -> state.count.read)
Your result
Complete file: examples/evm/Counter.omg · with imports

The compiler accepts this program

None of Counter's handlers is payable, so its runtime code checks once, before it looks for a handler, that a call sends no value. Dispatch through two interfaces follows a call from there.

Why the mode is in the type

The operations are fields of a record that the action receives, so an action is an ordinary function. The compiler applies it to the operations of the EVM and computes the result, and the record of operations computes away. What a handler cannot do is decided by the type checker, before any code is generated, and needs no analysis of the generated code.