API References

Methods of the Chain Object

This section describes some methods of the Chain object, including:

Chain.load, Chain.store, Chain.del, Chain.getBlockHash, Chain.tlog, Chain.getAccountMetadata, Chain.getBalance, Chain.getAccountAsset, Chain.getContractProperty, Chain.payCoin, Chain.issueAsset, Chain.payAsset, Chain.delegateCall, Chain.delegateQuery, Chain.contractCall, Chain.contractQuery, and Chain.contractCreate.

Chain.load

  • Description

    Get the metadata information of the contract account.

  • Function call

Chain.load(metadata_key);
  • Parameter description

    • Metadata_key: The keyword for metadata.

  • Example

    let value = Chain.load('abc');
    /*
      
      Permission: Read-only.
      Return value: Return a string if it succeeds, such as 'values'; return false if it fails.
    */

Chain.store

  • Description

    Store the metadata information of the contract account.

  • Function call

  • Parameter description

    • metadata_key: The keyword for metadata.

    • metadata_key: The content of metadata.

  • Example

Chain.del

  • Description

    Delete the metadata information of the contract account.

  • Function call

  • Parameter description

    • metadata_key: The keyword for metadata.

    • metadata_key: The metadata content.

  • Example

Chain.getBlockHash

  • Description

    Get block information.

  • Function call

  • Parameter description

    • offset_seq: The offset from the last block ranges: [0,1024).

  • Example

Chain.tlog

  • Description

    Output transaction logs.

  • Function call

  • Parameter description

    • tlog will generate a transaction written on the block.

    • topic: The log subject, which must be a string type with a parameter length of (0,128].

    • args...: It can contain up to 5 parameters, which can be string, numeric or Boolean type, with each parameter length (0,1024].

  • Example

Chain.getAccountMetadata

  • Description

    Get the metadata of the specified account.

  • Function call

  • Parameter description

    • account_address: The account address.

    • metadata_key: The keyword for metadata.

  • Example

Chain.getBalance

  • Description

    Get the coin amount of the account.

  • Function call

  • Parameter description

    • address: The account address

  • Example

Chain.getAccountAsset

  • Description

    Get asset information for an account

  • Function call

  • Parameter description

    • account_address: The account address.

    • asset_key: The asset attributes.

  • Example

Chain.getAccountPrivilege

  • Description

    Get privilege info for an account.

  • Function call

  • Parameter description

    • account_address: The account address。

  • Example

Chain.getContractProperty

  • Description

    Get the attributes of the contract account.

  • Function call

  • Parameter description

    • contract_address: The contract address.

  • Example

Chain.payCoin

  • Description

    Transfer gas.

  • Function call

  • Parameter description

    • address: The target address.

    • amount: The amount of Gas.

    • input: Optional, the contract parameter. By default, it is an empty string if it is not filled in.

  • Example

Chain.issueAsset

  • Description

    Issue assets.

  • Function call

  • Parameter description

    • code: The asset code.

    • amount: The amount of the asset to be issued.

  • Example

Chain.payAsset

  • Description

    Transfer assets

  • Function call

  • Parameter description

    • address: The target address.

    • issuer: The asset issuer.

    • code: The asset code.

    • amount: The amount to be transferred.

    • input: Optional, the contract parameter. By default, it is an empty string if it is not filled in.

  • Example

Chain.delegateCall

  • Description

    Delegate call.

  • Function call

  • Parameter description

    • contractAddress: The address of the contract to be called.

    • input:Input parameter.

    The Chain.delegateCall function will trigger the main function of the contract to be called, and the Chain.delegateCall function will assign the execution environment of the current contract to the contract to be called.

  • Example

Chain.delegateQuery

  • Description

    Delegate query.

  • Function call

  • Parameter description

    • contractAddress: The address of the contract to be called.

    • input:Input parameter.

    The Chain.delegateQuery function will trigger the query function of the contract to be called, and the Chain.delegateQuery function will assign the execution environment of the current contract to the contract to be called.

  • Example

Chain.contractCall

  • Description

    Call contracts.

  • Function call

  • Parameter description

    • contractAddress: The address of the contract to be called

    • asset: The asset class, true for Gas, object {"issue": adxxx, "code" : USDT} for assets.

    • amount: The amount of the asset.

    • input:Input parameter.

    The Chain.contractCall function triggers the main function entry of the contract to be called.

  • Example

Chain.contractQuery

  • Description

    Query contracts.

  • Function call

  • Parameter description

    • contractAddress: The address of the contract to be called

    • input:Input parameter.

    The Chain.contractQuery function will call the query interface of the contract.

  • Example

