Smart Contract Assets

Zetrix Smart Contract Assets Example

Overview

Zetrix smart contracts are written in JavaScript, conforming to the ECMAScript standard (ECMA-262). Each contract must define two key functions:

  • init: The initialization function

  • main: The execution entry point

Both functions accept a single input parameter, input, which is a string specified at the time of contract invocation.

Additional Resources

  • For contract fundamentals, see Introduction to Smart Contract.

  • For coding rules and standards, refer to Syntax in Smart Contract.

Java-based Development Scenarios

This section outlines three Java-integrated smart contract scenarios:

  1. Scenario One: Implements contract creation

  2. Scenario Two: Implements contract triggering

  3. Scenario Three: Implements contract querying

Each scenario provides contract code examples aligned with real use cases.

Scenario One

An asset issuer issues smart contract assets, the total amount of which is 1 billion, the issuance code is CGO, and the name is Contract Global. The details are as follows:

Field
Required?
Example
Description

name

Yes

Contract Global

token name

symbol

Yes

CGO

token code

decimals

Yes

8

Precision

totalSupply

Yes

1000000000

total amount

The execution flow for this scenario consists of the following steps:

  1. Compressing Text – Minify the smart contract code before deployment.

  2. Creating SDK Instances – Initialize the SDK to interact with the Zetrix blockchain.

  3. Creating the Asset Issuer Account – Generate the account that will issue digital assets.

  4. Activating the Asset Issuer Account – Fund the account to make it active on-chain.

  5. Obtaining the Serial Number (Nonce) – Retrieve the current nonce of the issuer account.

  6. Assembling the Contract Account Creation and CGO Token Issuance – Prepare the transaction payload combining account creation and token issuance.

  7. Serializing the Transaction – Convert the transaction into a byte stream format.

  8. Signing the Transaction – Use the private key to sign the serialized transaction.

  9. Sending the Transaction – Submit the signed transaction to the Zetrix network.

  10. Querying Execution Status – Check whether the transaction was successfully processed.

Compressing Text

Open the online text compression page: tool from the third partyarrow-up-right , copy the verified smart contract code to the edit box on the page, then click the Compress button to copy the compressed string, as shown below:

Creating SDK Instances-1

Create an instance and set the url (the IP and port of a deployed node).

Environment description:

Network Environment
IP

Experience Zetrix

seed1-node.zetrix.com

Sample code

In the Zetrix network, each block is generated every 10 seconds, and each transaction requires only one confirmation to get the final state of the transaction.

Creating the Asset Issuer Account

The code to create the asset issuer account is as follows:

Return value:

Note: An account created in this way is an account that is not activated.

Activating the Asset Issuer Account

When the account is not activated, it needs to be activated by an activated (chained) account. Please skip this section if your account has been activated.

Note: Use an activated account to transfer gas to an inactive account.

Obtaining the Serial Number of the Asset Issuer Account-1

Each account maintains its own serial number, which starts from 1 and is incremented. A serial number marks a transaction for that account. The code to obtain the serial number of the asset issuer account is as follows:

Return value:

Note: If an account is not queried, it means that the account is not activated.

Assembling the Creation of the Contract Account and the CGO Token Issuance

The code assigns the compressed contract code to the payload variable. The specific code is as follows:

Serializing the Transaction-1

Serializing transactions is for the convenience network transmission.

Note:

  • feeLimit: the maximum transaction fee that the originator of this transaction will pay for this transaction. Please fill in 10.08Gas for the issuance of this asset.

  • nonce: the transaction serial number of the originator of this transaction, which is obtained by adding 1 to the nonce value of the current account.

The specific code of serializing transactions is as follows. The parameter nonce in the example is the account serial number obtained by calling getAccountNonce, and the parameter operations is the asset issuance operation obtained by calling buildOperations.

Return value:

Signing the Transaction-1

All transactions need to be signed, and a transaction will not take effect until it is signed. The signature result includes signature data and a public key. The specific code for signing transactions is as follows. The parameter transactionBlob in the example is the serialized transaction string obtained by calling seralizeTransaction.

Return value:

Sending the Transaction-1

Send the serialized transaction and the signature to Zetrix. The specific code for sending the transaction is as follows. The parameter transactionBlob in the example is the serialized transaction string obtained by calling seralizeTransaction, and signatures is the signature data obtained by calling signTransaction.

Return value:

Querying Whether the Transaction Was Executed Successfully-1

The following code shows how to query by calling the interface. The parameter txHash in this example is the transaction hash (the unique identifier of the transaction) obtained by calling submitTransaction.

Return value: 0

Error code
Description

0

Successful operation

1

Inner service defect

2

Parameters error

3

Objects already exist, such as repeated transactions

4

Objects do not exist, such as null account, transactions and blocks etc.

5

Transactions expired. It means the transaction has been removed from the buffer, but it still has probability to be executed.

7

Math calculation is overflown

20

The expression returns false. It means the TX failed to be executed currently, but it still has probability to be executed in the following blocks .

21

The syntax of the expression returns are false. It means that the TX must fail.

90

Invalid public key

91

Invalid private key

92

Invalid assets

93

The weight of the signature does not match the threshold of the operation.

94

Invalid address

97

Absent operation of TX

98

Over 100 operations in a single transaction

99

Invalid sequence or nonce of TX

100

Low reserve in the account

101

Sender and receiver accounts are the same

102

The target account already exists

103

Accounts do not exist

104

Low reserve in the account

105

Amount of assets exceeds the limitation ( int64 )

