Deploy and Enlist
Coco supports persistent and ephemeral state types. Persistent state is the state of the module and ephemeral state refers to the state of the participant.
endpoint deploy SeedSupply()
// deploying persistent state
endpoint enlist Seed()
// enlising ephemeral state
In modules having a persistent state, it is necessary to have a deploy
endpoint to initialize this state. Modules with ephemeral states need at least one enlist
endpoint, but if a module has both persistent and ephemeral states, only deploy
is mandatory.
Accessing State
Persistent state values are accessed using the module name and the field name, e.g.
ERC20.name
Ephemeral states are accessed using the participant name, module name and field name, e.g.
Sender.Flipper.value
Persistent and Ephemental keywords
Endpoints that mutate persistent or ephemeral state, need persistent
or ephemeral
keword in the endpoint signature, e.g.
endpoint invoke persistent LoadAllBalances():
endpoint invoke ephemeral Flip():
If the endpoint mutates both persistent and ephemeral state, persistent
keyword needs to be in the signature.
deploy
and enlist
endpoints do not need explicit persistent/ephemeral keywords in their signatures as the default for deploy or enlist is to mutate the state.
coco TokenLedger
state persistent:
name String
symbol String
supply U64
balances Map[Address]U64
endpoint deploy Init(name String, symbol String):
mutate name -> TokenLedger.State.name
mutate symbol -> TokenLedger.State.symbol
endpoint deploy SeedSupply(name String, 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 invoke persistent LoadAllBalances():
memory local_balances Map[Address]U64
local_balances[Address(0x1111111111111111111111111111111111111111111111111111111111111111)] = 100
local_balances[Address(0x2222222222222222222222222222222222222222222222222222222222222222)] = 200
mutate balances <- TokenLedger.State.balances:
// this may be expensive if the map is large
// so we need to explicitly "disperse" the map
disperse balances <- local_balances
coco Eflipper
state ephemeral:
value Bool
endpoint enlist Init():
pass
endpoint invoke readonly Get() -> (value Bool):
observe value <- Sender.Eflipper.value
endpoint invoke ephemeral Set(value Bool):
set(value: value)
endpoint invoke ephemeral Toggle():
mutate value <- Sender.Eflipper.value:
value = !value
func ephemeral set(value Bool):
mutate value -> Sender.Eflipper.value