-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathAPIConsumer.sol
90 lines (78 loc) · 2.46 KB
/
APIConsumer.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol";
contract APIConsumer is ChainlinkClient {
using Chainlink for Chainlink.Request;
uint256 public volume;
address public oracle;
bytes32 public jobId;
uint256 public fee;
event DataFullfilled(uint256 volume);
constructor(
address _oracle,
bytes32 _jobId,
uint256 _fee,
address _link
) {
if (_link == address(0)) {
setPublicChainlinkToken();
} else {
setChainlinkToken(_link);
}
oracle = _oracle;
// jobId = stringToBytes32(_jobId);
jobId = _jobId;
fee = _fee;
}
/**
* Create a Chainlink request to retrieve API response, find the target
* data, then multiply by 1000000000000000000 (to remove decimal places from data).
*/
function requestVolumeData() 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",
"https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD"
);
// Set the path to find the desired data in the API response, where the response format is:
// {"RAW":
// {"ETH":
// {"USD":
// {
// "VOLUME24HOUR": xxx.xxx,
// }
// }
// }
// }
request.add("path", "RAW,ETH,USD,VOLUME24HOUR");
// Multiply the result by 1000000000000000000 to remove decimals
int timesAmount = 10**18;
request.addInt("times", timesAmount);
// Sends the request
return sendChainlinkRequestTo(oracle, request, fee);
}
/**
* Receive the response in the form of uint256
*/
function fulfill(bytes32 _requestId, uint256 _volume)
public
recordChainlinkFulfillment(_requestId)
{
volume = _volume;
emit DataFullfilled(volume);
}
// function stringToBytes32(string memory source) public pure returns (bytes32 result) {
// bytes memory tempEmptyStringTest = bytes(source);
// if (tempEmptyStringTest.length == 0) {
// return 0x0;
// }
// assembly {
// result := mload(add(source, 32))
// }
// }
}