LogoLogo
  • ⛓️INTRODUCTION
  • ⚡ONCHAIN AI vs. CENTRALIZED ORACLES
  • ⚙️Core Technology
    • Smart Contract Infrastructure: The Backbone
    • Off-Chain AI Compute Layer: Powering AI Inference for On-Chain Execution
    • Cryptographic Signing & Data Integrity: Verifiable AI Outputs
    • On-Chain Data Accessibility & Integration: AI-Powered Insights for Smart Contracts
  • ⚔️UTILITY
  • 🤖HOW IT WORKS
  • 👨‍💻DEVELOPER'S HUB
    • Build with Onchain AI
  • ❓WHY CHOOSE ONCHAIN AI?
  • 🛣️Roadmap
    • Phase 1: Foundation and Initial Launch
  • Phase 2: Expansion and Ecosystem Development
  • Phase 3: Optimization and Large-Scale AI Implementation
  • Phase 4: Global Adoption and AI Leadership
  • 💰TOKENOMICS
Powered by GitBook
On this page
  1. DEVELOPER'S HUB

Build with Onchain AI

PreviousDEVELOPER'S HUBNextWHY CHOOSE ONCHAIN AI?

Last updated 24 days ago

Onchain AI Oracle empowers blockchain applications with seamless access to AI-powered services directly via smart contracts. Developers can easily integrate sophisticated AI processing capabilities into their smart contracts by interacting with the Onchain AI Oracle smart contract. The following section outlines how developers can practically build and integrate applications with the Onchain AI service, demonstrating the essential interactions and code patterns.

Prerequisites

  • Browser wallet (e.g., MetaMask)


Step 1: Import Dependencies

Open Remix IDE, create a new Solidity file named yourSmartContract.sol, and paste the following interface definitions:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

interface IERC20 {
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
    function approve(address spender, uint256 amount) external returns (bool);
    function balanceOf(address account) external view returns (uint256);
}

interface IAIOracle {
    function submitPrompt(string calldata prompt, uint8 responseType) external;
    function requestCount() external view returns (uint256);
    function getResponse(uint256 requestId) external view returns (string memory);
    function token() external view returns (IERC20);
    function fees() external view returns (uint256, uint256, uint256);
}

Step 2: Define Your AI Service Contract

Below the interfaces, define your smart contract structure:

contract CustomAIService {
    address public owner;
    IAIOracle public aiOracle;

    event ResultRequested(uint256 indexed requestId, address indexed user, string prompt, uint8 responseType);
    event ResultFetched(uint256 indexed requestId, string response);

    constructor(address aiOracleAddress) {
        owner = msg.sender;
        aiOracle = IAIOracle(aiOracleAddress);
    }
}

Step 3: Implement AI Request Method

Add the method for requesting AI processing:

function requestAIResult(string calldata prompt, uint8 responseType) external {
    require(responseType <= 2, "Invalid response type");

    IERC20 token = aiOracle.token();
    (uint256 textFee, uint256 imageFee, uint256 audioFee) = aiOracle.fees();

    uint256 fee = responseType == 0 ? textFee : responseType == 1 ? imageFee : audioFee;
    require(token.balanceOf(msg.sender) >= fee, "Insufficient balance");
    require(token.transferFrom(msg.sender, address(this), fee), "Token transfer failed");

    token.approve(address(aiOracle), fee);
    aiOracle.submitPrompt(prompt, responseType);

    uint256 requestId = aiOracle.requestCount();
    emit ResultRequested(requestId, msg.sender, prompt, responseType);
}

Step 4: Implement AI Response Fetching

Now, add the method to fetch AI-generated responses:

function fetchAIResponse(uint256 requestId) external view returns (string memory response) {
    return aiOracle.getResponse(requestId);
}

Step 5: Deploy Contract on Remix

  • Compile your contract:

    • Select Solidity compiler version 0.8.19.

    • Click Compile.

  • Deploy your contract:

    • Choose your wallet network in MetaMask (e.g., Ethereum testnet).

    • In Remix's deployment tab, enter:

      • aiOracleAddress: Address of your deployed Onchain AI Oracle contract.

    • Click Deploy, and confirm the transaction in MetaMask.


Step 6: Interacting with the Contract

Interact using Remix IDE after deployment:

  1. Request AI result:

    • Enter the prompt text.

    • Choose responseType:

      • 0 for Text

      • 1 for Image

      • 2 for Audio

    • Click requestAIResult and confirm transaction.

  2. Fetch AI-generated response:

    • Get the requestId from the ResultRequested event emitted.

    • Enter the requestId into fetchAIResponse(requestId) to retrieve your AI-generated content.


Conclusion

Congratulations! You've successfully integrated your smart contract with the Onchain AI Oracle. By following these straightforward steps, you've set up a robust foundation that allows you to effortlessly leverage powerful AI capabilities—such as generating text, images, or audio—directly within your smart contracts. Happy building!

Remix IDE ()

👨‍💻
https://remix.ethereum.org