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
use crate::{
    compound_types::LimitedString,
    types::{
        LedgerKeyAccount, LedgerKeyClaimableBalance, LedgerKeyData, LedgerKeyLiquidityPool,
        LedgerKeyOffer, LedgerKeyTrustLine,
    },
    Asset, IntoAccountId, IntoClaimbleBalanceId, IntoHash, LedgerKey, StellarSdkError,
};

impl LedgerKey {
    pub fn from_account_id<T: IntoAccountId>(account_id: T) -> Result<Self, StellarSdkError> {
        let account_id = account_id.into_account_id()?;
        Ok(Self::Account(LedgerKeyAccount { account_id }))
    }

    pub fn from_trustline<T: IntoAccountId>(
        account_id: T,
        asset: Asset,
    ) -> Result<Self, StellarSdkError> {
        let account_id = account_id.into_account_id()?;
        Ok(Self::Trustline(LedgerKeyTrustLine { account_id, asset }))
    }

    pub fn from_offer<T: IntoAccountId>(
        seller_id: T,
        offer_id: i64,
    ) -> Result<Self, StellarSdkError> {
        let seller_id = seller_id.into_account_id()?;
        Ok(Self::Offer(LedgerKeyOffer {
            seller_id,
            offer_id,
        }))
    }

    pub fn from_data<T: IntoAccountId, S: AsRef<[u8]>>(
        account_id: T,
        data_name: S,
    ) -> Result<Self, StellarSdkError> {
        let account_id = account_id.into_account_id()?;
        let data_name = LimitedString::new(data_name.as_ref().to_vec())?;
        Ok(Self::Data(LedgerKeyData {
            account_id,
            data_name,
        }))
    }

    pub fn from_claimable_balance_id<T: IntoClaimbleBalanceId>(
        balance_id: T,
    ) -> Result<Self, StellarSdkError> {
        let balance_id = balance_id.into_claimable_balance_id()?;
        Ok(Self::ClaimableBalance(LedgerKeyClaimableBalance {
            balance_id,
        }))
    }

    pub fn from_liquidity_pool_id<T: IntoHash>(
        liquidity_pool_id: T,
    ) -> Result<Self, StellarSdkError> {
        let liquidity_pool_id = liquidity_pool_id.into_hash()?;
        Ok(Self::LiquidityPool(LedgerKeyLiquidityPool {
            liquidity_pool_id,
        }))
    }
}