An oracle is a midpoint that makes HTTP requests from contracts to off-chain endpoints. Unlike traditional oracle networks - midpoints allow developers to define arbitrarily complex requests with components such as parameters/headers defined on-chain, multistep authentication workflows, private off-chain data for keys, and cross-chain functionality.
/* * SPDX-License-Identifier: MIT * Midpoint Sample Storage Contract v3.0.0 * * This is a contract generated at 2023-02-28 20:07:34 for testing requests to midpoint 160. * This contract is intended to serve as a guide for interfacing with a midpoint and should not be used * as is in a production environment. * For more information on setting up a midpoint and using this contract see docs.midpointapi.com */pragma solidity>=0.8.0;interface IMidpoint {functioncallMidpoint(uint64 midpointId,bytescalldata_data) externalreturns(uint64 requestId);}contract TestMidpointOracleContract {// These events can be removed without impacting the functionality of your midpointeventRequestForWikipediaSummaryMade(string article);eventWikipediaSummaryReceived(uint64 Request_ID, uint64 Midpoint_ID, string wikipedia_summary);// A verified startpoint for Goerli Testnetaddressconstant startpointAddress =0x795c5292b9630d473d568079b73850F29344403c;// A verified midpoint transact to EVM function EOA for Goerli Testnetaddressconstant whitelistedCallbackAddress =0xC0FFEE4a3A2D488B138d090b8112875B90b5e6D9;// The globally unique identifier for your midpointuint64constant midpointID =160;// Mapping of Request ID to a flag that is checked when the request is satisfied// This can be removed without impacting the functionality of your midpointmapping(uint64=>bool) public request_id_satisfied;// Mappings from Request ID to each of your results// This can be removed without impacting the functionality of your midpointmapping(uint64=>string) public request_id_to_wikipedia_summary;/* * This function makes a call to your midpoint with On-Chain Variables specified as function inputs. * * Note that this is a public function and will allow any address or contract to call midpoint 160. * Configure your midpoint to permit calls from this contract when testing. Before using your midpoint * in a production environment, ensure that calls to 'callMidpoint' are protected. * Any call to 'callMidpoint' from a whitelisted contract will make a call to your midpoint; * there may be multiple places in this contract that call the midpoint or multiple midpoints called by the same contract.
*/functiongetWikipediaSummary(stringmemory article) public {// This packs together all of the On-Chain Variables for your midpoint into a single bytestringbytesmemory args = abi.encodePacked(article,bytes1(0x00));// This makes the call to your midpointuint64 requestId =IMidpoint(startpointAddress).callMidpoint(midpointID, args);// This logs that the call has been made, and can be removed without impacting your midpointemitRequestForWikipediaSummaryMade(article); request_id_satisfied[requestId] =false; }/* * This function is the callback target specified in your midpoint callback definition. * Note that the callback is placed in the same contract as the call to callMidpoint for simplicity when testing. * The callback does not need to be defined in the same contract as the request or live on the same chain. */functionrequestedWikipediaSummary(uint64 midpointId,uint64 requestId,stringmemory wikipediaSummary) public {// Only allow a verified callback address to submit information for your midpoint.require(tx.origin == whitelistedCallbackAddress,"Invalid callback address");// Only allow requests that came from your midpoint IDrequire(midpointID == midpointId,"Invalid Midpoint ID"); // This stores each of your response variables. This is where you would place any logic associated with your callback.
// Your midpoint can transact to a callback with arbitrary execution and gas cost. request_id_to_wikipedia_summary[requestId] = wikipediaSummary;// This logs that a response has been received, and can be removed without impacting your midpointemitWikipediaSummaryReceived(requestId, midpointId, wikipediaSummary); request_id_satisfied[requestId] =true; }}