1. write and deploy the application contract to the appchain

  • endpointInfo is a fixed-format json string, where endpoint_type is written as service.
  • callDataJsonStr is also a fixed-format json string, which is the call parameter of the chainlink service.
 //SPDX-License-Identifier: SimPL-2.0
 pragma solidity ^0.6.0;
 /**
 * @title iService interface
 */
 interface iServiceInterface {
    /**
     * @dev Send cross chain request
     * @param _endpointInfo information of endpoint
     * @param _method Target method name
     * @param _callData Target method callData
     * @param _callbackAddress Callback contract address
     * @param _callbackFunction Callback function selector
     * @return requestID Request id
     */

     function sendRequest(
        string calldata _endpointInfo,
        string calldata _method,
        bytes calldata _callData,
        address _callbackAddress,
        bytes4 _callbackFunction
     ) external returns (bytes32 requestID);

     /**
        * @dev Set the response of the specified service request
        * @param _requestID Request id
        * @param _errMsg Error message of the service invocation
        * @param _output Response output
        * @return True on success, false otherwise
     */
     function setResponse(
        bytes32 _requestID,
        string calldata _errMsg,
        string calldata _output
     ) external returns (bool);
 } 

 /*
 * @title Contract for the iService core extension client
 */
 contract iServiceClient {
    iServiceInterface iServiceCore; // iService Core contract address
    // mapping the request id to Request
    mapping(bytes32 => Request) requests;
    // request
struct Request {
    address callbackAddress; // callback contract address
    bytes4 callbackFunction; // callback function selector
    bool sent; // request sent
    bool responded; // request responded
 } 
 /*
  * @dev Event triggered when the iService request is sent
  * @param _requestID Request id
  */
 event RequestSent(bytes32 _requestID);
 /*
     * @dev Make sure that the given request is valid
     * @param _requestID Request id
 */
 modifier validRequest(bytes32 _requestID) {
     require(
         requests[_requestID].sent,
         "iServiceClient: request does not exist"
    );
 require(
         !requests[_requestID].responded,
         "iServiceClient: request has been responded"
    );
 _;
 }

 /**
     * @dev Send cross chain request
     * @param _endpointInfo information of endpoint
     * @param _method Target method name
     * @param _callData Target method callData
     * @param _callbackAddress Callback contract address
     * @param _callbackFunction Callback function selector
     * @return requestID Request id

 */
 function sendIServiceRequest(
     string memory _endpointInfo,
     string memory _method,
     bytes memory _callData,
     address _callbackAddress,
     bytes4 _callbackFunction
 ) internal returns (bytes32 requestID) {
 requestID = iServiceCore.sendRequest(
         _endpointInfo,
         _method,
         _callData,
         address(this),
         this.onResponse.selector
 );
 Request memory request = Request(
     _callbackAddress,
     _callbackFunction,
     true,
     false
     );
     requests[requestID] = request;
     emit RequestSent(requestID);
     return requestID;
 } 

 /*
 * @dev Callback function
 * @param _requestID Request id
 * @param _output Response output
 */
 function onResponse(bytes32 _requestID, string calldata _output)
     external
    validRequest(_requestID)
 {
 address cbAddr = requests[_requestID].callbackAddress;
 bytes4 cbFunc = requests[_requestID].callbackFunction;
 (bool success, ) = cbAddr.call(
         abi.encodeWithSelector(cbFunc, _requestID, _output)
     );
     if (!success) revert();
 }

 /**
  * @dev Set the iService core contract address
 * @param _iServiceCore Address of the iService core contract
 */
 function setIServiceCore(address _iServiceCore) internal {
     require(
         _iServiceCore != address(0),
         "iServiceClient: iService core address can not be zero"
     );
     iServiceCore = iServiceInterface(_iServiceCore);
    }
 } 

 /*
 * @title Contract for inter-chain NFT minting powered by iService
 * The service is supported by price service
 */
 contract ServiceConsumer is iServiceClient {
     string private endpointInfo =

 '{"dest_chain_id":"10227431719070003","dest_chain_type":"opb","endpoint_address":"iaa15s9sulrnm
 ctzluc42g7lkxh92ardkc9xccxsy9","endpoint_type":"service"}';
     event Hello(bytes32 _requestID, string _helloMsg);
     string public result;
 /*
 * @notice Constructor
 * @param _iServiceContract Address of the iService contract
 * @param _defaultTimeout Default service request timeout
 */
 constructor(address _iServiceCore) public {
     setIServiceCore(_iServiceCore);
 }

 /*
 * @notice Start workflow for minting nft
 * @param _method method name
 * @param _hello arguments
 */
 function helloWorld() external {
     string
         memory callDataJsonStr = '{"service_name":"eth-usdt-
 price","providers":["iaa16eu2jvgpa5ek9mn2tn985jlm89e6dch92qwj09"],"input":"
 {\\"header\\":{},\\"body\\":
 {}}","timeout":100,"service_fee_cap":"1000000upoint"}';
     bytes memory callData;
     callData = bytes(callDataJsonStr);
     sendIServiceRequest(
         endpointInfo,
         "service",
         callData,
         address(this),
         this.onHello.selector
     );
 }

 /*
 * @notice NFT service callback function
 * @param _requestID Request id
 * @param _output NFT service response output
 */
 function onHello(bytes32 _requestID, string calldata _output)
     external
     validRequest(_requestID)
     {
         requests[_requestID].responded = true;
         result = _output;
     }
 }

2. Service call

Execute the helloworld method in the application contract.

3. View the result

Open your browser and login to the Chainlink Operator Dashboard to view the result of the oracle service invocation in the interface.

Servicecall1
Servicecall2

The result returned by the cross-chain call can be viewed through the result method of the application contract.

Servicecall3
© 2019-2024 Red Date (Hong Kong) Technology Limited. all right reserved,powered by GitbookFile Modify: 2023-02-16 12:22:02

results matching ""

    No results matching ""

    results matching ""

      No results matching ""