-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequestGCP.sol
More file actions
112 lines (100 loc) · 3.11 KB
/
requestGCP.sol
File metadata and controls
112 lines (100 loc) · 3.11 KB
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
pragma solidity 0.4.24;
import "chainlink/solidity/contracts/Chainlinked.sol";
import "openzeppelin-solidity/contracts/ownership/Ownable.sol";
/*
* POC of Ocean / Chainlink Integration
* by Ocean Protocol Team
*/
contract requestGCP is Chainlinked, Ownable {
/*
* global variables
*/
uint256 constant private ORACLE_PAYMENT = 1 * LINK; // default price for each request
mapping (bytes32 => uint256) public results; // _requestId => result value
/*
* events
*/
event requestCreated(address indexed requester,bytes32 indexed jobId, bytes32 indexed requestId);
event requestFulfilled(bytes32 indexed _requestId, uint256 _data);
event tokenWithdrawn(address indexed recepient, uint256 amount);
event tokenDeposited(address indexed sender, uint256 amount);
/*
* constructor function
*/
constructor(address oracleAddress) public {
// Set the address for the LINK token for the Kovan network.
setLinkToken(0xa36085F69e2889c224210F603D836748e7dC0088);
// Set the address of the oracle in Kovan network to create requests to.
setOracle(oracleAddress);
}
/*
* view functions to get internal information
*/
function getChainlinkToken() public view returns (address) {
return chainlinkToken();
}
function getOracle() public view returns (address) {
return oracleAddress();
}
function getRequestResult(bytes32 _requestId) public view returns (uint256) {
return results[_requestId];
}
/*
* Create a request and send it to default Oracle contract
*/
function createRequest(
bytes32 _jobId,
string _coin,
string _market
)
public
onlyOwner
returns (bytes32 requestId)
{
// create request instance
Chainlink.Request memory req = newRequest(_jobId, this, this.fulfill.selector);
// fill in the pass-in parameters
req.add("endpoint", "price");
req.add("fsym", _coin);
req.add("tsyms", _market);
req.add("copyPath", _market);
req.addInt("times", 100);
// send request & payment to Chainlink oracle (Requester Contract sends the payment)
requestId = chainlinkRequestTo(getOracle(), req, ORACLE_PAYMENT);
// emit event message
emit requestCreated(msg.sender, _jobId, requestId);
}
/*
* callback function to keep the returned value from Oracle contract
*/
function fulfill(bytes32 _requestId, uint256 _data)
public
recordChainlinkFulfillment(_requestId)
{
results[_requestId] = _data;
emit requestFulfilled(_requestId, _data);
}
/*
* withdraw the remaining LINK tokens from the contract
*/
function withdrawTokens() public onlyOwner {
LinkTokenInterface link = LinkTokenInterface(chainlinkToken());
uint256 balance = link.balanceOf(address(this));
require(link.transfer(msg.sender, balance), "Unable to transfer");
emit tokenWithdrawn(msg.sender, balance);
}
/*
* cancel the pending request
*/
function cancelRequest(
bytes32 _requestId,
uint256 _payment,
bytes4 _callbackFunctionId,
uint256 _expiration
)
public
onlyOwner
{
cancelChainlinkRequest(_requestId, _payment, _callbackFunctionId, _expiration);
}
}