fuel_core/database/
coin.rs

1use crate::{
2    database::{
3        OffChainIterableKeyValueView,
4        OnChainIterableKeyValueView,
5    },
6    fuel_core_graphql_api::storage::coins::{
7        OwnedCoins,
8        owner_coin_id_key,
9    },
10};
11use fuel_core_storage::{
12    Result as StorageResult,
13    StorageAsRef,
14    iter::{
15        IterDirection,
16        IteratorOverTable,
17    },
18    not_found,
19    tables::Coins,
20};
21use fuel_core_types::{
22    entities::coins::coin::CompressedCoin,
23    fuel_tx::{
24        Address,
25        TxId,
26        UtxoId,
27    },
28};
29
30impl OffChainIterableKeyValueView {
31    pub fn owned_coins_ids(
32        &self,
33        owner: &Address,
34        start_coin: Option<UtxoId>,
35        direction: Option<IterDirection>,
36    ) -> impl Iterator<Item = StorageResult<UtxoId>> + '_ + use<'_> {
37        let start_coin = start_coin.map(|b| owner_coin_id_key(owner, &b));
38        self.iter_all_filtered_keys::<OwnedCoins, _>(
39            Some(*owner),
40            start_coin.as_ref(),
41            direction,
42        )
43        .map(|res| {
44            res.map(|key| {
45                UtxoId::new(
46                    TxId::try_from(&key[32..64]).expect("The slice has size 32"),
47                    u16::from_be_bytes(
48                        key[64..].try_into().expect("The slice has size 2"),
49                    ),
50                )
51            })
52        })
53    }
54}
55
56impl OnChainIterableKeyValueView {
57    pub fn coin(&self, utxo_id: &UtxoId) -> StorageResult<CompressedCoin> {
58        let coin = self
59            .storage_as_ref::<Coins>()
60            .get(utxo_id)?
61            .ok_or(not_found!(Coins))?
62            .into_owned();
63
64        Ok(coin)
65    }
66}