我有一个Oracle和JobID,我想提交给Oracle以获取ETH价格数据。我已经为节点提供了资金,并且正在关注文档。但是,每次我要求价格时,我的BTC值都不会更新。合同似乎是由LINK资助的,但我并没有收到错误通知,但由于某些原因,该数字不会改变。发生了什么事?
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);
_;
}
}
答案 0 :(得分:3)
您需要在工作中添加一个乘法适配器。在您的getBTCPrice()
中,添加一行:
run.addInt("times", 100000000);
从本质上讲,小数不起作用,因此,每当从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);
_;
}
}