Consumer Contract

Before engaging in AI-driven decision-making within the Agentic Data Coordination Service (ADCS), users must first deploy a Consumer Contract. This contract serves as a crucial intermediary between the user’s specific requirements and the AI inference capabilities of the system. Functioning similarly to a class in object-oriented programming, the Consumer Contract inherits key functionalities from the Coordinator Contract, enabling it to manage the intricate request and response processes essential for effective inference operations.

Primary Responsibilities

The Consumer Contract has two primary responsibilities:

1. Request Inference: It requests an inference from the Coordinator Contract by providing the necessary input data.

2. Handle Response: It receives and processes the inference response to make decisions or trigger further actions within the application.

Example Use Case: High-Risk Trading

Users interested in high-risk trades, such as options trading, require a decision-making process informed by solid reasoning, data analysis, and critical thinking. To facilitate this, they deploy a Consumer Contract and define the necessary parameters within the schema, which might include:

  • Trade Parameters - The type of trade (e.g., options), risk appetite, targeted assets, and the amount of funds they are willing to invest.

  • Data Sources: Reference data from resources like live market prices, volatility indicators, historical performance data, and economic indicators.

  • Decision Logic: Criteria used by the AI agent to make a decision, such as specific price movements, technical indicators, or market conditions.

Initiating an Inference Request

The Consumer Contract requests an inference from the Coordinator Contract by calling the requestInference function, providing the necessary input data:

  • Defined Schema: Specifies the structure and parameters of the inference request.

  • User’s Account ID: Identifies the user initiating the request.

  • Callback Gas Limit: The maximum amount of gas that can be used for processing the response, helping manage computational resources and costs.

Here is an example of how this function might be implemented:

function requestTradeDecision(bytes calldata schema, uint64 accId, uint32 callbackGasLimit) external 
returns (uint256) {
    uint256 requestId = requestInference(schema, accId, callbackGasLimit);
    emit TradeDecisionRequested(requestId, msg.sender);
    return requestId;
}

In this function:

  • schema: The defined schema outlining the inference request parameters.

  • accId: The user’s account ID for authentication.

  • callbackGasLimit: The gas limit for processing the response.

  • The function emits a TradeDecisionRequested event with the requestId and the caller’s address.

Handling the Inference Response

Once the required number of responses is received from the oracles, the Consumer Contract processes the aggregated insights to determine whether to execute the trade or take another action. This is done by implementing the handleInferenceResponse function, which processes the response data:

// For example:
function handleInferenceResponse(uint256 requestId, bytes calldata responseData) external {
    require(msg.sender == address(coordinator), "Only coordinator can call this function");
    // Process the response data to make a trade decision
    string memory decision = abi.decode(responseData, (string));
    if (keccak256(abi.encodePacked(decision)) == keccak256(abi.encodePacked("trade"))) {
        // Logic to execute the trade
        emit TradeDecisionMade(requestId, "trade");
    } else if (keccak256(abi.encodePacked(decision)) == keccak256(abi.encodePacked("not trade"))) {
        // Logic to not execute the trade
        emit TradeDecisionMade(requestId, "not trade");
    }
}

In this function:

  • It verifies that only the Coordinator Contract can call this function.

  • Decodes the response data to obtain the decision made by the AI agent.

  • Uses keccak256 hashing to compare strings securely.

  • Executes logic based on whether the decision is “trade” or “not trade.”

  • Emits a TradeDecisionMade event with the requestId and the decision.

Scalability and Oracle Integration

The Consumer Contract is designed to be highly scalable, allowing it to interface with multiple oracles via Oracle Routers. Instead of relying on a single source of truth, the contract gathers data from various oracles, increasing the reliability and accuracy of the inferences made. The Consumer Contract collects results from multiple oracles and checks if it has received the required number of fulfilled requests. For instance, in options trading, the contract may require a certain number of successful responses before proceeding.

Once this condition is met, the handleInferenceResponse function is triggered to process the aggregated insights. Based on these insights, the Consumer Contract determines whether to execute the trade or take another action, enhancing decision-making through AI-driven analysis.

By deploying a Consumer Contract within the ADCS framework, users can harness the power of AI and blockchain technology to make informed, data-driven decisions in a decentralized and secure manner.

Last updated