Skip to content

Commit

Permalink
gateway account as normal account
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Aug 19, 2024
1 parent c55e74f commit 19cd56f
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 59 deletions.
6 changes: 3 additions & 3 deletions libs/types/src/domain_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::EVMChainId;
const MAX_ADDRESS_SIZE: usize = 32;

/// By just clamping the value to a smaller address
pub fn account_to_eth_address(address: AccountId32) -> H160 {
pub fn truncate_into_eth_address(address: AccountId32) -> H160 {
let bytes: [u8; 32] = address.into();
H160::from(
*(bytes)
Expand Down Expand Up @@ -99,7 +99,7 @@ impl DomainAddress {
match domain {
Domain::Centrifuge => DomainAddress::Centrifuge(address.into()),
Domain::Evm(chain_id) => {
DomainAddress::Evm(chain_id, account_to_eth_address(address.into()))
DomainAddress::Evm(chain_id, truncate_into_eth_address(address.into()))
}
}
}
Expand All @@ -122,7 +122,7 @@ impl DomainAddress {
/// clamping the inner address if needed.
pub fn h160(&self) -> H160 {
match self.clone() {
Self::Centrifuge(x) => account_to_eth_address(x),
Self::Centrifuge(x) => truncate_into_eth_address(x),
Self::Evm(_, x) => x,
}
}
Expand Down
11 changes: 7 additions & 4 deletions pallets/axelar-router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ use cfg_traits::{
liquidity_pools::{MessageReceiver, MessageSender},
PreConditions,
};
use cfg_types::{domain_address::DomainAddress, EVMChainId};
use cfg_types::{
domain_address::{truncate_into_eth_address, DomainAddress},
EVMChainId,
};
use ethabi::{Contract, Function, Param, ParamType, Token};
use fp_evm::PrecompileHandle;
use frame_support::{
Expand All @@ -32,7 +35,7 @@ use frame_system::pallet_prelude::*;
pub use pallet::*;
use precompile_utils::prelude::*;
use scale_info::prelude::{format, string::String};
use sp_core::{H160, H256, U256};
use sp_core::{crypto::AccountId32, H160, H256, U256};
use sp_std::{boxed::Box, collections::btree_map::BTreeMap, vec, vec::Vec};

#[cfg(test)]
Expand Down Expand Up @@ -318,7 +321,7 @@ pub mod pallet {
impl<T: Config> MessageSender for Pallet<T> {
type Message = Vec<u8>;
type Middleware = AxelarId;
type Origin = DomainAddress;
type Origin = AccountId32;

fn send(
axelar_id: AxelarId,
Expand All @@ -340,7 +343,7 @@ pub mod pallet {
.map_err(DispatchError::Other)?;

T::Transactor::call(
origin.h160(),
truncate_into_eth_address(origin),
evm_config.target_contract_address,
message.as_slice(),
evm_config.fee_values.value,
Expand Down
4 changes: 2 additions & 2 deletions pallets/axelar-router/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const LP_CONTRACT_ADDRESS: H160 = H160::repeat_byte(1);
const AXELAR_CONTRACT_ADDRESS: H160 = H160::repeat_byte(2);
const SOURCE_ADDRESS: H160 = H160::repeat_byte(3);
const AXELAR_CONTRACT_HASH: H256 = H256::repeat_byte(42);
const SENDER: DomainAddress = DomainAddress::Centrifuge(AccountId32::new([0; 32]));
const SENDER: AccountId32 = AccountId32::new([0; 32]);
const MESSAGE: &[u8] = &[1, 2, 3];
const FEE_VALUE: U256 = U256::zero();
const GAS_LIMIT: U256 = U256::one();
Expand Down Expand Up @@ -91,7 +91,7 @@ mod send {
correct_configuration();

Transactor::mock_call(move |from, to, data, value, gas_price, gas_limit| {
assert_eq!(from, SENDER.h160());
assert_eq!(from, truncate_into_eth_address(SENDER));
assert_eq!(to, AXELAR_CONTRACT_ADDRESS);
assert_eq!(data, &wrap_message(MESSAGE.to_vec()));
assert_eq!(value, FEE_VALUE);
Expand Down
10 changes: 3 additions & 7 deletions pallets/liquidity-pools-forwarder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,7 @@ pub mod pallet {
+ FullCodec;

/// The entity of the messages coming from this chain.
type MessageSender: MessageSender<
Middleware = Self::RouterId,
Origin = DomainAddress,
Message = Self::Message,
>;
type MessageSender: MessageSender<Middleware = Self::RouterId, Message = Self::Message>;

/// The entity which acts on unwrapped messages.
type MessageReceiver: MessageReceiver<
Expand Down Expand Up @@ -202,11 +198,11 @@ pub mod pallet {
impl<T: Config> MessageSender for Pallet<T> {
type Message = T::Message;
type Middleware = T::RouterId;
type Origin = DomainAddress;
type Origin = <T::MessageSender as MessageSender>::Origin;

fn send(
router_id: T::RouterId,
origin: DomainAddress,
origin: Self::Origin,
message: T::Message,
) -> DispatchResult {
let msg = RouterForwarding::<T>::get(&router_id)
Expand Down
4 changes: 2 additions & 2 deletions pallets/liquidity-pools-gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub mod pallet {
/// The target of the messages coming from this chain
type MessageSender: MessageSender<
Middleware = Self::RouterId,
Origin = DomainAddress,
Origin = Self::AccountId,
Message = Self::Message,
>;

Expand All @@ -122,7 +122,7 @@ pub mod pallet {
/// The sender account that will be used in the OutboundQueue
/// implementation.
#[pallet::constant]
type Sender: Get<DomainAddress>;
type Sender: Get<Self::AccountId>;

/// Type used for queueing messages.
type MessageQueue: MessageQueue<Message = GatewayMessage<Self::Message, Self::RouterId>>;
Expand Down
4 changes: 2 additions & 2 deletions pallets/liquidity-pools-gateway/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ impl cfg_mocks::queue::pallet::Config for Runtime {
impl cfg_mocks::router_message::pallet::Config for Runtime {
type Message = Message;
type Middleware = RouterId;
type Origin = DomainAddress;
type Origin = AccountId32;
}

frame_support::parameter_types! {
pub Sender: DomainAddress = DomainAddress::Centrifuge(AccountId32::from([1; 32]));
pub const Sender: AccountId32 = AccountId32::new([1; 32]);
pub const MaxIncomingMessageSize: u32 = 1024;
pub const LpAdminAccount: AccountId32 = LP_ADMIN_ACCOUNT;
pub const MaxRouterCount: u32 = 8;
Expand Down
3 changes: 1 addition & 2 deletions runtime/altair/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use cfg_primitives::{
};
use cfg_traits::{investments::OrderManager, Millis, PoolUpdateGuard, Seconds};
use cfg_types::{
domain_address::DomainAddress,
fee_keys::{Fee, FeeKey},
fixed_point::{Quantity, Rate, Ratio},
investments::InvestmentPortfolio,
Expand Down Expand Up @@ -1767,7 +1766,7 @@ impl pallet_liquidity_pools_forwarder::Config for Runtime {
}

parameter_types! {
pub Sender: DomainAddress = gateway::get_gateway_domain_address::<Runtime>();
pub Sender: AccountId = gateway::get_gateway_account::<Runtime>();
pub const MaxIncomingMessageSize: u32 = 1024;
pub const MaxRouterCount: u32 = 8;
}
Expand Down
3 changes: 1 addition & 2 deletions runtime/centrifuge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ use cfg_traits::{
Seconds,
};
use cfg_types::{
domain_address::DomainAddress,
fee_keys::{Fee, FeeKey},
fixed_point::{Quantity, Rate, Ratio},
investments::InvestmentPortfolio,
Expand Down Expand Up @@ -1841,7 +1840,7 @@ impl pallet_liquidity_pools::Config for Runtime {
}

parameter_types! {
pub Sender: DomainAddress = gateway::get_gateway_domain_address::<Runtime>();
pub Sender: AccountId = gateway::get_gateway_account::<Runtime>();
pub const MaxIncomingMessageSize: u32 = 1024;
pub const MaxRouterCount: u32 = 8;
}
Expand Down
15 changes: 8 additions & 7 deletions runtime/common/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

use cfg_types::domain_address::{Domain, DomainAddress};
use cfg_primitives::AccountId;
use cfg_types::domain_address::truncate_into_eth_address;
use polkadot_parachain_primitives::primitives::Sibling;
use sp_core::crypto::AccountId32;
use sp_runtime::traits::{AccountIdConversion, Get};
use sp_runtime::traits::AccountIdConversion;

pub fn get_gateway_domain_address<T>() -> DomainAddress
use crate::account_conversion::AccountConverter;

pub fn get_gateway_account<T>() -> AccountId
where
T: pallet_evm_chain_id::Config + staging_parachain_info::Config,
{
let chain_id = pallet_evm_chain_id::Pallet::<T>::get();
let para_id = staging_parachain_info::Pallet::<T>::parachain_id();
let sender_account: AccountId32 = Sibling::from(para_id).into_account_truncating();
let sender_account: AccountId = Sibling::from(para_id).into_account_truncating();

DomainAddress::new(Domain::Evm(chain_id), sender_account.into())
AccountConverter::evm_address_to_account::<T>(truncate_into_eth_address(sender_account))
}
7 changes: 4 additions & 3 deletions runtime/common/src/routing.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use cfg_primitives::AccountId;
use cfg_traits::{
liquidity_pools::{LpMessageSerializer, MessageReceiver, MessageSender, RouterProvider},
PreConditions,
Expand Down Expand Up @@ -60,7 +61,7 @@ where
{
type Message = Vec<u8>;
type Middleware = RouterId;
type Origin = DomainAddress;
type Origin = AccountId;

fn send(router_id: RouterId, origin: Self::Origin, message: Self::Message) -> DispatchResult {
match router_id {
Expand All @@ -87,11 +88,11 @@ pub struct MessageSerializer<Sender, Receiver>(PhantomData<(Sender, Receiver)>);

impl<Sender, Receiver> MessageSender for MessageSerializer<Sender, Receiver>
where
Sender: MessageSender<Message = Vec<u8>, Middleware = RouterId, Origin = DomainAddress>,
Sender: MessageSender<Message = Vec<u8>, Middleware = RouterId, Origin = AccountId>,
{
type Message = Message;
type Middleware = RouterId;
type Origin = DomainAddress;
type Origin = AccountId;

fn send(
middleware: Self::Middleware,
Expand Down
3 changes: 1 addition & 2 deletions runtime/development/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ use cfg_traits::{
Seconds,
};
use cfg_types::{
domain_address::DomainAddress,
fee_keys::{Fee, FeeKey},
fixed_point::{Quantity, Rate, Ratio},
investments::InvestmentPortfolio,
Expand Down Expand Up @@ -1872,7 +1871,7 @@ impl pallet_liquidity_pools_forwarder::Config for Runtime {
}

parameter_types! {
pub Sender: DomainAddress = gateway::get_gateway_domain_address::<Runtime>();
pub Sender: AccountId = gateway::get_gateway_account::<Runtime>();
pub const MaxIncomingMessageSize: u32 = 1024;
pub const MaxRouterCount: u32 = 8;
}
Expand Down
17 changes: 1 addition & 16 deletions runtime/integration-tests/src/cases/foreign_investments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use pallet_pool_system::tranches::{TrancheInput, TrancheLoc, TrancheType};
use runtime_common::{
foreign_investments::IdentityPoolCurrencyConverter, routing::RouterId, xcm::general_key,
};
use sp_core::{Get, H160};
use sp_core::H160;
use sp_runtime::{
traits::{AccountIdConversion, EnsureAdd, One, Zero},
BoundedVec, DispatchError, FixedPointNumber, Perquintill, SaturatedConversion,
Expand All @@ -49,13 +49,6 @@ use crate::{
utils::{accounts::Keyring, genesis, genesis::Genesis, orml_asset_registry},
};

// ------------------
// NOTE
// This file only contains foreign investments tests, but the name must remain
// as it is until feature lpv2 is merged to avoid conflicts:
// (https://github.com/centrifuge/centrifuge-chain/pull/1909)
// ------------------

/// The AUSD asset id
pub const AUSD_CURRENCY_ID: CurrencyId = CurrencyId::ForeignAsset(3);
/// The USDT asset id
Expand Down Expand Up @@ -277,14 +270,6 @@ pub mod utils {
register_ausd::<T>();
register_glmr::<T>();

assert_ok!(orml_tokens::Pallet::<T>::set_balance(
<T as frame_system::Config>::RuntimeOrigin::root(),
T::Sender::get().account().into(),
GLMR_CURRENCY_ID,
DEFAULT_BALANCE_GLMR,
0,
));

assert_ok!(pallet_liquidity_pools_gateway::Pallet::<T>::set_routers(
<T as frame_system::Config>::RuntimeOrigin::root(),
BoundedVec::try_from(vec![DEFAULT_ROUTER_ID]).unwrap(),
Expand Down
2 changes: 1 addition & 1 deletion runtime/integration-tests/src/cases/lp/setup_lp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn setup<T: Runtime, F: FnOnce(&mut <RuntimeEnv<T> as EnvEvmExtension<T>>::E
evm.load_contracts();

// Fund gateway sender
give_balance::<T>(T::Sender::get().account(), DEFAULT_BALANCE * CFG);
give_balance::<T>(T::Sender::get(), DEFAULT_BALANCE * CFG);

// Register general local pool-currency
register_currency::<T>(LocalUSDC, |_| {});
Expand Down
4 changes: 2 additions & 2 deletions runtime/integration-tests/src/cases/lp/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use std::{cmp::min, fmt::Debug};

use cfg_primitives::{AccountId, Balance, TrancheId};
use cfg_types::domain_address::DomainAddress;
use cfg_types::domain_address::{truncate_into_eth_address, DomainAddress};
use ethabi::ethereum_types::{H160, H256, U256};
use fp_evm::CallInfo;
use frame_support::traits::{OriginTrait, PalletInfo};
Expand Down Expand Up @@ -92,7 +92,7 @@ pub fn verify_outbound_failure_on_lp<T: Runtime>(to: H160) {
.clone();

// The sender is the sender account on the gateway
assert_eq!(T::Sender::get().h160(), status.from);
assert_eq!(truncate_into_eth_address(T::Sender::get()), status.from);
assert_eq!(status.to.unwrap().0, to.0);
assert!(!receipt_ok(receipt));
assert!(matches!(
Expand Down
8 changes: 4 additions & 4 deletions runtime/integration-tests/src/cases/routers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cfg_primitives::Balance;
use cfg_traits::liquidity_pools::{LpMessageSerializer, MessageProcessor};
use cfg_types::{
domain_address::{Domain, DomainAddress},
domain_address::{truncate_into_eth_address, Domain, DomainAddress},
EVMChainId,
};
use ethabi::{Function, Param, ParamType, Token};
Expand All @@ -11,8 +11,8 @@ use pallet_axelar_router::{AxelarConfig, AxelarId, DomainConfig, EvmConfig, FeeV
use pallet_liquidity_pools::Message;
use pallet_liquidity_pools_gateway::message::GatewayMessage;
use runtime_common::{
account_conversion::AccountConverter, evm::precompile::LP_AXELAR_GATEWAY,
gateway::get_gateway_domain_address, routing::RouterId,
account_conversion::AccountConverter, evm::precompile::LP_AXELAR_GATEWAY, gateway,
routing::RouterId,
};
use sp_core::{H160, H256, U256};
use sp_runtime::traits::{BlakeTwo256, Hash};
Expand Down Expand Up @@ -123,7 +123,7 @@ mod axelar_evm {

utils::evm::mint_balance_into_derived_account::<T>(AXELAR_CONTRACT_ADDRESS, cfg(1));
utils::evm::mint_balance_into_derived_account::<T>(
get_gateway_domain_address::<T>().h160(),
truncate_into_eth_address(gateway::get_gateway_account::<T>()),
cfg(1),
);

Expand Down

0 comments on commit 19cd56f

Please sign in to comment.