11package workflows
22
33import (
4+ "encoding/json"
5+ "fmt"
6+ "strconv"
47 "time"
58
69 gethCommon "github.com/ethereum/go-ethereum/common"
710)
811
12+ const (
13+ PaymentRequestType = "payment_request"
14+ )
15+
916// CursorInfo contains parsed info from a "block-logIndex-txHash" string.
1017type CursorInfo struct {
1118 BlockNumber uint64
@@ -14,39 +21,32 @@ type CursorInfo struct {
1421}
1522
1623type VerifiableEvent struct {
17- CreatedAt time.Time `json:"created_at"`
18- Workflow Workflow `json:"workflow"`
19- Trigger Trigger `json:"trigger"`
24+ Domain string `json:"domain"`
2025 Event Event `json:"event"`
2126 ReferenceData ReferenceData `json:"reference_data"`
22- }
23-
24- type Workflow struct {
25- WorkflowName string `json:"workflow_name"`
26- WatcherID string `json:"watcher_id"`
27- DonID string `json:"don_id"`
28- Domain string `json:"domain"`
27+ Trigger Trigger `json:"trigger"`
2928}
3029
3130type Trigger struct {
3231 ChainID string `json:"chain_id"`
33- TxHash string `json:"tx_hash"`
3432 LogIndex uint64 `json:"log_index"`
33+ TxHash string `json:"tx_hash"`
3534}
3635
3736type Event struct {
37+ BlockNumber uint64 `json:"block_number"`
38+ BlockTimestamp time.Time `json:"block_timestamp"`
39+ ContractAddress string `json:"contract_address"`
3840 EventName string `json:"event_name"`
3941 EventSignature string `json:"event_signature"`
40- ContractAddress string `json:"contract_address"`
4142 TopicHash string `json:"topic_hash"`
42- BlockNumber uint64 `json:"block_number"`
43- BlockTimestamp time.Time `json:"block_timestamp"`
4443 Args map [string ]any `json:"args"`
4544}
4645
4746type ReferenceData struct {
48- OnChain []OnChainReferenceData `json:"on_chain"`
49- OffChain []OffChainReferenceData `json:"off_chain"`
47+ OnChain []OnChainReferenceData `json:"on_chain,omitempty"`
48+ OffChain []OffChainReferenceData `json:"off_chain,omitempty"`
49+ Requests []OffChainRequest `json:"requests,omitempty"`
5050}
5151
5252type OnChainReferenceData struct {
@@ -71,6 +71,69 @@ type OffChainReferenceDataSource struct {
7171 Identifier string `json:"identifier"`
7272}
7373
74+ // OffChainRequest represents a request to be forwarded to an off-chain application.
75+ // The Type field determines which request type is contained in the Request field.
76+ // Use json.RawMessage to allow consumers to unmarshal the Request based on Type.
77+ type OffChainRequest struct {
78+ Type string `json:"type" example:"payment"`
79+ Request json.RawMessage `json:"request"`
80+ }
81+
82+ // Fixed2 represents a fixed-point decimal number with 2 decimal places, stored as a string.
83+ // Example: "100.00" represents 100.00
84+ type Fixed2 float64
85+
86+ func (f Fixed2 ) MarshalJSON () ([]byte , error ) {
87+ return fmt .Appendf (nil , "%.2f" , f ), nil
88+ }
89+
90+ func (f * Fixed2 ) UnmarshalJSON (data []byte ) error {
91+ // Try unmarshaling as a float64
92+ var num float64
93+ err := json .Unmarshal (data , & num )
94+ if err == nil {
95+ * f = Fixed2 (num )
96+ return nil
97+ }
98+
99+ // Fallback: try as a quoted string
100+ var s string
101+ if err := json .Unmarshal (data , & s ); err != nil {
102+ return fmt .Errorf ("Fixed2: invalid JSON input: %w" , err )
103+ }
104+ parsed , err := strconv .ParseFloat (s , 64 )
105+ if err != nil {
106+ return fmt .Errorf ("Fixed2: cannot parse string value: %w" , err )
107+ }
108+ * f = Fixed2 (parsed )
109+ return nil
110+ }
111+
112+ // PaymentRequest contains the details needed for an off-chain payment request.
113+ type PaymentRequest struct {
114+ ApplicationType string `json:"applicationType" example:"DVP"`
115+ ApplicationAddr string `json:"applicationAddr" example:"0xApplicationAddress"`
116+ E2EID string `json:"e2eId" example:"E2E1234"`
117+ Sender string `json:"sender" example:"0xSenderAddress"`
118+ Receiver string `json:"receiver" example:"0xReceiverAddress"`
119+ Currency string `json:"currency" example:"USD"`
120+ ChainID string `json:"chainId" example:"1337"`
121+ Amount Fixed2 `json:"amount" example:"100.00"`
122+ Expiration * int64 `json:"expiration,omitempty" example:"1257894000"`
123+ // Callback specifies how the off-chain payment handler should call back to the application.
124+ // This can be a function signature (e.g., "fulfillPayment(bytes32,uint256)") or
125+ // a more detailed callback specification.
126+ CustomCallback * PaymentCallback `json:"customCallback,omitempty"`
127+ }
128+
129+ // PaymentCallback specifies how to call back to the application after payment processing.
130+ type PaymentCallback struct {
131+ // FunctionSignature is the ABI function signature to call (e.g., "fulfillPayment(bytes32,uint256)")
132+ FunctionSignature string `json:"functionSignature,omitempty" example:"fulfillPayment(bytes32,uint256)"`
133+ // ContractAddress is the contract address to call. If empty, uses ApplicationAddr from PaymentRequest.
134+ ContractAddress string `json:"contractAddress,omitempty" example:"0xContractAddress"`
135+ }
136+
74137// PreConsensusEventResults holds the encoded event and hash used for consensus.
75138type PreConsensusEventResults struct {
76139 Base64Event string
0 commit comments