Ethereum 如何使用oracle在智能合约中获取不同的硬币价格

Ethereum 如何使用oracle在智能合约中获取不同的硬币价格,ethereum,solidity,smartcontracts,truffle,Ethereum,Solidity,Smartcontracts,Truffle,我试图在智能合约中使用可证明的oracle获取不同加密货币的价格,但我在同时获取不同的硬币奖励时出错,如果您有任何智能合约的示例,请分享 如何使用oracle在智能合约中获取不同的硬币价格 pragma solidity ^0.4.25; import "github.com/provable-things/ethereum-api/provableAPI_0.4.25.sol"; contract DateOracle is usingProvable { b

我试图在智能合约中使用可证明的oracle获取不同加密货币的价格,但我在同时获取不同的硬币奖励时出错,如果您有任何智能合约的示例,请分享

如何使用oracle在智能合约中获取不同的硬币价格

pragma solidity ^0.4.25;
import "github.com/provable-things/ethereum-api/provableAPI_0.4.25.sol";


contract DateOracle is usingProvable {

    bytes32 coin_pointer; // variable to differentiate different callbacks
    bytes32 temp_ID;
    address public owner;
    bytes32 public BTC=bytes32("BTC"); //32-bytes equivalent of BTC
    bytes32 public ETH=bytes32("ETH");
    bytes32 public USDT=bytes32("USD"); 
    bytes32 public USDC=bytes32("USD");
    bytes32 public TUSD=bytes32("TUSD");
    bytes32 public BUSD=bytes32("USD");
    bytes32 public BCH=bytes32("BCH");
    bytes32 public XTZ=bytes32("XTZ");
    bytes32 public COMP=bytes32("COMP");

    uint constant CUSTOM_GASLIMIT = 150000;



    mapping (bytes32 => bytes32) oraclizeIndex; // mapping oraclize IDs with coins

    mapping(bytes32=>bool) validIds;


    // tracking events
    event newOraclizeQuery(string description);
    event newPriceTicker(uint price);
    event LogConstructorInitiated(string nextStep);
    event LogPriceUpdated(string price);
    
        modifier onlyOwner {
        require(owner == msg.sender);
        _;
    }

        function changeOraclizeGasPrice(uint _newGasPrice) external onlyOwner {
        provable_setCustomGasPrice(_newGasPrice);
    }
    


    // constructor
    constructor()public payable {
        provable_setProof(proofType_TLSNotary | proofStorage_IPFS);
        emit LogConstructorInitiated("Constructor was initiated. Call 'updatePrice()' to send the Provable Query.");
        owner = msg.sender;
        provable_setCustomGasPrice(1000000000 wei);
    }



    //oraclize callback method
    function __callback(bytes32 myid, bytes32 result) public {
        if (!validIds[myid]) revert();
        if (msg.sender != provable_cbAddress()) revert();
        coin_pointer = oraclizeIndex[myid];
        delete validIds[myid];
        ETH = result;
        BTC = result;
        BCH = result;
        TUSD = result;
        BUSD = result;
        USDT = result;
        USDC = result;
        COMP = result;
        XTZ = result;

        updatePrice();
    }


    // method to place the oraclize queries
    function updatePrice() onlyOwner public payable returns(bool) {
        if (provable_getPrice("URL") > (owner.balance)) {
            emit newOraclizeQuery("Oraclize query was NOT sent, please add some ETH to cover for the query fee");
        } else {
            emit newOraclizeQuery("Oraclize query was sent, standing by for the answer..");
            
            temp_ID = provable_query(60, "URL", "json(https://api.pro.coinbase.com/products/ETH-USD/ticker).price", CUSTOM_GASLIMIT);
            oraclizeIndex[temp_ID] = ETH;

            temp_ID = provable_query(360, "URL", "json(https://api.pro.coinbase.com/products/BTC-USD/ticker).price",CUSTOM_GASLIMIT);
            oraclizeIndex[temp_ID] = BTC;

            temp_ID = provable_query(360, "URL", "json(https://api.pro.coinbase.com/products/USD/ticker).price",CUSTOM_GASLIMIT);
            oraclizeIndex[temp_ID] = USDT;
            
            temp_ID = provable_query(360, "URL", "json(https://api.pro.coinbase.com/products/USD/ticker).price",CUSTOM_GASLIMIT);
            oraclizeIndex[temp_ID] = USDC;
            
            temp_ID = provable_query(360, "URL", "json(https://api.pro.coinbase.com/products/TUSD-USD/ticker).price",CUSTOM_GASLIMIT);
            oraclizeIndex[temp_ID] = TUSD;
            
            temp_ID = provable_query(360, "URL", "json(https://api.pro.coinbase.com/products/USD/ticker).price",CUSTOM_GASLIMIT);
            oraclizeIndex[temp_ID] = BUSD;
            
            temp_ID = provable_query(360, "URL", "json(https://api.pro.coinbase.com/products/BCH-USD/ticker).price",CUSTOM_GASLIMIT);
            oraclizeIndex[temp_ID] = BCH;
            
            temp_ID = provable_query(360, "URL", "json(https://api.pro.coinbase.com/products/XTZ-USD/ticker).price",CUSTOM_GASLIMIT);
            oraclizeIndex[temp_ID] = XTZ;
            
            temp_ID = provable_query(360, "URL", "json(https://api.pro.coinbase.com/products/COMP-USD/ticker).price",CUSTOM_GASLIMIT);
            oraclizeIndex[temp_ID] = COMP;
            

    
            validIds[temp_ID] = true;

        }
        return true;
    }

 


}'
你会用的

  • 根据网络和价格对选择一个
  • 将地址弹出到构造函数中(参见下面的代码)
  • 部署智能合约,点击查看功能获取价格
  • 你可以试试这个演示。这将得到ETH价格或任何有效价格对的分散值

    /**此示例代码旨在使用Remix快速部署示例合同。
    *如果您从未使用过混音,请尝试我们的示例演练:https://docs.chain.link/docs/example-walkthrough
    *您将需要testneteth和LINK。
    *-科万ETH水龙头:https://faucet.kovan.network/
    *-科万连接水龙头:https://kovan.chain.link/
    */
    pragma-solidity^0.6.7;
    进口”https://github.com/smartcontractkit/chainlink/blob/master/evm-contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";
    合同价格3{
    AggregatorV3接口内部价格源;
    /**
    *网络:科万
    *聚合器:ETH/美元
    *地址:0x9326BFA02AD2366B30BACB15260AF641031331
    */
    构造函数()公共{
    priceFeed=AggregatorV3接口(0x9326BFA02AD2366B30BACB15260AF641031331);
    }
    /**
    *返回最新价格
    */
    函数getLatestPrice()公共视图返回(int){
    (
    uint80圆形ID,
    国际价格,
    开始时,
    uint时间戳,
    uint80回答
    )=priceFeed.latestRoundData();
    //如果回合尚未完成,则时间戳为0
    要求(时间戳>0,“轮未完成”);
    退货价格;
    }
    }
    

    注意,我是Chainlink DevRel,正在研究集成Chainlink,想知道对于赞助商/Eth数据源,“如果回合尚未完成,时间戳为0”的预期频率是多少?我们显然不想看到太多的回复…我想我从来没有见过这种情况发生。谢谢你的回复。如何解析价格?它返回一个整数,但实际值是一个浮点数。小数不稳定,所以您只需将其乘以小数数即可。参见此处以供参考;