Hello World Example
Biibot Oracle is 100% compatible with Chainlink. If you have any questions, please don't hesitate to reach out to us(Twitter) for help.
Deploy your comsumer contract to Meer network
Open Remix
Create OracleConsumer.sol file in Remix
Copy consumer contract code to OracleConsumer.sol file (The source code is at the bottom of this page)
On the Compiler tab, click the Compile button for OracleConsumer.sol
On the Deploy and Run tab, configure the following settings:
- Select Injected Provider as your environment. Make sure your metamask is connected to Sepolia.
- Select OracleConsumer from the Contract menu.
Click Deploy. MetaMask prompts you to confirm the transaction.
Set oracle fee payment token
- Input oracle fee token address, see the payment token address list
- Click the transact button for the
setLinkToken
Set oracle contract address
- Input oracle address, see the oracle contract address list
- Click the transact button for the
setOracle
Set oracle job id
Input oracle id, see the oracle job id list
Click the transact button for the
setJobId
Send funds to your contract
- See the fund your contract page for instructions.
Create a price request
- Input the Base and quote coin in the form, base coin support:
btc
eth
meer
usdt
, quote coin support:usdt
- Input the Base and quote coin in the form, base coin support:
After the transaction processes completed, you can get price by click
currentPrice
function.
Consumer contract code
// 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 OracleConsumer 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 jobId;
address public oracle;
event RequestEthereumPriceFulfilled(
bytes32 indexed requestId,
uint256 indexed price
);
constructor() ConfirmedOwner(msg.sender) {}
function setLinkToken(address _link) public onlyOwner {
_setChainlinkToken(_link);
}
function setOracle(address _oracle) public onlyOwner {
oracle = _oracle;
}
function setJobId(string memory _jobId) public onlyOwner {
jobId = _jobId;
}
function requestPrice(
string memory _base,
string memory _quote
) public onlyOwner {
Chainlink.Request memory req = _buildChainlinkRequest(
stringToBytes32(jobId),
address(this),
this.fulfillEthereumPrice.selector
);
req._add("base", _base);
req._add("quote", _quote);
_sendChainlinkRequestTo(oracle, req, ORACLE_PAYMENT);
}
function requestEthereumPrice(
address _oracle,
string memory _jobId,
string memory _base,
string memory _quote
) public onlyOwner {
Chainlink.Request memory req = _buildChainlinkRequest(
stringToBytes32(_jobId),
address(this),
this.fulfillEthereumPrice.selector
);
req._add("base", _base);
req._add("quote", _quote);
_sendChainlinkRequestTo(_oracle, req, ORACLE_PAYMENT);
}
function fulfillEthereumPrice(
bytes32 _requestId,
uint256 _price
) public recordChainlinkFulfillment(_requestId) {
emit RequestEthereumPriceFulfilled(_requestId, _price);
currentPrice = _price;
}
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))
}
}
}