我正在构建一个与chainlink oracle交互的智能合约,以从api调用中获取外部数据。我在 ethereum kovan 测试网上做到了这一点,它适用于以下 oracle 地址和工作 ID,但它不适用于 Avalanche 测试网
- 网络:高文
- 作业名称:获取> Uint256
- 甲骨文:0xc57B33452b4F7BB189bB5AfaE9cc4aBa1f7a4FD8(Chainlink Devrel节点)
- 作业编号:d5270d1c311941d0b08bead21fea7747
- 费用:0.1 链接
和
- 网络:雪崩测试网
- 作业名称:获取> Uint256
- 甲骨文:0xCC80934EAf22b2C8dBf7A69e8E0D356a7CAc5754(Chainlink Devrel节点)
- 作业 ID:5ca4fa9b2d64462290abfbda84e38cf4
- 费用:0.1 链接
您可以在下面找到我使用 Remix 在 ethereum kovan 测试网和 avalanche fuji 测试网上部署的 Solidity 代码。(在构造函数中,您可以找到 Avalanche fuji 测试网的注释脚本。)
以下是部署的两个合约:
以太坊 Kovan:https ://kovan.etherscan.io/address/0xD20dd5ee7844E27Fa2DD437744f986C121aDDE0f
雪崩富士:https ://testnet.snowtrace.io/address/0xfb0368e7a97b2ea12980c3900e9c52806e68e8a6
我注意到在“事件”选项卡上,在 Kovan 我有两个事件:
chainlinkRequested & chainlinkFulfilled
在富士我只有一个事件......:
只有chainlinkRequested,所以看来oracle没有返回数据来发出事件:chainlinkFulfilled ...
你能帮我完成这个并将api数据取回智能合约吗?否则,您是否有任何文档可以帮助我构建自己的预言机?(我已经有了 cryptozombies.io 文档,所以如果你有,请分享其他文档。)
// SPDX-License-Identifier: MIT
pragma 可靠性 ^0.8.7;
import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol";
/**
* Request testnet LINK and ETH here: https://faucets.chain.link/
* Find information on LINK Token Contracts and get the latest ETH and LINK faucets here: https://docs.chain.link/docs/link-token-contracts/
*/
/**
* THIS IS AN EXAMPLE CONTRACT WHICH USES HARDCODED VALUES FOR CLARITY.
* PLEASE DO NOT USE THIS CODE IN PRODUCTION.
*/
contract APIPlayerScore is ChainlinkClient {
using Chainlink for Chainlink.Request;
uint256 public playerScore;
address private oracle;
bytes32 private jobId;
uint256 private fee;
constructor() {
//Ethereum Kovan
setPublicChainlinkToken();
oracle = 0xc57B33452b4F7BB189bB5AfaE9cc4aBa1f7a4FD8;
jobId = "d5270d1c311941d0b08bead21fea7747";
fee = 0.1 * 10 ** 18; // (Varies by network and job)
//Avalanche Fuji
//setChainlinkToken(0x0b9d5D9136855f6FEc3c0993feE6E9CE8a297846);
//oracle = 0xCC80934EAf22b2C8dBf7A69e8E0D356a7CAc5754;
//jobId = "5ca4fa9b2d64462290abfbda84e38cf4";
//fee = 0.1 * 10 ** 18;
}
/**
* Create a Chainlink request to retrieve API response, find the target
* data, then multiply by 1000000000000000000 (to remove decimal places from data).
*/
function requestPlayerScoreData() public returns (bytes32 requestId)
{
Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfill.selector);
// Set the URL to perform the GET request on
request.add("get", "https://****database.app/data.json");
// Set the path to find the desired data in the API response, where the response format is:
// {"player": {
// "id": "4291820",
// "score": 560
// }
// }
request.add("path", "player.score");
// Sends the request
return sendChainlinkRequestTo(oracle, request, fee);
}
/**
* Receive the response in the form of uint256
*/
function fulfill(bytes32 _requestId, uint256 _score) public recordChainlinkFulfillment(_requestId)
{
playerScore = _score;
}
// function withdrawLink() external {} - Implement a withdraw function to avoid locking your LINK in the contract
}