1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//! One place to submit a transaction and resolve its outcome (#1355).
//!
//! Every on-chain write in this workspace needs the same three steps — send,
//! await the receipt, fail on a revert — and every hand-rolled copy of them got
//! the same detail wrong: the tx hash was read *after* `get_receipt()`, so a
//! receipt that could not be fetched left the caller with an error naming no
//! transaction at all. That is the one failure mode where the operator most
//! needs the hash, because the transaction is in flight and re-sending it is
//! what causes duplicate spends.
//!
//! Lives in `decdn-incentive` rather than the CLI so every on-chain write in
//! this crate shares one send/receipt path.
//!
//! One caller is deliberately NOT migrated: `cli::commands::channel`'s
//! `submit_close` / `submit_settle` / `submit_reclaim` classify a revert as an
//! expected *outcome* (`TxOutcome::Reverted`, an `Ok`) rather than an error,
//! because a channel that someone else already closed is a normal race, not a
//! failure. Routing them through here would turn that into a hard error. Any
//! other hand-rolled send/receipt pair is a bug — it will lose the hash on a
//! receipt timeout.
use ;
use B256;
use Provider;
use Context;
/// What a [`send_for_receipt`] call did on-chain, recorded through its out-param
/// independently of the `Result` it returns.
///
/// `send_for_receipt` still fails on every non-success path; this says *which*
/// non-success, and carries the tx hash on every arm that captured one — so a
/// machine-readable receipt need not scrape the hash out of the human error
/// text. That gap is the bug behind #1550 (a confirmed revert lost its hash) and
/// #1577 (a lost send response was reported as a definite "nothing happened").
///
/// The six states are mutually exclusive and cover every path:
///
/// - [`NotSent`](Self::NotSent) — the initial value; also what a dry run leaves.
/// - [`Rejected`](Self::Rejected) — `send()` failed with a JSON-RPC error
/// response (a pre-flight gas-estimate revert, a stale nonce, an under-funded
/// account). The node answered, and nothing entered the mempool.
/// - [`MaybeBroadcast`](Self::MaybeBroadcast) — `send()` failed with a
/// transport-class error. The request may have reached the node and broadcast
/// the transaction, but no hash was captured. The outcome is UNKNOWN.
/// - [`InFlight`](Self::InFlight) — broadcast, but the receipt could not be
/// fetched. In flight, may still confirm. Carries the hash.
/// - [`Reverted`](Self::Reverted) — broadcast and mined, but reverted: it burned
/// gas and had no effect. Carries the hash as a permanent on-chain handle.
/// - [`Confirmed`](Self::Confirmed) — confirmed success. Carries the hash.
/// Whether a `send()` failure leaves the transaction's fate genuinely unknown.
///
/// The question is whether the request could have reached the node and broadcast
/// the transaction before its response was lost. Two kinds of failure are
/// definite rejections that broadcast nothing:
///
/// - a JSON-RPC error *response* — the node processed the request and rejected
/// it (a pre-flight gas-estimate revert, the modal `createNamespace` /
/// `addOrigin` failure; a stale nonce; an under-funded account);
/// - a client-side fault raised before the request left the process (an
/// unsupported feature, a local-usage error, a body that failed to serialize),
/// or an HTTP 4xx where the gateway refused the request outright (a bad path,
/// an expired key) without forwarding it to the node.
///
/// Everything else is genuinely uncertain: a timeout, a connection reset, an HTTP
/// 5xx from an RPC load balancer, or a response body that could not be
/// deserialized. There the transaction may already be in the mempool, and
/// `eth_sendRawTransaction` is not idempotent from the client's view, so the
/// caller must not report "nothing happened".
///
/// The `as_error_resp().is_none()` shorthand is deliberately NOT used: it would
/// fold the local faults above into the uncertain bucket, warning about a
/// possibly-broadcast transaction on a bug that never touched the wire.
///
/// `const` because the workspace `missing_const_for_fn` clippy lint requires it —
/// the body is a pure classification with no non-const calls.
pub const
/// Whether a send failed because another transaction from the same account took
/// its nonce: the node answered with a JSON-RPC rejection (`nonce too low`, or a
/// same-nonce `replacement transaction underpriced`). Nothing was broadcast, so
/// re-sending is safe, and a provider that reads the pending nonce on every send
/// picks the next free nonce on the retry. A node's seller and buyer services
/// sign with one operator key, so their transactions can collide this way.
/// Send one call, await its receipt, and fail on a revert.
///
/// `landed` is the caller's record of whether this step may have taken effect,
/// and is the reason an out-param exists rather than just a return value:
///
/// - rejected send → `None` (nothing was broadcast)
/// - transport-class send failure → `None`, but the error text warns that the tx
/// MAY have been broadcast — the outcome is unknown ([`SendOutcome`] carries
/// this as [`MaybeBroadcast`](SendOutcome::MaybeBroadcast) for callers that
/// render structured receipts)
/// - broadcast, receipt unreadable → `Some(hash)` **and** `Err` — the outcome is
/// UNKNOWN, and the caller must treat it as possibly-applied
/// - confirmed revert → `None` and `Err` (definitively no effect)
/// - confirmed success → `Some(hash)` and `Ok`
///
/// So `Some` means "may have taken effect", never merely "was broadcast", and an
/// `Err` from here does **not** imply nothing landed. Callers that report what
/// happened must read it that way; asserting "nothing was transferred" on an
/// `Err` is how an operator gets told to re-run a transaction that is still
/// pending. Callers that need the full outcome — the revert hash, the
/// broadcast-unknown case — call [`send_for_receipt`] with a [`SendOutcome`]
/// out-param instead of this `Option<B256>` collapse.
///
/// `hint` is a per-step semantic diagnostic, attached to **both** the send and
/// revert arms deliberately. A wallet provider built with alloy's recommended
/// fillers runs `eth_estimateGas` inside `send()`, so a call that would revert
/// usually fails *there*, before it is ever broadcast, surfacing as a bare
/// `execution reverted` selector. A hint attached only to the receipt arm would
/// therefore almost never print.
///
/// # Errors
///
/// Fails if the call cannot be sent, if the receipt cannot be fetched, or if the
/// transaction reverted.
pub async
/// [`send`], recording the full [`SendOutcome`] and returning the whole receipt
/// for callers that must decode event logs from it (a contract's return value is
/// not in a receipt, so an emitted event is the only way to read one back).
///
/// `outcome` is set on every path, so a caller can render a machine-readable
/// receipt from it after the `Result` propagates: the revert hash it needs for
/// #1550, and the broadcast-unknown case it needs for #1577, are both here even
/// though the `Result` cannot carry them.
///
/// # Errors
///
/// Fails if the call cannot be sent, if the receipt cannot be fetched, or if the
/// transaction reverted.
pub async
/// [`send`] for callers with no outcome to record — a step whose hash they do
/// not report. The in-flight hash still reaches the operator through the error
/// message; this only drops the structured record.
///
/// # Errors
///
/// As [`send`].
pub async