🖥️ Examples
Array example
Here is a simple example of how to declare and initialize an array.
contract test {
import Array;
public getStrings(): array<string> {
local result : array<string> = {"A", "B", "C"};
return result;
}
}Call contract from another contract
Example showcasing calling contract storage directly from another contract. Basic version where another storage map is called, and this value is incremented and stored in another contract storage.
contract test {
import Map;
import Storage;
import Call;
global counters: storage_map<number, number>;
private getContractCount(tokenId:number):number {
local count:number = Call.interop<number>("Map.Get", "OTHERCONTRACT", "_storageMap", tokenId, $TYPE_OF(number));
return count;
}
public updateCount(tokenID:number) {
local contractCounter:number = this.getContractCount(tokenID);
contractCounter += 1;
counters.set(tokenID, contractCounter);
}
public getCount(tokenID:number):number {
local temp:number = counters.get(tokenID);
return temp;
}
}Conditions
Conditional branching uses if and else. The logical operators are && and ||.
contract test {
public isEvenAndPositive(n:number):bool
{
if (n > 0 && n % 2 == 0)
{
return true;
}
else
{
return false;
}
}
}Counter per Address
Another contract that implements a counter, this time unique per user address. Showcases how to validate that a transaction was done by user possessing private keys to ‘from’ address
contract test {
import Runtime;
import Map;
global counters: storage_map<address, number>;
public increment(from:address)
{
Runtime.expect(Runtime.isWitness(from), "witness failed");
local temp: number;
temp = counters.get(from);
temp += 1;
counters.set(from, temp);
}
}Custom Events
emit compiles to a Runtime.NotifyV2 call. The chain stores the event with kind Custom_V2 and the event name, for example MyPayment. An application can identify the event without the contract ABI: RPC returns the name in the event’s name field.
Contracts compiled with pha-tomb before 2.2.0 emit events through Runtime.Notify with kind Custom plus the index of the event. The current chain accepts Runtime.Notify from a contract only with kind Custom_V2 and rejects every other kind. Recompile such a contract with pha-tomb 2.2.0 or later and upgrade it. RPC returns the events that such contracts emitted earlier as Custom_V2, with the name from the contract ABI when the ABI declares that event.
Example 1
Showcases how a contract can declare and emit custom events.
contract test {
import Runtime;
import Token;
event MyPayment:number = "{address} paid {data}"; // here we use a short-form description
public paySomething(from:address, x:number)
{
Runtime.expect(Runtime.isWitness(from), "witness failed");
local price: number = 10;
local thisAddr:address = $THIS_ADDRESS;
Token.transfer(from, thisAddr, "SOUL", price);
emit MyPayment(from, price);
}
}Example 2
A more complex version of the previous example, showcasing custom description scripts.
description payment_event {
code(from:address, amount:number): string {
local result:string = "";
result += from;
result += " paid ";
result += amount;
return result;
}
}
contract test {
event MyPayment:number = payment_event; // here we use a short-form declaration
// everything else would be same as previous example
}Example 3
A yet more complex version of the previous examples, showcasing custom description scripts and also struct declarations.
struct my_event_data {
amount:number;
symbol:string;
}
description payment_event {
code(from:address, data:my_event_data): string {
local result:string = "";
result += from;
result += " paid ";
result += data.amount;
result += " ";
result += data.symbol;
return result;
}
}
contract test {
event MyPayment:my_event_data = payment_event; // here we use a short-form declaration
// everything else would be same as previous examples
}Decimals
There is compiler support for decimal numbers. Note that internally those are converted to Number types in fixed point format.
contract test {
global val: decimal<4>; // the number between <> is the number of decimal places
constructor(owner:address)
{
val = 2.1425;
}
public getValue():number
{
return val; // this will return 21425, which is the previous value in fixed point format
}
public getDecimals():number
{
return val.decimals(); // this returns 4 as result
}
public sum(other:decimal<4>):number
{
return val + other;
}
}Enums
There is compiler support for enumerated value types, that map directly to the Phantasma VM enum type.
// NOTE - like other custom types, it is declared outside the scope of a contract
enum MyEnum { A = 0, B = 1, C = 2}
// if the numbers are sequential, it is ok to ommit them, eg:
//enum MyEnum { A, B, C}
contract test {
global state: MyEnum;
constructor(owner:address)
{
state = MyEnum.B;
}
public getValue():MyEnum
{
return state;
}
}Inline asm
Inline asm inserts VM assembly into the compiled code. A local the assembly writes must be initialized before the block and used after it, because the compiler rejects uninitialized and unused locals.
script startup {
import Runtime;
code() {
local temp:string = "";
asm {
LOAD $temp "hello"
}
Runtime.log(temp);
}
}Map support
The compiler supports generic types, including maps. Maps are one of the few types that don’t have to initialized in the constructor.
contract test {
import Map;
global my_state: storage_map<address, number>;
constructor(owner:address)
{
my_state.set(owner, 42);
}
public getState(target:address):number
{
return my_state.get(target);
}
}Calling A Method Through Call.method
Call.method<T>(name, args...) calls one of the contract’s own methods by
name and returns its result as T.
contract test {
import Call;
private sum(x:number, y:number) : number
{
return x + y;
}
public fetch(val:number) : number
{
return Call.method<number>(sum, val, 1);
}
}Returning multiple values
It is possible in TOMB to return multiple results from a single method. The method return type must be marked with an asterisk, then multiple returns can be issued. A return without expression will terminate the method execution.
contract test {
// this method returns an array of strings (could also be numbers, structs, etc)
public getStrings(): string* {
return "hello";
return "world";
return;
}
}NFTs
Showcases how to implement an NFT, showcasing all details including ROM, RAM and token series.
When creating an NFT, they must have 4 default properties implemented and they’re:
name, returns the name of the NFTdescription, returns the descriptions of the NFTimageURL, returns the image URL of the NFTinfoURL, returns the info URL of the NFT
struct luchador_rom
{
genes:bytes;
name:string;
}
struct luchador_ram
{
experience:number;
level:number;
}
struct item_rom
{
kind:number;
quality:number;
}
struct item_ram
{
power:number;
level:number;
}
token NACHO {
import NFT;
global _owner: address;
const LUCHADOR_SERIES: number = 1;
const LUCHADOR_SUPPLY: number = 100000;
const ITEM_SERIES: number = 2;
const ITEM_SUPPLY: number = 500000;
property name: string = "Nachomen";
property isBurnable: bool = true;
property isFinite: bool = true;
property isFungible: bool = false;
property maxSupply: number = LUCHADOR_SUPPLY + ITEM_SUPPLY;
nft luchador<luchador_rom, luchador_ram> {
property name: string {
return _ROM.name;
}
property description: string {
return "Luchador with level " + _RAM.level;
}
property imageURL: string {
return "https://example.invalid/img/luchador/"+ _tokenID;
}
property infoURL: string {
return "https://example.invalid/api/luchador/"+ _tokenID;
}
}
nft item<item_rom, item_ram> {
property name: string {
local kind: number = _ROM.kind;
switch (kind)
{
case 1: return "Potion";
case 2: return "Gloves";
default: return "Item #" + _ROM.kind;
}
}
property description: string {
return "Item level " + _RAM.level;
}
property imageURL: string {
return "https://example.invalid/img/item/"+ _tokenID;
}
property infoURL: string {
return "https://example.invalid/api/item/"+ _tokenID;
}
}
constructor (addr:address) {
_owner = addr;
// at least one token series must exist, here we create 2 series
// they don't have to be created in the constructor though, can be created later
NFT.createSeries(_owner, $THIS_SYMBOL, LUCHADOR_SERIES, LUCHADOR_SUPPLY, TokenSeries.Unique, luchador);
NFT.createSeries(_owner, $THIS_SYMBOL, ITEM_SERIES, ITEM_SUPPLY, TokenSeries.Unique, item);
}
}Scripts
Default
A script is something that can be used either for a transaction or for an API invokeScript call. This example showcases a simple script with one argument, that calls a contract. Note that for scripts with arguments, for them to run properly you will have to push them into the stack before.
script startup {
import Call;
code(target:address) {
local temp:number = 50000;
Call.contract<none>("stake", "Unstake", target, temp);
}
}Deploy contract
This example showcases a script that deploys a token contract.
token GHOST {
property name: string = "Ghost";
}
script deploy {
import Token;
import Module;
code() {
Token.create(@P2KAkQRrL62zYvb5198CHBLiKHKr4bJvAG7aXwV69rtbeSz, Module.getScript(GHOST), Module.getABI(GHOST));
}
}Minting script
This example showcases a script that mints a custom NFT.
struct my_rom_data {
name:string;
counter:number;
}
script token_minter {
import NFT;
code(source:address, target:address) {
local rom_data:my_rom_data = Struct.my_rom_data("hello", 123);
NFT.mint(source, target, "LOL", rom_data, "ram_can_be_anything", 1);
}
}Simple Counter
Simple contract that implements a global counter (that can be incremented by anyone who calls the contract). Note that any global variable that is not generic must be initialized in the contract constructor.
contract test {
global counter: number;
constructor(owner:address)
{
counter= 0;
}
public increment()
{
if (counter < 0){
throw "invalid state";
}
counter += 1;
}
}Simple Sum
Simple contract that sums two numbers and returns the result.
contract test {
public sum(a:number, b:number):number
{
return a + b;
}
}Strings
Default usage
Simple contract that shows how to use strings and built-in type methods, string.length() in this specific case.
contract test {
global val: string;
constructor(owner:address)
{
val = "hello";
val += " world";
}
public getLength():number
{
return val.length();
}
}String Manipulation
The compiler supports casting strings into number arrays (unicode values) and number arrays back to strings.
contract test {
import Array;
public toUpper(s:string):string
{
local my_array: array<number>;
// extract chars from string into an array
my_array = s.toArray();
local length :number = Array.length(my_array);
for (local i = 0; i<length; i+=1)
{
local ch : number = my_array[i];
if (ch >= 97) {
if (ch <= 122) {
my_array[i] = ch - 32;
}
}
}
// convert the array back into a unicode string
local result:string = String.fromArray(my_array);
return result;
}
}Switch case
Simple contract that received a number and returns a string, while using a switch case.
contract test {
public check(x:number): string {
switch (x) {
case 0: return "zero";
case 1: return "one";
case 2: return "two";
default: return "other";
}
}
}Type interface in variable declarations
It is possible to let the TOMB compiler auto-detect the type of a local variable if you omit the type and provide an initialization expression.
contract test {
public calculate():string {
local a = "hello ";
local b = "world";
return a + b;
}
}