繁体   English   中英

以太坊 Chainlink HTTP 无法 ping 我的 HTTP 端点

[英]Ethereum Chainlink HTTP Get not pinging my HTTP endpoint

我试图让我的以太坊智能合约使用 Chainlink 连接到外部 HTTP 端点。 按照 Chainlink 的文档( https://docs.chain.link/docs/advanced-tutorial/ ),我将此合约部署到了 Rinkeby 测试网上。

pragma solidity ^0.8.7;

import "github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/ChainlinkClient.sol";

// MyContract inherits the ChainlinkClient contract to gain the
// functionality of creating Chainlink requests


contract getHTTP is ChainlinkClient {
  using Chainlink for Chainlink.Request;

  bytes32 private thisDoesNotWork;
  address private owner;
  address private ORACLE_ADDRESS = 0x718Cc73722a2621De5F2f0Cb47A5180875f62D60;
  bytes32 private JOBID = stringToBytes32("86b489ec4d84439c96181a8df7b22223");
  string private url = "<myHTTPAddressAsString>"; 

// This endpoint URL is hard coded in my contract, and stored as a string (as in the example code). 
// I control it and can have it reply with whatever I want, which might be an issue, returning data in a format that the oracle rejects

  uint256 constant private ORACLE_PAYMENT = 100000000000000000;

  constructor() public {
    // Set the address for the LINK token for the network
    setPublicChainlinkToken();
    owner = msg.sender;
  }

  function requestBytes() 
    public
    onlyOwner
  {
    Chainlink.Request memory req = buildChainlinkRequest(JOBID, address(this), this.fulfill.selector);
    req.add("get", url);
    sendChainlinkRequestTo(ORACLE_ADDRESS, req, ORACLE_PAYMENT);
  }

  function fulfill(bytes32 _requestId, bytes32 recVal)
    public
    recordChainlinkFulfillment(_requestId)
  {
    thisDoesNotWork = recVal;
  }
  function cancelRequest(
    bytes32 _requestId,
    uint256 _payment,
    bytes4 _callbackFunctionId,
    uint256 _expiration
  )
    public
    onlyOwner
  {
    cancelChainlinkRequest(_requestId, _payment, _callbackFunctionId, _expiration);
  }

  
  // withdrawLink allows the owner to withdraw any extra LINK on the contract
  function withdrawLink()
    public
    onlyOwner
  {
    LinkTokenInterface link = LinkTokenInterface(chainlinkTokenAddress());
    require(link.transfer(msg.sender, link.balanceOf(address(this))), "Unable to transfer");
  }
  
  modifier onlyOwner() {
    require(msg.sender == owner);
    _;
  }
  
   // A helper funciton to make the string a bytes32
  function stringToBytes32(string memory source) private pure returns (bytes32 result) {
    bytes memory tempEmptyStringTest = bytes(source);
    if (tempEmptyStringTest.length == 0) {
      return 0x0;
    }
    assembly { // solhint-disable-line no-inline-assembly
      result := mload(add(source, 32))
    }
  }
}

我在 Chainlink 市场( https://market.link/jobs/529c7194-c665-4b30-8d25-5321ea49d9cc )上找到了一个节点,该节点目前在 rinkeby 上活跃(根据 Etherscan,它在过去 3 天内一直活跃,大概是还在工作)。

我使用 LINK 部署合同并为合同提供资金。 我通过 remix 调用 requestBytes() function,一切都按预期工作。 Metamask 支付了 gas,从我的合约中删除了 LINK,我得到了一笔交易 hash,并且没有错误。

但是,我的端点从未记录请求尝试,oracle 从未在其 Etherscan 页面上列出事务,并且我的数据不存在。

我曾尝试使用来自 Chainlink 市场的其他工作,并获得类似的结果。

我还尝试使用其他 HTTP 端点,例如 Chainlink 示例中的端点,具有类似的结果,但我怀疑这是问题所在,因为似乎 HTTP 请求甚至从未被调用(正如我的 Z2986C9EA246FF978A 事实所引用的那样端点不记录请求)

没有错误消息,并且是 Web3 开发的新手,我不确定从哪里开始调试。 我在 Github: https://github.com/smartcontractkit/documentation/issues/513上找到了这条评论,并在这里没有运气地实施了这个建议。

我还发现了这个: Chainlink - 工作没有完成,但这也没有帮助。

我目前对错误可能在哪里的考虑:

  1. 预言机被列入白名单并直接拒绝我的请求。 考虑过创建自己的节点,但在此阶段尽可能避免。

  2. 我在如何格式化合同中的请求时遇到类型错误,例如我在上面找到并引用的 GitHub 交换中的示例。

编辑:如果有人有任何建议,我也愿意接受 Chainlink 之外的其他选项,以将我的合约连接到 HTTP GET 端点。 谢谢!

我最近一直在做类似的事情,建议你尝试使用 kovan 网络和 chainlink 那里的 oracle。 更具体地说,我认为确认您可以使用 api、oracle 和您正在关注的页面上的示例中列出的 jobid 使其正常工作是一个好主意……这里:

https://docs.chain.link/docs/advanced-tutorial/#contract-example

一旦您使该示例正常工作,您就可以对其进行修改以供您使用。 该教程中的 jobid 用于返回(相乘的)uint256 ... 对于您的 API,我认为这不是您想要的,因为您想要 bytes32返回 bytes32 的 jobid 将是: 7401f318127148a894c00c292e486ffd 如下所示:

https://docs.chain.link/docs/decentralized-oracles-ethereum-mainnet/

可能是您的问题的另一件事是您的 api。 你说你控制它返回的内容......我认为它可能必须返回字节格式的响应,就像帕特里克在他的回复(以及他对他的回复的评论)中所说的那样:

使用 Chainlink 大响应示例从任何 API 获取字符串

希望这会有所帮助。 如果您无法让 chainlink 文档中的示例正常工作,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM