Solidity

通過呼叫另一個合約並從中獲取結果來設置價格

  • June 19, 2019

我想在每次部署合約時通過在建構子中呼叫ETHUSD並從中獲取結果來設置Token的價格。

pragma solidity ^0.5.0;

import "./ETHUSD.sol";

contract Token {

   uint256 public price;

   constructor() public {
       price =
   }

我應該在“ price = ”之後添加什麼****Token才能使其工作?

pragma solidity ^0.5.0;

   import "./oraclizeAPI.sol";

   contract ETHUSD is usingOraclize {

       string public priceETHUSD;

       event LogNewCoinBaseOraclizeQuery(string description);
       event LogNewCoinBaseTicker(string price);

       constructor() public {
           oraclize_setProof(proofType_Android | proofStorage_IPFS);
           update(); // Update price on contract creation...
       }

       function __callback(bytes32 _myid, string memory _result, bytes memory _proof) public {
           require(msg.sender == oraclize_cbAddress());
           update(); // Recursively update the price stored in the contract...
           priceETHUSD = _result;
           emit LogNewCoinBaseTicker(priceETHUSD);
       }

       function update() public payable {
           if (oraclize_getPrice("URL") > address(this).balance) {
               emit LogNewCoinBaseOraclizeQuery("Oraclize query was NOT sent, please add some ETH to cover for the query fee!");
           } else {
               emit LogNewCoinBaseOraclizeQuery("Oraclize query was sent, standing by for the answer...");
               oraclize_query(5, "URL", "json(https://api.pro.coinbase.com/products/ETH-USD/ticker).price");
           }
       }
   }

高於ETHUSD的合約只是範例,它從 API 獲取價格。

price = ETHUSD(0x123abc...).priceETHUSD();

替換0x123abc...為您要使用的已部署 ETHUSD 合約的實際地址。

@向上

這是可行的,它從“ 0x123abc… ”地址合約中獲得了具體價格。

但是,每次呼叫 Token 合約時,如何才能自動更新“ 0x123abc… ”地址欄位呢?

我的意思是:

Token => call ETHUSD => ETHUSD 被部署並生成新地址**=>** Token

price = ETHUSD(新地址).priceETHUSD();

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