簡體   English   中英

Solidity 中的區塊鏈出站 oracle 代碼示例

[英]Blockchain outbound oracle code sample in solidity

作為以太坊區塊鏈的新手,在學習 chainlink/oracles 時,發現入站 oracles 的例子很少。 例如從價格源或通過調用 API 獲取比特幣或柴油價格等的價格。即使在 Inte.net 上搜索了很長時間后,我也找不到 oracle 的任何示例代碼。

希望獲得任何指導以獲取一些示例代碼來練習出站 oracle。

我的理解是您正在尋找示例代碼以在 Solidity 中創建出站 oracle,這是一個示例:

contract Chainlink is usingOraclize {
    string public EURUSD;
    function updatePrice() public payable {
        if (oraclizegetPrice("URL") > this.balance) { 
            //Handle out of funds error 
        } else {
            oraclizequery("URL", "json(http://api.fixer.io/latest?symbols=USD).rates.USD");
        }
    }
    function _callback(bytes32 myid, string result) public {
        require(msg.sender == oraclizecbAddress());
        EURUSD = result;
    }
}

如果你想在 solidity 代碼中滿足某些條件並通過出站 oracle 將此信息發送到另一個智能合約或任何其他應用程序,你可以通過使用 Solidity 語言的事件系統來實現,這個事件系統將幫助你發送數據到外部服務。

這是一個例子:

  contract DummyContract {
    event LogSent(bytes32 data);
    function sendData(address otherContractAddress, bytes32 data) public {
        // execute functions when certain conditions are met
        if (someCondition) {
            someFunction();
        }

    emit LogSent(data);
    // call other contract
    otherContractAddress.call(data);

    // call outbound oracle
    Oracle.sendData(data);
}


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM