You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chainlink Functions完成计数器不更新故障排查求助

我编写了这份Chainlink Functions消费者合约用于测试自定义端点,合约功能正常,能获取到预期响应且无错误,但Chainlink Functions仪表盘的完成计数器始终停留在0,没有任何更新。

另外,由于端点返回的数据暂时无法适配character变量,我临时将其硬编码为'philip',这仅为测试用途,正式代码会移除该设置。我原本预期请求成功完成后计数器会增加,甚至尝试用Chainlink官方文档的示例代码搭配对应端点测试,虽然能得到预期结果,但计数器依然保持为0。

合约代码

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

import {FunctionsClient} from "@chainlink/contracts@1.1.1/src/v0.8/functions/v1_0_0/FunctionsClient.sol";
import {ConfirmedOwner} from "@chainlink/contracts@1.1.1/src/v0.8/shared/access/ConfirmedOwner.sol";
import {FunctionsRequest} from "@chainlink/contracts@1.1.1/src/v0.8/functions/v1_0_0/libraries/FunctionsRequest.sol";

/**
 * Request testnet LINK and ETH from official faucets
 * Find information on LINK Token Contracts and get the latest ETH and LINK faucets from official resources
 */

/**
 * @title GettingStartedFunctionsConsumer
 * @notice This is an example contract to show how to make HTTP requests using Chainlink
 * @dev This contract uses hardcoded values and should not be used in production.
 */
contract GettingStartedFunctionsConsumer is FunctionsClient, ConfirmedOwner {
    using FunctionsRequest for FunctionsRequest.Request;

    // State variables to store the last request ID, response, and error
    bytes32 public s_lastRequestId;
    bytes public s_lastResponse;
    bytes public s_lastError;

    // Custom error type
    error UnexpectedRequestID(bytes32 requestId);

    // Event to log responses
    event Response(
        bytes32 indexed requestId,
        string character,
        bytes response,
        bytes err
    );

    // Router address - Hardcoded for Sepolia
    // Check official docs for router address of your supported network
    address router = 0xf9B8fc078197181C841c296C876945aaa425B278;

    // JavaScript source code
    // Fetch character name from the Star Wars API.
    // Documentation available via official Star Wars API resources
     string poiProofSource =
        "const sender = args[0];"
        "const receiver = args[1];"
        "const apiResponse = await Functions.makeHttpRequest({"
        "url:`https://www.profiles.blue/api/getProofOfInteractionData?SenderAddress=${sender}&ReceiverAddress=${receiver}`"
        "});"
        "if(apiResponse.error){"
        "console.error(apiResponse.error);"
        "throw new Error(apiResponse.error);"
        "}"
        "const { data } = apiResponse;"
        "const sender_id = data.sender_id;"
        "const receiver_id = data.receiver_id;"
        "const timestamp = data.timestamp;"
        "const unixTimestamp = Math.floor(new Date(timestamp) / 1000);"
        "const resultString = JSON.stringify({ sender_id, receiver_id, timestamp: unixTimestamp ,sender, receiver});"
        "const encodedResult = Functions.encodeString(resultString);"
        "return encodedResult;";


    string exchangeProofSource = 
        "const sender = args[0];"
        "const receiver = args[1];"
        "const apiResponse = await Functions.makeHttpRequest({"
        "url:`https://www.profiles.blue/api/getInteractionBstData?SenderAddress=${sender}&ReceiverAddress=${receiver}`"
        "});"
        "if(apiResponse.error){"
        "console.error(apiResponse.error);"
        "throw new Error(apiResponse.error);"
        "}"
        "const { data } = apiResponse;"
        "const sender_id = data.sender_id;"
        "const receiver_id = data.receiver_id;"
        "const timestamp = data.timestamp;"
        "const unixTimestamp = Math.floor(new Date(timestamp) / 1000);"
        "const resultString = JSON.stringify({ sender_id, receiver_id, timestamp: unixTimestamp ,sender, receiver});"
        "const encodedResult = Functions.encodeString(resultString);"
        "return encodedResult;";
     

    //Callback gas limit
    uint32 gasLimit = 300000;

    // donID - Hardcoded for Sepolia
    // Check official docs for donID of your supported network
    bytes32 donID =
        0x66756e2d626173652d7365706f6c69612d310000000000000000000000000000;

    // State variable to store the returned character information
    string public character;

    /**
     * @notice Initializes the contract with the Chainlink router address and sets the contract owner
     */
    constructor() FunctionsClient(router) ConfirmedOwner(msg.sender) {}

    /**
     * @notice Sends an HTTP request for character information
     * @param subscriptionId The ID for the Chainlink subscription
     * @param args The arguments to pass to the HTTP request
     * @return requestId The ID of the request
     */
    function sendRequest(
        uint64 subscriptionId,
        string[] calldata args
    ) external onlyOwner returns (bytes32 requestId) {
        FunctionsRequest.Request memory req;
        req.initializeRequestForInlineJavaScript(exchangeProofSource); // Initialize the request with JS code
        if (args.length > 0) req.setArgs(args); // Set the arguments for the request
//77,["3J98t1WpEZ73CNmQviecrnyiWrnqDhUEWl","3J98t1WpEZ73CNmQviecrnyiWrnqDhTEWh"];
        // Send the request and store the request ID
        s_lastRequestId = _sendRequest(
            req.encodeCBOR(),
            subscriptionId,
            gasLimit,
            donID
        );

        return s_lastRequestId;
    }

    /**
     * @notice Callback function for fulfilling a request
     * @param requestId The ID of the request to fulfill
     * @param response The HTTP response data
     * @param err Any errors from the Functions request
     */
    function fulfillRequest(
        bytes32 requestId,
        bytes memory response,
        bytes memory err
    ) internal override {
        if (s_lastRequestId != requestId) {
            revert UnexpectedRequestID(requestId); // Check if request IDs match
        }
        // Update the contract's state variables with the response and any errors
        s_lastResponse = response;
        //hard coded for test purpose
        character = "philip";
        s_lastError = err;

        // Emit an event to log the response
        emit Response(requestId, character, s_lastResponse, s_lastError);
    }
}

内容的提问来源于stack exchange,提问作者william

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 13:29:51