-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTfiExample.sol
More file actions
112 lines (97 loc) · 3.59 KB
/
TfiExample.sol
File metadata and controls
112 lines (97 loc) · 3.59 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;
import "./TfiClient.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@chainlink/contracts/src/v0.8/interfaces/OperatorInterface.sol";
contract TfiExample is Initializable, OwnableUpgradeable, TfiClient {
using Chainlink for Chainlink.Request;
bytes public result;
mapping(bytes32 => bytes) public results;
address public oracleId;
string public jobId;
uint256 public fee;
function initialize(
address oracleId_,
string memory jobId_,
uint256 fee_,
address token_) public initializer {
__Ownable_init();
__TfiClient_init();
setChainlinkToken(token_);
oracleId = oracleId_;
jobId = jobId_;
fee = fee_;
}
function doRequest(
string memory service_,
string memory data_,
string memory keypath_,
string memory abi_,
string memory multiplier_) public returns (bytes32 requestId) {
Chainlink.Request memory req = buildChainlinkRequest(
bytes32(bytes(jobId)),
address(this), this.fulfillBytes.selector);
req.add("service", service_);
req.add("data", data_);
req.add("keypath", keypath_);
req.add("abi", abi_);
req.add("multiplier", multiplier_);
return sendChainlinkRequestTo(oracleId, req, fee);
}
function doTransferAndRequest(
string memory service_,
string memory data_,
string memory keypath_,
string memory abi_,
string memory multiplier_,
uint256 fee_) public returns (bytes32 requestId) {
require(LinkTokenInterface(getToken()).transferFrom(
msg.sender, address(this), fee_), 'transfer failed');
Chainlink.Request memory req = buildChainlinkRequest(
bytes32(bytes(jobId)),
address(this), this.fulfillBytes.selector);
req.add("service", service_);
req.add("data", data_);
req.add("keypath", keypath_);
req.add("abi", abi_);
req.add("multiplier", multiplier_);
req.add("refundTo",
Strings.toHexString(uint160(msg.sender), 20));
return sendChainlinkRequestTo(oracleId, req, fee_);
}
function fulfillBytes(bytes32 _requestId, bytes memory bytesData)
public recordChainlinkFulfillment(_requestId) {
result = bytesData;
results[_requestId] = bytesData;
}
function changeOracle(address _oracle) public onlyOwner {
oracleId = _oracle;
}
function changeJobId(string memory _jobId) public onlyOwner {
jobId = _jobId;
}
function changeFee(uint256 _fee) public onlyOwner {
fee = _fee;
}
function changeToken(address _address) public onlyOwner {
setChainlinkToken(_address);
}
function getToken() public view returns (address) {
return chainlinkTokenAddress();
}
function getChainlinkToken() public view returns (address) {
return getToken();
}
function withdrawLink() public onlyOwner {
LinkTokenInterface link = LinkTokenInterface(chainlinkTokenAddress());
require(link.transfer(msg.sender, link.balanceOf(address(this))), "Unable to transfer");
}
function typeAndVersion() external pure virtual returns (string memory) {
return "TFI 0.2";
}
// do not allow renouncing ownership
function renounceOwnership() public view override onlyOwner {
}
}