Skip to content

Instantly share code, notes, and snippets.

Created March 10, 2017 04:03
Show Gist options
  • Save anonymous/f420bdbaeb9c50f69c33a3642414fa29 to your computer and use it in GitHub Desktop.
Save anonymous/f420bdbaeb9c50f69c33a3642414fa29 to your computer and use it in GitHub Desktop.
Created using browser-solidity: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://ethereum.github.io/browser-solidity/#version=soljson-v0.4.9+commit.364da425.js&optimize=undefined&gist=
// Ethereum + Solidity
// This code sample & more @ dev.oraclize.it
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";
contract PriceFeed is usingOraclize {
uint public ETHUSD;
function PriceFeed(){
oraclize_setProof(proofType_TLSNotary | proofStorage_IPFS);
update(0); // first check at contract creation
}
function __callback(bytes32 myid, string result, bytes proof) {
if (msg.sender != oraclize_cbAddress()) throw;
ETHUSD = parseInt(result, 2); // save it as $ cents
// do something with ETHUSD
//update(60); //recursive update disabled
}
function update(uint delay){
oraclize_query(delay, "URL",
"json(https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD).USD");
}
}
// Ethereum + Solidity
// This code sample & more @ dev.oraclize.it
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";
contract PriceFeed is usingOraclize {
uint public ETHUSD;
function PriceFeed(){
oraclize_setProof(proofType_TLSNotary | proofStorage_IPFS);
update(0); // first check at contract creation
}
function __callback(bytes32 myid, string result, bytes proof) {
if (msg.sender != oraclize_cbAddress()) throw;
ETHUSD = parseInt(result, 2); // save it as $ cents
// do something with ETHUSD
update(60); // schedule another check in 60 seconds
}
function update(uint delay){
oraclize_query(delay, "URL",
"json(https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD).USD");
}
}
// Ethereum + Solidity
// This code sample & more @ dev.oraclize.it
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";
contract PriceFeed is usingOraclize {
uint public ETHUSD;
function PriceFeed(){
oraclize_setProof(proofType_TLSNotary | proofStorage_IPFS);
update(0); // first check at contract creation
}
function __callback(bytes32 myid, string result, bytes proof) {
if (msg.sender != oraclize_cbAddress()) throw;
ETHUSD = parseInt(result, 2); // save it as $ cents
// do something with ETHUSD
//update(60); //recursive update disabled
}
function update(uint delay){
// call oraclize and retrieve the latest USD/ETH price from Poloniex APIs
oraclize_query(delay, "URL",
"json(https://poloniex.com/public?command=returnTicker).USDT_ETH.last");
}
}
// Ethereum + Solidity
// This code sample & more @ dev.oraclize.it
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";
contract PriceFeed is usingOraclize {
uint public ETHUSD;
function PriceFeed(){
oraclize_setProof(proofType_TLSNotary | proofStorage_IPFS);
update(0); // first check at contract creation
}
function __callback(bytes32 myid, string result, bytes proof) {
if (msg.sender != oraclize_cbAddress()) throw;
ETHUSD = parseInt(result, 2); // save it as $ cents
// do something with ETHUSD
update(60); // schedule another check in 60 seconds
}
function update(uint delay){
// call oraclize and retrieve the latest USD/ETH price from Poloniex APIs
oraclize_query(delay, "URL",
"json(https://poloniex.com/public?command=returnTicker).USDT_ETH.last");
}
}
// Ethereum + Solidity
// This code sample & more @ dev.oraclize.it
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";
contract SimpleDice is usingOraclize {
mapping (bytes32 => address) bets;
function __callback(bytes32 myid, string result) {
if (msg.sender != oraclize_cbAddress()) throw;
if ((parseInt(result) > 3)&&(bets[myid].send(2)))
log0('win'); // winner AND send didn't fail!
else log0('lose'); // loser OR sending failed
}
function bet() {
// we accept just test bets worth 1 Wei :)
if ((msg.value != 1)||(this.balance < 2)) throw;
rollDice();
}
function rollDice() private {
bytes32 myid = oraclize_query("WolframAlpha",
"random number between 1 and 6");
bets[myid] = msg.sender;
}
}
pragma solidity ^0.4.0;
contract SimpleStorage {
uint storedData;
function set(uint x) {
storedData = x;
}
function get() constant returns (uint) {
return storedData;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment