Introduction
Introduction to Zetrix Smart Contract
Overview
The Zetrix Smart Contract is a snippet of JavaScript
code, with a standard (ECMAScript as specified in ECMA-262). The initialization function of the contract is init
, the entry function of the execution is the main
function, and the query interface is query
. The parameter string input
of these functions is specified when the contract is called.
The following is a simple example:
Objects in the Interfaces
The global objects Chain
and Utils
are provided in the Zetrix smart contract. These two objects provide various methods and variables, which can get some information about the blockchain, and can also drive the account to initiate all transactions, excluding setting thresholds and weights.
Note: The custom variables should not be duplicated with built-in objects, because this can result in uncontrollable data errors.
Method Usage
Object. method (variable)
Obtain the account balance
Print logs
The current block number
Read and Write Privileges
Each function in the object has a fixed read-only or write privilege.
Read-only permissions are interface functions that do not write data to the blockchain, such as getting the balance Chain.getBalance.
Write permissions refer to interface functions that write data to the blockchain, such as transfer gas Chain.payCoin.
When writing smart contracts, you should be noted that different entry functions have different calling permissions.
init
andmain
can call all built-in functions.query
can only call functions with read-only permissions, otherwise the interface will be prompted undefined during debugging or execution.
Return Value
When calling an internal function, return false
if it fails or throw an exception and terminate the call, otherwise it is successful for other objects. If a parameter error is encountered, the parameter position error will be indicated in the error description. The position here refers to the index number of the parameter, that is, counting from 0. For example, parameter 1
indicates that the 2 parameter is incorrect.
The following is an example:
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
Parameter description
Metadata_key: The keyword for metadata.
Example
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 themain
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 thequery
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 themain
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 thequery
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 introduces some variables of the Chain object, respectively Chain.block, Chain.tx , Chain.msg related variables and Chain.thisAddress. The variables of the block information include Chain.block.timestamp, Chain.block.number. Variables for transaction information include Chain.tx.initiator, Chain.tx.sender, Chain.tx.gasPrice, Chain.tx.hash, chain.tx.feeLimit. The variables of the message include Chain.msg.initiator, Chain.msg.sender, Chain.msg.coinAmount, Chain.msg.asset, Chain.msg.nonce, Chain.msg.operationIndex.
Chain.block
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 is the information that triggers the execution of a smart contract in a transaction. During the execution of the triggered contract, the transaction information will not be changed and the message will change. For example, when calling Chain.contractCall,Chain.contractQuery in a contract, the message will change.
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
This section describes some of the methods of the Utils object, including 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
Output logs.
Function call
Parameter description
info: The log content.
Example
Utils.stoI64Check
Description
Legal check for string numerics.
Function call
Parameter description
strNumber: String numeric parameter
Example
Utils.int64Add
Description
64-bit addition.
Function call
Parameter description
left_value: Left value.
right_value: Right value.
Example
Utils.int64Sub
Description
64-bit subtraction.
Function call
Parameter description
left_value: Left value.
right_value:Right value.
Example
Utils.int64Mul
Description
64-bit multiplication.
Function call
Parameter description
left_value: Left value.
right_value:Right value.
Example
Utils.int64Mod
Description
64-bit modulo.
Function call
Parameter description
left_value: Left value.
right_value: Right value.
Example
Utils.int64Div
Description
64-bit division.
Function call
Parameter description
left_value: Left value.
right_value: Right value.
Example
Utils.int64Compare
Description
64-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
64 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
JavaScript exceptions
When an uncaught JavaScript exception occurs during contract execution, the processing rules:
The execution of this contract fails and all transactions made in the contract will not take effect.
The transaction that triggered this contract is a failure. The error code is
151
.
Failure in transaction execution
Multiple transactions can be executed in a contract. If one transaction fails, an exception will be thrown, causing the entire transaction to fail
Last updated