-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequestOutputLog.sol
105 lines (91 loc) · 3.04 KB
/
RequestOutputLog.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol";
import "@chainlink/contracts/src/v0.8/shared/access/ConfirmedOwner.sol";
/**
* THIS IS AN EXAMPLE CONTRACT THAT USES UN-AUDITED CODE.
* DO NOT USE THIS CODE IN PRODUCTION.
*/
contract RequestOutputLog is ChainlinkClient, ConfirmedOwner {
using Chainlink for Chainlink.Request;
uint256 private constant ORACLE_PAYMENT = (1 * LINK_DIVISIBILITY) / 10; // 0.1 * 10**18
// uint256 public currentPrice;
string public currentReceivedID;
event RequestOutputFulfilled(
bytes32 indexed requestId,
// uint256 indexed price
string indexed receivedID
);
/**
* Goerli
*@dev LINK address in Goerli network: 0x326C977E6efc84E512bB9C30f76E30c160eD06FB
* @dev Check https://docs.chain.link/docs/link-token-contracts/ for LINK address for the right network
*/
constructor() ConfirmedOwner(msg.sender) {
setChainlinkToken(0x326C977E6efc84E512bB9C30f76E30c160eD06FB);
}
function requestOutput(
address _oracle,
string memory _jobId,
string memory _folderID
) public onlyOwner {
Chainlink.Request memory req = buildChainlinkRequest(
stringToBytes32(_jobId),
address(this),
this.fulfillOutputRequest.selector
);
// Create a bytes array to store the concatenated string
bytes memory url = abi.encodePacked(
"https://kkpy.onrender.com/output?fileUrl=",
_folderID
);
req.add(
"get",
string(url)
);
req.add("path", "ipfshash");
sendChainlinkRequestTo(_oracle, req, ORACLE_PAYMENT);
}
function fulfillOutputRequest(
bytes32 _requestId,
string memory _receivedID
) public recordChainlinkFulfillment(_requestId) {
emit RequestOutputFulfilled(_requestId, _receivedID);
currentReceivedID = _receivedID;
}
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))
}
}
}