We are delighted to announce that the latest version of SmartX is now available. SmartX now fully supports Wasm contracts, which are now live on the Ontology MainNet. In order to further the cause of global developers, the latest version of SmartX now supports the deployment and call of Wasm contracts. For further information, read below.

1. Generating and Testing Contracts

Before introducing how to deploy and invoke Wasm contracts with SmartX, let’s have a look at how to develop a simple testing contract using the Wasm contract template. For a detailed tutorial on developing Ontology Wasm contracts, please refer to:

Step 1: Clone the contract template

Step 2: Modify the src/lib.rs file

Note: The above contract contains three methods:

fn add(a: U128, b: U128) -> U128

Return the sum of A and B；

Return the sum of A and B； fn hello() -> String

Return “hello world”；

Return “hello world”； fn say(data: &str) -> String

Return “say”.to_string() + data；

Step 3: Compile and optimize contracts

The build.sh script encapsulates the function of compiling and optimizing contracts, which can be executed directly.

The bytecode file generated by compilation is placed in the generated output folder. The directory structure is as follows:

rust_wasm_contract_template.wasm is the Wasm bytecode file required for testing SmartX;

is the Wasm bytecode file required for testing SmartX; rust_wasm_contract_template.wasm.strIs is the hex encoding of Wasm bytecode;

is the hex encoding of Wasm bytecode; rust_wasm_contract_template.wasm.wast is the Wasm contract in .wast format.

2. Deploying Contracts

Firstly, users have to log onto SmartX and then enter the Project List page. On this page, click the Open Wasm file button.

Enter the name of Wasm smart contract and choose the rust_wasm_contract_template.wasm file generated above. Click the confirm button, which will lead to a project of Wasm contract, for better management in the future.

After opening the newly created Wasm contract project, the bytecode of the Wasm contract was shown in the area of contract editing. The bytecode in this part can be replaced.

Next step, it is time to deploy the Wasm contract. This step is no different to that of deploying a NeoVM contract. Fill the related information in the right deploy tool page, and click the deploy button, the Chrome plug-in wallet Cyano Wallet (which should be installed to the most updated version in advance) will be called, and users will be asked to fill in the wallet password. If there is enough ONG in Cyano Wallet, the Wasm contract will be deployed to Ontology network after a short period. You can click Apply ONG as shown on the below image to apply for ONG on TestNet.

3. Calling

After successful deployment, you will encounter the Run page of the contract. Here we can call the Wasm contract.

We need to enter the name of the function to be called add, add the parameters required for the function, select the correct type for the parameter, and enter the parameter value. The add function requires two data types of u128. The u128 in the contract corresponds to the Long type in SmartX, so the parameter type should be Long. Then choose either pre-execute or run button. The result of the contract execution will be displayed in the log area on the lower left-hand side. You only need to click on pre-execution, because data on the chain will not yet be updated, and the printed result in log area is hex encoding, which is used to decode.

Now that we have completed the deployment and invocation of Wasm contracts on SmartX, let’s dive into the call of hello and say functions. The hello function just returns the “hello world” string therefore no parameters need to be entered. Check the call screenshot as follows:

The method of say results in adding the prefix ”say” to in-parameters and return. Parameters of type & STR are required. Type & STR parameters in the contract correspond to type string on SmartX. The call example is as follows:

Summary

In this article, we have used an example to briefly introduce the main process of deploying and invoking a Wasm contract on SmartX. Note the following points:

Before use, the following preparation is required: