Endpoint Syntax
A endpoint in Coco is a callable element for code organization and reusability. A single endpoint is expected to perform simple and singular operations. Each endpoint has a particular type of Callsite, which describes the constraints of its execution.
- An endpoint can accept zero or more arguments and return some outputs. All arguments and return variables for an endpoint must be named. All return variables of the endpoint are automatically initialized at the beginning of its execution. See
Functions.DoSomething()
- Some endpoint names have a ‘persistent’ keyword. This is intended to emphasize that the endpoint modifies the state of the module. It is mandatory for any endpoint that uses the mutate keyword in its block. See
TokenLedger.SeedSupply()
- If multiple consecutive endpoint parameters are of the same data type, the type can be omitted from all but the last parameter. For example,
Functions.Combine()
can also be declared as follows:
// We have combined the type declaration of the x and y function paramaters
endpoint invokable Combine(x, y String) -> (z String):
yield z join(x, y)
Types of Endpoint Callsites
Coco supports multiple types of endpoints, each enabling different capabilities around the module. It can be useful to think each callsite type as a different endpoint for a hosted Logic, similar to REST APIs
1. Deployers
Deployers are endpoints used to initialize a persistent state and can be thought of as the constructor of the module. A given module can describe multiple deployers but only can be used at the time of activating or “deploying” the logic module. See
- Deployers must always have the ‘persistent’ keyword as they are always attached to a persistent state.
- A module with a persistent state must describe atleast one deployer.
2. Invokables
Invokables are endpoints that can only be invoked externally by a single participant.
They can be thought of as the external endpoint to the logic with most of their primary capabilities being wrapped in functions
3. Enlisters & Interactables
- Enlisters are endpoints used to initialize ephemeral state.
- Interactables are endpoints that facilitate an external interaction between two participants.
Function Syntax
Functions are used for code reusability and maintainability. They only exist within the module scope. These are the only types of function that can be called from within the module. See Functions.Combine()
coco TokenLedger
state persistent:
name String
symbol String
supply U64
balances Map[Address]U64
endpoint deployer persistent Init(name String, symbol String):
mutate name -> TokenLedger.State.name
mutate symbol -> TokenLedger.State.symbol
endpoint deployer persistent SeedSupply(name, symbol String, supply U64, seed Address):
mutate name -> TokenLedger.State.name
mutate symbol -> TokenLedger.State.symbol
mutate supply -> TokenLedger.State.supply
mutate balances <- TokenLedger.State.balances:
balances[seed] = supply
endpoint invokable Name() -> (name String):
observe name <- TokenLedger.State.name
endpoint invokable Symbol() -> (symbol String):
observe symbol <- TokenLedger.State.symbol
endpoint invokable Decimals() -> (decimals U64):
decimals = 18
endpoint invokable BalanceOf(address Address) -> (balance U64):
memory balance = {balance} <- {balance} <- Balance(address)
yield balance state_bal
func Balance(address Address) -> (balance U64):
observe balances <- TokenLedger.State.balances:
balance = balances[address]
coco Functions
func DoSomething() -> (output String):
yield output "done!"
endpoint invokable Combine(x String, y String) -> (z String):
yield z join(x, y)