Skip to content

Commit

Permalink
address the BTreeMap comment
Browse files Browse the repository at this point in the history
  • Loading branch information
freddyli7 committed Oct 11, 2023
1 parent 0b90681 commit 44441e6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
14 changes: 5 additions & 9 deletions bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -87,7 +88,7 @@ pub mod pallet {

/// Bridge transfer reserve accounts mapping with designated assets
#[pallet::constant]
type TransferReserveAccounts: Get<Vec<(Self::AccountId, Vec<AssetId>)>>;
type TransferReserveAccounts: Get<BTreeMap<AssetId, Self::AccountId>>;

/// EIP712 Verifying contract address
/// This is used in EIP712 typed data domain
Expand Down Expand Up @@ -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<usize> = 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
Expand Down
11 changes: 10 additions & 1 deletion bridge/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -207,13 +208,21 @@ impl sygma_percentage_feehandler::Config for Runtime {
type WeightInfo = sygma_percentage_feehandler::weights::SygmaWeightInfo<Runtime>;
}

fn bridge_accounts_generator() -> BTreeMap<XcmAssetId, AccountId32> {
let mut account_map: BTreeMap<XcmAssetId, AccountId32> = 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<XcmAssetId>)> = vec![(BridgeAccountNative::get(), vec![NativeLocation::get().into()]), (BridgeAccountOtherTokens::get(), vec![UsdtLocation::get().into(), AstrLocation::get().into()])];
pub BridgeAccounts: BTreeMap<XcmAssetId, AccountId32> = bridge_accounts_generator();
pub CheckingAccount: AccountId32 = AccountId32::new([102u8; 32]);
pub RelayNetwork: NetworkId = NetworkId::Polkadot;
pub AssetsPalletLocation: MultiLocation =
Expand Down
12 changes: 11 additions & 1 deletion substrate-node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<XcmAssetId, AccountId32> {
let mut account_map: BTreeMap<XcmAssetId, AccountId32> = 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
Expand All @@ -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<XcmAssetId>)> = vec![(BridgeAccountNative::get(), vec![NativeLocation::get().into()]), (BridgeAccountOtherToken::get(), vec![UsdtLocation::get().into(), ERC20TSTLocation::get().into(), ERC20TSTD20Location::get().into()])];
pub BridgeAccounts: BTreeMap<XcmAssetId, AccountId32> = 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
Expand Down

0 comments on commit 44441e6

Please sign in to comment.