📄Single Step FHE Request
In this example, we show how to make a simple request to Sight Oracle and get the response.
Example Contract
// SPDX-License-Identifier: BSD-3-Clause-Clear
pragma solidity ^0.8.20;
import "@sight-oracle/contracts/Oracle/Types.sol";
import "@sight-oracle/contracts/Oracle/Oracle.sol";
import "@sight-oracle/contracts/Oracle/RequestBuilder.sol";
import "@sight-oracle/contracts/Oracle/ResponseResolver.sol";
contract Example {
// Use Sight Oracle's RequestBuilder and ResponseResolver to interact with Sight Oracle
using RequestBuilder for RequestBuilder.Request;
using ResponseResolver for CapsulatedValue;
Oracle public oracle;
CapsulatedValue private _target;
constructor(address oracle_) payable {
oracle = Oracle(payable(oracle_));
}
function makeRequest() public payable {
// Initialize new FHE computation request of a single step.
RequestBuilder.Request memory r = RequestBuilder.newRequest(
msg.sender,
1,
address(this),
this.callback.selector, // specify the callback for Oracle
''
);
// Generate a random encrypted value and store in Sight Network
r.rand();
// Call request.complete() to complete build process
r.complete();
// Send the request via Sight FHE Oracle
oracle.send(r);
}
// only Oracle can call this
function callback(bytes32 /** requestId **/, CapsulatedValue[] memory values) public onlyOracle {
// Decode value from Oracle callback
CapsulatedValue memory result = values[0];
// Keep this encrypted target value
_target = result;
}
modifier onlyOracle() {
require(msg.sender == address(oracle), "Only Oracle Can Do This");
_;
}
}
Explanation
Contract Initialization:
The
Examplecontract imports the necessary modules from the Sight Oracle package.The contract uses the
RequestBuilderlibrary to construct requests and theResponseResolverlibrary to interpret the responses.
Constructor:
The constructor initializes the contract with the address of the Sight Oracle. This address is used to send requests and handle callbacks.
makeRequest Function:
This function initiates a new request to the Sight Oracle.
A new request is created using
RequestBuilder.newRequest, specifying the sender, the number of steps in the computation, the address to which the callback should be sent, and the callback function.The
randfunction is called to generate a random encrypted value and store it in the Sight Network.The
completefunction finalizes the request build process.The
sendfunction sends the request to the Sight Oracle.
callback Function:
This function is called by the Sight Oracle once the computation is complete.
It decodes the returned value using the
ResponseResolverand stores the encrypted target value in the_targetvariable.
Modifiers:
onlyOracle: Ensures that only the Sight Oracle can call thecallbackfunction.
By following this guide, you can easily make a request to the Sight Oracle and handle the response securely and efficiently.
Last updated