abstract_sdk/base/features/
identification.rs

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
use abstract_std::{account::state::ACCOUNT_ID, registry::Account};
use cosmwasm_std::Deps;

use crate::std::objects::AccountId;
// see std::account::state::ADMIN
use crate::AbstractSdkResult;

/// Retrieve identifying information about an Account.
/// This includes the account and account_id.
pub trait AccountIdentification: Sized {
    /// Get the account address
    fn account(&self, deps: Deps) -> AbstractSdkResult<Account>;

    /// Get the Account id for the current account.
    fn account_id(&self, deps: Deps) -> AbstractSdkResult<AccountId> {
        ACCOUNT_ID
            .query(&deps.querier, self.account(deps)?.into_addr())
            .map_err(Into::into)
    }
}

#[cfg(test)]
mod test {
    #![allow(clippy::needless_borrows_for_generic_args)]
    use abstract_testing::prelude::*;
    use cosmwasm_std::testing::MockApi;

    use super::*;

    struct MockBinding {
        mock_api: MockApi,
    }

    impl AccountIdentification for MockBinding {
        fn account(&self, _deps: Deps) -> AbstractSdkResult<Account> {
            let account = test_account(self.mock_api);
            Ok(account)
        }
    }

    mod account {
        use cosmwasm_std::testing::mock_dependencies;

        use super::*;

        #[coverage_helper::test]
        fn test_account_address() {
            let deps = mock_dependencies();
            let binding = MockBinding { mock_api: deps.api };

            let account = test_account(deps.api);

            let res = binding.account(deps.as_ref());
            assert_eq!(res, Ok(account));
        }

        #[coverage_helper::test]
        fn account_id() {
            let mut deps = mock_dependencies();
            let account = test_account(deps.api);

            deps.querier = MockQuerierBuilder::default()
                .with_contract_item(account.addr(), ACCOUNT_ID, &TEST_ACCOUNT_ID)
                .build();

            let binding = MockBinding { mock_api: deps.api };
            assert_eq!(binding.account_id(deps.as_ref()), Ok(TEST_ACCOUNT_ID));
        }
    }
}