This project demonstrates a simple decentralized oracle network (DON) where users can submit off-chain source code for execution by oracle nodes. The system allows smart contracts to interact with external data via oracle nodes, which fetch, execute, and return results to the requesting contract.

This contract allows users to register a request for off-chain code execution by providing:
- A URL to the source code (e.g., IPFS link).
- The encoded callback function signature from the requesting contract.
Oracle nodes listen for these requests, execute the source code in a sandbox, and return the result on-chain.
This is a sample contract that integrates with the GeneralizedOracle contract. It uses the oracle to fetch and execute predictions based on external data. Once the oracle has processed the data, it calls back a function in the PredictionMarket contract to update the result.
A contract (such as PredictionMarket.sol
) calls the registerSourceCode()
function in the GeneralizedOracle contract. This function takes two parameters:
- A
sourceCodeURL
string, which points to the location of the off-chain source code (e.g., an IPFS URL). - A
callbackFunctionSignature
, which is the encoded signature of the function in the calling contract that will handle the result.
Example:
function requestOracle() public {
GeneralizedOracle oracle = GeneralizedOracle(oracleAddress);
string memory sourceCodeURL = "https://ipfs.io/ipfs/QmYourSourceCodeHash";
bytes memory callbackSignature = getEncodedCallback();
oracle.registerSourceCode(sourceCodeURL, callbackSignature);
}
The oracle emits an OracleRequest event that off-chain nodes listen to.
Off-chain oracle nodes pick up the OracleRequest event, fetch the source code from the provided URL, and execute the logic. After execution, the result is encoded and sent back on-chain by calling the respondToRequest()
function in the GeneralizedOracle contract.
const result = 42; // Simulated result
const encodedResult = ethers.utils.defaultAbiCoder.encode(["uint256"], [result]);
const tx = await oracleContract.respondToRequest(requestId, encodedResult);
await tx.wait();
Once the oracle processes the request, it calls the oracleCallback()
function in the requesting contract (e.g., PredictionMarket.sol
). The function signature and result are passed, and the contract handles the result.
function oracleCallback(uint256 result) external {
require(msg.sender == oracleAddress, "Unauthorized oracle");
predictionOutcome = result;
}
Deploy the GeneralizedOracle contract and note its address.
Deploy the PredictionMarket contract, passing the address of the deployed GeneralizedOracle contract to its constructor.
PredictionMarket market = new PredictionMarket("0xYourOracleContractAddress");
From the PredictionMarket contract, call the requestOracle()
function to register a new request with the oracle.
Simulate the off-chain oracle node fetching the request, executing the code, and responding with the result by calling respondToRequest()
in the GeneralizedOracle contract.
oracle.respondToRequest(1, abi.encode(42));
This will call the callback function in PredictionMarket and update the predictionOutcome
variable.
string memory sourceCodeURL = "https://ipfs.io/ipfs/Qm<>";
bytes memory callbackSignature = hex"03e78d5d"; // Encoded callback signature
-
mockUsdc
View on Morph Explorer -
Predict and Earn (Polymarket)
View on Morph Explorer -
Generalized Oracle
View on Morph Explorer
-
Generalized Oracle
View on AirDAO Explorer -
MockUsdc*
View on AirDAO Explorer -
Predict and Earn
View on AirDAO Explorer