Websocket

Websocket Interfaces

The websocket interface of Zetrix handles various defined message types.

Message Type

enum ChainMessageType {
    CHAIN_TYPE_NONE = 0;
    CHAIN_HELLO = 10; // response with CHAIN_STATUS = 2;
    CHAIN_TX_STATUS = 11;
    CHAIN_PEER_ONLINE = 12;
    CHAIN_PEER_OFFLINE = 13;
    CHAIN_PEER_MESSAGE = 14;
    CHAIN_SUBMITTRANSACTION = 15;
    CHAIN_LEDGER_HGasDER = 16; //Zetrix notifies the client ledger(protocol::LedgerHeader) when closed
    CHAIN_SUBSCRIBE_TX = 17; //response with CHAIN_RESPONSE
    CHAIN_TX_ENV_STORE = 18;
}

Notification Message Registration

  • Function

    The client registers the message with the blockchain through the interface, that is, the type of the message that needs to be received (currently the function is unavailable). The version information of the blockchain can only be obtained through this interface.

  • Request Message Type

    CHAIN_HELLO

  • Request Data Object

    message ChainHello {
      repeated ChainMessageType api_list = 1; //By default, enable all apis
      int64   timestamp = 2;
    }
  • Request Parameter

  • Reponse Message Type

    CHAIN_HELLO

  • Reponse Data Object

    message ChainStatus {
      string self_addr        = 1;
      int64 ledger_version    = 2;
      int64 monitor_version   = 3;
      string buchain_version      = 4;
      int64   timestamp       = 5;
    }
  • Reponse Result

Submit Transaction

  • Function

    The transaction that will need to be executed is sent to the blockchain execution through the message type. Please refer to Transaction structure for details of the transaction structure.

  • Request Message Type

    CHAIN_SUBMITTRANSACTION

  • Request Message Object

    // Signature
    message Signature {
      string public_key = 1;
      bytes sign_data = 2;
    }
    
    // Request object
    message TransactionEnv {
      Transaction transaction = 1;
      repeated Signature signatures   = 2;
      Trigger trigger = 3;
    }
  • Request Parameter

  • Reponse Message Type

    • CHAIN_TX_STATUS:Returns the result of the transaction submission (the successful submission of the transaction does not mean that the transaction was executed successfully).

    • CHAIN_TX_ENV_STORE:Returns the result of the transaction execution.

  • CHAIN_TX_STATUS Object

    message ChainTxStatus {
      enum TxStatus {
          UNDEFINED   = 0;
          CONFIRMED   = 1;    // web server will check tx parameters, signatures etc fist, noitfy CONFIRMED if pass
          PENDING     = 2;    // master will check futher before put it into pending queue
          COMPLETE    = 3;    // notify if Tx write ledger successfully
          FAILURE     = 4;    // notify once failed and set error_code
      };
    
      TxStatus    status = 1;
      string      tx_hash = 2;
      string      source_address = 3;
      int64       source_account_seq = 4;
      int64       ledger_seq = 5;         //on which block this tx records
      int64       new_account_seq = 6;        //new account sequence if COMPLETE
      ERRORCODE   error_code = 7;         //use it if FAIL
      string      error_desc = 8  ;           //error desc
      int64       timestamp = 9;          
    }
  • ChainTxStatus Member

  • CHAIN_TX_ENV_STORE Object

    message TransactionEnvStore{
      TransactionEnv transaction_env = 1;
      int32 error_code = 2;
      string error_desc = 3;
      int64 ledger_seq = 4;
      int64 close_time = 5;
      //for notify
      bytes hash = 6;
      int64 actual_fee = 7;
      repeated bytes contract_tx_hashes = 8;
    }
  • TransactionEnvStore Member

Message Subscription

  • Function

    This interface implements transaction notifications that only specify the account address of the interface.

  • Request Message Type

    CHAIN_SUBSCRIBE_TX

  • Request Data Object

    message ChainSubscribeTx{
      repeated string address = 1;
    }
  • Request Parameter

  • Reponse Message Type

    ChainResponse

  • Reponse Data Object

    message ChainResponse{
          int32 error_code = 1;
          string error_desc = 2;
    }
  • Reponse Result

Transaction structure

  • In protobuf format

    message Transaction {
        enum Limit{
            UNKNOWN = 0;
            OPERATIONS = 1000;
        };
        string source_address = 1;
        int64 nonce = 2;
        int64  fee_limit = 3;
        int64  gas_price =4;
        int64 ceil_ledger_seq = 5;
        bytes metadata = 6;
        repeated Operation operations = 7;
        int64 chain_id = 8;
    }
  • Keywords in protobuf

Operations structure

The corresponding operations in the protobuf structure of the transaction can contain one or more operations.

  • In protobuf format

    message Operation {
        enum Type {
            UNKNOWN = 0;
            CREATE_ACCOUNT          = 1;
            ISSUE_ASSET             = 2;
            PAY_ASSET               = 3;
            SET_METADATA            = 4;
            SET_SIGNER_WEIGHT       = 5;
            SET_THRESHOLD           = 6;
            PAY_COIN                = 7;
            LOG                     = 8;
            SET_PRIVILEGE           = 9;
        };
        Type type = 1;
        string source_address = 2;
        bytes metadata  = 3;
    
        OperationCreateAccount      create_account     = 4;
        OperationIssueAsset         issue_asset        = 5;
        OperationPayAsset           pay_asset          = 6;
        OperationSetMetadata        set_metadata       = 7;
        OperationSetSignerWeight    set_signer_weight  = 8;
        OperationSetThreshold       set_threshold      = 9;
        OperationPayCoin            pay_coin           = 10;
        OperationLog                log                = 11;
        OperationSetPrivilege       set_privilege      = 12;
    }
  • Keyword in protobuf

