Solidity

如何從 chailnlink 或任何其他方法獲取 BAYC NFT 價格?

  • July 13, 2022

我想使用 oracle 獲取 BAYC NFT 價格。我檢查了chainlink,但沒有找到任何解決方案。

要獲得 BAYC NFT 價格,您可以建構和使用任何類型的Chainlink Any API請求。你可以使用像 coingecko 這樣的任何 API 來獲取合約中的價格。

例如,我們可以從這里拉取:

https://api.coingecko.com/api/v3/coins/bayc-vault-nftx

例如,如果您想要以美元為單位的價格,您可以這樣構造它:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

import '@chainlink/contracts/src/v0.8/ChainlinkClient.sol';
import '@chainlink/contracts/src/v0.8/ConfirmedOwner.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 APIConsumer is ChainlinkClient, ConfirmedOwner {
   using Chainlink for Chainlink.Request;

   uint256 public price;
   bytes32 private jobId;
   uint256 private fee;

   event RequestPrice(bytes32 indexed requestId, uint256 price);

   /**
    * @notice Initialize the link token and target oracle
    *
    * Rinkeby Testnet details:
    * Link Token: 0x01BE23585060835E02B77ef475b0Cc51aA1e0709
    * Oracle: 0xf3FBB7f3391F62C8fe53f89B41dFC8159EE9653f (Chainlink DevRel)
    * jobId: ca98366cc7314957b8c012c72f05aeeb
    *
    */
   constructor() ConfirmedOwner(msg.sender) {
       setChainlinkToken(0x01BE23585060835E02B77ef475b0Cc51aA1e0709);
       setChainlinkOracle(0xf3FBB7f3391F62C8fe53f89B41dFC8159EE9653f);
       jobId = 'ca98366cc7314957b8c012c72f05aeeb';
       fee = (1 * LINK_DIVISIBILITY) / 10; // 0,1 * 10**18 (Varies by network and job)
   }

   function requestBaycPrice() public returns (bytes32 requestId) {
       Chainlink.Request memory req = buildChainlinkRequest(jobId, address(this), this.fulfill.selector);

       // Set the URL to perform the GET request on
       req.add('get', 'https://api.coingecko.com/api/v3/coins/bayc-vault-nftx');

       // request.add("path", "market_data.current_price.usd"); // Chainlink nodes prior to 1.0.0 support this format
       req.add('path', 'market_data,current_price,usd'); // Chainlink nodes 1.0.0 and later support this format

       // Multiply the result by 1000000000000000000 to remove decimals
       int256 timesAmount = 10**18;
       req.addInt('times', timesAmount);

       // Sends the request
       return sendChainlinkRequest(req, fee);
   }

   /**
    * Receive the response in the form of uint256
    */
   function fulfill(bytes32 _requestId, uint256 _price) public recordChainlinkFulfillment(_requestId) {
       emit RequestPrice(_requestId, _price);
       price = _price;
   }

   /**
    * Allow withdrawal of Link tokens from the contract
    */
   function withdrawLink() public onlyOwner {
       LinkTokenInterface link = LinkTokenInterface(chainlinkTokenAddress());
       require(link.transfer(msg.sender, link.balanceOf(address(this))), 'Unable to transfer');
   }
}

您必鬚根據您使用的鍊和節點更改 oracle 地址和 jobId。

這將是通過單個節點從單個 API 中提取的範例。為了使這種去中心化,您必須通過許多不同的節點提取許多 API。所以上面的例子展示瞭如何做到這一點的建構塊。

出納員可以幫忙。

他們可以做任何類型的數據。您可以嘗試自己實現它(這是一個範例:https ://github.com/tellor-io/sampleUsingTellor )

或者您可以填寫 github 問題,他們會幫助您: https ://github.com/tellor-io/dataSpecs/issues/25

引用自:https://ethereum.stackexchange.com/questions/131619