Clique Docs
  • What Is Clique
    • TEE Network
    • Compute Coordination Network
  • Build with Clique
    • Clique Application Structure
    • Clique CLI
      • Installation
      • Develop Task
      • Build Task
      • Test Task
      • Deploy Task
    • Clique Client SDK
    • Smart Contract SDK
      • Smart Contract Integration
      • Clique Official Tasks
  • References
    • Clique Manifest
    • Clique Query
    • Verification
  • Sample Task Tutorials
    • Data Attestation
    • Social Verification
      • Github
      • Twitter
    • Making Arbitrary TLS Calls (TLS Oracle)
    • Custom Executor
  • Toolchain
    • Clique Pipelines SDK
    • Clique Attestation SDK
      • Attestation Protocols
      • Reading Attestations On-chain
      • Reading Attestations Off-Chain
      • What are Attestors ?
        • Data Sources
    • Clique Browser Extension
  • FAQ
  • Glossaries
  • Socials
Powered by GitBook
On this page
  • On-Chain Query
  • Off-Chain Query
  1. References

Clique Query

Clique supports JSON-RPC-styled queries.

Field
description
required

id

Unique query ID

Yes

method

Task name

Yes

params

Task input

Yes

input_types

Task input types

Only required by Clique Client SDK

custom_types

Custom types used in task input types

Only required by Clique Client SDK

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:

import {ICliqueTaskManager} from "clique-contracts/ICliqueTaskManager.sol";

struct Transformation {
    string from;
    string soltype;
}

struct Params {
    string url;
    Transformation[] encoding;
}

contract MyContract {
    function run() public {
        Transformation[] memory transformations = new Transformation[](2);
        transformations[0] = Transformation({from: "/0/id", soltype: "int16"});
        transformations[1] = Transformation({from: "/0/name", soltype: "string"});

        uint32 id = 0;
        bytes memory method = "clique_httpsRequest";
        Params memory params = Params({
            url: "https://jsonplaceholder.typicode.com/users",
            encoding: transformations
        });
        
        ICliqueTaskManager.Task memory task = ICliqueTaskManager.Task(
            id,
            method,
            abi.encode(params)
        );
        
        // use ICliqueTaskManager to create new query
        // ...
    }
}

For further details, please refer to Smart Contract Integration.

Off-Chain Query

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:

// JSON query for clique_fibonacci task
{
    "id": 1, 
    "method": "clique_fibonacci", 
    "params": {"n": "10"}
}

// JSON query for clique_httpsRequest task
{
    "id": 1,
    "method": "clique_httpsRequest",
    "params": {
        "url": "https://jsonplaceholder.typicode.com/users", 
        "encoding": [
            {"from": "/0/id", "soltype": "int16"},
            {"from": "/0/name", "soltype": "string"},
        ]
    },
}

Batch Query

If use our Clique Client SDK:

use serde_json::json;

// Create batch query
let json_query = json!([
    {"id": 2, "method": "clique_fibonacci", "params": {"n": "10"}, "input_types": {"n": "u256"}, "custom_types": {}},
    {"id": 3, "method": "clique_fibonacci", "params": {"n": "11"}, "input_types": {"n": "u256"}, "custom_types": {}},
    {"id": 4, "method": "clique_fibonacci", "params": {"n": "12"}, "input_types": {"n": "u256"}, "custom_types": {}}
]);

If accessing the Clique Network directly:

[
    {"id": 2, "method": "clique_fibonacci", "params": {"n": "10"}},
    {"id": 3, "method": "clique_fibonacci", "params": {"n": "11"}},
    {"id": 4, "method": "clique_fibonacci", "params": {"n": "12"}}
]

PreviousClique ManifestNextVerification

Last updated 8 months ago