Contract-Invocation
智能合約呼叫外部 API?
乙太坊智能合約是否可以呼叫外部 API,然後返回值列表?
內森·胡
為了在合約中呼叫外部 API,您必須使用 oraclize 服務。
範常式式碼供您參考….
pragma solidity ^0.4.16; import "./usingOraclize.sol"; //Importing Oraclize contract TestOraclizeCall is usingOraclize { uint public price; event Log(string text); //Constructor function TestOraclizeCall() { OAR = OraclizeAddrResolverI(0x5049063e4a7704ac155e4f1f42a4954bbef5bbde); } function __callback(bytes32 _myid, string _result) { require (msg.sender == oraclize_cbAddress()); Log(_result); price = parseInt(_result, 2); } function update() payable { oraclize_query("URL","json(https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD).USD"); }
}