Oracle: Call any API from a smart contract

Overview

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.

We will create a contract to request a summary of any Wikipedia article. We will use the Startpoint Called source, the Make HTTP Request task, and the Transact to EVM Function task.

StartpointCalledSource:

  1. Listens for the caller to send the title of a wikipedia article

  2. Extracts the title and sends it to MakeHttpRequestTask

MakeHttpRequestTask:

  1. Makes a request to the Wikimedia API

  2. Extracts the summary from the HTTP response

  3. Sends the summary to the TranasctToEvmFunctionTask

TransactToEvmFunctionTask:

  1. Submits the summary via a transaction to an on-chain function defined by the developer

Midpoint

$ npm install -g midpoint-cli

$ midpoint init blank my-oracle

1 . StartpointCalledSource

$ midpoint add-source startpointCalledSource oracle-source

We define the Startpoint Called source so that contract can make a midpoint request.

2. MakeHTTPRequestTask

$ midpoint add-task makeHttpRequest call-wikipedia

We define the Make HTTP Request Task to make the call to the Wikipedia endpoint.

3. TransactToEVMFunction

$ midpoint add-task transactToEvmFunctionTestnet callback

We define the Transact to EVM FunctionTask to send the summary back to the contract.

4. Define Path

We define our path file.

5. Deploy Contract

6. Publish Midpoint

$ midpoint publish

Last updated

Was this helpful?