Skip to content

Instantly share code, notes, and snippets.

@nepalbitcoin
Created January 9, 2018 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nepalbitcoin/602148b55e693811f5d84450bd415cf3 to your computer and use it in GitHub Desktop.
Save nepalbitcoin/602148b55e693811f5d84450bd415cf3 to your computer and use it in GitHub Desktop.
Solidity Temperature module for Toricoin deployed on Rinkeby testnet https://rinkeby.etherscan.io/address/0x1e80cd7450b45b1a7f192a0d756904ba4879d774#code
[{"constant":false,"inputs":[],"name":"devKillSwitch","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"ID","type":"bytes32"},{"name":"result","type":"string"}],"name":"__callback","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"oraclize","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"average7Calls","outputs":[{"name":"_average","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"DATASOURCE","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"QUERY","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"counter","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"nextCall","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"updateTemperature","outputs":[{"name":"","type":"bool"}],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[],"name":"network","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"accessCheck","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"callbackID","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"CALLGAP","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"dev","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"devWithdraw","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"temperature","outputs":[{"name":"time","type":"uint256"},{"name":"temp","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"OAR","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_newContract","type":"address"},{"name":"_status","type":"bool"}],"name":"accessManager","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"payable":true,"stateMutability":"payable","type":"fallback"},{"anonymous":false,"inputs":[{"indexed":false,"name":"_time","type":"uint256"},{"indexed":false,"name":"_temp","type":"uint256"}],"name":"TemperatureUpdate","type":"event"}]
pragma solidity ^0.4.18;
/**
The MIT License (MIT)
Copyright (c) 2015-2016 Oraclize SRL (https:/oraclize.it/)
Copyright (c) 2016 Oraclize LTD (https:/oraclize.it/)
Copyright (c) 2016 Smart Contract Solutions, Inc. (https://openzeppelin.org/)
Copyright (c) 2018 Nepal Blockchain R&D. (https://github.com/nepal-blockchain)
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**/
library SafeMath {
// https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/contracts/math/SafeMath.sol
function mul(uint256 a, uint256 b) internal pure returns(uint256) {
if (a == 0) {
return 0;
}
uint256 c = a * b;
assert(c / a == b);
return c;
}
function div(uint256 a, uint256 b) internal pure returns(uint256) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
function sub(uint256 a, uint256 b) internal pure returns(uint256) {
assert(b <= a);
return a - b;
}
function add(uint256 a, uint256 b) internal pure returns(uint256) {
uint256 c = a + b;
assert(c >= a);
return c;
}
}
contract OraclizeI {
address public cbAddress;
function query(uint _timestamp, string _datasource, string _arg) public payable returns(bytes32 _id);
function getPrice(string _datasource) public returns(uint _dsprice);
}
contract OARI {
function getAddress() public constant returns(address _addr);
}
contract UsingOraclize {
/// https://github.com/oraclize/ethereum-api/blob/master/oraclizeAPI_0.4.sol
using SafeMath for uint256;
string public network = "rinkeby";
OraclizeI public oraclize;
OARI public OAR = OARI(0x146500cfd35B22E4A392Fe0aDc06De1a1368Ed48);
/// Oraclize Address Resolver (OAR)
// Oraclize browser OAR >> 0x51efaF4c8B3C9AfBD5aB9F4bbC82784Ab6ef8fAA
// Rinkeby testnet OAR >> 0x146500cfd35B22E4A392Fe0aDc06De1a1368Ed48
// Ethereum mainnet >> 0x1d3B2638a7cC9f2CB3D298A3DA7a90B67E5506ed
modifier oraclizeAPI {
if (address(oraclize) != OAR.getAddress()) {
oraclize = OraclizeI(OAR.getAddress());
}
_;
}
function dataPrice(string datasource) internal returns (uint256 price) {
price = oraclize.getPrice(datasource);
}
function oraclizeQuery(string _datasource, string _arg, uint256 _value) oraclizeAPI internal returns(bytes32 id) {
uint price = dataPrice(_datasource);
assert(price <= _value);
return oraclize.query.value(price)(0, _datasource, _arg);
}
function callbackAddress() oraclizeAPI internal returns(address) {
return oraclize.cbAddress();
}
// parseInt
function parseInt(string _a) internal pure returns(uint) {
return parseInt(_a, 0);
}
// parseInt(parseFloat*10^_b)
function parseInt(string _a, uint _b) internal pure returns(uint) {
bytes memory bresult = bytes(_a);
uint mint = 0;
bool decimals = false;
for (uint i = 0; i < bresult.length; i++) {
if ((bresult[i] >= 48) && (bresult[i] <= 57)) {
if (decimals) {
if (_b == 0)
break;
else
_b--;
}
mint *= 10;
mint += uint(bresult[i]) - 48;
} else if (bresult[i] == 46)
decimals = true;
}
if (_b > 0)
mint *= 10 ** _b;
return mint;
}
}
contract NepalTemperatureI {
address public dev;
struct TempDB {
uint256 time;
uint256 temp;
}
mapping(uint256 => TempDB) public temperature; // data[counter] = (time, temperature)
mapping(address => bool) public accessCheck; // write access check
function updateTemperature() payable public returns(bool);
function average7Calls() public view returns(uint256 _average);
}
contract UnderConstruction is NepalTemperatureI {
function UnderConstruction() {
dev = msg.sender;
}
function devWithdraw() public returns(bool) {
require(msg.sender == dev && this.balance > 1e12);
msg.sender.transfer(this.balance);
return true;
}
function devKillSwitch() public {
require(msg.sender == dev);
selfdestruct(msg.sender);
}
function accessManager(address _newContract, bool _status) public {
require (msg.sender == dev && accessCheck[_newContract] != _status);
accessCheck[_newContract] = _status;
}
function () public payable {
revert();
}
}
contract NepalTemperature is UnderConstruction, UsingOraclize {
using SafeMath for uint256;
uint256 public nextCall; // time for next call
uint256 public counter; // data counter
bytes32 public callbackID; // callback Id from oraclize
uint256 public constant CALLGAP = 6 hours; // min gap between each call
string public constant DATASOURCE = "WolframAlpha"; // data source
string public constant QUERY = "Average temperature in Nepal"; // data
event TemperatureUpdate(uint256 _time,uint256 _temp);
function NepalTemperature() oraclizeAPI {
//init 7 days of data
for (uint i = 0; i <= 7; i++) {
temperature[i] = TempDB(now, uint256(9).add(i));
}
counter = 7;
accessCheck[dev] = true;
}
function average7Calls() public view returns(uint256 _average) {
uint256 end = counter.sub(1); ///gap between oraclize calls
for (uint256 i = counter.sub(8); i < end; i++) {
_average = _average.add(temperature[i].temp);
}
return _average.div(7);
}
function __callback(bytes32 ID, string result) public {
require(msg.sender == callbackAddress() && ID == callbackID);
uint256 _temp = uint256(parseInt(result));
TempDB memory newData;
newData.time = now;
newData.temp = _temp;
temperature[counter++] = newData;
TemperatureUpdate(newData.time, newData.temp);
}
function updateTemperature() payable public returns(bool) {
require(accessCheck[msg.sender] && nextCall < now && msg.value > 1e10);
nextCall = now.add(CALLGAP);
callbackID = oraclizeQuery(DATASOURCE, QUERY, msg.value);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment