Introduction
Smart Contracts can be programmed using our own interpreted language running in a Virtual Machine, allowing safe and secure execution of code on the blockchain.
Smart Contracts are a way to automate the execution of a contract, without the need of a third party. They are self-executing contracts with the terms of the agreement directly written into lines of code.
A lot of use cases can be found for Smart Contracts, such as:
- Decentralized applications (dApps)
- Tokenization of assets
- Voting systems
- Supply chain management
- etc.
Please note that they are not yet available on the mainnet, but are planned to be released in the future.
You can find the documentation of the XVM (opens in a new tab) below.
XELIS VM
XVM is a virtual machine with its own interpreted language for the XELIS network developed in Rust. It supports constants, functions, while/foreach loops, arrays and structures. The syntax is strongly inspired by Rust and Golang.
All the verifications are mainly made at the level of the Parser to check the conformity of the code to be interpreted.
The different primitive types are:
u8
(unsigned 8 bits)u16
(unsigned 16 bits)u32
(unsigned 32 bits)u64
(unsigned 64 bits)u128
(unsigned 128 bits)bool
string
struct
optional<T>
where T is another type (it allow the value to be nullable)
File extension is .xel
Documentation
the semicolon is optional, thus can be added if desired without any difference in the code.
Recursive functions are allowed, but limited to a configurable depth.
A environment system is completely customizable to set your own native functions. This helps to manage exactly what a program can interact with. Custom structs are also available.
Numbers
An error will be returned by the interpreter if an overflow is detected without causing a panic.
Rules
- The value must be greater than or equal to
0
. - You can put
_
(underscore) for a better readability. - If no type is specified on the value, then
u64
will be the default.
Examples
let my_byte: u8 = 10
let my_u16: u16 = 70
let my_u32: u32 = 999
let my_int: u64 = 25655
let my_u128: u128 = 100_000_000L
Variable
Constant variables must be declared outside a function using the const
keyword.
Rules
- All variables must be declared with
let
orconst
keyword. - Variable names must only consist of alphanumeric characters.
- Must specify a type.
- If no value is set,
null
is set by default.
Examples
const hello: string = "hello"
...
let world: string = "world"
Casting
Values of built-in types can be cast into other built-in types easily using the as
keyword.
Rules
- Both value types must be a built-in type.
Examples
let id: u128 = 1337
let b: u8 = id as u8
let id_str: string = id as string
Function
The entry
function is a "public callable" function and must return a u64
value.
Rules
- Must start with
func
orentry
keyword. - Signature is based on function name and parameters.
- For type functions, the type must not be primitive.
- Recursive functions are allowed.
Examples
entry foo() { ... }
func foo() { ... }
func foo(): u64 { ... }
func foo(a: u64, b: u64) { ... }
func (f Foo) bar() { ... }
Structure
A structure can contain other structures.
Rules
- The name must be unique.
- Name should start with a uppercase letter.
- Only letters are allowed in name.
- The last field does not need a comma.
Examples
struct MyStruct {
message: string,
value: u64
}
Ternary
Rules
- A
bool
condition is required. - The two values that can be returned must be of the same type.
Examples
let score: u64 = is_winner() ? 20 : 0
Negation operator
Rules
- A
bool
condition is required after it.
Examples
let negative: bool = !condition
Array
Rules
- All values must be of the same specified type.
Examples
let array: u64[] = [10, 20, 30, 40]
...
let dim: u64[][] = [[34, 17], [8, 14], [0, 69]]
If
Rules
- Have a
bool
condition.
Examples
if condition {
...
}
if (i > 20 && i != 25) || i == 0 {
...
}
Else
Rules
- It must be preceded by an
if
condition.
Examples
else {
...
}
Else if
Rules
- It must be preceded by an
if
or anelse if
condition. - Have a boolean condition.
Examples
else if condition {
...
}
else if my_struct != null {
...
}
While
Rules
- Have a boolean condition.
Examples
while condition {
...
}
Foreach
Rules
- Have the name of a variable.
- Have an array to go through
Examples
foreach val in values {
...
}
For
Rules
- Have the name of a variable.
- Have a boolean condition.
- Have an assign operator.
Examples
for i: u64 = 0; i < 10; i += 1 {
...
}
Break
Rules
- Must be in a loop (
foreach
,for
,while
).
Examples
while condition {
if i % 10 == 0 {
break;
}
...
}
Continue
Rules
- Must be in a loop (
foreach
,for
,while
).
Examples
while condition {
if i % 10 == 0 {
continue;
}
...
}
Return
Rules
- Must not have any code after.
- If the function returns a value, the return must return a value.
Examples
func foo(): string {
return "Hello World!"
}
func bar() {
if condition {
return
}
foo()
}
Scope
Allows you to isolate a part of the code / variables created.
Rules
- No specific rules.
Examples
{
...
}