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.
- Oracle service integration
- External API data requests
- JSON response parsing
- Implementation of the IOracle interface
- Asynchronous callback handling
The SampleOracle contract demonstrates several key aspects of utilizing Neo's Oracle service:
- Request Initiation: The contract initiates a request to an external API endpoint
- JSONPath Query: Specifies what data to extract using JSONPath syntax
- Callback Registration: Registers a method to receive and process the Oracle response
- Fee Payment: Handles the Oracle service fee
- Implementation of the
IOracleinterface - Security validation of the Oracle callback
- Error handling for different response codes
- JSON response deserialization
- Storage of retrieved data
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
The OnOracleResponse method showcases:
- Validation that the caller is the Oracle service
- Response code validation
- JSON array deserialization
- Data extraction and storage
- 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
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
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
To adapt this example for your own Oracle needs:
- Update the request URL to your desired API endpoint
- Modify the JSONPath query to extract specific data
- Adjust the response handling logic for your data format
- Implement appropriate storage or processing of the retrieved data
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.