Skip to content

Commit

Permalink
Use try-catch for sending Functions requests (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
KuphJr authored Nov 24, 2023
1 parent dc3fba4 commit 62cc367
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
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;

Check warning on line 26 in contracts/AutomatedFunctionsConsumer.sol

View workflow job for this annotation

GitHub Actions / Lint

Variable name must be in mixedCase
uint256 public s_lastUpkeepTimeStamp;

Check warning on line 27 in contracts/AutomatedFunctionsConsumer.sol

View workflow job for this annotation

GitHub Actions / Lint

Variable name must be in mixedCase
uint256 public s_upkeepCounter;

Check warning on line 28 in contracts/AutomatedFunctionsConsumer.sol

View workflow job for this annotation

GitHub Actions / Lint

Variable name must be in mixedCase
uint256 public s_requestCounter;

Check warning on line 29 in contracts/AutomatedFunctionsConsumer.sol

View workflow job for this annotation

GitHub Actions / Lint

Variable name must be in mixedCase
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(
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

0 comments on commit 62cc367

Please sign in to comment.