Chain.contractCreate

  • Description

    Create Contracts.

  • Function call

  • Parameter description

    • balance: The asset that is transferred to the contract created, in string.

    • type :0 indicates javascript, in integer.

    • code: The contract code, in string.

    • input:The initiation parameter of the init function.

    The Chain.contractCreate function create contracts.

  • Example

Variables of the Chain Object

This section outlines key variables provided by the Chain object in Zetrix smart contracts, which offer access to contextual blockchain, transaction, and message-level data:

Chain.block

  • The Chain.block object enables smart contracts to make decisions based on the current blockchain state, ensuring time-sensitive and sequence-sensitive logic executes reliably.

Chain.block.timestamp

  • Variable description

    The timestamp of the block when the current transaction is executed.

Chain.block.number

  • Variable description

    The height of the block where the current transaction is executed.

Chain.tx

  • Variable description

    The transaction information signed by the user at the time of the transaction.

Chain.tx.initiator

  • Variable description

    The original originator of the transaction, that is the fee payer of the transaction.

Chain.tx.sender

  • Variable description

    The most primitive trigger of the transaction, that is the account in the transaction that triggers the execution of the contract. For example, an account initiates a transaction, and an operation in the transaction is to call the contract Y (the source_address of the operation is x), then the value of the sender is the address of the account x during the execution of the contract Y.

  • Example

Chain.tx.gasPrice

  • Variable description

    The price of the gas in the transaction signature.

Chain.tx.hash

  • Variable description

    The hash value of the transaction.

Chain.tx.feeLimit

  • Variable description

    The limit fee for the transaction.

Chain.msg

A message in Zetrix smart contracts refers to the contextual data that initiates contract execution within a transaction. While the transaction data remains immutable during execution, the message object (Chain.msg) can be updated as the contract interacts with other contracts. For instance, when using functions like Chain.contractCall or Chain.contractQuery, the message context switches to reflect the called contract’s perspective. This allows the contract to correctly process the execution flow and maintain state awareness across inter-contract interactions.

Chain.msg.initiator

  • Variable description

    The original originator account for this message.

Chain.msg.sender

  • Variable description

    The account number for triggering this message.

  • Example

    For example, an account initiates a transaction, and an operation in the transaction is to call the contract Y (the source_address of the operation is x), then the value of the sender is the address of the account x during the execution of the contract Y.

Chain.msg.coinAmount

  • Variable description

    The Gas for this payment operation

Chain.msg.asset

  • Variable description

    The assets for this payment operation

  • Example

Chain.msg.nonce

  • Variable description

    The nonce value of the initiator in this transaction, ie the nonce value of the Chain.msg.initiator account.

Chain.msg.operationIndex

  • Variable description

    The sequence number for triggering this contract calling operation.

  • Example

    For example, an account A initiates a transaction tx0, and tx0 has a 0th (counting from 0) operation which is to transfer assets to a contract account (contract call), then the value of Chain.msg.operationIndex is 0.

Chain.thisAddress

  • Variable description

    The address of the current contract account.

  • Example

    For example, the account x initiates a transaction to call contract Y. During this execution, the value is the address of the contract account Y.

Methods of the Utils Object

The Utils object in Zetrix smart contracts provides a comprehensive set of utility functions that assist in logging, mathematical operations, cryptographic verification, and data validation. These methods are essential for ensuring accurate, secure, and efficient smart contract execution. The Utils object includes the following methods:

Utils.log, Utils.stoI64Check, Utils.int64Add, Utils.int64Sub , Utils.int64Mul, Utils.int64Mod, Utils.int64Div, Utils.int64Compare, Utils.assert, Utils.sha256, Utils.ecVerify, Utils.toBaseUnit, Utils.addressCheck and Utils.toAddress.

Utils.log

  • Description

    • Utils.log(message): Outputs logs for debugging purposes during contract execution.

  • Function call

  • Parameter description

    • info: The log content.

  • Example

Utils.stoI256Check

  • Description

    Legal check for string numerics.

  • Function call

  • Parameter description

    • strNumber: String numeric parameter

  • Example

Utils.int256Add

  • Description

    256-bit addition.

  • Function call

  • Parameter description

    • left_value: Left value.

    • right_value: Right value.

  • Example

Utils.int256Sub

  • Description

    256-bit subtraction.

  • Function call

  • Parameter description

    • left_value: Left value.

    • right_value:Right value.

  • Example

Utils.int256Mul

  • Description

    256-bit multiplication.

  • Function call

  • Parameter description

    • left_value: Left value.

    • right_value:Right value.

  • Example

