This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tensorConsumer.sol
61 lines (51 loc) · 1.97 KB
/
tensorConsumer.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
// 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 BAYCPriceFetcher is ChainlinkClient, ConfirmedOwner {
using Chainlink for Chainlink.Request;
uint256 constant private ORACLE_PAYMENT = 1 * LINK_DIVISIBILITY;
uint256 public currentPrice;
event RequestBAYCPriceFulfilled(
bytes32 indexed requestId,
uint256 indexed price
);
constructor() ConfirmedOwner(msg.sender){
setPublicChainlinkToken();
}
function getBAYCPrice(address _oracle, string memory _jobId)
public
onlyOwner
{
Chainlink.Request memory req = buildChainlinkRequest(stringToBytes32(_jobId), address(this), this.fulfillBAYCPrice.selector);
req.add("get", "https://api.tensor.so/eth/collections/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d/floor");
req.add("path", "price");
sendChainlinkRequestTo(_oracle, req, ORACLE_PAYMENT);
}
function getBAYCPriceAt(address _oracle, string memory _jobId, string memory at)
public
onlyOwner
{
Chainlink.Request memory req = buildChainlinkRequest(stringToBytes32(_jobId), address(this), this.fulfillBAYCPrice.selector);
string memory url = string(abi.encodePacked("https://api.tensor.so/eth/collections/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d/floor?at=", at));
req.add("get", url);
req.add("path", "price");
sendChainlinkRequestTo(_oracle, req, ORACLE_PAYMENT);
}
function fulfillBAYCPrice(bytes32 _requestId, uint256 _price)
public
recordChainlinkFulfillment(_requestId)
{
emit RequestBAYCPriceFulfilled(_requestId, _price);
currentPrice = _price;
}
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))
}
}
}