diff --git a/bridge/src/lib.rs b/bridge/src/lib.rs index 6891a8e..78eadb4 100644 --- a/bridge/src/lib.rs +++ b/bridge/src/lib.rs @@ -47,6 +47,7 @@ pub mod pallet { use xcm_executor::traits::TransactAsset; use crate::eip712; + use sp_std::collections::btree_map::BTreeMap; use sygma_traits::{ ChainID, DecimalConverter, DepositNonce, DomainID, ExtractDestinationData, FeeHandler, MpcAddress, ResourceId, TransferType, VerifyingContractAddress, @@ -87,7 +88,7 @@ pub mod pallet { /// Bridge transfer reserve accounts mapping with designated assets #[pallet::constant] - type TransferReserveAccounts: Get)>>; + type TransferReserveAccounts: Get>; /// EIP712 Verifying contract address /// This is used in EIP712 typed data domain @@ -708,14 +709,9 @@ pub mod pallet { /// Return the TokenReservedAccount address by the given token pub fn get_token_reserved_account(token_id: &AssetId) -> Option<[u8; 32]> { - let idx = T::TransferReserveAccounts::get().iter().position(|a| { - let i: Option = a.1.iter().position(|b| b == token_id); - if i.is_some() { - return true; - } - false - })?; - Some(T::TransferReserveAccounts::get()[idx].0.clone().into()) + T::TransferReserveAccounts::get() + .get(token_id) + .map(|account| (*account).clone().into()) } /// convert the ECDSA 64-byte uncompressed pubkey to H160 address diff --git a/bridge/src/mock.rs b/bridge/src/mock.rs index 58095db..9163a53 100644 --- a/bridge/src/mock.rs +++ b/bridge/src/mock.rs @@ -18,6 +18,7 @@ use sp_runtime::{ traits::{AccountIdConversion, BlakeTwo256, IdentityLookup}, AccountId32, Perbill, }; +use sp_std::collections::btree_map::BTreeMap; use sp_std::{borrow::Borrow, marker::PhantomData, prelude::*, result}; use sygma_traits::{ ChainID, DecimalConverter, DomainID, ExtractDestinationData, ResourceId, @@ -207,13 +208,21 @@ impl sygma_percentage_feehandler::Config for Runtime { type WeightInfo = sygma_percentage_feehandler::weights::SygmaWeightInfo; } +fn bridge_accounts_generator() -> BTreeMap { + let mut account_map: BTreeMap = BTreeMap::new(); + account_map.insert(NativeLocation::get().into(), BridgeAccountNative::get()); + account_map.insert(UsdtLocation::get().into(), BridgeAccountOtherTokens::get()); + account_map.insert(AstrLocation::get().into(), BridgeAccountOtherTokens::get()); + account_map +} + parameter_types! { pub TreasuryAccount: AccountId32 = AccountId32::new([100u8; 32]); pub EIP712ChainID: ChainID = primitive_types::U256([1u64; 4]); pub DestVerifyingContractAddress: VerifyingContractAddress = primitive_types::H160([1u8; 20]); pub BridgeAccountNative: AccountId32 = SygmaBridgePalletId::get().into_account_truncating(); pub BridgeAccountOtherTokens: AccountId32 = SygmaBridgePalletId::get().into_sub_account_truncating(1u32); - pub BridgeAccounts: Vec<(AccountId32, Vec)> = vec![(BridgeAccountNative::get(), vec![NativeLocation::get().into()]), (BridgeAccountOtherTokens::get(), vec![UsdtLocation::get().into(), AstrLocation::get().into()])]; + pub BridgeAccounts: BTreeMap = bridge_accounts_generator(); pub CheckingAccount: AccountId32 = AccountId32::new([102u8; 32]); pub RelayNetwork: NetworkId = NetworkId::Polkadot; pub AssetsPalletLocation: MultiLocation = diff --git a/substrate-node/runtime/src/lib.rs b/substrate-node/runtime/src/lib.rs index 6eb6b86..44b60e7 100644 --- a/substrate-node/runtime/src/lib.rs +++ b/substrate-node/runtime/src/lib.rs @@ -26,6 +26,7 @@ use sp_runtime::{ transaction_validity::{TransactionSource, TransactionValidity}, AccountId32, ApplyExtrinsicResult, MultiSignature, Perbill, }; +use sp_std::collections::btree_map::BTreeMap; use sp_std::{borrow::Borrow, marker::PhantomData, prelude::*, result, vec::Vec}; #[cfg(feature = "std")] use sp_version::NativeVersion; @@ -394,6 +395,15 @@ impl sygma_fee_handler_router::Config for Runtime { // This address is defined in the substrate E2E test of sygma-relayer const DEST_VERIFYING_CONTRACT_ADDRESS: &str = "6CdE2Cd82a4F8B74693Ff5e194c19CA08c2d1c68"; +fn bridge_accounts_generator() -> BTreeMap { + let mut account_map: BTreeMap = BTreeMap::new(); + account_map.insert(NativeLocation::get().into(), BridgeAccountNative::get()); + account_map.insert(UsdtLocation::get().into(), BridgeAccountOtherToken::get()); + account_map.insert(ERC20TSTLocation::get().into(), BridgeAccountOtherToken::get()); + account_map.insert(ERC20TSTD20Location::get().into(), BridgeAccountOtherToken::get()); + account_map +} + parameter_types! { // TreasuryAccount is an substrate account and currently used for substrate -> EVM bridging fee collection // TreasuryAccount address: 5ELLU7ibt5ZrNEYRwohtaRBDBa3TzcWwwPELBPSWWd2mbgv3 @@ -403,7 +413,7 @@ parameter_types! { // BridgeAccountOtherToken 5EYCAe5jLbHcAAMKvLFiGhk3htXY8jQncbLTDGJQnpnPMAVp pub BridgeAccountOtherToken: AccountId32 = SygmaBridgePalletId::get().into_sub_account_truncating(1u32); // BridgeAccounts is a list of accounts for holding transferred asset collection - pub BridgeAccounts: Vec<(AccountId32, Vec)> = vec![(BridgeAccountNative::get(), vec![NativeLocation::get().into()]), (BridgeAccountOtherToken::get(), vec![UsdtLocation::get().into(), ERC20TSTLocation::get().into(), ERC20TSTD20Location::get().into()])]; + pub BridgeAccounts: BTreeMap = bridge_accounts_generator(); // EIP712ChainID is the chainID that pallet is assigned with, used in EIP712 typed data domain pub EIP712ChainID: ChainID = U256::from(5); // DestVerifyingContractAddress is a H160 address that is used in proposal signature verification, specifically EIP712 typed data