Utils.int256Mod

  • Description

    256-bit modulo.

  • Function call

  • Parameter description

    • left_value: Left value.

    • right_value: Right value.

  • Example

Utils.int256Div

  • Description

    256-bit division.

  • Function call

  • Parameter description

    • left_value: Left value.

    • right_value: Right value.

  • Example

Utils.int256Compare

  • Description

    256-bit comparison.

  • Function call

  • Parameter description

    • left_value: Left value.

    • right_value: Right value.

  • Example

  • Return value

    1: left value is greater than right value, 0: left value equals to right value, -1: left value less than right value.

Utils.assert

  • Description

    256 assertion.

  • Function call

  • Parameter description

    • condition: Assertive variable

    • message: Optional, an exception message is thrown when it fails

  • Example

Utils.sha256

  • Description

    sha256 computation.

  • Function call

  • Parameter description

    • data: The raw data of the hash to be calculated. According to the dataType, fill in the data in different formats.

    • dataType: The data type, integer, optional field, by default is 0. 0: base16 encoded string, such as "61626364"; 1: ordinary original string, such as "abcd"; 2: base64 encoded string, such as "YWJjZA==". If you are calculating binary data, it is recommended to use base16 or base64 encoding.

  • Return value

    Return a base16 encoded string if it succeeds, or return false if it fails.

  • Example

Utils.ecVerify

  • Description

    Check if the signature is legal.

  • Function call

  • Parameter description

    • signedData: The signature data, a string encoded by base16.

    • publicKey: The public key, a string encoded by base16.

    • blobData: The raw data, fill in different formats of data per blobDataType.

    • blobDataType: The blobData type, integer, optional field, the default is 0. 0: base16 encoded string, such as "61626364"; 1: ordinary original string, such as "abcd"; 2: base64 encoded string, such as "YWJjZA==". If you are verifying binary data, it is recommended to use base16 or base64 encoding.

  • Return value

    Return true if it succeeds, or return false if it fails.

  • Example

Utils.toBaseUnit

  • Description

Transform the unit.

  • Function call

  • Parameter description

    • value: The converted number, only string is allowed to pass in, and it can contain a decimal point, which allows up to 6 digits after the decimal point.

  • Return value

    Return a string multiplied by 10^6 if it succeeds or return false if it fails.

  • Example

Utils.addressCheck

  • Description

    Address legality check.

  • Function call

  • Parameter description

    • address The address parameter in string.

  • Return value

    Return true if it succeeds, or return false if it fails.

  • Example

Utils.toAddress

  • Description

    Transform a public key to an address.

  • Function call

  • Parameter description

    • public_key The public key, a base16 encoded string

  • Return value

    Return the account address if it succeeds, or return false if it fails.

  • Example

Utils.bcRangeProofVerify

  • Description

    Verifying the certificate for scope (for privacy protection).

  • Function call

  • Parameter description

    • commit, 66 bit Pedersen commitment string

    • proof, Range proof string, variable length

  • Return value

    Return true if it succeeds, or return false if it fails.

  • Example

Utils.pedersenTallyVerify

  • Description

    Verifying the equal for Output and input (for privacy protection).

  • Function call

  • Parameter description

    • input_commits, Enter Pedersen commitment list, JSON array

    • output_commits, Output Pedersen commitment list, JSON array

    • excess_msg, Message to be signed, 32-bit string, no encoding requirements

    • excess_sig, Signature result string of transaction core (input minus output remainder)

  • Return value

    Return true if it succeeds, or return false if it fails.

  • Example

Exception Handling in Zetrix Smart Contracts

Zetrix smart contracts use JavaScript-style exception handling. When an exception occurs and is not caught during execution, it triggers specific failure behavior defined by the blockchain environment.

JavaScript Exceptions

  • If an uncaught exception occurs during contract execution:

    • The entire contract execution fails.

    • All state changes and transactions initiated by the contract are reverted—nothing takes effect.

    • The transaction that triggered the contract is marked as failed, and an error code 151 is returned to indicate the failure.

Transactional Integrity

  • A contract may initiate multiple internal transactions (e.g., transfers, state updates).

  • If any single transaction within the contract fails, it throws an exception.

  • This causes the entire contract execution to abort, and all previous operations in that call are rolled back, ensuring atomicity.

Implication

  • Developers must use try-catch logic and proper assertions (Utils.assert) to validate conditions and handle potential failures gracefully.

  • Failing to do so will result in complete transaction failure, impacting contract reliability and user experience.

This strict exception handling ensures consistency, security, and predictability in the Zetrix smart contract environment.

Last updated