regit-web3 1.0.1

Modular Web3 primitives for exact values, chain and provider reads, swap quotes, and transaction preparation
Documentation
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 Regit

use super::{BlockfrostClient, wire};
use crate::{
    domain::cardano::{
        AddressDetails, AssetDetails, AssetEntry, AssetHolder, AssetId, AssetKind, Epoch,
        EpochSelector, Hash, IndexPage, NetworkData, Observation, Operation, Order, PageRequest,
        PageTarget, PaymentAddress, PaymentEstimate, PaymentIntent, ProtocolParameters, Reward,
        SignedSubmission, StakeAccount, StakeAddress, SubmissionResult, Transaction,
        TransactionReference, TransactionStatus, TransactionUtxos,
    },
    error::{Error, ValidationError},
    transport::{OperationBudget, submission_unknown},
};

impl BlockfrostClient {
    async fn indexed_bytes(
        &self,
        path: &[&str],
        query: &[(&str, &str)],
        budget: &OperationBudget,
    ) -> Result<Vec<u8>, Error> {
        let response = self.http.read(path, query, None, budget).await?;
        if response.status.as_u16() == 404 {
            return Err(Error::UnavailableData);
        }
        response.into_success()
    }
    fn payment_address(&self, address: &PaymentAddress) -> Result<(), Error> {
        if !address.is_compatible_with(self.network().identity()) {
            return Err(ValidationError::NetworkMismatch.into());
        }
        Ok(())
    }
    fn stake_address(&self, address: &StakeAddress) -> Result<(), Error> {
        if !address.is_compatible_with(self.network().identity()) {
            return Err(ValidationError::NetworkMismatch.into());
        }
        Ok(())
    }
    fn token_asset(&self, asset: &AssetId) -> Result<String, Error> {
        if asset.network() != self.network().identity() {
            return Err(ValidationError::NetworkMismatch.into());
        }
        match asset.asset() {
            AssetKind::Native => Err(Error::UnsupportedCapability),
            AssetKind::Token {
                policy_id,
                asset_name,
            } => Ok(format!("{policy_id}{asset_name}")),
        }
    }
    async fn epoch_bytes(
        &self,
        selector: EpochSelector,
        parameters: bool,
        budget: &OperationBudget,
    ) -> Result<Vec<u8>, Error> {
        let selected = match selector {
            EpochSelector::Latest => "latest".into(),
            EpochSelector::Number(n) => n.to_string(),
        };
        if parameters {
            self.indexed_bytes(&["epochs", &selected, "parameters"], &[], budget)
                .await
        } else {
            self.indexed_bytes(&["epochs", &selected], &[], budget)
                .await
        }
    }
    async fn page_bytes(
        &self,
        path: &[&str],
        page: PageRequest,
        budget: &OperationBudget,
    ) -> Result<Vec<u8>, Error> {
        let number = page.page().to_string();
        let count = page.count().to_string();
        let order = match page.order() {
            Order::Asc => "asc",
            Order::Desc => "desc",
        };
        self.indexed_bytes(
            path,
            &[("page", &number), ("count", &count), ("order", order)],
            budget,
        )
        .await
    }
    /// Retrieves exact indexed source supply/stake, without a claimed evaluation block.
    ///
    /// # Errors
    /// Returns fixed network, provider, malformed-data or whole-operation deadline failures.
    pub async fn get_network_data(&self) -> Result<Observation<NetworkData>, Error> {
        let budget = OperationBudget::new(self.config.http_config().limits())?;
        budget
            .run(async {
                self.verify_network(&budget).await?;
                let bytes = self.indexed_bytes(&["network"], &[], &budget).await?;
                Observation::new(
                    wire::indexed::network(&bytes, self.network())?,
                    self.context(Operation::NetworkData)?,
                )
            })
            .await
    }
    /// Retrieves latest or exact indexed epoch facts and their actual time units.
    ///
    /// # Errors
    /// Rejects mismatched selected epoch, malformed data, provider/network/deadline failures.
    pub async fn get_epoch(&self, selector: EpochSelector) -> Result<Observation<Epoch>, Error> {
        let budget = OperationBudget::new(self.config.http_config().limits())?;
        budget
            .run(async {
                self.verify_network(&budget).await?;
                let bytes = self.epoch_bytes(selector, false, &budget).await?;
                Observation::new(
                    wire::indexed::epoch(&bytes, self.network(), selector)?,
                    self.context(Operation::Epoch)?,
                )
            })
            .await
    }
    /// Retrieves explicit source protocol parameters; prices retain exact decimal values.
    ///
    /// # Errors
    /// Rejects selected-epoch mismatch, malformed/excessive data and provider/deadline failures.
    pub async fn get_protocol_parameters(
        &self,
        selector: EpochSelector,
    ) -> Result<Observation<ProtocolParameters>, Error> {
        let budget = OperationBudget::new(self.config.http_config().limits())?;
        budget
            .run(async {
                self.verify_network(&budget).await?;
                let bytes = self.epoch_bytes(selector, true, &budget).await?;
                Observation::new(
                    wire::indexed::parameters(&bytes, self.network(), selector)?,
                    self.context(Operation::ProtocolParameters)?,
                )
            })
            .await
    }
    /// Retrieves supporting address classification/stake metadata and all indexed balances.
    ///
    /// # Errors
    /// Rejects address/network/echo mismatch, malformed or unavailable data and deadline failures.
    pub async fn get_address_details(
        &self,
        address: PaymentAddress,
    ) -> Result<Observation<AddressDetails>, Error> {
        self.payment_address(&address)?;
        let budget = OperationBudget::new(self.config.http_config().limits())?;
        budget
            .run(async {
                self.verify_network(&budget).await?;
                let bytes = self
                    .indexed_bytes(&["addresses", address.as_str()], &[], &budget)
                    .await?;
                Observation::new(
                    wire::indexed::address(&bytes, self.network(), &address)?,
                    self.context(Operation::AddressDetails)?,
                )
            })
            .await
    }
    /// Retrieves one exact address's bounded transaction page without hidden fetching.
    ///
    /// # Errors
    /// Rejects incompatible addresses, excess/duplicate/out-of-order rows and provider failures.
    pub async fn get_address_transactions(
        &self,
        address: PaymentAddress,
        page: PageRequest,
    ) -> Result<Observation<IndexPage<TransactionReference>>, Error> {
        self.payment_address(&address)?;
        let budget = OperationBudget::new(self.config.http_config().limits())?;
        budget
            .run(async {
                self.verify_network(&budget).await?;
                let bytes = self
                    .page_bytes(
                        &["addresses", address.as_str(), "transactions"],
                        page,
                        &budget,
                    )
                    .await?;
                Observation::new(
                    wire::indexed::transaction_page(
                        &bytes,
                        self.network(),
                        PageTarget::AddressTransactions { address },
                        page,
                    )?,
                    self.context(Operation::AddressTransactions)?,
                )
            })
            .await
    }
    /// Retrieves actual source stake registration/delegation and exact reward balances.
    ///
    /// # Errors
    /// Rejects wrong-network/echo identities, malformed/null substitution and provider failures.
    pub async fn get_stake_account(
        &self,
        address: StakeAddress,
    ) -> Result<Observation<StakeAccount>, Error> {
        self.stake_address(&address)?;
        let budget = OperationBudget::new(self.config.http_config().limits())?;
        budget
            .run(async {
                self.verify_network(&budget).await?;
                let bytes = self
                    .indexed_bytes(&["accounts", address.as_str()], &[], &budget)
                    .await?;
                Observation::new(
                    wire::indexed::account(&bytes, self.network(), &address)?,
                    self.context(Operation::StakeAccount)?,
                )
            })
            .await
    }
    /// Retrieves one explicit reward page; rewards are not assumed withdrawable.
    ///
    /// # Errors
    /// Rejects wrong-network identities, excessive/duplicate rows and provider/deadline failures.
    pub async fn get_stake_rewards(
        &self,
        address: StakeAddress,
        page: PageRequest,
    ) -> Result<Observation<IndexPage<Reward>>, Error> {
        self.stake_address(&address)?;
        let budget = OperationBudget::new(self.config.http_config().limits())?;
        budget
            .run(async {
                self.verify_network(&budget).await?;
                let bytes = self
                    .page_bytes(&["accounts", address.as_str(), "rewards"], page, &budget)
                    .await?;
                Observation::new(
                    wire::indexed::reward_page(&bytes, self.network(), address, page)?,
                    self.context(Operation::Rewards)?,
                )
            })
            .await
    }
    /// Retrieves one bounded native-asset catalogue page with exact base quantities.
    ///
    /// # Errors
    /// Rejects excess/duplicate/malformed asset rows and provider/network/deadline failures.
    pub async fn get_assets(
        &self,
        page: PageRequest,
    ) -> Result<Observation<IndexPage<AssetEntry>>, Error> {
        let budget = OperationBudget::new(self.config.http_config().limits())?;
        budget
            .run(async {
                self.verify_network(&budget).await?;
                let bytes = self.page_bytes(&["assets"], page, &budget).await?;
                Observation::new(
                    wire::indexed::asset_page(&bytes, self.network(), page)?,
                    self.context(Operation::Assets)?,
                )
            })
            .await
    }
    /// Retrieves a full exact token identity/supply/provenance and source display metadata.
    ///
    /// # Errors
    /// Rejects ADA, mismatched policy/name/fingerprint, malformed data and provider failures.
    pub async fn get_asset(&self, asset: AssetId) -> Result<Observation<AssetDetails>, Error> {
        let unit = self.token_asset(&asset)?;
        let budget = OperationBudget::new(self.config.http_config().limits())?;
        budget
            .run(async {
                self.verify_network(&budget).await?;
                let bytes = self.indexed_bytes(&["assets", &unit], &[], &budget).await?;
                Observation::new(
                    wire::indexed::asset(&bytes, self.network(), &asset)?,
                    self.context(Operation::AssetDetails)?,
                )
            })
            .await
    }
    /// Retrieves one bounded token transaction page without claiming an atomic index.
    ///
    /// # Errors
    /// Rejects ADA/wrong-network inputs, excess/duplicate/out-of-order rows and provider failures.
    pub async fn get_asset_transactions(
        &self,
        asset: AssetId,
        page: PageRequest,
    ) -> Result<Observation<IndexPage<TransactionReference>>, Error> {
        let unit = self.token_asset(&asset)?;
        let budget = OperationBudget::new(self.config.http_config().limits())?;
        budget
            .run(async {
                self.verify_network(&budget).await?;
                let bytes = self
                    .page_bytes(&["assets", &unit, "transactions"], page, &budget)
                    .await?;
                Observation::new(
                    wire::indexed::transaction_page(
                        &bytes,
                        self.network(),
                        PageTarget::AssetTransactions { asset },
                        page,
                    )?,
                    self.context(Operation::AssetTransactions)?,
                )
            })
            .await
    }
    /// Retrieves one exact token's bounded holder page with precision unreported.
    ///
    /// # Errors
    /// Rejects ADA/wrong-network identities, excess/duplicate rows and provider failures.
    pub async fn get_asset_holders(
        &self,
        asset: AssetId,
        page: PageRequest,
    ) -> Result<Observation<IndexPage<AssetHolder>>, Error> {
        let unit = self.token_asset(&asset)?;
        let budget = OperationBudget::new(self.config.http_config().limits())?;
        budget
            .run(async {
                self.verify_network(&budget).await?;
                let bytes = self
                    .page_bytes(&["assets", &unit, "addresses"], page, &budget)
                    .await?;
                Observation::new(
                    wire::indexed::holder_page(&bytes, self.network(), asset, page)?,
                    self.context(Operation::AssetHolders)?,
                )
            })
            .await
    }
    async fn transaction_data(
        &self,
        id: Hash,
        budget: &OperationBudget,
    ) -> Result<Transaction, Error> {
        let hash = id.to_string();
        let bytes = self.indexed_bytes(&["txs", &hash], &[], budget).await?;
        let summary = wire::transaction::Summary::decode(&bytes, id)?;
        let bytes = self
            .indexed_bytes(&["txs", &hash, "cbor"], &[], budget)
            .await?;
        summary.into_domain(self.network(), wire::transaction::cbor(&bytes)?)
    }
    /// Retrieves full source transaction summary and bounded original-body-hash CBOR.
    ///
    /// # Errors
    /// Rejects source/payload identity, fee/validity mismatch, malformed data and deadline failures.
    pub async fn get_transaction(&self, id: Hash) -> Result<Observation<Transaction>, Error> {
        let budget = OperationBudget::new(self.config.http_config().limits())?;
        budget
            .run(async {
                self.verify_network(&budget).await?;
                let tx = self.transaction_data(id, &budget).await?;
                Observation::new(tx, self.context(Operation::Transaction)?)
            })
            .await
    }
    /// Retrieves complete bounded source input/output facts, including separate role metadata.
    ///
    /// # Errors
    /// Rejects source hash/identity/quantity mismatch, more than 1000 rows or provider failures.
    pub async fn get_transaction_utxos(
        &self,
        id: Hash,
    ) -> Result<Observation<TransactionUtxos>, Error> {
        let budget = OperationBudget::new(self.config.http_config().limits())?;
        budget
            .run(async {
                self.verify_network(&budget).await?;
                let hash = id.to_string();
                let bytes = self
                    .indexed_bytes(&["txs", &hash, "utxos"], &[], &budget)
                    .await?;
                Observation::new(
                    wire::transaction::utxos(&bytes, self.network(), id)?,
                    self.context(Operation::TransactionUtxos)?,
                )
            })
            .await
    }
    /// Returns actual indexed inclusion/contract validity or explicit current unavailability.
    /// It does not invent confirmations, permanent absence, rejection or finality.
    ///
    /// # Errors
    /// Rejects malformed/cross-field source data and network/provider/deadline failures.
    pub async fn get_transaction_status(
        &self,
        id: Hash,
    ) -> Result<Observation<TransactionStatus>, Error> {
        let budget = OperationBudget::new(self.config.http_config().limits())?;
        budget
            .run(async {
                self.verify_network(&budget).await?;
                let hash = id.to_string();
                let bytes = match self.indexed_bytes(&["txs", &hash], &[], &budget).await {
                    Ok(bytes) => bytes,
                    Err(Error::UnavailableData) => {
                        return Observation::new(
                            TransactionStatus::NotIndexed {
                                network: self.network().clone(),
                                transaction_id: id,
                            },
                            self.context(Operation::TransactionStatus)?,
                        );
                    }
                    Err(error) => return Err(error),
                };
                let summary = wire::transaction::Summary::decode(&bytes, id)?;
                let bytes = self
                    .indexed_bytes(&["txs", &hash, "cbor"], &[], &budget)
                    .await?;
                let transaction =
                    summary.into_domain(self.network(), wire::transaction::cbor(&bytes)?)?;
                let status = TransactionStatus::Indexed {
                    transaction: Box::new(transaction),
                };
                Observation::new(status, self.context(Operation::TransactionStatus)?)
            })
            .await
    }
    /// Calculates the exact ordinary payment profile with caller-selected fresh epoch parameters.
    /// No scripts are remotely simulated and no fee/change/output is silently altered.
    ///
    /// # Errors
    /// Rejects unsupported profiles, wrong network, inconsistent values/limits and provider failures.
    pub async fn estimate_payment_fee(
        &self,
        intent: PaymentIntent,
        selector: EpochSelector,
    ) -> Result<Observation<PaymentEstimate>, Error> {
        if intent.network().identity() != self.network().identity() {
            return Err(ValidationError::NetworkMismatch.into());
        }
        let budget = OperationBudget::new(self.config.http_config().limits())?;
        budget
            .run(async {
                self.verify_network(&budget).await?;
                let bytes = self.epoch_bytes(selector, true, &budget).await?;
                let params = wire::indexed::parameters(&bytes, self.network(), selector)?;
                Observation::new(
                    PaymentEstimate::new(intent, params)?,
                    self.context(Operation::PaymentEstimate)?,
                )
            })
            .await
    }
    /// Submits one explicit externally signed supported ordinary transaction as raw CBOR.
    /// Source acknowledgement is not execution. Safe-read retry settings never retry this write.
    ///
    /// # Errors
    /// Preflight failures remain ordinary errors; after dispatch, any unestablished
    /// outcome retains possible submission through `SubmissionOutcomeUnknown`.
    pub async fn submit_signed(
        &self,
        submission: SignedSubmission,
    ) -> Result<Observation<SubmissionResult>, Error> {
        if submission.network().identity() != self.network().identity() {
            return Err(ValidationError::NetworkMismatch.into());
        }
        let budget = OperationBudget::new(self.config.http_config().limits())?;
        budget.run(self.verify_network(&budget)).await?;
        let response = self
            .http
            .write_once_cbor(
                &["tx", "submit"],
                &[],
                submission.transaction().bytes(),
                &budget,
            )
            .await?;
        let finish = || {
            let id = wire::transaction::acknowledged(&response.into_success()?)?;
            let result = SubmissionResult::new(submission, id)?;
            let observed = Observation::new(result, self.context(Operation::Submission)?)?;
            budget.check_remaining()?;
            Ok(observed)
        };
        finish().map_err(submission_unknown)
    }
}

