The contract platform provides multiple query operations for you to query blocks, transactions, accounts, and contracts.
Query block headers
You can call the QueryBlockHeader operation to query a specified block header. You can use the block height number or the block hash hash to query the target block header.
Request parameters
One of these two parameters must be used.
| Name | Required | Type | Description |
|---|---|---|---|
| block_number | false | number | The height value of the target block. |
| hash | false | string | The hash of the target block. It is a hexadecimal string prefixed with “0x”. |
Example 1
Query a block using the block height.
chain.ctr.QueryBlockHeader({block_number: 30, // Specify the value of the block height based on your business requirements.}, (err, data) => {console.log(data)})
Example 2
Query a block using the block hash.
chain.ctr.QueryBlockHeader({hash: '0xe6eebd5b7a6eff8f0af9ac151160e59cc7ec98429822400fc78906c3895bc1aa', // Specify the block hash based on your business requirements.}, (err, data) => {console.log(data)})
Query blocks
QueryLastBlock
You can call this operation to query the last block. This operation does not require any request parameters.
Examples
chain.ctr.QueryLastBlock({}, (err, data) => {console.log(data)})
QueryBlock
You can call this operation to query a specified block header. You can use the block height number or the block hash hash to query the target block header.
Request parameters
| Name | Required | Type | Description |
|---|---|---|---|
| block_number | false | number | The height value of the target block. |
| hash | false | string | The hash of the target block. It is a hexadecimal string prefixed with “0x”. |
Example 1
Query a block using the block height.
chain.ctr.QueryBlock({block_number: 30, // Specify the value of the block height based on your business requirements}, (err, data) => {console.log(data)})
Example 2
Query a block using the block hash.
chain.ctr.QueryBlock({hash: '0xe6eebd5b7a6eff8f0af9ac151160e59cc7ec98429822400fc78906c3895bc1aa', // Specify the block hash based on your business requirements}, (err, data) => {console.log(data)})
Query transactions
You can call the QueryTransaction operation to query a transaction using the transaction hash.
Request parameters
| Name | Required | Type | Description |
|---|---|---|---|
| hash | true | string | The hash of the target transaction. It is a hexadecimal string prefixed with “0x”. |
Examples
chain.ctr.QueryTransaction({hash: '0xf66621372a13c813fe978c060e11a93c28b75f90d2200b9fa700f21433a96a77' //Specify the value based on your business requirements}, (err, data) => {console.log('QueryTransaction:', data)})
Query receipts
You can call the QueryTransactionReceipt operation to query a transaction receipt using the transaction hash.
Request parameters
| Name | Required | Type | Description |
|---|---|---|---|
| hash | true | string | The hash value of the target transaction. It is a hexadecimal string prefixed with “0x”. |
Examples
chain.ctr.QueryTransactionReceipt({hash: '0xf66621372a13c813fe978c060e11a93c28b75f90d2200b9fa700f21433a96a77' //Specify the value based on your business requirements}, (err, data) => {console.log('QueryTransactionReceipt:', data)})
Query accounts
You can call the QueryAccount operation to query a specified account. The name of the account is required. The hash value of thename is the identity of the target account. You can also directly provide a hexadecimal string prefixed with “0x” as the identity of the target account.
Request parameters
| Name | Required | Type | Description |
|---|---|---|---|
| from | true | string | The name of the target account (not prefixed with “0x”) or the identity of the target account represented by a hexadecimal string prefixed with “0x”. The system calculates the accountname, and the hash of the account name is the identity of the account. |
Example 1
Use the account name.
chain.ctr.QueryAccount({from: 'Tester001'}, (err, data) => {console.log('QueryAccount:', data)})
Example 2
Use the identity of the target account. The identity is represented by a hexadecimal string.
chain.ctr.QueryAccount({from: '0xc60a9d48105950a0cca07a4c6320b98c303ad42d694a634529e8e1a0a16fcdb5'}, (err, data) => {console.log('QueryAccount:', data)})
Query contract accounts
You can call the QueryContract operation to query a specified contract account. The name of the target contract is required. The hash value of the name is the identity of the target contract. You can also directly provide a hexadecimal string prefixed with “0x” as the identity of the target contract.
Request parameters
The following parameters are encapsulated and passed in as an object.
| Name | Required | Type | Description |
|---|---|---|---|
| from | true | string | The name of the target contract (not prefixed with “0x”) or the identity of the target contract represented by a hexadecimal string prefixed with “0x”. If the account name is used, the hash value of the account name is the identity of the contract. |
Examples
contractName = 'contract'+Date.now()let myContract = chain.ctr.contract(contractName, abi)myContract.new(bytecode, {from: 'Tester001'}, (err, contract, data) => {console.log('contract deploy result:', data)// Query the deployed contractchain.ctr.QueryContract({from: contractName}, (err, data) => {console.log('QueryContract:', data)})})