Welcome to Open Puter's AIOracle service! This guide will help you quickly integrate our AI-powered oracle into your Solidity smart contracts. We'll use an example contract that requests Bitcoin whitepaper quotes in the style of Satoshi Nakamoto.
Here are the current Open Puter AIOracle contract addresses:
| Network | Oracle Address |
|---|---|
| Base | 0x8eF04a34aAacf0299B6fe01BB6d19A8f027e84c4 |
First, create a new Solidity contract that will interact with the Open Puter AIOracle. Here's a basic structure:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface IAIOracle {
function requestAIResponse(string memory prompt) external payable returns (bytes32);
function getAIResponse(bytes32 requestId) external view returns (string memory);
function fee() external view returns (uint256);
}
contract YourContract {
IAIOracle public aiOracle;
constructor(address _aiOracleAddress) {
aiOracle = IAIOracle(_aiOracleAddress);
}
// ... (rest of the contract)
}
Add functions to request AI responses and handle the results:
contract YourContract {
// ... (previous code)
bytes32 public lastRequestId;
string public lastResponse;
function requestAIResponse(string memory prompt) external payable {
uint256 oracleFee = aiOracle.fee();
require(msg.value == oracleFee, "Incorrect fee. Check getOracleFee() for the exact amount.");
lastRequestId = aiOracle.requestAIResponse{value: oracleFee}(prompt);
}
function handleAIResponse(bytes32 requestId, string memory prompt, string memory response) external {
require(msg.sender == address(aiOracle), "Only Open Puter AIOracle can call this function");
lastResponse = response;
// Add your custom logic here to handle the response
}
function getLastResponse() external view returns (string memory) {
return lastResponse;
}
function getOracleFee() external view returns (uint256) {
return aiOracle.fee();
}
}
Modify the contract to fit your specific needs. For example, our Satoshi quote contract uses a fixed prompt:
contract SatoshiQuoteContract {
// ... (previous code)
string public constant FIXED_PROMPT = "As Satoshi Nakamoto, quote a sentence from the Bitcoin whitepaper.";
function requestSatoshiQuote() external payable {
uint256 oracleFee = aiOracle.fee();
require(msg.value == oracleFee, "Incorrect fee. Check getOracleFee() for the exact amount.");
lastRequestId = aiOracle.requestAIResponse{value: oracleFee}(FIXED_PROMPT);
}
// ... (rest of the contract)
}
getOracleFee() to check the current fee.requestSatoshiQuote()) with the exact fee amount.handleAIResponse() in your contract.getLastResponse().You're now ready to leverage the power of AI in your smart contracts with Open Puter's AIOracle! Remember to thoroughly test your integration and handle edge cases appropriately. For more advanced usage and detailed documentation, please visit our website at openputer.com.
Happy coding with Open Puter AIOracle!