Smart Contract
Last updated
Last updated
In Nebulas, there are two supported smart contract languages:
They are supported by the integration of Chrome V8, a widely used JavaScript engine developed by The Chromium Project for Google Chrome and Chromium web browsers.
The diagram below is the Execution Model of Smart Contract:
All src of Smart Contract and arguments are packaged in Transaction and deployed on Nebulas.
The execution of Smart Contract are divided into two phases:
Preprocess: inject tracing instruction, etc.
Execute: generate executable src and execute it.
Contracts in Nebulas are similar to classes in object-oriented languages. They contain persistent data in state variables and functions that can modify these variables.
A contract must be a Prototype Object or Class in JavaScript or TypeScript.
A Contract must include an init
function, it will be executed only once when deploying. Functions, named starting with _
are private
, can't be executed in Transaction. The others are all public
and can be executed in Transaction.
Since Contract is executed in Chrome V8, all instance variables are in memory, it's not wise to save all of them to state trie in Nebulas. In Nebulas, we provide LocalContractStorage
and GlobalContractStorage
objects to help developers define fields needing to be saved to state trie. And those fields should be defined in constructor
of Contract, before other functions.
The following is a sample contract:
In JavaScript, there is no function visibility, all functions defined in prototype object are public.
In Nebulas, we define two kinds of visibility public
and private
:
public
All functions whose name matches regexp ^[a-zA-Z$][A-Za-z0-9_$]*$
are public, except init
. Public functions can be called via Transaction.
private
All functions whose name starts with _
are private. A private function can only be called by public functions.