Clique Network supports both on-chain and off-chain queries. Although they both follow our JSON-RPC-styled queries, the specific types naturally differ due to the different programming languages.
Note that you cannot create an identical Clique Query. If you attempt to create an identical Clique Query, you will encounter a QueryAlreadyExists error. If you do not need to rerun the Clique Query, you can simply retrieve the previous results instead of creating the same query again. If you indeed wish to rerun the query, specify a different id to create a new query.
On-Chain Query
For example, this is the manifest file for the clique_fibonacci sample task:
spec-version = "1"
name = "clique_fibonacci"
type = "Dynamic"
proof-type = ["TEE"]
[input]
n = { type = "u256", description = "n" }
[output]
result = { type = "u256", description = "fibonacci sequence for n" }
To create a query for running the clique_fibonacci task, you can define the task input using a struct, such as Params, and construct the query via ICliqueTaskManager.Task:
import {ICliqueTaskManager} from "clique-contracts/ICliqueTaskManager.sol";
struct Params {
uint256 n;
}
contract MyContract {
function run(uint256 n) public {
uint32 id = 0;
bytes memory method = "clique_fibonacci";
Params memory params = Params(n);
ICliqueTaskManager.Task memory task = ICliqueTaskManager.Task(
id,
method,
abi.encode(params)
);
// use ICliqueTaskManager to create new query
// ...
}
}
The above example is quite straightforward. Next, we will demonstrate a slightly more complex example. Below is the manifest file for our clique_httpsRequest task, which can be used to provide TLS calls for smart contracts:
spec-version = "1"
name = "clique_httpsRequest"
type = "BuiltIn"
proof-type = ["TEE"]
[types.Transformation]
from = { type = "string", description = "json pointer to field in response" }
soltype = { type = "string", description = "solidity type" }
[input]
url = { type = "string", description = "Request url" }
encoding = { type = "Transformation[]", description = "encoding format" }
[output]
response = { type = "bytes", description = "ABI encoded" }
Note that this task includes a custom type Transformation. Next, we will demonstrate how to create a query for this task:
For off-chain tasks, we use JSON format to create queries. Users can create queries via the Clique Client SDK, or you can directly access the Clique Network to create queries. However, we strongly recommend using the Clique Client SDK, as it helps you validate the query, eliminating the need for you to manually perform complex validation.
Creating off-chain queries is straightforward and supports the creation of batch queries.
Single Query
If use our Clique Client SDK, here we use the Rust SDK as an example:
use serde_json::json;
// Create a single query for running clique_fibonacci task
let json_query = json!({
"id": 1,
"method": "clique_fibonacci",
"params": {"n": "10"}
"input_types": {"n": "u256"},
"custom_types": {}
});
// Create a single query for running clique_httpsRequest task
let url = "https://jsonplaceholder.typicode.com/users";
let encoding = json!([
{"from": "/0/id", "soltype": "int16"},
{"from": "/0/name", "soltype": "string"},
]);
let json_query = json!({
"id": 1,
"method": "clique_httpsRequest",
"params": {"url": url, "encoding": encoding },
"input_types": {"url": "string", "encoding": "Transformation[]"},
"custom_types": {"Transformation": {"from": "string", "soltype": "string"}}
});
When using the Clique Client SDK, you need to specify input_types and custom_types. These type information must be filled according to the input section of the task's manifest and will be used by the client SDK to validate the query.
If you wish to directly access the Clique Network, you can construct the JSON query in the following format, but you will be responsible for validating the entire process yourself: