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

Like most programming languages, it is possible to do conditional branching use if statement. Logic operators supported include and, or and xor.

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

Custom Events

Example 1

Showcases how a contract can declare and emit custom events.

Example 2

A more complex version of the previous example, showcasing custom description scripts.

Example 3

A yet more complex version of the previous examples, showcasing custom description scripts and also struct declarations.

Decimals

There is compiler support for decimal numbers. Note that internally those are converted to Number types in fixed point format.

Enums

There is compiler support for enumerated value types, that map directly to the Phantasma VM enum type.

Inline asm

Inline asm allows to write assembly code that is then inserted and merged into the rest of the code. This feature is useful as an workaround for missing features in the compiler.

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.

Methods variables

It is possible to use methods as variables.

Returning multiple values

It is possible in Phoenix Smart Language 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.

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 NFT

  • description, returns the descriptions of the NFT

  • imageURL, returns the image URL of the NFT

  • infoURL, returns the info URL of the NFT

Random numbers

It is possible to generate pseudo random numbers, and also to control the generation seed. If a seed is not specified, then the current transaction hash will be used as seed, if available.

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.

Deploy contract

This example showcases a script that deploys a token contract.

Minting script

This example showcases a script that mints a custom NFT.

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.

Simple Sum

Simple contract that sums two numbers and returns the result.

Strings

Default usage

Simple contract that shows how to use strings and built-in type methods, string.length() in this specific case.

String Manipulation

The compiler supports casting strings into number arrays (unicode values) and number arrays back to strings.

Switch case

Simple contract that received a number and returns a string, while using a switch case.

Tasks

A task allows a contract method to run periodically without user intervention. Tasks can't have parameters, however you can use Task.current() along with a global Map to associate custom user data to each task.

Type interface in variable declarations

It is possible to let Phoenix Smart Language compiler auto-detect type of a local variable if you omit the type and provide an initialization expression.

Last updated