-
Notifications
You must be signed in to change notification settings - Fork 0
/
ATestnetConsumer.sol
116 lines (98 loc) · 4.42 KB
/
ATestnetConsumer.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import '@chainlink/contracts/src/v0.8/ChainlinkClient.sol';
import '@chainlink/contracts/src/v0.8/ConfirmedOwner.sol';
contract ATestnetConsumer is ChainlinkClient, ConfirmedOwner {
using Chainlink for Chainlink.Request;
uint256 private constant ORACLE_PAYMENT = 10**15;
uint256 public currentPrice;
int256 public changeDay;
bytes32 public lastMarket;
event RequestEthereumPriceFulfilled(bytes32 indexed requestId, uint256 indexed price);
event RequestEthereumChangeFulfilled(bytes32 indexed requestId, int256 indexed change);
event RequestEthereumLastMarket(bytes32 indexed requestId, bytes32 indexed market);
/**
* Rinkeby
*@dev LINK address in Rinkeby network: 0x01BE23585060835E02B77ef475b0Cc51aA1e0709
* @dev Check https://docs.chain.link/docs/link-token-contracts/ for LINK address for the right network
*/
constructor() ConfirmedOwner(msg.sender) {
setChainlinkToken(0xb0897686c545045aFc77CF20eC7A532E3120E0F1);
}
function requestEthereumPrice(address _oracle, string memory _jobId) public onlyOwner {
Chainlink.Request memory req = buildChainlinkRequest(
stringToBytes32(_jobId),
address(this),
this.fulfillEthereumPrice.selector
);
req.add('get', 'https://i.kym-cdn.com/entries/icons/original/000/018/012/this_is_fine.jpeg');
sendChainlinkRequestTo(_oracle, req, ORACLE_PAYMENT);
}
function requestEthereumChange(address _oracle, string memory _jobId) public onlyOwner {
Chainlink.Request memory req = buildChainlinkRequest(
stringToBytes32(_jobId),
address(this),
this.fulfillEthereumChange.selector
);
req.add('get', 'https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD');
// req.add("path", "RAW.ETH.USD.CHANGEPCTDAY"); // Chainlink nodes prior to 1.0.0 support this format
req.add('path', 'RAW,ETH,USD,CHANGEPCTDAY'); // Chainlink nodes 1.0.0 and later support this format
req.addInt('times', 1000000000);
sendChainlinkRequestTo(_oracle, req, ORACLE_PAYMENT);
}
function requestEthereumLastMarket(address _oracle, string memory _jobId) public onlyOwner {
Chainlink.Request memory req = buildChainlinkRequest(
stringToBytes32(_jobId),
address(this),
this.fulfillEthereumLastMarket.selector
);
req.add('get', 'https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD');
string[] memory path = new string[](4);
path[0] = 'RAW';
path[1] = 'ETH';
path[2] = 'USD';
path[3] = 'LASTMARKET';
req.addStringArray('path', path);
sendChainlinkRequestTo(_oracle, req, ORACLE_PAYMENT);
}
function fulfillEthereumPrice(bytes32 _requestId, uint256 _price) public recordChainlinkFulfillment(_requestId) {
emit RequestEthereumPriceFulfilled(_requestId, _price);
currentPrice = _price;
}
function fulfillEthereumChange(bytes32 _requestId, int256 _change) public recordChainlinkFulfillment(_requestId) {
emit RequestEthereumChangeFulfilled(_requestId, _change);
changeDay = _change;
}
function fulfillEthereumLastMarket(bytes32 _requestId, bytes32 _market)
public
recordChainlinkFulfillment(_requestId)
{
emit RequestEthereumLastMarket(_requestId, _market);
lastMarket = _market;
}
function getChainlinkToken() public view returns (address) {
return chainlinkTokenAddress();
}
function withdrawLink() public onlyOwner {
LinkTokenInterface link = LinkTokenInterface(chainlinkTokenAddress());
require(link.transfer(msg.sender, link.balanceOf(address(this))), 'Unable to transfer');
}
function cancelRequest(
bytes32 _requestId,
uint256 _payment,
bytes4 _callbackFunctionId,
uint256 _expiration
) public onlyOwner {
cancelChainlinkRequest(_requestId, _payment, _callbackFunctionId, _expiration);
}
function stringToBytes32(string memory source) private pure returns (bytes32 result) {
bytes memory tempEmptyStringTest = bytes(source);
if (tempEmptyStringTest.length == 0) {
return 0x0;
}
assembly {
// solhint-disable-line no-inline-assembly
result := mload(add(source, 32))
}
}
}