The request parameters of the reference code are fixed, you can modify the code according to your own requirements.
// 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 APIConsumer is ChainlinkClient, ConfirmedOwner {
using Chainlink for Chainlink.Request;
uint256 public floor;
bytes32 private jobId;
uint256 private fee;
event fillFloor(bytes32 indexed requestId, uint256 floor);
/**
* @notice Initialize the link token and target oracle
*
* Rinkeby Goerli details:
* Link Token: 0x326C977E6efc84E512bB9C30f76E30c160eD06FB
* Oracle: 0xCC79157eb46F5624204f47AB42b3906cAA40eaB7 (Chainlink DevRel)
* jobId: ca98366cc7314957b8c012c72f05aeeb
*
*/
constructor() ConfirmedOwner(msg.sender) {
setChainlinkToken(0x326C977E6efc84E512bB9C30f76E30c160eD06FB);
setChainlinkOracle(0xCC79157eb46F5624204f47AB42b3906cAA40eaB7);
jobId = "ca98366cc7314957b8c012c72f05aeeb";
fee = (1 * LINK_DIVISIBILITY) / 10; // 0,1 * 10**18 (Varies by network and job)
}
/**
* Create a Chainlink request to retrieve API response, find the target
* data, then multiply by 1000000000000000000 (to remove decimal places from data).
*/
function requestFloor() public returns (bytes32 requestId) {
Chainlink.Request memory req = buildChainlinkRequest(
jobId,
address(this),
this.fulfill.selector
);
// Set the URL to perform the GET request on
req.add(
"get",
"https://oracle-api.banksea.finance/nft/v1/eth/floor_price/current_status?collection=0x08D7C0242953446436F34b4C78Fe9da38c73668d&apikey=test-api-key"
);
req.add("path", "data,floor_price");
// Multiply the result by 1000000000000000000 to remove decimals
int256 timesAmount = 10 ** 18;
req.addInt("times", timesAmount);
// Sends the request
return sendChainlinkRequest(req, fee);
}
/**
* Receive the response in the form of uint256
*/
function fulfill(
bytes32 _requestId,
uint256 _floor
) public recordChainlinkFulfillment(_requestId) {
emit fillFloor(_requestId, _floor);
floor = _floor;
}
/**
* Allow withdraw of Link tokens from the contract
*/
function withdrawLink() public onlyOwner {
LinkTokenInterface link = LinkTokenInterface(chainlinkTokenAddress());
require(
link.transfer(msg.sender, link.balanceOf(address(this))),
"Unable to transfer"
);
}
}
Install dependencies:
npm install
Create a .env file, and fill it with the privatekey of your wallet and infura key:
npx hardhat run .\scripts\deploy.ts --network goerli
Record the address of APIConsumer and fund some LINK to the address. You can call the requestFloor interface on the contract. After a while, you can check the floor value on it.
Refer to this contract on the Goerli testnet network: