@@ -15,8 +15,6 @@ import (
1515 mercury_v4 "github.com/smartcontractkit/chainlink-framework/capabilities/writetarget/beholder/report/mercury/v4"
1616)
1717
18- // DecodeAsFeedUpdated decodes a 'platform.write-target.WriteConfirmed' message
19- // as a 'data-feeds.registry.ReportProcessed' message
2018func DecodeAsFeedUpdated (m * wt_msg.WriteConfirmed ) ([]* FeedUpdated , error ) {
2119 // Decode the confirmed report (WT -> DF contract event)
2220 r , err := platform .Decode (m .Report )
@@ -35,20 +33,14 @@ func DecodeAsFeedUpdated(m *wt_msg.WriteConfirmed) ([]*FeedUpdated, error) {
3533
3634 // Iterate over the underlying Mercury reports
3735 for _ , rf := range * reports {
38- // Notice: we assume that Mercury will be the only source of reports used for Data Feeds,
39- // at least for the foreseeable future. If this assumption changes, we should check the
40- // the report type here (potentially encoded in the feed ID) and decode accordingly.
41-
42- // Decode the common Mercury report
43- rm , err := mercury_vX .Decode (rf .Data )
36+ // Decode the common Mercury report and get report type
37+ rmCommon , err := mercury_vX .Decode (rf .Data )
4438 if err != nil {
4539 return nil , fmt .Errorf ("failed to decode Mercury report: %w" , err )
4640 }
4741
48- // Parse the report type
49- t := mercury_vX .GetReportType (rm .FeedID )
50-
51- // Notice: we publish the DataFeed FeedID, not the unrelying DataStream FeedID
42+ // Parse the report type from the common header
43+ t := mercury_vX .GetReportType (rmCommon .FeedID )
5244 feedID := datafeeds .FeedID (rf .FeedID )
5345
5446 switch t {
@@ -57,99 +49,15 @@ func DecodeAsFeedUpdated(m *wt_msg.WriteConfirmed) ([]*FeedUpdated, error) {
5749 if err != nil {
5850 return nil , fmt .Errorf ("failed to decode Mercury v%d report: %w" , t , err )
5951 }
60-
61- msgs = append (msgs , & FeedUpdated {
62- // Event data
63- FeedId : feedID .String (),
64- ObservationsTimestamp : rm .ObservationsTimestamp ,
65- Benchmark : rm .BenchmarkPrice .Bytes (), // Map big.Int as []byte
66- Report : rf .Data ,
67-
68- // Notice: i192 will not fit if scaled number bigger than f64
69- BenchmarkVal : toBenchmarkVal (feedID , rm .BenchmarkPrice ),
70-
71- // Head data - when was the event produced on-chain
72- BlockHash : m .BlockHash ,
73- BlockHeight : m .BlockHeight ,
74- BlockTimestamp : m .BlockTimestamp ,
75-
76- // Transaction data - info about the tx that mained the event (optional)
77- // Notice: we skip SOME head/tx data here (unknown), as we map from 'platform.write-target.WriteConfirmed'
78- // and not from tx/event data (e.g., 'platform.write-target.WriteTxConfirmed')
79- TxSender : m .Transmitter ,
80- TxReceiver : m .Forwarder ,
81-
82- // Execution Context - Source
83- MetaSourceId : m .MetaSourceId ,
84-
85- // Execution Context - Chain
86- MetaChainFamilyName : m .MetaChainFamilyName ,
87- MetaChainId : m .MetaChainId ,
88- MetaNetworkName : m .MetaNetworkName ,
89- MetaNetworkNameFull : m .MetaNetworkNameFull ,
90-
91- // Execution Context - Workflow (capabilities.RequestMetadata)
92- MetaWorkflowId : m .MetaWorkflowId ,
93- MetaWorkflowOwner : m .MetaWorkflowOwner ,
94- MetaWorkflowExecutionId : m .MetaWorkflowExecutionId ,
95- MetaWorkflowName : m .MetaWorkflowName ,
96- MetaWorkflowDonId : m .MetaWorkflowDonId ,
97- MetaWorkflowDonConfigVersion : m .MetaWorkflowDonConfigVersion ,
98- MetaReferenceId : m .MetaReferenceId ,
99-
100- // Execution Context - Capability
101- MetaCapabilityType : m .MetaCapabilityType ,
102- MetaCapabilityId : m .MetaCapabilityId ,
103- MetaCapabilityTimestampStart : m .MetaCapabilityTimestampStart ,
104- MetaCapabilityTimestampEmit : m .MetaCapabilityTimestampEmit ,
105- })
52+ // For Mercury v3, include TxSender and TxReceiver
53+ msgs = append (msgs , newFeedUpdated (m , feedID , rm .ObservationsTimestamp , rm .BenchmarkPrice , rf .Data , true ))
10654 case uint16 (4 ):
10755 rm , err := mercury_v4 .Decode (rf .Data )
10856 if err != nil {
10957 return nil , fmt .Errorf ("failed to decode Mercury v%d report: %w" , t , err )
11058 }
111-
112- msgs = append (msgs , & FeedUpdated {
113- // Event data
114- FeedId : feedID .String (),
115- ObservationsTimestamp : rm .ObservationsTimestamp ,
116- Benchmark : rm .BenchmarkPrice .Bytes (), // Map big.Int as []byte
117- Report : rf .Data ,
118-
119- // Notice: i192 will not fit if scaled number bigger than f64
120- BenchmarkVal : toBenchmarkVal (feedID , rm .BenchmarkPrice ),
121-
122- // Notice: we skip head/tx data here (unknown), as we map from 'platform.write-target.WriteConfirmed'
123- // and not from tx/event data (e.g., 'platform.write-target.WriteTxConfirmed')
124-
125- BlockHash : m .BlockHash ,
126- BlockHeight : m .BlockHeight ,
127- BlockTimestamp : m .BlockTimestamp ,
128-
129- // Execution Context - Source
130- MetaSourceId : m .MetaSourceId ,
131-
132- // Execution Context - Chain
133- MetaChainFamilyName : m .MetaChainFamilyName ,
134- MetaChainId : m .MetaChainId ,
135- MetaNetworkName : m .MetaNetworkName ,
136- MetaNetworkNameFull : m .MetaNetworkNameFull ,
137-
138- // Execution Context - Workflow (capabilities.RequestMetadata)
139- MetaWorkflowId : m .MetaWorkflowId ,
140- MetaWorkflowOwner : m .MetaWorkflowOwner ,
141- MetaWorkflowExecutionId : m .MetaWorkflowExecutionId ,
142- MetaWorkflowName : m .MetaWorkflowName ,
143- MetaWorkflowDonId : m .MetaWorkflowDonId ,
144- MetaWorkflowDonConfigVersion : m .MetaWorkflowDonConfigVersion ,
145- MetaReferenceId : m .MetaReferenceId ,
146-
147- // Execution Context - Capability
148- MetaCapabilityType : m .MetaCapabilityType ,
149- MetaCapabilityId : m .MetaCapabilityId ,
150- MetaCapabilityTimestampStart : m .MetaCapabilityTimestampStart ,
151- MetaCapabilityTimestampEmit : m .MetaCapabilityTimestampEmit ,
152- })
59+ // For Mercury v4, skip TxSender and TxReceiver (if not applicable)
60+ msgs = append (msgs , newFeedUpdated (m , feedID , rm .ObservationsTimestamp , rm .BenchmarkPrice , rf .Data , false ))
15361 default :
15462 return nil , fmt .Errorf ("unsupported Mercury report type: %d" , t )
15563 }
@@ -158,6 +66,61 @@ func DecodeAsFeedUpdated(m *wt_msg.WriteConfirmed) ([]*FeedUpdated, error) {
15866 return msgs , nil
15967}
16068
69+ // newFeedUpdated creates a FeedUpdated from the given common parameters.
70+ // If includeTxInfo is true, TxSender and TxReceiver are set.
71+ func newFeedUpdated (
72+ m * wt_msg.WriteConfirmed ,
73+ feedID datafeeds.FeedID ,
74+ observationsTimestamp uint32 ,
75+ benchmarkPrice * big.Int ,
76+ report []byte ,
77+ includeTxInfo bool ,
78+ ) * FeedUpdated {
79+ fu := & FeedUpdated {
80+ FeedId : feedID .String (),
81+ ObservationsTimestamp : observationsTimestamp ,
82+ Benchmark : benchmarkPrice .Bytes (),
83+ Report : report ,
84+ BenchmarkVal : toBenchmarkVal (feedID , benchmarkPrice ),
85+
86+ // Head data - when was the event produced on-chain
87+ BlockHash : m .BlockHash ,
88+ BlockHeight : m .BlockHeight ,
89+ BlockTimestamp : m .BlockTimestamp ,
90+
91+ // Execution Context - Source
92+ MetaSourceId : m .MetaSourceId ,
93+
94+ // Execution Context - Chain
95+ MetaChainFamilyName : m .MetaChainFamilyName ,
96+ MetaChainId : m .MetaChainId ,
97+ MetaNetworkName : m .MetaNetworkName ,
98+ MetaNetworkNameFull : m .MetaNetworkNameFull ,
99+
100+ // Execution Context - Workflow (capabilities.RequestMetadata)
101+ MetaWorkflowId : m .MetaWorkflowId ,
102+ MetaWorkflowOwner : m .MetaWorkflowOwner ,
103+ MetaWorkflowExecutionId : m .MetaWorkflowExecutionId ,
104+ MetaWorkflowName : m .MetaWorkflowName ,
105+ MetaWorkflowDonId : m .MetaWorkflowDonId ,
106+ MetaWorkflowDonConfigVersion : m .MetaWorkflowDonConfigVersion ,
107+ MetaReferenceId : m .MetaReferenceId ,
108+
109+ // Execution Context - Capability
110+ MetaCapabilityType : m .MetaCapabilityType ,
111+ MetaCapabilityId : m .MetaCapabilityId ,
112+ MetaCapabilityTimestampStart : m .MetaCapabilityTimestampStart ,
113+ MetaCapabilityTimestampEmit : m .MetaCapabilityTimestampEmit ,
114+ }
115+
116+ if includeTxInfo {
117+ fu .TxSender = m .Transmitter
118+ fu .TxReceiver = m .Forwarder
119+ }
120+
121+ return fu
122+ }
123+
161124// toBenchmarkVal returns the benchmark i192 on-chain value decoded as an double (float64), scaled by number of decimals (e.g., 1e-18)
162125// Where the number of decimals is extracted from the feed ID.
163126//
0 commit comments