Created
November 15, 2019 14:40
-
-
Save thodges-gh/f0d451a2cf24034ee1646642f40ffeee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity 0.4.24; | |
import "https://github.com/smartcontractkit/chainlink/evm/contracts/ChainlinkClient.sol"; | |
contract Betterman is ChainlinkClient { | |
uint256 private ORACLE_PAYMENT = 1 * LINK; | |
uint256 public currentTemp; | |
constructor() public { | |
setPublicChainlinkToken(); | |
} | |
function requestTemperature(address _oracle, string memory _jobId, string memory _appId) public { | |
Chainlink.Request memory req = buildChainlinkRequest(stringToBytes32(_jobId), address(this), this.fulfillTemperature.selector); | |
req.add("get", concat2("https://openweathermap.org/data/2.5/weather?id=2177091&appid=", _appId)); | |
req.add("path", "main.temp"); | |
req.addInt("times", 100); | |
sendChainlinkRequestTo(_oracle, req, ORACLE_PAYMENT); | |
} | |
function fulfillTemperature(bytes32 _requestId, uint256 _temp) public recordChainlinkFulfillment(_requestId) { | |
currentTemp = _temp; | |
} | |
function concat2(string memory _s1, string memory _s2) private pure returns (string) { | |
return string(abi.encodePacked(_s1, _s2)); | |
} | |
function stringToBytes32(string memory source) private pure returns (bytes32 result) { | |
bytes memory tempEmptyStringTest = bytes(source); | |
if (tempEmptyStringTest.length == 0) { | |
return 0x0; | |
} | |
assembly { | |
result := mload(add(source, 32)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment