Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

Neo N3 Oracle Smart Contract Example

Overview

This example demonstrates how to implement and use the Oracle service in Neo N3 smart contracts. Oracles solve the critical problem of accessing off-chain data within blockchain applications, enabling smart contracts to interact with external data sources securely and reliably.

Key Features

  • Oracle service integration
  • External API data requests
  • JSON response parsing
  • Implementation of the IOracle interface
  • Asynchronous callback handling

Technical Implementation

The SampleOracle contract demonstrates several key aspects of utilizing Neo's Oracle service:

Oracle Request Process

  1. Request Initiation: The contract initiates a request to an external API endpoint
  2. JSONPath Query: Specifies what data to extract using JSONPath syntax
  3. Callback Registration: Registers a method to receive and process the Oracle response
  4. Fee Payment: Handles the Oracle service fee

Response Handling

  • Implementation of the IOracle interface
  • Security validation of the Oracle callback
  • Error handling for different response codes
  • JSON response deserialization
  • Storage of retrieved data

Oracle Component Details

Request Configuration

The example demonstrates requesting data from a JSONBin API with:

  • Complete URL specification
  • JSONPath selector for targeted data extraction
  • Callback method registration
  • Fee payment using Oracle.MinimumResponseFee

Response Processing

The OnOracleResponse method showcases:

  • Validation that the caller is the Oracle service
  • Response code validation
  • JSON array deserialization
  • Data extraction and storage

Security Considerations

  • Caller Verification: Ensures only the Oracle service can trigger the callback
  • Response Validation: Checks the response code before processing data
  • Error Handling: Properly handles and reports various error scenarios

Use Cases

This Oracle example can be adapted for numerous applications:

  • Price feeds for DeFi applications
  • Weather data for parametric insurance
  • Sports results for betting platforms
  • Random number generation
  • Cross-chain communication
  • Real-world event verification

Limitations

When using the Neo Oracle service, be aware of:

  • Oracle requests incur fees
  • Response times are not immediate
  • Data sources must be reliable and accessible
  • JSONPath queries have syntax requirements
  • Response data size limitations

Customization Guide

To adapt this example for your own Oracle needs:

  1. Update the request URL to your desired API endpoint
  2. Modify the JSONPath query to extract specific data
  3. Adjust the response handling logic for your data format
  4. Implement appropriate storage or processing of the retrieved data

Educational Value

This example teaches:

  • How to bridge on-chain and off-chain worlds
  • Asynchronous programming patterns in smart contracts
  • JSONPath query construction
  • Interface implementation in Neo contracts
  • Proper error handling for external services

The Oracle service is a critical component for building real-world applications on Neo N3, enabling smart contracts to react to external events and data.