Skip to content

Instantly share code, notes, and snippets.

@PatrickAlphaC
Last active August 19, 2020 04:01
Show Gist options
  • Save PatrickAlphaC/d649da41ca5c53dcebc7f552ffc7779f to your computer and use it in GitHub Desktop.
Save PatrickAlphaC/d649da41ca5c53dcebc7f552ffc7779f to your computer and use it in GitHub Desktop.
Showcasing getting an interesting API with maps and arrays. Deploy it to remix with https://remix.ethereum.org/#version=soljson-v0.6.6+commit.6c089d02.js&optimize=false&gist=d649da41ca5c53dcebc7f552ffc7779f
pragma solidity ^0.6.0;
import "https://github.com/smartcontractkit/chainlink/blob/master/evm-contracts/src/v0.6/ChainlinkClient.sol";
contract APIConsumer is ChainlinkClient {
bool public isWinner;
address private oracle;
bytes32 private jobId;
uint256 private fee;
uint256 public timesCalled;
constructor() public {
timesCalled = 0;
isWinner = false;
setPublicChainlinkToken();
oracle = 0xB36d3709e22F7c708348E225b20b13eA546E6D9c;
jobId = "a880cb0cb8964be5ae8fdcecc50aa53f";
fee = 0.1 * 10 ** 18; // 0.1 LINK
}
function requestIsWinner() public returns (bytes32 requestId)
{
Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfill.selector);
// Set the URL to perform the GET request on
request.add("get", "http://statsapi.mlb.com/api/v1/schedule/games/?sportId=1&teamId=114&season=2018&startDate=2020-08-16&endDate=2020-08-16");
// Set the path to find the desired data in the API response, where the response format is:
// {"USD":243.33}
request.add("path", "dates.0.games.0.teams.away.isWinner");
// Sends the request
return sendChainlinkRequestTo(oracle, request, fee);
}
/**
* Receive the response in the form of bool
*/
function fulfill(bytes32 _requestId, bool _isWinner) public recordChainlinkFulfillment(_requestId)
{
isWinner = _isWinner;
timesCalled = timesCalled + 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment