forked from smartcontractkit/functions-hardhat-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctionsConsumer.sol
94 lines (84 loc) · 3.07 KB
/
FunctionsConsumer.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "./dev/functions/FunctionsClient.sol";
// import "@chainlink/contracts/src/v0.8/dev/functions/FunctionsClient.sol"; // Once published
import "@chainlink/contracts/src/v0.8/ConfirmedOwner.sol";
/**
* @title Functions Consumer contract
* @notice This contract is a demonstration of using Functions.
* @notice NOT FOR PRODUCTION USE
*/
contract FunctionsConsumer is FunctionsClient, ConfirmedOwner {
using Functions for Functions.Request;
bytes32 public latestRequestId;
bytes public latestResponse;
bytes public latestError;
event OCRResponse(bytes32 indexed requestId, bytes result, bytes err);
/**
* @notice Executes once when a contract is created to initialize state variables
*
* @param oracle - The FunctionsOracle contract
*/
constructor(address oracle) FunctionsClient(oracle) ConfirmedOwner(msg.sender) {}
/**
* @notice Send a simple request
*
* @param source JavaScript source code
* @param secrets Encrypted secrets payload
* @param secretsLocation Location of encrypted secrets (0 for inline, 1 for remote)
* @param args List of arguments accessible from within the source code
* @param subscriptionId Funtions billing subscription ID
* @param gasLimit Maximum amount of gas used to call the client contract's `handleOracleFulfillment` function
* @return Functions request ID
*/
function executeRequest(
string calldata source,
bytes calldata secrets,
Functions.Location secretsLocation,
string[] calldata args,
uint64 subscriptionId,
uint32 gasLimit
) public onlyOwner returns (bytes32) {
Functions.Request memory req;
req.initializeRequest(Functions.Location.Inline, Functions.CodeLanguage.JavaScript, source);
if (secrets.length > 0) {
if (secretsLocation == Functions.Location.Inline) {
req.addInlineSecrets(secrets);
} else {
req.addRemoteSecrets(secrets);
}
}
if (args.length > 0) req.addArgs(args);
bytes32 assignedReqID = sendRequest(req, subscriptionId, gasLimit);
latestRequestId = assignedReqID;
return assignedReqID;
}
/**
* @notice Callback that is invoked once the DON has resolved the request or hit an error
*
* @param requestId The request ID, returned by sendRequest()
* @param response Aggregated response from the user code
* @param err Aggregated error from the user code or from the execution pipeline
* Either response or error parameter will be set, but never both
*/
function fulfillRequest(
bytes32 requestId,
bytes memory response,
bytes memory err
) internal override {
latestResponse = response;
latestError = err;
emit OCRResponse(requestId, response, err);
}
/**
* @notice Allows the Functions oracle address to be updated
*
* @param oracle New oracle address
*/
function updateOracleAddress(address oracle) public onlyOwner {
setOracle(oracle);
}
function addSimulatedRequestId(address oracleAddress, bytes32 requestId) public onlyOwner {
addExternalRequest(oracleAddress, requestId);
}
}