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 deployer persistent SeedSupply()
In modules having a persistent state, it is necessary to have a deployer endpoint to initialize this state. The ‘persistent’ keyword after the deployer endpoint denotes that it performs a stateful change.
ERC20.State.name
In this the dot is used to access the state of the module followed by the name of the field.
Enlisters will be used to initialize the ephemeral state similar to deployers that initialize the persistent state.
disperse
keyword
Coco takes care to avoid expensive operations like transferring a large amount of data from the storage (state). E.g., if we want to only alter a single value in the map, there’s no need to load a complete map and store it back.
If we actually want to transfer (load or store) a complete array, map or a class from the storage, we need to explicitly prepend the assignment or append statement with disperse
keyword.
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 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 invokable 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