Operation Codes

Creating Accounts

The source account creates a new account on the blockchain. Creating Accounts are divided into Creating Normal Accounts and Creating Contract Accounts.

Protobuf format as follow:

// Key-Value pair
message KeyPair{
    string key = 1;
    string value = 2;
    int64 version = 3;
}

// Privilege
message Signer {
    enum Limit{
        SIGNER_NONE = 0;
        SIGNER = 100;
    };
    string address = 1;
    int64 weight = 2;
}
message OperationTypeThreshold{
    Operation.Type type = 1;
    int64 threshold = 2;
}
message AccountThreshold{
    int64 tx_threshold = 1; //required, [-1,MAX(INT64)] -1: indicates no setting
    repeated OperationTypeThreshold type_thresholds = 2;
}
message AccountPrivilege {
    int64 master_weight = 1;
    repeated Signer signers = 2;
    AccountThreshold thresholds = 3;
}

// Contract
message Contract{
    enum ContractType{
        JAVASCRIPT = 0;
    }
    ContractType type = 1;
    string payload = 2;
}

// Create Account Operation
message OperationCreateAccount{
    string dest_address = 1;
    Contract contract = 2;
    AccountPrivilege priv = 3;
    repeated KeyPair metadatas = 4; 
    int64   init_balance = 5;
    string  init_input = 6;
}

Creating Normal Accounts

Note: Both master_weight and tx_threshold must be 1 in the current operation. And only the following keywords are allowed to be initialized.

  • Keyword in protobuf

  • Query

    The account information is queried through the getAccount interface in HTTP.

Creating Contract Accounts

Note: In the current operation, master_weight must be 0 and tx_threshold must be 1. And only the following keywords are allowed to be initialized

  • Keyword in protobuf

  • Query

    • The account information is queried through the getAccount interface in HTTP.

    • Query with the getTransactionHistory interface in HTTP, and the result is as follows:

      [
          {
              "contract_address": "ztxSn8xpL7c2xkxwbreVCs6EZ7KZbBvtDaLtV", //The contract account
              "operation_index": 0                                        //The operation index value in the transaction array, 0 means the first transaction
          }
      ]

Issuing Assets

  • Function

    The source account of this operation issues a digital asset, and this asset appears in the asset balance of the source account after successful execution.

  • In protobuf format

    message OperationIssueAsset{
        string code = 1;
        int64 amount = 2;
    }
  • Keyword in protobuf

Transferring Assets

Note: If the target account is a contract account, the current operation triggers the contract execution of the target account.

  • Function

    The source account of this operation transfers an asset to the target account.

  • In protobuf format

    message AssetKey{
         string issuer = 1;
         string code = 2;
         int32 type = 3;
    }
    message Asset{
         AssetKey   key = 1;
         int64      amount = 2;
    }
    
    // Pay asset operation
    message OperationPayAsset{
        string dest_address = 1;
        Asset asset = 2;
        string input = 3;
    }
  • Keyword in protobuf

Setting Metadata

  • Function

    The source account of this operation modifies or adds metadata to the metadata table.

  • In protobuf format

    message OperationSetMetadata{
        string  key = 1;  
        string  value = 2;
        int64   version = 3;
        bool    delete_flag = 4;
    }
  • Keyword in protobuf

Setting Privileges

  • Function

    Set the weights that the signer has and set the thresholds required for each operation. For details, see Assignment of Control Rights in HTTP.

  • In protobuf format

    message Signer {
        enum Limit{
            SIGNER_NONE = 0;
            SIGNER = 100;
        };
        string address = 1;
        int64 weight = 2;
    }
    message OperationTypeThreshold{
        Operation.Type type = 1;
        int64 threshold = 2;
    }
    
    // Set privilege object
    message OperationSetPrivilege{
        string master_weight = 1;
        repeated Signer signers = 2;
        string tx_threshold = 3;
        repeated OperationTypeThreshold type_thresholds = 4;
    }
  • Keywords in protobuf

Transferring Gas Assets

Note: If the target account is a contract account, the current operation triggers the contract execution of the target account.

  • Function

    Two functions:

    1. The source account of this operation transfers a Gas asset to the target account.

    2. The source account of this operation creates a new account on the blockchain.

  • In protobuf format

    message OperationPayCoin{
        string dest_address = 1;
        int64 amount = 2;
        string input = 3;
    }
  • protobufKeyword

Recording Logs

  • Function

    The source account of this operation writes the log to the blockchain.

  • In protobuf format

    message OperationLog{
        string topic = 1;
        repeated string datas = 2;
    }
  • protobufKeyword

Error Codes

The error code is composed of two parts:

  • error_code : Error code, approximate error classification

  • error_desc : Error Description, which can accurately find the error specific information from the error description

The error list is as follows:

Last updated