Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use try-catch for sending Functions requests #195

Merged
merged 3 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ For other detailed tutorials and examples, check out the [Chainlink Functions Tu

Install **both** of the following:

- Node.js version [18.18.0](https://nodejs.org/en/download/) (or the latest release of Node.js v18 if a later one is available)
- Node.js version [20](https://nodejs.org/en/download/)
- Deno version [1.36](https://deno.land/[email protected]/getting_started/installation) (or the latest release of Deno v1 if a later one is available)

## Steps on live testnet
Expand Down
22 changes: 20 additions & 2 deletions contracts/AutomatedFunctionsConsumer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ contract AutomatedFunctionsConsumer is FunctionsClient, ConfirmedOwner, Automati
uint256 public s_updateInterval;
uint256 public s_lastUpkeepTimeStamp;
uint256 public s_upkeepCounter;
uint256 public s_requestCounter;
bolekk marked this conversation as resolved.
Show resolved Hide resolved
uint256 public s_responseCounter;

event OCRResponse(bytes32 indexed requestId, bytes result, bytes err);
event RequestRevertedWithErrorMsg(string reason);
event RequestRevertedWithoutErrorMsg(bytes data);

/**
* @notice Executes once when a contract is created to initialize state variables
Expand Down Expand Up @@ -86,8 +89,23 @@ contract AutomatedFunctionsConsumer is FunctionsClient, ConfirmedOwner, Automati
s_lastUpkeepTimeStamp = block.timestamp;
s_upkeepCounter = s_upkeepCounter + 1;

bytes32 requestId = _sendRequest(s_requestCBOR, s_subscriptionId, s_fulfillGasLimit, donId);
s_lastRequestId = requestId;
try
i_router.sendRequest(
bolekk marked this conversation as resolved.
Show resolved Hide resolved
s_subscriptionId,
s_requestCBOR,
FunctionsRequest.REQUEST_DATA_VERSION,
s_fulfillGasLimit,
donId
)
returns (bytes32 requestId) {
s_requestCounter = s_requestCounter + 1;
s_lastRequestId = requestId;
emit RequestSent(requestId);
} catch Error(string memory reason) {
emit RequestRevertedWithErrorMsg(reason);
} catch (bytes memory data) {
emit RequestRevertedWithoutErrorMsg(data);
}
}

/**
Expand Down
30 changes: 22 additions & 8 deletions tasks/Functions-consumer/performManualUpkeep.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,36 @@ task("functions-perform-upkeep", "Manually call performUpkeep in an Automation c
// Call performUpkeep
const performData = taskArgs.data ?? []

const autoConsumerContractFactory = await ethers.getContractFactory("AutomatedFunctionsConsumer")
const autoConsumerContract = await autoConsumerContractFactory.attach(taskArgs.contract)

console.log(
`Calling performUpkeep for Automation consumer contract ${taskArgs.contract} on network ${network.name}${
`\nCalling performUpkeep for Automation consumer contract ${taskArgs.contract} on network ${network.name}${
taskArgs.data ? ` with data ${performData}` : ""
}`
)
const autoConsumerContractFactory = await ethers.getContractFactory("AutomatedFunctionsConsumer")
const autoConsumerContract = await autoConsumerContractFactory.attach(taskArgs.contract)

const checkUpkeep = await autoConsumerContract.performUpkeep(performData, overrides)
const performUpkeepTx = await autoConsumerContract.performUpkeep(performData, overrides)

console.log(
`Waiting ${networks[network.name].confirmations} blocks for transaction ${checkUpkeep.hash} to be confirmed...`
`\nWaiting ${networks[network.name].confirmations} blocks for transaction ${
performUpkeepTx.hash
} to be confirmed...`
)
await checkUpkeep.wait(networks[network.name].confirmations)
const events = (await performUpkeepTx.wait(networks[network.name].confirmations)).events

const requestRevertedWithErrorMsg = events.find((e) => e.event === "RequestRevertedWithErrorMsg")
if (requestRevertedWithErrorMsg) {
console.log(`\nRequest reverted with error message: ${requestRevertedWithErrorMsg.args.reason}`)
return
}

console.log(`\nSuccessfully called performUpkeep`)
const requestRevertedWithoutErrorMsg = events.find((e) => e.event === "RequestRevertedWithoutErrorMsg")
if (requestRevertedWithoutErrorMsg) {
console.log(
`\nRequest reverted without error message. Ensure your request has been set correctly, the subscription is funded and the consumer contract is authorized.\n(Raw data: ${requestRevertedWithoutErrorMsg.data})`
)
return
}

const reqId = await autoConsumerContract.s_lastRequestId()
console.log("\nLast request ID received by the Automation Consumer Contract...", reqId)
Expand Down