Ethereum and solidity define state variables which are held in the contract. What is the structure in EOS? Is there also a state variable system at the contract level? Is the virtual machine defined as in Ethereum as a deterministic environment in which API calls etc are not permitted and only events link to the outside world (I understand events are not implemented in EOS now).
1 Answer
To preserve state between different actions
or transactions
, EOS offers the multi-index DB API, which allows you to create an indexed, persistent table
/ struct
to preserve state. See here for a guide, and here for the API reference.
Excerpt:
EOSIO provides a set of services and interfaces that enable contract developers to persist state across action, and consequently transaction, boundaries. Without persistence, state that is generated during the processing of actions and transactions will be lost when processing goes out of scope. The persistence components include:
- Services to persist state in a database
- Enhanced query capabilities to find and retrieve database content
- C++ APIs to these services, intended for use by contract developers
- C APIs for access to core services, of interest to library and system developers
Example from docs:
struct limit_order {
uint64_t id;
uint128_t price;
uint64_t expiration;
account_name owner;
auto primary_key() const { return id; }
uint64_t get_expiration() const { return expiration; }
uint128_t get_price() const { return price; }
EOSLIB_SERIALIZE( limit_order, ( id )( price )( expiration )( owner ) )
};
...
eosio::multi_index< N( orders ), limit_order,
indexed_by< N( byexp ), const_mem_fun< limit_order, uint64_t, &limit_order::get_expiration> >,
indexed_by< N( byprice ), const_mem_fun< limit_order, uint128_t, &limit_order::get_price> >
> orders( N( limitorders ), N( limitorders ) );
...
And, yes, EOS doesn't currently allow external API calls from the blockchain, but inter-blockchain communication (IBC) and oracles are the suggested solutions for this in the future, with IBC being actively worked on by BlockOne.