106

Insufficient initial reserve for account creation

111

Low transaction fee

114

TX buffer is full

120

Invalid weight

121

Invalid threshold

144

Invalid data version of metadata

146

Exceeds upper limitation

151

Failure in contract execution

152

Failure in syntax analysis

153

The depth of contract recursion exceeds upper limitation

154

The TX submitted from the contract exceeds upper limitation

155

Contract expired

160

Fail to insert the TX into buffer

11060

BlockNumber must be bigger than 0

11007

Failed to connect to the network

12001

Request parameter cannot be null

20000

System error

Scenario Two

This example demonstrates the contract triggering function:

The asset issuer ztxSXVGt5ujjAe4hr11VqhjWpJdKqn6QfDVUX allocates 20,000 CGO to itself via smart contract account ztxSfaizkSkNovPtVCh5X2mwtd649R6pG9YS4 on Zetrix, and transfers 10,000 CGO to account ztxSqAo6moyR5bYyTnKQXgkrzyiSBKXSwKHu4.

The implementation steps include: Creating SDK instances, Retrieving asset issuer serial number, Assembling CGO allocation and transfer operations, Serializing, Signing, Sending the transaction, and Querying execution status.

Creating SDK Instances-2

Create an instance and set the url (the IP and port of a deployed node).

In the Zetrix network, each block is generated every 10 seconds, and each transaction requires only one confirmation to get the final state of the transaction.

Environment description:

Network Environment
IP

Experience Zetrix

seed1-node.zetrix.com

Obtaining the Serial Number of the Asset Issuer Account-2

Each Zetrix account maintains a unique serial number (also known as a nonce) that starts at 1 and increments with every successful transaction. This serial number ensures proper transaction ordering and prevents replay attacks.

Return value:

Assembling CGO Allocation and CGO Transfer

This section contains two operations: allocating CGO and transferring CGO. The following is the sample code:

Serializing Transactions-2

Serializing transactions for the convenience of network transmission.

Note:

  • feeLimit: the maximum transaction fee that the originator of this transaction will pay for this transaction.To create a contract account and issue token operation, please fill in 0.02 Gas.

  • nonce: the transaction serial number of the originator of this transaction, which is obtained by adding 1 to the nonce value of the current account.

The specific code of serializing the transaction is as follows. The parameter nonce in the example is the account serial number obtained by calling getAccountNonce, and the parameter operations is the asset issuance operation obtained by calling buildOperations. The following is the sample code for serializing the transaction:

Return value:

Signing Transactions-2

All transactions need to be signed, and a transaction will not take effect until it is signed. The signature result includes signature data and a public key. The specific code for signing transactions is as follows. The parameter transactionBlob in the example is the serialized transaction string obtained by calling seralizeTransaction.

Return value:

Sending Transactions-2

Send the serialized transaction and signature to Zetrix. The specific code for sending transactions is as follows. The parameter transactionBlob in the example is the serialized transaction string obtained by calling seralizeTransaction, and the parameter signatures is the signature data obtained by calling signTransaction.

Return value:

Querying whether the Transaction Was Executed Successfully-2

The following code shows how to query by calling the interface. The parameter txHash in this example is the transaction hash (the unique identifier of the transaction) obtained by calling submitTransaction.

Return value: 0

Error code
Description

0

Successful operation

1

Inner service defect

2

Parameters error

3

Objects already exist, such as repeated transactions

4

Objects do not exist, such as null account, transactions and blocks etc.

5

Transactions expired. It means the transaction has been removed from the buffer, but it still has probability to be executed.

7

Math calculation is overflown

20

The expression returns false. It means the TX failed to be executed currently, but it still has probability to be executed in the following blocks .

21

The syntax of the expression returns are false. It means that the TX must fail.

90

Invalid public key

91

Invalid private key

92

Invalid assets

93

The weight of the signature does not match the threshold of the operation.

94

Invalid address

97

Absent operation of TX

98

Over 100 operations in a single transaction

99

Invalid sequence or nonce of TX

100

Low reserve in the account

101

Sender and receiver accounts are the same

102

The target account already exists

103

Accounts do not exist

104

Low reserve in the account

105

Amount of assets exceeds the limitation ( int64 )

106

Insufficient initial reserve for account creation

111

Low transaction fee

114

TX buffer is full

120

Invalid weight

121

Invalid threshold

144

Invalid data version of metadata

146

Exceeds upper limitation

151

Failure in contract execution

152

Failure in syntax analysis

153

The depth of contract recursion exceeds upper limitation

154

The TX submitted from the contract exceeds upper limitation

155

Contract expired

160

Fail to insert the TX into buffer

11060

BlockNumber must be bigger than 0

11007

Failed to connect to the network

12001

Request parameter cannot be null

20000

System error

Scenario Three

This example mainly implements contract query function.

Check the CGO balance of the account ztxSfaizkSkNovPtVCh5X2mwtd649R6pG9YS4 via the smart contract account ztxSqAo6moyR5bYyTnKQXgkrzyiSBKXSwKHu4 on Zetrix.

This section mainly introduces Creating SDK Instances and Querying Balance.

Creating SDK Instances-3

Create an instance and set the url (the IP and port of a deployed node).

In the Zetrix network, each block is generated every 10 seconds, and each transaction requires only one confirmation to get the final state of the transaction. Environment description:

Network Environment
IP

Experience Zetrix

seed1-node.zetrix.com

Querying Balance

The sample code for querying the balance is as follows:

Return value:

Last updated