Skip to content

Instantly share code, notes, and snippets.

@alexroan
Last active June 23, 2021 10:38
Show Gist options
  • Save alexroan/e9781902cc92bdd5ad6db256692b0034 to your computer and use it in GitHub Desktop.
Save alexroan/e9781902cc92bdd5ad6db256692b0034 to your computer and use it in GitHub Desktop.
Example implementations of Multi-word response
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@chainlink/contracts/src/v0.8/dev/ChainlinkClient.sol";
/**
* @notice DO NOT USE THIS CODE IN PRODUCTION. This is an example contract.
*/
contract Consumer is ChainlinkClient {
using Chainlink for Chainlink.Request;
// variable bytes returned in a signle oracle response
bytes public data;
// multiple params returned in a single oracle response
uint256 public usd;
uint256 public eur;
uint256 public jpy;
/**
* @notice Initialize the link token and target oracle
* @dev The oracle address must be an Operator contract for multiword response
*/
constructor(
address link,
address oracle
) {
setChainlinkToken(link);
setChainlinkOracle(oracle);
}
////---- MULTIPLE PARAMETERS REQUESTED & RETURNED BY ORACLE ----////
/**
* @notice Request mutiple parameters from the oracle in a single transaction
* @param specId bytes32 representation of the jobId in the Oracle
* @param payment uint256 cost of request in LINK (JUELS)
*/
function requestMultipleParameters(
bytes32 specId,
uint256 payment
)
public
{
Chainlink.Request memory req = buildChainlinkRequest(specId, address(this), this.fulfillMultipleParameters.selector);
requestOracleData(req, payment);
}
event RequestMultipleFulfilled(
bytes32 indexed requestId,
uint256 indexed usd,
uint256 indexed eur,
uint256 jpy
);
/**
* @notice Fulfillment function for multiple parameters in a single request
* @dev This is called by the oracle. recordChainlinkFulfillment must be used.
*/
function fulfillMultipleParameters(
bytes32 requestId,
uint256 usdResponse,
uint256 eurResponse,
uint256 jpyResponse
)
public
recordChainlinkFulfillment(requestId)
{
emit RequestMultipleFulfilled(requestId, usdResponse, eurResponse, jpyResponse);
usd = usdResponse;
eur = eurResponse;
jpy = jpyResponse;
}
////---- END ----////
////---- VARIABLE BYTES ARRAY RETURNED BY ORACLE ----////
/**
* @notice Request variable bytes from the oracle
* @param specId bytes32 representation of the jobId in the Oracle
* @param payment uint256 cost of request in LINK (JUELS)
*/
function requestBytes(
bytes32 specId,
uint256 payment
)
public
{
Chainlink.Request memory req = buildChainlinkRequest(specId, address(this), this.fulfillBytes.selector);
requestOracleData(req, payment);
}
event RequestFulfilled(
bytes32 indexed requestId,
bytes indexed data
);
/**
* @notice Fulfillment function for variable bytes
* @dev This is called by the oracle. recordChainlinkFulfillment must be used.
*/
function fulfillBytes(
bytes32 requestId,
bytes memory bytesData
)
public
recordChainlinkFulfillment(requestId)
{
emit RequestFulfilled(requestId, bytesData);
data = bytesData;
}
////---- END ----////
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment