Skip to content

Instantly share code, notes, and snippets.

@iisaint
Last active April 13, 2017 06:07
Show Gist options
  • Save iisaint/6aff67bff3a62e1beaa0a5042f86fb74 to your computer and use it in GitHub Desktop.
Save iisaint/6aff67bff3a62e1beaa0a5042f86fb74 to your computer and use it in GitHub Desktop.
Dice.sol for My Oracle
pragma solidity ^0.4.10;
// Oracle interface
contract Oracle {
address public cbAddress;
function query(string _query) returns (bytes32 id);
}
// OracleResolver interface
contract OracleResolver {
function getOracleAddress() constant returns(address);
}
// 給dapp開發者使用的合約
contract UsingMyOracle {
OracleResolver resolver;
Oracle oracle;
modifier myOracleAPI {
if (address(resolver) == 0) {
// 指定OracleResolver的合約地址,要替換成你自己的
resolver = OracleResolver(0x9431dBA1450345B92d84980dc6EAB81741624ba3);
oracle = Oracle(resolver.getOracleAddress());
}
_;
}
modifier onlyFromCbAddress {
if (msg.sender != oracle.cbAddress()) throw;
_;
}
function myOracleQuery(string _query) internal myOracleAPI returns(bytes32 id) {
return oracle.query(_query);
}
function _callback(bytes32 _id, string result) onlyFromCbAddress {
// do nothing, 只是確保Oracle有一個_callback可以使用
}
}
// 要繼承UsingMyOracle
contract Dice is UsingMyOracle {
address owner;
mapping(address => bytes32) myids;
mapping(bytes32 => string) dice_result;
// 輔助的event,沒有也不影響功能
event newMyOracleQuery(string description);
event diceResult(string result);
function Dice() {
owner = msg.sender;
}
// 擲骰子
function dice() {
newMyOracleQuery("MyOracle query was sent, standing by for the answer..");
bytes32 myid = myOracleQuery("0-1000"); //指定範圍
myids[msg.sender] = myid;
}
// override
function _callback(bytes32 _id, string result) onlyFromCbAddress {
dice_result[_id] = result;
diceResult(result);
}
function checkResult() constant returns (string) {
return dice_result[myids[msg.sender]];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment