# Contract API functions

  • Use the global function transfer_from_contract_to_address to transfer a certain amount of an asset from the current contract (the contract in which this function calls the code) to an address. The first parameter is the target address (string), and the second parameter is the asset name (such as XWC), the third parameter is 100,000 times the number of transfers (int64 type), requires a positive number
    Return value
         0   Transfer success
        -1  Unknown system exception
        -2  Asset_symbol exception
        -3  illegal contract address
        -4  illegal target address
        -5  account balance is insufficient to pay the transfer amount
        -6  transfer amount is negative  
  • Use the global function get_contract_balance_amount to get the balance of a contract with precision (accuracy is 100000), the first parameter is the contract address (support for querying the balance of other contracts), the second parameter is the asset name (such as XWC), return with precision Contract balance (int64 type), if an error occurs or the contract does not exist, return a negative number
    Return value
        Non-negative contract account balance
        -1  asset id is abnormal
        -2  abnormal contract address
  • Use the global function get_chain_now to get the current time on the chain without parameters.
    Return value
        positive number  timestamp integer
        0     system exception
  • Use the global function get_chain_random to obtain a pseudo-random number on the chain, but the same operation operation on this chain, the different results of different nodes executed at different times are the same (actually, the prev_secret_hash on the block where the operation takes place is combined with this transaction. hope)
    Return value
        random result
  • Use the global function get_header_block_num to get the block number of the previous block
    Return value
        The sequence number of the latest block in the current chain
  • Use the global function get_waited(num), which means to obtain a pseudo-random number based on the data of the future block, num is the block number of the future block (but it needs to be called again in the future, then the num block is already the past block and you can know the result)
    Return value
        Positive integer Result value
        -1  Target block not reached
        -2  The set target block is not greater than 1
  • Use the global function get_current_contract_address to get the contract address where this function call appears, with no parameters

  • The global variable caller stores the public key of the user calling the contract, and the global variable caller_address stores the account address of the user calling the contract

  • When the transfer to the contract occurs, if the contract defines the on_deposit_asset (parameter is "asset identification, transfer amount") API, then this API will be called after the transfer occurs, and to ensure that the transfer and trigger this API is atomic, If an error occurs in the middle, the whole rollback is performed and the transfer fails.

  • Use the statement emit EventName(arg: string) to throw an event, where emit is a keyword, EventName is written to the event name as needed, and it is recorded by the blockchain. When other nodes synchronize to the event triggered by emit, they can call the locally set callback

  • Use the global function is_valid_address(arg: string) to check whether an address string is a legal blockchain address

  • Use the global function is_valid_contract_address(arg: string) to check whether an address string is a legal contract address

  • Use the global function get_transaction_fee() to get the transaction fee for a transaction

    Return value 
        Positive intege Result value
        -1  Abnormal handling asset id
        -2  System anomaly
  • Use the global function get_transaction_id(): string to get the transaction id of this transaction

  • Use the global function transfer_from_contract_to_public_account(to_account_name: string, asset_type: string, amount: int) to transfer from the current contract to the account name on the chain, and return the status of whether the transfer is made

    Return value
        0  Transfer success
       -1  Unknown system exception
       -2  Asset_symbol exception
       -3  illegal contract address
       -4  illegal target address
       -5  account balance is insufficient to pay the transfer amount
       -6  transfer amount is negative 
       -7  no specified account name exists
  • import_contract: (string) => table refers to the contract, the parameter is the name string of the contract, and returns the table corresponding to the contract

  • import_contract_from_address: (string) => table refers to the contract according to the contract address and returns the table corresponding to the contract

  • get_prev_call_frame_contract_address: () => string Get the previous contract address of the contract call stack (if the previous contract call stack is not a contract, it returns nil)

  • get_prev_call_frame_api_name: () => string Get the previous contract API name of the contract call stack (if the previous contract call stack is not a contract, it returns nil)

  • get_contract_call_frame_stack_size: () => int get contract call stack depth

  • wait_for_future_random: (int) => int according to the block height of the parameter to obtain the pseudo-random number obtained from the data of this block, if the block height has not been reached, it returns 0

  • get_system_asset_symbol: () => string Get the asset symbol of the system's basic assets

  • get_system_asset_precision: () => int Get the accuracy of the system's basic assets, this value is generally a power of 10

  • hex_to_bytes: (string) => table convert hexadecimal string into byte array

  • bytes_to_hex: (table) => string convert byte array to hexadecimal string

  • sha1_hex: (string) => string Perform sha1 operation on the byte array obtained by converting the hexadecimal string of the parameter, and then convert it into a hexadecimal string and return

  • sha3_hex: (string) => string Perform sha3-256 operation on the byte array obtained by converting the hexadecimal string of the parameter, and then convert it into a hexadecimal string and return

  • sha256_hex: (string) => string Perform sha256 operation on the byte array obtained by converting the hexadecimal string of the parameter, and then convert it into a hexadecimal string and return

  • ripemd160_hex: (string) => string Performs the ripemd160 operation on the byte array obtained by converting the hexadecimal string of the parameter, and then converts it into a hexadecimal string and returns

  • cbor_encode: (object) => string Serialize the parameters into a binary byte array according to cbor serialization and return it as a hexadecimal string

  • cbor_decode: (string) => object De-serialize the binary byte array corresponding to the hexadecimal string according to the cbor serialization method into a gula object

  • signature_recover: (string, string) => string The signature from the first parameter (compressed signature format, 65 bytes, expressed as a hexadecimal string) and the original digest of the second parameter (32 bytes, entered in 16) String representation)