Skip to content

Commit

Permalink
update query
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo committed Sep 16, 2023
1 parent 6c766ad commit 1525cd5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
8 changes: 5 additions & 3 deletions contracts/identity/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,16 @@ mod identity {
}
}

/// A list of all the available chains each associated with a `ChainId`.
/// A list of all the available chains each associated with the associated
/// `ChainId`.
#[ink(message)]
pub fn available_chains(&self) -> Vec<(ChainId, ChainInfo)> {
pub fn available_chains(&self, network: Network) -> Vec<(u32, ChainInfo)> {
self.chain_ids
.clone()
.into_iter()
.map(|id| (id.clone(), self.chain_info_of(id)))
.filter_map(|(id, maybe_chain)| maybe_chain.map(|info| (id, info)))
.filter(|(id, _)| id.1 == network)
.filter_map(|(id, maybe_chain)| maybe_chain.map(|info| (id.0, info)))
.collect()
}

Expand Down
42 changes: 34 additions & 8 deletions contracts/identity/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ fn constructor_works() {
assert_eq!(identity.latest_identity_no, 0);
assert_eq!(identity.admin, alice);
assert_eq!(identity.chain_ids, vec![]);
assert_eq!(identity.available_chains(), Vec::default());
assert_eq!(identity.available_chains(Polkadot), Vec::default());
assert_eq!(identity.available_chains(Kusama), Vec::default());
}

#[ink::test]
Expand Down Expand Up @@ -334,7 +335,7 @@ fn add_chain_works() {

// Check storage items updated
assert_eq!(identity.chain_info_of.get(chain_id.clone()), Some(info.clone()));
assert_eq!(identity.available_chains(), vec![(chain_id, info)]);
assert_eq!(identity.available_chains(Kusama), vec![(chain_id.0, info)]);
assert_eq!(identity.chain_ids, vec![(0, Kusama)]);

// Only the contract creator can add a new chain
Expand Down Expand Up @@ -374,7 +375,7 @@ fn remove_chain_works() {

assert!(identity.chain_info_of.get(chain_id.clone()).is_none());

assert!(identity.available_chains().is_empty());
assert!(identity.available_chains(Kusama).is_empty());

// Check emitted events
let last_event = recorded_events().last().unwrap();
Expand Down Expand Up @@ -584,16 +585,41 @@ fn init_with_chains_works() {

assert_eq!(identity.chain_ids, chain_ids);
assert_eq!(
identity.available_chains(),
identity.available_chains(Polkadot),
vec![
((0, Polkadot), ChainInfo { account_type: AccountId32 }),
((2000, Polkadot), ChainInfo { account_type: AccountId32 }),
((2004, Polkadot), ChainInfo { account_type: AccountKey20 }),
((2006, Polkadot), ChainInfo { account_type: AccountId32 })
(0, ChainInfo { account_type: AccountId32 }),
(2000, ChainInfo { account_type: AccountId32 }),
(2004, ChainInfo { account_type: AccountKey20 }),
(2006, ChainInfo { account_type: AccountId32 })
]
);
}

#[ink::test]
fn available_chains_works() {
let chains = vec![
ChainInfo { account_type: AccountId32 },
ChainInfo { account_type: AccountId32 },
ChainInfo { account_type: AccountKey20 },
ChainInfo { account_type: AccountId32 },
];
let chain_ids = vec![(0, Polkadot), (2000, Polkadot), (2004, Polkadot), (2006, Kusama)];
let identity = Identity::init_with_chains(chains, chain_ids);

assert_eq!(
identity.available_chains(Polkadot),
vec![
(0, ChainInfo { account_type: AccountId32 }),
(2000, ChainInfo { account_type: AccountId32 }),
(2004, ChainInfo { account_type: AccountKey20 }),
]
);
assert_eq!(
identity.available_chains(Kusama),
vec![(2006, ChainInfo { account_type: AccountId32 })]
);
}

#[ink::test]
fn getting_transaction_destination_works() {
let DefaultAccounts::<DefaultEnvironment> { alice, .. } = get_default_accounts();
Expand Down

0 comments on commit 1525cd5

Please sign in to comment.