Skip to content

Commit 38d8e3b

Browse files
authored
Update getting started w/ latest ReceiverTemplate (#3349)
* update getting started w/ latest ReceiverTemplate * nit
1 parent 8ac0f5f commit 38d8e3b

5 files changed

Lines changed: 201 additions & 246 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.19;
3+
4+
import {ReceiverTemplate} from "./ReceiverTemplate.sol";
5+
6+
/**
7+
* @title CalculatorConsumer (Testing Version)
8+
* @notice This contract receives reports from a CRE workflow and stores the results of a calculation onchain.
9+
* @dev Inherits from ReceiverTemplate which provides security checks. The forwarder address must be
10+
* configured at deployment. Additional security checks (workflowId, workflowName, author) can be enabled via setter
11+
* functions.
12+
*/
13+
contract CalculatorConsumer is ReceiverTemplate {
14+
// Struct to hold the data sent in a report from the workflow
15+
struct CalculatorResult {
16+
uint256 offchainValue;
17+
int256 onchainValue;
18+
uint256 finalResult;
19+
}
20+
21+
// --- State Variables ---
22+
CalculatorResult public latestResult;
23+
uint256 public resultCount;
24+
mapping(uint256 => CalculatorResult) public results;
25+
26+
// --- Events ---
27+
event ResultUpdated(uint256 indexed resultId, uint256 finalResult);
28+
29+
/**
30+
* @notice Constructor requires the forwarder address for security
31+
* @param _forwarderAddress The address of the Chainlink Forwarder contract (for testing: MockForwarder)
32+
* @dev The forwarder address enables the first layer of security - only the forwarder can call onReport.
33+
* Additional security checks can be configured after deployment using setter functions.
34+
*/
35+
constructor(
36+
address _forwarderAddress
37+
) ReceiverTemplate(_forwarderAddress) {}
38+
39+
/**
40+
* @notice Implements the core business logic for processing reports.
41+
* @dev This is called automatically by ReceiverTemplate's onReport function after security checks.
42+
*/
43+
function _processReport(
44+
bytes calldata report
45+
) internal override {
46+
// Decode the report bytes into our CalculatorResult struct
47+
CalculatorResult memory calculatorResult = abi.decode(report, (CalculatorResult));
48+
49+
// --- Core Logic ---
50+
// Update contract state with the new result
51+
resultCount++;
52+
results[resultCount] = calculatorResult;
53+
latestResult = calculatorResult;
54+
55+
emit ResultUpdated(resultCount, calculatorResult.finalResult);
56+
}
57+
58+
// This function is a "dry-run" utility. It allows an offchain system to check
59+
// if a prospective result is an outlier before submitting it for a real onchain update.
60+
// It is also used to guide the binding generator to create a method that accepts the CalculatorResult struct.
61+
function isResultAnomalous(
62+
CalculatorResult memory _prospectiveResult
63+
) public view returns (bool) {
64+
// A result is not considered anomalous if it's the first one.
65+
if (resultCount == 0) {
66+
return false;
67+
}
68+
69+
// Business logic: Define an anomaly as a new result that is more than double the previous result.
70+
// This is just one example of a validation rule you could implement.
71+
return _prospectiveResult.finalResult > (latestResult.finalResult * 2);
72+
}
73+
}

0 commit comments

Comments
 (0)