Write your first contract
A Counter in Solidity and Omegyu, from one storage word to a constructor, a getter and a transaction that changes the count.
Start with a contract whose entire state you can predict. Deploy it with 10, call increment(), and the next count() must return 11. Both versions below expose those two Solidity ABI functions. The caller does not need to know which language produced the bytecode.
Start from the Solidity contract
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.33;
contract Counter {
uint256 public count;
constructor(uint256 start) {
count = start;
}
function increment() external {
unchecked { count += 1; }
}
}Solidity does three jobs with uint256 public count: it chooses a representation, reserves storage, and generates a getter. The unchecked block is intentional: this counter wraps from the largest uint256 to zero. Solidity's default checked + would instead revert there.
Write the Omegyu contract
Here is the whole file. You can open this listing in the playground and edit it; there are no imports from a starter project.
let C = std:EVM/Contract
let State = { count : Word 256 }
let Args = { start : Word 256 }
let step = Word/fromNatural 256 1
let contract =
(C.storage State).bind
(\state ->
let write = C.monad @State C.Mode.Write
let methods =
C.methods
{ count = C.define (C.reading { Args = {}, Result = Word 256 }) (\_ -> state.count.read)
, increment =
C.define (C.writing { Args = {}, Result = {} })
(\_ -> do write {
previous <- state.count.read;
state.count.write (Word/add previous step)
})
}
in C.compose
{ State, Args = C.solidityCodec Args }
{ constructor = \a -> state.count.write a.start, facets = [ methods.facet ] })
in { contract }EVM interfaceruntime 153 bytes, deployment 204 bytes
0x06661abd count()
0xd09de08a increment()
0x4f4d4701bf3133a4 count (view)
0x4f4d4701cdcc9267 increment (nonpayable)Name the state and its starting value.
Word 256is the bounded unsigned word that the ABI callsuint256.Stateis what survives a transaction.Argsis a separate record:startexists during construction, but there is nostartstorage slot.Give the methods access to storage.
(C.storage State).bindgives the body astate.countaccessor, not the current integer. Reading and writing that word are actions.C.readingpermits the read in the getter but not the write inincrement.Sequence the update.
previous <- state.count.readruns the read before the next line.Word/addwraps at 256 bits, matching the Solidityuncheckedblock. The last action writes the sum. Its result is empty: look at a receipt to learn whether the write succeeded, then call the getter for the new value.Choose what callers see.
C.methodsuses the field labels for the method names.{}means no arguments in both declarations and no returned values inincrement.C.solidityCodec Argsselects the Solidity ABI for the constructor.C.composejoins the constructor and method group;in { contract }exports what the compiler builds. The default methods also have native routes. A Solidity client uses only the four-byte ABI selectors.
Deploy, call, then change the code
Open the Omegyu listing in the playground. Choose Build for the EVM, then switch from Editor to VM lab in the workspace bar. The compiler answer describes the build; the lab runs it. Use Deploy instance for each build and the address rows under Deployed code to return to the first instance. A build produces creation code; it does not create an account until you deploy it.
| Action | What to look for |
|---|---|
Deploy with start set to 10. Keep the address; call it A. | Creation runs the constructor once. Slot 0 now holds 10 (0xa). The deployment receipt reports gas used. |
Read count() on A. | The result decodes to 10. A simulated read does not commit any state. |
Send increment() to A, then read again. | The transaction returns no count. Slot 0 changes from 10 to 11 and the read returns 11. Its gas is the cost of this transaction, not of compiling the program. |
Edit Word/fromNatural 256 1 to Word/fromNatural 256 2. Build again and deploy with 10. Call this address B. | B starts at 10, not at A's current count. Editing a source file cannot replace the code at A. |
Send increment() to B. Read B, then A. | B returns 12; A still returns 11. Same function selector, different deployed code and separate storage. |
On address A, switch Solidity ABI to Native and read count() again. The call now uses the eight-byte native header shown above, not the Solidity selector; it still reads 11 from the same address. Switching the interface does not deploy another instance.
Want a checked overflow policy or a getter with a different public name? Continue with the larger Counter. For how a Solidity contract calls an Omegyu address, see the Solidity caller.