AIOracle Quick Start Guide for Developers

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.

Prerequisites

Oracle Addresses

Here are the current Open Puter AIOracle contract addresses:

Network Oracle Address
Base 0x8eF04a34aAacf0299B6fe01BB6d19A8f027e84c4

Step 1: Set Up Your Contract

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)
}

Step 2: Implement Core Functionality

Add functions to request AI responses and handle the results:

contract YourContract {
    // ... (rest of the code)

    bytes32 public lastRequestId;
    string public lastResponse;

    event AIResponseReceived(bytes32 indexed requestId, string prompt, string response);

    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;
        emit AIResponseReceived(requestId, prompt, 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();
    }
}

Step 3: Customize for Your Use Case

Modify the contract to fit your specific needs. For example, our Satoshi quote contract uses a fixed prompt:

contract SatoshiQuoteContract {
    // ... (rest of the 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)
}

Step 4: Deploy and Interact

  1. Deploy your contract, providing the address of the Open Puter AIOracle on your target network.
  2. To request an AI response:
    • Call getOracleFee() to check the current fee.
    • Call your request function (e.g., requestSatoshiQuote()) with the exact fee amount.
  3. The Open Puter AIOracle will process your request and call handleAIResponse() in your contract.
  4. Retrieve the response by calling getLastResponse().

Best Practices

  1. Always check the oracle fee before making a request, as it may change.
  2. Implement proper error handling and event emissions for better tracking and debugging. For example, emit an event when receiving an AI response.
  3. Ensure your contract has enough balance to pay for oracle fees if you're automating requests.
  4. Use events to track important state changes and make it easier for off-chain applications to monitor your contract's activity.

Conclusion

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!