Smart Contracts
Contract structure
Variables
Nested Contracts
File upload
Queries in JSON format
Queries with date and time operations
Needle contracts language
Needle functions classification
Needle functions reference
System contracts
Smart Contract (hereinafter referred to as Contract) is one of the basic elements of an application. The implementation of a contract on a page by the user is usually a single operation that the purpose is to update or create a database entry. All data operations of an application form a contract system, and these contracts interact with each other through database or contract content functions.
Contract Structure
Use the keyword contract
to declare a contract, followed by the contract name, and the contract content must be enclosed in braces. A contract mainly consists of three sections:
data - data section, where declares the variables of the input data, including variable name and variable type;
conditions - conditions section, where validates the correctness of the data;
action - operation section, where defines the data manipulations.
Data section
The data
section describes the contract data inputs and the form parameters received.
The structure of each line by sequence:
Variable name - only receive variables, not arrays;
Variable data type - the data type of the variable;
optional - an optional parameter that do not need to fill in the form element.
Conditions section
The conditions
section describes the validation of data received.
The following commands are used for error warnings: serious errors error, warning errors warning, suggestive errors info. These three commands will generate an error that terminates the execution of contracts, and each error will print a different type of error log information. For example:
if fuel == 0 { error "fuel cannot be zero!" } if money <limit { warning Sprintf("You don't have enough money: %v <%v", money, limit) } if idexist > 0 { info "You have already been registered" }
Action section
The action
section describes the main code of the contract, which retrieves other data and records the result values in tables. For example:
Variables
Variables declared in the data section are passed to other contract sections through the $ symbol followed by the variable name. The $ symbol can also be used to declare other variables that are not within the data section, which are considered as global variables of this contract and all contracts that this contract is nested.
Pre-defined variables can be used in contracts, which contain transaction data that called the contract:
$time
- transaction timestamp;$ecosystem_id
- ecosystem ID;$block
- ID of the block containing the transaction;$key_id
- address of the account that signed the current transaction;$type
- contract ID in the virtual machine;$block_key_id
- account address of the node generated the block;$block_time
- block generation timestamp;$original_contract
- name of the contract that initially processed the transaction. It means the contract is called during transaction validation if the variable is an empty string. To check whether the contract is called by another contract or directly by the transaction, you need to compare the values of $original_contract and $this_contract. It means that the contract is called by the transaction if they are equal;$this_contract
- name of the contract currently being executed;$guest_key
- guest account address;$stack
- contract array stack with a data type of array, containing all contracts executed. The first element of the array represents the name of the contract currently being executed, while the last element represents the name of the contract that initially processed the transaction;$node_position
- the index number of the verification node array where the block is located;$txhash
- transaction hash;$contract
- the current contract structure array.
Predefined variables can be accessed not only in contracts, but also in permission fields that defines the access permission conditions of the application elements. When used in permission fields, predefined variables for block information are always equal to zero, such as $time
, $block
, etc.
A predefined variable $result
is assigned with the return result of the contract.
Nested Contracts
You can nest contracts in the conditions and action sections of the contract. Nested contracts can be called directly, and the contract parameters are specified in parentheses after the contract name, for example, @1NameContract(Params)
. You may also call nested contracts with the CallContract function.
File upload
To upload a file using a form in the format of multipart/form-data
, the data type of the contract must be file.
The UploadBinary contract is used to upload and store files. With the Logicor language function Binary in the page editor, you can get the file download link.
Queries in JSON format
In the contract language, JSON can be specified as a field type. You can use the syntax: columnname->fieldname to process the entry field. The value obtained is recorded in columnname.fieldname. The above syntax can be used in Columns,One,Where of the DBFind function.
Queries with date and time operations
You cannot directly query and update the date and time with the contract language functions, but you can use PostgreSQL functions and features in the Where statement as in the example below. For example, you need to compare the field date_column with the current time. If date_column is a timestamp type, the expression should be date_column <NOW()
; if date_column is a Unix type, the expression should be to_timestamp(date_column)> NOW()
.
The following Needle function is used to process date and time in SQL format:
BlockTime
DateTime
UnixDateTime
Needle contract language
The contract language includes a set of functions, operators and structures, which can realize data algorithm processing and database operations.
The contract content can be modified if the contract editing permission is not set to false. The complete history of contract changes is stored in the blockchain, which is available in Weaver.
Data operations in the blockchain are executed in accordance with the latest version of the contract.
Basic elements and structure
Data types and variables
Data type must be defined for every variables. Normally, data types are converted automatically. The following data types can be used:
bool - Boolean, true or false;
bytes - a byte format;
Int - a 64-bit integer;
Array - an array of any type;
map - an object array;
money - a big integer;
float - a 64-bit float number;
string - a string must be defined with double quotes or escape format: "This is a string" or
This is a string
;file - an object array:
Name - file name, string type;
MimeType - mime-type file, string type;
Body - file content, bytes type.
All identifiers, including the names of variables, functions and contracts, are case sensitive (MyFunc and myFunc are different names).
Use the var keyword to declare a variable, followed by the name and type of the variable. Variables declared in braces must be used in the same pair of braces.
The default value of any variable declared is zero: the zero value of bool type is false, the zero value of all numeric types is 0, and the zero value, for strings, empty strings. An example of variable declaration:
Array
The contract language supports two array types:
Array - an array with index starting from 0;
map - an array of objects.
When allocating and retrieving array elements, the index must be placed in square brackets. Multiple indexes are not supported in the array, and the array elements cannot be treated as myarr[i][j].
You can also define arrays of array type by specifying elements in []. For map type arrays, please use {}.
You can use such initialization in expressions. For example, use it in function parameters.
For an array of objects, you must specify a key. Key are specified as strings in double quotes (""). If the key name is limited to letters, numbers and underscores, you can omit the double quotes.
An array can contain strings, numbers, variable names of any type, and variable names with the $ symbol. It supports nested arrays. You can specify different maps or arrays as values.
Expressions cannot be used as array elements. Use a variable to store the expression result and specify this variable as an array element.
If and While statements
The contract language supports standard if conditional statements and while loops, which can be used in contracts and functions. These statements can be nested within each other.
if and while must be followed by a conditional statement. If the conditional statement returns a number, it is regarded as false when its value is 0.
val == 0 is equal to !val, val != 0 is equal to val. The if statement can have an else code block, and the else is executed when the if conditional statement is false.
The following comparison operators can be used in conditional statements: <, >, >=, <=, ==, !=, ||, &&
The code block is executed when the conditional statement of the while loop is true. break means to terminate the loop of the code block. If you want to start a loop from the beginning, use continue.
In addition to conditional statements, Needle also supports standard arithmetic operations: +, -, *, /.
Variables of string and bytes types can be used as a conditional statement. If the length of the type is greater than zero, the condition is true, otherwise it is false.
Functions
Functions can perform some operations on the data received by the data section of a contract: read and write data from the database, convert the type of value, and establish the interaction between contracts.
Function declaration
Use the func keyword to declare a function, followed by the name and the list of parameters passed to it and their types. All parameters are enclosed in parentheses and separated by commas. After the parentheses, the data type of the value returned by the function must be declared. The function body must be enclosed in braces. If the function has no parameters, the braces can be omitted. To return a value from a function, use the return keyword.
Function do not return errors, because all error checks are performed automatically. If there is an error in any function, the contract will terminate its operation and present the error description in a window.
Variable-length parameters
Functions can define variable-length parameters, use the ... symbol as the last parameter type of the function to indicate variable-length parameters, with a data type of array. Variable-length parameters include all variables from the time the parameter is passed in the call. All types of variables can be passed, but you need to deal with conflicts of mismatching of data types.
Optional parameters
A function has many parameters, but we only need some of them when calling it. In this case, you can declare optional parameters in the following way: func myfunc(name string).Param1(param string).Param2(param2 int) {...}
, then you can call the specified parameters in any order: myfunc("name").Param2(100)
.
In the function body, you can handle these variables normally. If no specified optional parameters called, their default values are zero. You can also use ... to specify a variable-length parameter: func DBFind(table string).Where(request string, params ...)
and then call it: DBFind("mytable").Where({" id": $myid, "type": 2})
Needle functions classification
Retrieving values from the database:
AppParam
EcosysParam
GetDataFromXLSX
DBFind
GetHistory
GetDataFromXLSX
DBRow
GetHistoryRow
GetBlock
DBSelectMetrics
GetColumnType
LangRes
Updating data in tables:
DBInsert
DBUpdateExt
DelTable
DBUpdate
DelColumn
Operations with arrays:
Append
Len
GetMapKeys
Join
Row
SortedKeys
Split
One
Operations with contracts and permissions:
CallContract
GetContractById
TransactionInfo
ContractAccess
RoleAccess
Throw
ContractConditions
GetContractByName
ValidateCondition
EvalCondition
Operations with addresses:
AddressToId
IdToAddress
PubToID
Operations with variable values:
DecodeBase64
FormatMoney
Hash
EncodeBase64
Random
Sha256
Float
Int
Str
HexToBytes
Arithmetic operations:
Floor
Log10
Round
Log
Pow
Sqrt
Operations with JSON:
JSONEncode
JSONEncodeIndent
JSONDecode
Operations with strings:
HasPrefix
Size
ToLower
Contains
Sprintf
ToUpper
Replace
Substr
TrimSpace
Operations with bytes:
StringToBytes
BytesToString
Operations with date and time in SQL format:
BlockTime
DateTime
UnixDateTime
Operations with platform parameters:
HTTPRequest
HTTPPostJSON
Functions for master CLB nodes:
CreateOBS
RunOBS
RemoveOBS
GetOBSList
StopOBS
Needle functions reference
AppParam
Returns the value of a specified application parameter (from the application parameter table app_params).
Syntax
App
Application ID.
name Application parameter name.
Ecosystemid
Ecosystem ID.
Example
DBFind
Queries data from a specified table with the specified parameters and returns an array array consisting of an array of objects map.
.Row()
can get the first map element in the query, .One(column string)
can get the first map element of a specified column in the query.
Syntax
table Table name.
сolumns Returns a list of columns. If not specified, all columns will be returned. The value is an array or a string separated by commas.
where Query conditions. Example:
.Where({name: "John"})
or.Where({"id": {"$gte": 4}})
. This parameter must contain an array of objects with search criteria. The array can contain nested elements. Following syntactic constructions are used:{"field1": "value1", "field2": "value2"}
Equivalent tofield1 = "value1" AND field2 = "value2"
.{"field1": {"$eq":"value"}}
Equivalent tofield = "value"
.{"field1": {"$neq": "value"}}
Equivalent tofield != "value"
.{"field1: {"$in": [1,2,3]}
Equivalent tofield IN (1,2,3)
.{"field1": {"$nin": [1,2,3]}
Equivalent to field NOT IN (1,2,3).{"field": {"$lt": 12}}
Equivalent tofield <12
.{"field": {"$lte": 12}}
Equivalent to field <= 12
.{"field": {"$gt": 12}}
Equivalent tofield> 12
.{"field": {"$gte": 12}}
Equivalent tofield >= 12
.{"$and": [<expr1>, <expr2>, <expr3>]}
Equivalent toexpr1 AND expr2 AND expr3
.{"$or": [<expr1>, <expr2>, <expr3>]}
Equivalent toexpr1 OR expr2 OR expr3
.{field: {"$like": "value"}}
Equivalent tofield like'%value%'
(fuzzy search).{field: {"$begin": "value"}}
Equivalent tofield like'value%'
(starts withvalue
).{field: {"$end": "value"}}
Equivalent tofield like'%value'
(ends withvalue
).{field: "$isnull"}
Equivalent to field is null.
Make sure not to overwrite the keys of object arrays. For example, if you want to query with id>2 and id<5
, you cannot use {id:{"$gt": 2}, id:{"$lt": 5}}
, because the first element will be overwritten by the second element. You should use the following query structure: {id: [{"$gt": 2}, {"$lt": 5}]} {"$and": [{id:{"$gt": 2}}, {id:{"$lt": 5}}]}
Id Queries by ID. For example, .WhereId(1).
Order
Used to sort the result set by a specified column, or by id by default.
If you use only one field for sorting, you can specify it as a string. To sort multiple fields, you need to specify an array of string objects:
Descending order:
{"field": "-1"}
Equivalent tofield desc
.Ascending order:
{"field": "1"}
Equivalent tofield asc
.limit
Returns the number of entries. 25, by default. The maximum number is 10,000.
Offset
Offset.
Ecosystemid
Ecosystem ID. By default, the table of the current ecosystem is queried.
Example
DBRow
Queries data from a specified table with the specified parameters. Returns an array array consisting of an array of objects map.
Syntax
table Table name. columns Returns a list of columns. If not specified, all columns will be returned. The value is an array or a string separated by commas.
where Query conditions. For example:
.Where({name: "John"})
or.Where({"id": {"$gte": 4}})
. For more details, see DBFind.Id Query by ID. For example,
.WhereId(1)
.Order Used to sort the result set by a specified column, or by id by default. For more details, see DBFind.
Ecosystemid Ecosystem ID. By default, the table of the current ecosystem is queried.
Example
DBSelectMetrics
Returns the aggregated data of a metric. The metrics are updated each time 100 blocks are generated. And the aggregated data is stored on a 1-day cycle.
Syntax
metric Metric name
ecosystem_pages Number of ecosystem pages. Return value: key - ecosystem ID, value - number of ecosystem pages.
ecosystem_members Number of ecosystem members. Return value: key - ecosystem ID, value - number of ecosystem members.
ecosystem_tx Number of ecosystem transactions. Return value: key - ecosystem ID, value - number of ecosystem transactions.
timeInterval The time interval for aggregating metric data. For example:
1 day
,30 days
.aggregateFunc Aggregate function. For example,
max
,min
,avg
.
Example
EcosysParam
Returns the value of a specified parameter in the ecosystem parameters table parameters.
Syntax
name Parameter name.
Example
GetHistory
Returns the history of changes to entries in a specified table.
Syntax
table Table name.
Id Entry ID.
Return value Returns an array of objects of type map, which specify the history of changes to entries in tables. Each array contains the fields of a record before making the next change. The array is sorted by order of most recent changes. The id field in the array points to the id of the rollback_tx table. block_id represents the block ID, while block_time represents the block generation timestamp.
Example
GetHistoryRow
Returns a single snapshot from the change history of a specified entry in a specified table.
Syntax
table Table name.
Id Entry ID.
rollbackId rollback_tx The entry ID of the table.
GetColumnType
Returns the data type of a specified field in a specified table.
Syntax
table Table name.
column Field Name.
Return value The following types can be returned: text, varchar, number, money, double, bytes, json, datetime, double.
Example
GetDataFromXLSX
Returns data from XLSX spreadsheets.
Syntax
binId ID in XLSX format in the binary table binary.
line The starting line number, starting from 0 by default.
count The number of rows that need to be returned.
sheet List number, starting from 1 by default.
Example
GetRowsCountXLSX
Returns the number of lines in a specified XLSX file.
Syntax
binId ID in XLSX format in the binary table binary.
sheet List number, starting from 1 by default.
Example
LangRes
Returns a multilingual resource with name label for language lang, specified as a two-character code, for example: en, zh. If there is no language for a selected language, then the language resource of the en label is returned.
Syntax
label Language resource name.
lang Two-character language code.
Example
GetBlock
Returns relevant information about a specified block.
Syntax
blockID Block ID.
Return value Return an array of objects:
id Block ID.
time Block generation timestamp.
key_id The account address of the verification node generated the block.
Example
DBInsert
Adds an entry to a specified table and return the entry ID.
Syntax
tblname Table name.
params An array of objects where keys are field names and values are inserted values.
Example
DBUpdate
Changes the column value of a specified entry ID in a specified table. If the entry ID does not exist in the table, an error is returned.
Syntax
tblname Table name.
Id Entry ID.
params An array of objects where keys are field names and values are new values after changes.
Example
DBUpdateExt
Changes the value of a column in a specified table that matches the query condition.
Syntax
tblname Table name.
where Query conditions. For more details, see DBFind.
params An array of objects where keys are field names and values are new values after changes.
Example
DelColumn
Deletes a field in a specified table that has no records.
Syntax
tblname Table name.
column The field to be deleted.
DelTable
Deletes a specified table that has e no records.
Syntax
tblname Table name.
Example
Append
Inserts any type of val into the src array.
Syntax
Append(src array, val anyType) array
src The original array.
val The value to be inserted.
Example
Join
Combines elements of the in array into a string with a specified sep separator.
Syntax
In Array name.
sep Separator.
Example
Split
Uses the sep separator to split the in string into elements and put them into an array.
Syntax
In String.
sep Separator.
Example
Len
Returns the number of elements in a specified array.
Syntax
val Array.
Example
Row
The list parameter must not be specified in this case. Return the first object array in the array list. If the list is empty, an empty result is returned. This function is mostly used in conjunction with the DBFind function. When using this function, you cannot specify parameters.
Syntax
list The array of objects returned by the DBFind function.
Example
One
Returns the field value of the first object array in the array list. If the list array is empty, nil is returned. It is mostly used in conjunction with the DBFind function. When using this function, you cannot specify parameters.
Syntax
list
The array of objects returned by the DBFind function.
column
Field Name.
Example
GetMapKeys
Returns the key array in the object array.
Syntax
val Object array.
Example
SortedKeys
Returns a sorted key array in the object array.
Syntax
val Object array.
Example
CallContract
Calls the contract with a specified name. All parameters of the data section in the contract must be included in an object array. This function returns the value assigned to the $result variable by a specified contract.
Syntax
name The name of the contract being called.
params An associative array of the contract input data.
Example
ContractAccess
Checks if the name of contract being executed matches one of the names listed in the parameters. Usually it is used to control contract access to tables. When editing table fields or inserting and adding new column fields in the permissions section of the table, please specify this function in the permissions fields.
Syntax
name Contract name.
Example
ContractConditions
Calls the conditions section in the contract with a specified name. For this type of contracts, the data section must be empty. If the conditions section is executed without error, it returns true. If there is an error during execution, the parent contract will also be terminated due to the error. This function is usually used to control the contract's access to tables and can be called in the permission fields when editing system tables.
Syntax
name Contract name.
Example
EvalCondition
Gets the value of the condfield field in the record with a 'name' field from the tablename table, and checks the conditions of the condfield field value.
Syntax
tablename Table name.
name Queries the value with the 'name' field.
condfield The name of the field whose conditions needs to be checked.
Example
GetContractById
Returns its contract name by contract ID. If not found the contract, an empty string is returned.
Syntax
Id The contract ID in the contract table contracts.
Example
GetContractByName
This function returns its contract ID by contract name. If not found the contract, zero is returned.
Syntax
name The contract name in the contract table contracts.
Example
RoleAccess
Checks whether the role ID of the contract caller matches one of the IDs specified in the parameter. You can use this function to control contract access to tables and other data.
Syntax
Id Role ID.
Example
TransactionInfo
Queries transactions by specified hash value and returns information about the contract executed and its parameters.
Syntax
hash Transaction hash in hexadecimal string format.
Return value This function returns a string in JSON format:
contract Contract name.
params Data passed to contract parameters. block ID of the block that processed the transaction.
Example
Throw
Generates an error of type exception.
Syntax
ErrorId Error identifier.
ErrDescription Error description.
Return value The format of this type of transaction results:
Example
ValidateCondition
Tries to compile the conditions specified by the condition parameter. If there is an error during the compilation process, an error is generated and the contract called is terminated. This function is designed to check the correctness of the conditional format.
Syntax
condition The conditional format that needs to be verified.
state Ecosystem ID. If you check the global condition, please specify it as 0.
Example
AddressToId
Returns the corresponding account address by wallet address. If an invalid address is specified, '0' is returned.
Syntax
Address Wallet address in XXXX-...-XXXX format or number format.
Example
IdToAddress
Returns the corresponding wallet address by account address. If an invalid address is specified, the invalid address 'invalid' is returned.
Syntax
Id Account address.
Example
PubToID
The account address is returned by public key in hexadecimal format.
Syntax
hexkey The public key in hexadecimal format.
Example
DecodeBase64
Returns a string by specifying the base64 format
Syntax
Input String in base64 format.
Example
EncodeBase64
Returns a string in base64 format by specifying a string.
Syntax
Input The string to be encoded.
Example
Float
Converts an integer or string to a float number.
Syntax
val An integer or string.
Example
HexToBytes
Converts a string in hexadecimal format to byte type bytes.
Syntax
hexdata A string in hexadecimal format.
Example
FormatMoney
Returns the string value of exp / 10 ^ digit.
Syntax
Exp A number in string format. digit The exponent (positive or negative) of 10 in the expression
Exp/10^digit
. Positive values determine decimal places.
Example
Random
Returns a random number between min and max (min <= result <max). Both min and max must be positive numbers.
Syntax
min The minimum value among random numbers.
max The upper limit of random numbers. The random number generated will be less than this value.
Example
Int
Converts a value in string format to an integer.
Syntax
val A number in string format.
Example
Hash
Returns the hash of a specified byte array or string, which is generated by the system encryption library crypto.
Syntax
val A string or byte array.
Example
Sha256
Returns the SHA256 hash of a specified string.
Syntax
val A string requires the Sha256 hash operation.
Example
Str
Converts an integer int or float float number to a string.
Syntax
val An integer or float number.
Example
JSONEncode
Converts a number, string or array to a string in JSON format.
Syntax
src Data to convert.
Example
JSONEncodeIndent
Uses the specified indentation to convert a number, string, or array to a string in JSON format.
Syntax
src Data to convert.
Indent The string will be used as indentation.
Example
JSONDecode
Converts a string in JSON format to a number, string or array.
Syntax
src A string containing data in JSON format.
Example
HasPrefix
Checks whether the string starts with a specified string.
Syntax
s A string.
prefix The prefix to check.
Return value If the string starts with a specified string, true is returned.
Example
Contains
Checks whether the string contains a specified substring.
Syntax
s A string.
substr A substring.
Return value If the string contains the substring, it returns true.
Example
Replace
Replaces old (the old string) with new (the new string) in the string.
Syntax
s The original string.
Old The substring to replace.
new The new string.
Example
Size
Returns the number of bytes in a specified string.
Syntax
val A string.
Example
Sprintf
This function creates a string using the specified template and parameters. Available wildcards:
%d
(integer)%s
(string)%f
(float)%v
(any type)
Syntax
pattern A string template.
Example
Substr
Returns the substring obtained from a specified string starting from the offset offset (calculated from 0 by default), and the maximum length is limited to length. If the offset or length is less than zero, or the offset is greater than the length, an empty string is returned. If the sum of the offset and length is greater than the string size, then, the substring will be returned starting from the offset to the end of the string.
Syntax
val A string.
Offset Offset.
length Length of the substring.
Example
ToLower
Returns a specified string in lowercase.
Syntax
val A string.
Example
ToUpper
Returns a specified string in uppercase.
Syntax
val A string.
Example
TrimSpace
Deletes the leading and trailing spaces, tabs and newlines of a specified string.
Syntax
val A string.
Example
Floor
Returns the largest integer value less than or equal to a specified number, float number, and string.
Syntax
x A number, float number, and string.
Example
Log
Returns the natural logarithm of a specified number, float number, and string.
Syntax
x A number, float number, and string.
Example
Log10
Returns the base-10 logarithm of a specified number, float number, and string.
Syntax
x A number, float number, and string.
Example
Pow
Returns the specified base to the specified power (xy).
Syntax
x Base number.
y Exponent.
Example
Round
Returns the value of a specified number rounded to the nearest integer.
Syntax
x A number.
Example
Sqrt
Returns the square root of a specified number.
x A number.
Example
StringToBytes
Converts a string to bytes.
Syntax
src A string.
Example
BytesToString
Converts bytes to string.
Syntax
src Byte.
Example
SysParamString
Returns the value of a specified platform parameter.
Syntax
name Parameter name.
Example
SysParamInt
Returns the value of a specified platform parameter in the form of a number.
Syntax
name Parameter name.
Example
DBUpdateSysParam
Updates the value and conditions of a platform parameter. If you do not need to change the value or conditions, please specify an empty string in the corresponding parameter.
Syntax
name Parameter name.
value New value of a parameter.
conditions New conditions for updating a parameter.
Example
UpdateNotifications
Obtains the notification list of a specified key from the database, and sends the notification obtained to Centrifugo.
Syntax
EcosystemID Ecosystem ID.
key A list of account addresses, separated by commas. Or you can use an array to specify a list of account addresses.
Example
UpdateRolesNotifications
Obtains the notification list of all account addresses of a specified role ID in the database, and sends the notification obtained to Centrifugo.
Syntax
EcosystemID Ecosystem ID.
roles A list of role IDs, separated by commas. Or you can use an array to specify a list of role IDs.
Example
HTTPRequest
Sends HTTP requests to the specified address. Note This function can only be used in CLB contracts.
Syntax
Url Address, to which the request will be sent.
method Request type (GET or POST).
heads An array of request headers, objects.
pars Request parameters.
Example
HTTPPostJSON
This function is similar to the HTTPRequest function, but it sends a POST request and the request parameters are strings.
Note This function can only be used in CLB contracts
Syntax
Url Address, to which the request will be sent.
heads An array of request headers, objects.
pars Request parameters as a JSON string.
Example
BlockTime
Returns the generation time of the block in SQL format.
Syntax
Example
DateTime
Converts the timestamp unixtime to a string in YYYY-MM-DD HH:MI:SS format.
Syntax
Example
UnixDateTime
Converts a string in YYYY-MM-DD HH:MI:SS format to a timestamp unixtime
Syntax
Example
CreateOBS
Creates a child CLB. This function can only be used in the master CLB mode.
Syntax
OBSName CLB name.
DBUser The role name of the database.
DBPassword The password of the role.
OBSAPIPort The port of the API request.
Example
GetOBSList
Returns the list of child CLBs. This function can only be used in the master CLB mode.
Syntax
Return value An array of objects, where the key is the CLB name and the value is the process state.
RunOBS
A process running the CLB. This function can only be used in the master CLB mode.
Syntax
OBSName CLB name.
It can only contain letters and numbers, and the space symbol cannot be used.
StopOBS
Stop the process of a specified CLB. This function can only be used in the master CLB mode.
Syntax
OBSName CLB name.
It can only contain letters and numbers, and the space symbol cannot be used.
RemoveOBS
Deletes the process of a specified CLB. This function can only be used in the master CLB mode.
Syntax
OBSName CLB name.
It can only contain letters and numbers, and the space symbol cannot be used.
System Contracts
System contracts are created by default when the IBax blockchain platform is launched. All these contracts were created in the first ecosystem. This is why you need to specify their full names when calling them from other ecosystems, for example, @1NewContract.
NewEcosystem
This contract creates a new ecosystem. To obtain the ID of the ecosystem created, you must quote the result filed returned in txstatus. Parameters:
Name string - name of the ecosystem. It can be changed later.
EditEcosystemName
Changes the name of the ecosystem in the 1_ecosystems table that only exists in the first ecosystem. Parameters:
EcosystemID int - changes the name of the ecosystem ID;
NewName string - new name of the ecosystem.
NewContract
Creates a new contract in the current ecosystem. Parameters:
ApplicationId int - the application to which a new contract belongs;
Value string - contract source code. The upper layer must have only one contract;
Conditions string - changes the conditions of the contract;
TokenEcosystem int "optional" - ecosystem ID. It determines which token will be used for transactions when the contract is activated.
EditContract
Edits the contract in the current ecosystem. Parameters:
Id int - the contract ID changed;
Value string "optional" - source code of the contract;
Conditions string "optional" - changes the conditions of the contract.
BindWallet
Binding the contract to the wallet address in the current ecosystem. After binding with the contract, the contract execution fee will be paid under this address. Parameters:
Id int - the contract ID to be bound.
WalletId string "optional" - the wallet address bound to the contract.
UnbindWallet
Unbinding the contract from the wallet address in the current ecosystem. Only addresses bound to the contract can be unbound. After unbinding the contract, users who execute the contract will pay the execution fee. Parameters:
Id int - the ID of the contract being bound.
NewParameter
A new ecosystem parameter has been added to the current ecosystem. Parameters:
Name string - parameter name;
Value string - parameter value;
Conditions string - conditions to change the parameter.
EditParameter
Changes existing ecosystem parameters in the current ecosystem. Parameters:
Name string - name of the parameter to be changed;
Value string - new parameter value;
Conditions string - new conditions to change the parameter.
NewMenu
Adds a new menu in the current ecosystem. Parameters:
Name string - menu name;
Value string - menu source code;
Title string "optional" - menu title;
Conditions string - conditions to change the menu.
EditMenu
Changes the existing menu in the current ecosystem. Parameters:
Id int - menu ID to be changed;
Value string "optional" - source code of the new menu;
Title string "optional" - title of the new menu;
Conditions string "optional" - new conditions to change the menu.
AppendMenu
Adds the source code content to existing menus in the current ecosystem Parameters:
Id int - menu ID;
Value string - source code to be added.
NewPage
Adds a new page in the current ecosystem. Parameters:
Name string - name of the page;
Value string - source code of the page;
Menu string - name of the menu associated with the page;
Conditions string - conditions to change the page;
ValidateCount int "optional" - number of nodes required for page validation. If this parameter is not specified, the min_page_validate_count ecosystem parameter value is used. The value of this parameter cannot be less than min_page_validate_count and greater than max_page_validate_count;
ValidateMode int "optional" - mode of page validity check. The page will be checked when it is loaded if the value of this parameter is 0; or checked when it is loaded or exit the page if the value of this parameter is 1.
EditPage
Changes existing pages in the current ecosystem. Parameters:
Id int - ID of the page to be changed;
Value string "optional" - source code of the new page;
Menu string "optional" - name of the new menu associated with the page;
Conditions string "optional" - new conditions to change the page;
ValidateCount int "optional" - number of nodes required for page validation. If this parameter is not specified, the min_page_validate_count ecosystem parameter value is used. The value of this parameter cannot be less than min_page_validate_count and greater than max_page_validate_count;
ValidateMode int "optional" - mode of page validity check. The page will be checked when it is loaded if the value of this parameter is 0; or checked when it is loaded or exit the page if the value of this parameter is 1.
AppendPage
Adds the source content to existing pages in the current ecosystem. Parameters:
Id int - ID of the page to be changed;
Value string - the source code to be added.
NewBlock
Adds a page module to the current ecosystem. Parameters:
Name string - name of the module;
Value string - source code of the module;
Conditions string - conditions to change the module.
EditBlock
Changes existing page modules in the current ecosystem. Parameters:
Id int - module ID to be changed;
Value string - source code of the new module;
Conditions string - new conditions to change the module.
NewTable
Adds a new table to the current ecosystem. Parameters:
ApplicationId int - application ID of the associated table;
Name string - name of the new table;
Columns string - field array in JSON format [{"name":"...", "type":"...","index": "0", "conditions":".. ."},...], where –name - field name, only Latin characters; –type - data type varchar,bytea,number,datetime,money,text,double,character; –index - non-primary key field 0, primary key 1; –conditions - conditions to change the field data, and the access permissions must be specified in JSON format "{"update":"ContractConditions(MainCondition)", "read":"ContractConditions(MainCondition)"} ``;
Permissions string - access permissions in JSON format {"insert": "...", "new_column": "...", "update": "...", "read": ".. ."}. –insert - permission to insert entries; –new_column - permission to add a new column; –update - permission to change entry data; –read - permission to read entry data.
EditTable
Changes the access permissions of a table in the current ecosystem. Parameters:
Name string - name of the table;
InsertPerm string - permission to insert entries into the table;
UpdatePerm string - permission to update entries in the table;
ReadPerm string - permission to read entries in the table;
NewColumnPerm string - permission to create a new column;
NewColumn
Adds a new field to the table of the current ecosystem. Parameters:
TableName string - table name;
Name string - field name in Latin characters;
Type string - data type varchar,bytea,number,money,datetime,text,double,character;
UpdatePerm string - permission to change the value in the column;
ReadPerm string - permission to read the value in the column.
EditColumn
Changes the permission of a specified table field in the current ecosystem. Parameters:
TableName string - table name;
Name string - field name in Latin characters to be changed;
UpdatePerm string - new permission to change the value in the column;
ReadPerm string - new permission to read the value in the column.
NewLang
Adds language resources to the current ecosystem, and the permission to do so is set in the changing_language parameter of the ecosystem parameters.
Parameters:
Name string - name of the language resources in Latin characters;
Trans string - string in JSON format, with a two-character lang code as the key and the translated string as the value. For example, {"en": "English text", "zh": "Chinese text"}.
EditLang
Changes the language resources in the current ecosystem, and the permission to do so is set in the changing_language parameter of the ecosystem parameter.
Parameters:
Id int - language resources ID.
Trans - string in JSON format, with a two-character lang code as the key and the translated string as the value. For example, {"en": "English text", "zh": "Chinese text"}.
Import
Imports an application into the current ecosystem and imports the data loaded from the ImportUpload contract.
Parameters:
Data string - data imported in text format, which comes from a file exported by the ecosystem.
ImportUpload
Loads an external application file into the buffer_data table of the current ecosystem for subsequent import.
Parameters:
InputFile file - a file written to the buffer_data table of the current ecosystem.
NewAppParam
Adds new application parameters to the current ecosystem.
Parameters:
ApplicationId int - application ID;
Name string - parameter name;
Value string - parameter value;
Conditions string - permission to change the parameter.
EditAppParam
Changes existing application parameters in the current ecosystem.
Parameters:
Id int - application parameter ID;
Value string "optional" - new parameter value;
Conditions string "optional" - new permissions to change the parameter.
NewDelayedContract
Adds a new task to the delayed contracts scheduler daemon.
The delayered contracts scheduler runs contracts required by the currently generated block.
Parameters:
Contract string - contract name;
EveryBlock int - the contract will be executed every such amount of blocks;
Conditions string - permission to change the task;
BlockID int "optional" - the block ID where the contract must be executed. If not specified, it will be calculated automatically by adding the "current block ID" + EveryBlock;
Limit int "optional" - the maximum number of task execution. If not specified, the task will be executed for an unlimited times.
EditDelayedContract
Changes a task in the delayed contracts scheduler daemon.
Parameters:
Id int - task ID;
Contract string - contract name;
EveryBlock int - the contract will be executed every such amount of blocks;
Conditions string - permission to change the task;
BlockID int "optional" - the block ID where the contract must be executed. If not specified, it will be calculated automatically by adding the "current block ID" + EveryBlock;
Limit int "optional" - the maximum number of task execution. If not specified, the task will be executed for an unlimited times.
Deleted int "optional" - task switching. A value of 1 will disable the task. A value of 0 will enable the task.
UploadBinary
Adds or overwrites a static file in the X_binaries table. When calling a contract via HTTP API, the request must be in multipart/form-data format; the DataMimeType parameter will be used in conjunction with the form data.
Parameters:
Name string - name of the static file;
Data bytes - content of the static file;
DataMimeType string "optional" - a static file in mime-type format;
ApplicationId int - the application ID associated with the X_binaries table. If the DataMimeType parameter is not passed, the application/octet-stream format is used by default.
Last updated