Chainlink 作业不返回值

Chainlink job not returning value

我有一个预言机和 JobID,我想提交给一个预言机以获取 ETH 价格数据。我已经资助了这个节点,并且正在关注文档。但是,每次我请求价格时,我的 BTC 值都不会更新。合同似乎是由 LINK 资助的,我没有收到 gas 错误,但由于某种原因,这个数字不会改变。怎么回事?

solidity
pragma solidity ^0.6.0;
import "github.com/smartcontractkit/chainlink/evm-contracts/src/v0.6/ChainlinkClient.sol";
contract testingData is ChainlinkClient {
  address public owner;
  uint256 public btc;
  address ORACLE =  0xB36d3709e22F7c708348E225b20b13eA546E6D9c;
  bytes32 constant JOB = "f9528decb5c64044b6b4de54ca7ea63e";
  uint256 constant private ORACLE_PAYMENT = 1 * LINK;
  constructor() public {
    setPublicChainlinkToken();
    owner = msg.sender;
  }
  function getBTCPrice() 
    public
    onlyOwner
  {
    Chainlink.Request memory req = buildChainlinkRequest(JOB, address(this), this.fulfill.selector);
    req.add("get", "https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=BTC&to_currency=USD&apikey=xxxx");
    string[] memory copyPath = new string[](2);
    copyPath[0] = "Realtime Currency Exchange Rate";
    copyPath[1] = "5. Exchange Rate";
    sendChainlinkRequestTo(ORACLE, req, ORACLE_PAYMENT);
  }
  function fulfill(bytes32 _requestId, uint256 _price)
    public
    recordChainlinkFulfillment(_requestId)
  {
    btc = _price;
  }
  modifier onlyOwner() {
    require(msg.sender == owner);
    _;
  }
}

对于这个特定问题,请执行:

您需要为作业添加乘法适配器。在您的 getBTCPrice() 中添加一行:

run.addInt("times", 100000000);

下面有一个完整的示例代码,但是这里有一些关于可能发生的事情的提示。

  1. 节点地址不是 funded with ETH - 您必须联系节点运营商。

Fund the Ethereum address that your Chainlink node uses. You can find the address in the node Operator GUI under the Keys tab. The address of the node is the Regular type. You can obtain test ETH from several faucets.

  1. jobid 或 oracle 地址错误 - 请仔细检查。

  2. 节点当前宕机-询问节点运营商。

  3. 您已列入黑名单,或未列入白名单。联系节点运营商。


在 solidity 中,小数不起作用,所以每当你从 oracle 得到一个带小数的数字时,你需要添加一个 multiply adapter 以便它可以理解。

完整代码如下:

solidity
pragma solidity ^0.6.0;
import "github.com/smartcontractkit/chainlink/evm-contracts/src/v0.6/ChainlinkClient.sol";
contract testingData is ChainlinkClient {
  address public owner;
  uint256 public btc;
  address ORACLE =  0xB36d3709e22F7c708348E225b20b13eA546E6D9c;
  bytes32 constant JOB = "f9528decb5c64044b6b4de54ca7ea63e";
  uint256 constant private ORACLE_PAYMENT = 1 * LINK;
  constructor() public {
    setPublicChainlinkToken();
    owner = msg.sender;
  }
  function getBTCPrice() 
    public
    onlyOwner
  {
    Chainlink.Request memory req = buildChainlinkRequest(JOB, address(this), this.fulfill.selector);
    req.add("get", "https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=BTC&to_currency=USD&apikey=xxxx");
    string[] memory copyPath = new string[](2);
    copyPath[0] = "Realtime Currency Exchange Rate";
    copyPath[1] = "5. Exchange Rate";
    sendChainlinkRequestTo(ORACLE, req, ORACLE_PAYMENT);
  }
  function fulfill(bytes32 _requestId, uint256 _price)
    public
    recordChainlinkFulfillment(_requestId)
  {
    btc = _price;
  }
  modifier onlyOwner() {
    require(msg.sender == owner);
    _;
  }
}