我目前两次使用Chainlink的示例HTTP GET代码从CoinGecko API获取Uniswap价格和Uniswap供应(见下文(。我想从API中提取价格和供应量;请求";以及";实现";函数,以便我可以在";实现";作用(我意识到我可以从API中提取市值,但出于开发原因,我想计算一下。(
有没有办法更改下面的代码来做到这一点?Chainlink允许这样做吗?
pragma solidity ^0.6.0;
import "https://raw.githubusercontent.com/smartcontractkit/chainlink/master/evm-contracts/src/v0.6/ChainlinkClient.sol";
contract APIConsumer is ChainlinkClient {
uint256 public uniPrice;
uint256 public uniSupply;
//uint256 public uniMarketCap;
address private oracle;
bytes32 private jobId;
uint256 private fee;
/**
* Network: Kovan
* Chainlink - 0x2f90A6D021db21e1B2A077c5a37B3C7E75D15b7e
* Chainlink - 29fa9aa13bf1468788b7cc4a500a45b8
* Fee: 0.1 LINK
*/
constructor() public {
setPublicChainlinkToken();
oracle = 0x2f90A6D021db21e1B2A077c5a37B3C7E75D15b7e;
jobId = "29fa9aa13bf1468788b7cc4a500a45b8";
fee = 0.1 * 10 ** 18; // 0.1 LINK
}
//dont forget to fund contract with link
function requestUniPrice() public returns (bytes32 requestId)
{
Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfillUniPrice.selector);
request.add("get", "https://api.coingecko.com/api/v3/coins/markets?vs_currency=USD&ids=uniswap%2C%20sushi%2C%20pancakeswap-token&order=market_cap_desc&per_page=100&page=1&sparkline=false");
request.add("path","0.current_price");
return sendChainlinkRequestTo(oracle, request, fee);
}
function requestUniSupply() public returns (bytes32 requestId)
{
Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfillUniSupply.selector);
request.add("get", "https://api.coingecko.com/api/v3/coins/markets?vs_currency=USD&ids=uniswap%2C%20sushi%2C%20pancakeswap-token&order=market_cap_desc&per_page=100&page=1&sparkline=false");
request.add("path","0.circulating_supply");
return sendChainlinkRequestTo(oracle, request, fee);
}
function fulfillUniPrice(bytes32 _requestId, uint256 _uniPrice) public recordChainlinkFulfillment(_requestId)
{
uniPrice = _uniPrice;
}
function fulfillUniSupply(bytes32 _requestId, uint256 _uniSupply) public recordChainlinkFulfillment(_requestId)
{
uniSupply = _uniSupply;
}
function withdrawLink() external {
LinkTokenInterface linkToken = LinkTokenInterface(chainlinkTokenAddress());
require(linkToken.transfer(msg.sender, linkToken.balanceOf(address(this))), "Unable to transfer");
}
没有,您必须发出多个请求。
目前,EthTX核心适配器只能在区块链上写入不大于32字节的单个值。如果需要多个值,则需要通过发出多个请求来返回数据。
参考:Chainlink文档常见问题