impl crate::chains::cardano::CardanoReader for BlockfrostClient {
    async fn get_network_data(&self) -> Result<Observation<NetworkData>, Error> {
        Self::get_network_data(self).await
    }
    async fn get_epoch(&self, selector: EpochSelector) -> Result<Observation<Epoch>, Error> {
        Self::get_epoch(self, selector).await
    }
    async fn get_protocol_parameters(
        &self,
        selector: EpochSelector,
    ) -> Result<Observation<ProtocolParameters>, Error> {
        Self::get_protocol_parameters(self, selector).await
    }
    async fn get_address_details(
        &self,
        address: PaymentAddress,
    ) -> Result<Observation<AddressDetails>, Error> {
        Self::get_address_details(self, address).await
    }
    async fn get_address_transactions(
        &self,
        address: PaymentAddress,
        page: PageRequest,
    ) -> Result<Observation<IndexPage<TransactionReference>>, Error> {
        Self::get_address_transactions(self, address, page).await
    }
    async fn get_stake_account(
        &self,
        address: StakeAddress,
    ) -> Result<Observation<StakeAccount>, Error> {
        Self::get_stake_account(self, address).await
    }
    async fn get_stake_rewards(
        &self,
        address: StakeAddress,
        page: PageRequest,
    ) -> Result<Observation<IndexPage<Reward>>, Error> {
        Self::get_stake_rewards(self, address, page).await
    }
    async fn get_assets(
        &self,
        page: PageRequest,
    ) -> Result<Observation<IndexPage<AssetEntry>>, Error> {
        Self::get_assets(self, page).await
    }
    async fn get_asset(&self, asset: AssetId) -> Result<Observation<AssetDetails>, Error> {
        Self::get_asset(self, asset).await
    }
    async fn get_asset_transactions(
        &self,
        asset: AssetId,
        page: PageRequest,
    ) -> Result<Observation<IndexPage<TransactionReference>>, Error> {
        Self::get_asset_transactions(self, asset, page).await
    }
    async fn get_asset_holders(
        &self,
        asset: AssetId,
        page: PageRequest,
    ) -> Result<Observation<IndexPage<AssetHolder>>, Error> {
        Self::get_asset_holders(self, asset, page).await
    }
    async fn get_transaction(&self, id: Hash) -> Result<Observation<Transaction>, Error> {
        Self::get_transaction(self, id).await
    }
    async fn get_transaction_utxos(
        &self,
        id: Hash,
    ) -> Result<Observation<TransactionUtxos>, Error> {
        Self::get_transaction_utxos(self, id).await
    }
    async fn get_transaction_status(
        &self,
        id: Hash,
    ) -> Result<Observation<TransactionStatus>, Error> {
        Self::get_transaction_status(self, id).await
    }
    async fn estimate_payment_fee(
        &self,
        intent: PaymentIntent,
        selector: EpochSelector,
    ) -> Result<Observation<PaymentEstimate>, Error> {
        Self::estimate_payment_fee(self, intent, selector).await
    }
}

impl crate::chains::cardano::CardanoSubmitter for BlockfrostClient {
    fn network(&self) -> &crate::domain::cardano::Network {
        Self::network(self)
    }
    async fn submit_signed(
        &self,
        submission: SignedSubmission,
    ) -> Result<Observation<SubmissionResult>, Error> {
        Self::submit_signed(self, submission).await
    }
}