Bitcoin

比特幣價格預言機

  • December 26, 2021

有沒有可用於比特幣價格的預言機?我想在智能合約程式碼中做一個貨幣轉換數學。

只需在此處添加更新的答案,因為這個問題經常出現在搜尋中。您有 BTC/USD 的三個選項:wbtc/usdc uni pool、將其置於鏈上的 LINK 合約,或允許您請求更新(或自己質押並更新)的 Tellor

以下是 Tellor 拉動 BTC 價格的範例:https ://github.com/tellor-io/sampleUsingTellor

來自Oraclize的 Thomas 在這裡。

將比特幣參考價格納入合約的一種簡單但非常安全的方法是使用現有交易所作為數據源,這要歸功於它們的 API。我想在這裡你想要的是 BTCUSD,如果它是 BTCETH 而不是需要小的程式碼更改,但同樣的推理適用。

通過 Oraclize 在 Solidity 中這樣做是微不足道的,我建議不要僅依賴於單個交換,但為簡單起見,以下範例僅使用一個 (Kraken)。

我認為以下程式碼是不言自明的,但如果您有任何問題,請隨時在此處或通過我們的支持渠道(gitter、電子郵件、..)提問。

/*
  Kraken-based ETH/USD price ticker

  This contract keeps in storage an updated ETH/USD price,
  which is optionally updated every ~60 seconds.
*/

pragma solidity ^0.4.0;
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";

contract KrakenPriceTicker is usingOraclize {

   uint public ETHUSD;

   event newOraclizeQuery(string description);
   event newKrakenPriceTicker(string price);

   function KrakenPriceTicker() {
       // FIXME: enable oraclize_setProof is production
       // oraclize_setProof(proofType_TLSNotary | proofStorage_IPFS);
       update(0);
   }

   function __callback(bytes32 myid, string result, bytes proof) {
       if (msg.sender != oraclize_cbAddress()) throw;
       newKrakenPriceTicker(result);
       ETHUSD = parseInt(result, 2); // save it in storage as $ cents
       // do something with ETHUSD
       // update(60); // FIXME: comment this out to enable recursive price updates
   }

   function update(uint delay) payable {
       if (oraclize_getPrice("URL") > this.balance) {
           newOraclizeQuery("Oraclize query was NOT sent, please add some ETH to cover for the query fee");
       } else {
           newOraclizeQuery("Oraclize query was sent, standing by for the answer..");
           oraclize_query(delay, "URL", "json(https://api.kraken.com/0/public/Ticker?pair=ETHUSD).result.XETHZUSD.c.0");
       }
   }

} 

借助 Oraclize 瀏覽器-solidity 集成,您可以在此處看到這一點,只需點擊連結,然後點擊右側面板上的契約“創建”按鈕!

Oraclize Kraken 價格程式碼範例

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