Biomass and Tree Validation Integration
The below Examples show how to directly Query the API via Oracle Node
Setting Up The Request
The Request requires the above versions of solidity and Chainlink libs. The first example shows a simply biomass request to the node. Please use the above endpoints for adjusting to your network.
The below code shows the smart contract function that will call the oracle node.
function requestTreePresenceToMintExample() public onlyOwner {
address _oracle = 0x67CB6fc6C1Fd1C2D01698f074F50E2DE5F4E2970;
string memory _jobId = "fc009ee445d848c7b8e1632e840e635c";
int256 _month = 6;
// will return 1 (uint256) in variable mintFlag when called
Chainlink.Request memory req = buildChainlinkRequest(
stringToBytes32(_jobId),
address(this),
this.fulfillTreePresence.selector
);
req.add('siteId', '0001');
req.addInt('year', 2022);
req.addInt('month', _month);
sendChainlinkRequestTo(_oracle, req, ORACLE_PAYMENT);
}
The key elemenets here are wapping the required fields into the request body, which when is sent via the sendChainlinkRequestTo() method at the end of the function. The next part is unwrapping the response.
function fulfillTreePresence(bytes32 _requestId, uint256 _mintflg) public recordChainlinkFulfillment(_requestId) {
emit RequestTreePresenceMintFlag(_requestId, _mintflg);
mintFlag = _mintflg;
}
Which unwraps into the public variable.
The full example is below:
// 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 ATestnetConsumer is ChainlinkClient, ConfirmedOwner {
using Chainlink for Chainlink.Request;
uint256 private constant ORACLE_PAYMENT = 1 * LINK_DIVISIBILITY / 10; // 1 * 10**18, costs .1 link
uint256 public currentPrice;
uint256 public mintFlag;
uint256 public year;
uint256 public month;
event RequestTreePresenceMintFlag(bytes32 indexed requestId, uint256 indexed mintflg);
event RequestTreePresenceMultipleFulfilled(bytes32 indexed requestId, uint256 _mintflg, uint256 _year, uint256 _month);
constructor() ConfirmedOwner(msg.sender) {
setChainlinkToken(0x326C977E6efc84E512bB9C30f76E30c160eD06FB);
setChainlinkOracle(0x67CB6fc6C1Fd1C2D01698f074F50E2DE5F4E2970);
}
function requestTreePresenceToMintExample() public onlyOwner {
address _oracle = 0x67CB6fc6C1Fd1C2D01698f074F50E2DE5F4E2970;
string memory _jobId = "fc009ee445d848c7b8e1632e840e635c";
int256 _month = 6;
// will return 1 (uint256) in variable mintFlag when called
Chainlink.Request memory req = buildChainlinkRequest(
stringToBytes32(_jobId),
address(this),
this.fulfillTreePresence.selector
);
req.add('siteId', '0001');
req.addInt('year', 2022);
req.addInt('month', _month);
sendChainlinkRequestTo(_oracle, req, ORACLE_PAYMENT);
}
function requestTreePresenceToMint(
address _oracle,
string memory _jobId,
int256 _month,
int256 _year,
string calldata _siteId
) public onlyOwner {
Chainlink.Request memory req = buildChainlinkRequest(
stringToBytes32(_jobId),
address(this),
this.fulfillTreePresence.selector
);
req.add('siteId', _siteId);
req.addInt('year', _year);
req.addInt('month', _month);
sendChainlinkRequestTo(_oracle, req, ORACLE_PAYMENT);
}
function requestTreePresenceToMintFullReponse(string memory _jobId, int256 _month) public {
// uses oracle: 0x67CB6fc6C1Fd1C2D01698f074F50E2DE5F4E2970
// _jobId: 2c15d3cf0fd7405983a3d137aa4ef5a2
// month: 6
// will return 1 (uint256) in varuable mintFlag when called
// TODO: Further parameterize and test.
Chainlink.Request memory req = buildChainlinkRequest(
stringToBytes32(_jobId),
address(this),
this.fulfillTreePresenceFullResponse.selector
);
req.add('siteId', '0001');
req.addInt('year', 2022);
req.addInt('month', _month);
sendChainlinkRequest(req, ORACLE_PAYMENT);
}
function fulfillTreePresence(bytes32 _requestId, uint256 _mintflg) public recordChainlinkFulfillment(_requestId) {
emit RequestTreePresenceMintFlag(_requestId, _mintflg);
mintFlag = _mintflg;
}
function fulfillTreePresenceFullResponse(
bytes32 _requestId,
uint256 _mintflg,
uint256 _year,
uint256 _month
) public recordChainlinkFulfillment(_requestId) {
emit RequestTreePresenceMultipleFulfilled(_requestId, _mintflg, _year, _month);
mintFlag = _mintflg;
year = _year;
month = _month;
}
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))
}
}
}
The above also highlights other ways to query the node.
Multi Reponse and More details
Below we will highlight how to get multiple response back from the node with a full example.
Good to know: Multi Reponse requires a slightly different address, so please note the details. The example shows details for the Mumbai test network.
// 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 ATestnetConsumerMulti is ChainlinkClient {
using Chainlink for Chainlink.Request;
uint256 private constant ORACLE_PAYMENT = 1 * LINK_DIVISIBILITY / 10; // 1 * 10**18, costs .1 link
uint256 public currentPrice;
uint256 public mintFlag;
uint256 public year;
uint256 public month;
uint256 public prevMintFlag;
bytes4 public siteId;
string public siteIdString;
constructor() {
setChainlinkToken(0x326C977E6efc84E512bB9C30f76E30c160eD06FB);
// 0x67CB6fc6C1Fd1C2D01698f074F50E2DE5F4E2970 oracle
// 0x102D769e8e5e8ED11eC2b8e91c35A9Ef7E3C5DA9 ne operator.sol
setChainlinkOracle(0x102D769e8e5e8ED11eC2b8e91c35A9Ef7E3C5DA9);
}
function requestTreePresenceToMintFullReponse(string memory _jobId, int256 _month) public {
// _jobId: 5b32cec9f4444bf2ad6bcb71c82b9d2d
// month: 6
// will return 1 (uint256) in varuable mintFlag when called
// TODO: Further parameterize and test.
Chainlink.Request memory req = buildChainlinkRequest(
stringToBytes32(_jobId),
address(this),
this.fulfillTreePresenceFullResponse.selector
);
req.add('siteId', '0001');
req.addInt('year', 2022);
req.addInt('month', _month);
sendOperatorRequest(req, ORACLE_PAYMENT);
}
event RequestTreePresenceMultipleFulfilled(
bytes32 indexed requestId,
uint256 _mintflg,
uint256 _year,
uint256 _month,
uint256 _prevMintFlg,
bytes4 _siteId
);
function fulfillTreePresenceFullResponse(
bytes32 _requestId,
uint256 _mintflg,
uint256 _year,
uint256 _month,
uint256 _prevMintFlg,
bytes4 _siteId
) public recordChainlinkFulfillment(_requestId) {
emit RequestTreePresenceMultipleFulfilled(_requestId, _mintflg, _year, _month, _prevMintFlg, _siteId);
mintFlag = _mintflg;
year = _year;
month = _month;
prevMintFlag = _prevMintFlg;
siteId = _siteId;
siteIdString = convertByteToString(_siteId);
}
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))
}
}
function convertByteToString(bytes4 symbol) private pure returns(string memory){
string memory result = string(abi.encodePacked(symbol));
return result;
}
}
Last updated