📄Multi Step FHE Request

In this example, we show how to make a multi-step request to Sight Oracle and get the response.

// SPDX-License-Identifier: BSD-3-Clause-Clear

pragma solidity ^0.8.20;

import "@sight-oracle/contract/Types.sol";
import "@sight-oracle/contract/Oracle.sol";
import "@sight-oracle/contract/ResponseResolver.sol";

contract ExampleMultiStep {

    // Use Sight Oracle's RequestBuilder and ResponseResolver to interact with Sight Oracle
    using RequestBuilder for RequestBuilder.Request;
    using ResponseResolver for CapsulatedValue;

    SightOracle public oracle;

    // We assume _target is already fulfilled with previous value
    CapsulatedValue private _target;

    constructor(address oracle_) payable {
        oracle = SightOracle(payable(oracle_));
    }

    function makeRequest(uint64 input) public payable {
        // For simplicity, we ignore the code for paying SIGHT token

        // Initialize new FHE computation request of a single step.
        RequestBuilder.Request memory r = RequestBuilder.newRequest(
            msg.sender,
            2,
            address(this),
            this.callback.selector // specify the callback for Oracle
        );

        // Step 1 - Load euint64 into Request Execution Context
        op e_target = r.getEuint64(_target.asEuint64());

        // Step 2 - Add _target with plaintext input and store in Sight Network
        op e_target_sum = r.add(e_target, input);

        // 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
        // this is same as current _target as values are immutable in Sight Network
        CapsulatedValue memory result = values[0];

        // this is the new encrypted target value;
        CapsulatedValue memory new_result = values[1];

        // Keep this new encrypted target value
        _target = new_result;
    }

    modifier onlyOracle() {
        require(msg.sender == address(oracle), "Only Oracle Can Do This");
        _;
    }

}

Explanation

makeRequest Function:

  • This function initiates a new multi-step request to the Sight Oracle.

  • A new request is created using RequestBuilder.newRequest, specifying the sender, the number of steps in the computation (2 steps in this example), the address to which the callback should be sent, and the callback function.

  • The getEuint64 function is called to load an encrypted value into the request execution context.

  • The add function is used to add the loaded encrypted value (_target) with a plaintext input.

  • The complete function finalizes the request build process.

  • The send function 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 values using the ResponseResolver.

  • The first value (values[0]) is the same as the current _target, as values are immutable in the Sight Network.

  • The second value (values[1]) is the new encrypted target value, which is then stored in the _target variable.

Last updated