Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: recover tokens #1964

Merged
merged 8 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions pallets/liquidity-pools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,44 @@ pub mod pallet {

Ok(())
}

/// Initiate the recovery of assets which were sent to an incorrect
/// contract by the account represented by `domain_address`.
///
/// NOTE: Asset and contract addresses in 32 bytes in order to support
/// future non-EVM chains.
///
/// Origin: Root.
#[pallet::call_index(17)]
#[pallet::weight(T::WeightInfo::update_tranche_hook())]
pub fn recover_assets(
origin: OriginFor<T>,
domain_address: DomainAddress,
incorrect_contract: [u8; 32],
asset: [u8; 32],
// NOTE: Solidity balance is `U256` per default
amount: sp_core::U256,
lemunozm marked this conversation as resolved.
Show resolved Hide resolved
) -> DispatchResult {
ensure_root(origin)?;

ensure!(
matches!(domain_address.domain(), Domain::EVM(_)),
Error::<T>::InvalidDomain
);

T::OutboundMessageHandler::handle(
T::TreasuryAccount::get(),
domain_address.domain(),
Message::RecoverAssets {
contract: incorrect_contract,
asset,
recipient: T::DomainAddressToAccountId::convert(domain_address).into(),
amount,
},
)?;

Ok(())
}
}

impl<T: Config> Pallet<T> {
Expand Down
13 changes: 13 additions & 0 deletions pallets/liquidity-pools/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,19 @@ mod tests {
)
}

#[test]
fn recover_assets() {
test_encode_decode_identity(
Message::RecoverAssets {
contract: default_address_32(),
asset: default_address_32(),
recipient: default_address_32(),
amount: Default::default(),
},
"0de.....",
)
}

#[test]
fn batch_empty() {
test_encode_decode_identity(Message::Batch(BatchMessages::default()), concat!("04"))
Expand Down
2 changes: 1 addition & 1 deletion pallets/liquidity-pools/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub const CHAIN_ID: u64 = 1;
pub const ALICE_32: [u8; 32] = [2; 32];
pub const ALICE: AccountId = AccountId::new(ALICE_32);
pub const ALICE_ETH: [u8; 20] = [2; 20];
pub const ALICE_EVM_DOMAIN_ADDRESS: DomainAddress = DomainAddress::EVM(42, ALICE_ETH);
pub const ALICE_EVM_DOMAIN_ADDRESS: DomainAddress = DomainAddress::EVM(CHAIN_ID, ALICE_ETH);
wischli marked this conversation as resolved.
Show resolved Hide resolved
// TODO(future): Can be removed after domain conversion refactor
pub const ALICE_EVM_LOCAL_ACCOUNT: AccountId = {
let mut arr = [0u8; 32];
Expand Down
99 changes: 99 additions & 0 deletions pallets/liquidity-pools/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2008,3 +2008,102 @@ mod update_tranche_hook {
}
}
}

mod recover_assets {
use super::*;

const CONTRACT: [u8; 32] = [42; 32];
const ASSET: [u8; 32] = [43; 32];

fn config_mocks() {
DomainAddressToAccountId::mock_convert(move |_| DOMAIN_HOOK_ADDRESS_32.into());
Permissions::mock_has(|_, _, _| false);
Gateway::mock_handle(|sender, destination, msg| {
assert_eq!(sender, TreasuryAccount::get());
assert_eq!(destination, EVM_DOMAIN);
assert_eq!(
msg,
Message::RecoverAssets {
contract: CONTRACT,
asset: ASSET,
recipient: ALICE_EVM_LOCAL_ACCOUNT.into(),
amount: AMOUNT.into(),
}
);
Ok(())
});
}

#[test]
fn success() {
System::externalities().execute_with(|| {
config_mocks();

assert_ok!(LiquidityPools::recover_assets(
RuntimeOrigin::root(),
ALICE_EVM_DOMAIN_ADDRESS,
CONTRACT,
ASSET,
AMOUNT.into(),
));
});
}

mod erroring_out {
use super::*;

#[test]
fn with_wrong_origin_none() {
System::externalities().execute_with(|| {
config_mocks();

assert_noop!(
LiquidityPools::recover_assets(
RuntimeOrigin::none(),
ALICE_EVM_DOMAIN_ADDRESS,
CONTRACT,
ASSET,
AMOUNT.into(),
),
DispatchError::BadOrigin
);
});
}

#[test]
fn with_wrong_origin_signed() {
System::externalities().execute_with(|| {
config_mocks();

assert_noop!(
LiquidityPools::recover_assets(
RuntimeOrigin::signed(ALICE.into()),
ALICE_EVM_DOMAIN_ADDRESS,
CONTRACT,
ASSET,
AMOUNT.into(),
),
DispatchError::BadOrigin
);
});
}
wischli marked this conversation as resolved.
Show resolved Hide resolved

#[test]
fn with_wrong_domain() {
System::externalities().execute_with(|| {
config_mocks();

assert_noop!(
LiquidityPools::recover_assets(
RuntimeOrigin::root(),
DomainAddress::Centrifuge(ALICE.into()),
CONTRACT,
ASSET,
AMOUNT.into(),
),
Error::<Runtime>::InvalidDomain
);
});
}
}
}
6 changes: 3 additions & 3 deletions pallets/liquidity-pools/src/tests/inbound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ mod handle_tranche_tokens_transfer {
AMOUNT,
)
.unwrap();
let origin = EVM_DOMAIN_ADDRESS.domain().into_account();
assert_eq!(Tokens::balance(TRANCHE_CURRENCY, &origin), AMOUNT);
wischli marked this conversation as resolved.
Show resolved Hide resolved

assert_ok!(LiquidityPools::handle(
EVM_DOMAIN_ADDRESS,
Expand All @@ -181,10 +183,8 @@ mod handle_tranche_tokens_transfer {
}
));

let origin = EVM_DOMAIN_ADDRESS.domain().into_account();
assert_eq!(Tokens::balance(TRANCHE_CURRENCY, &origin), 0);

wischli marked this conversation as resolved.
Show resolved Hide resolved
let destination = ALICE_EVM_DOMAIN_ADDRESS.domain().into_account();
assert_eq!(destination, origin);
assert_eq!(Tokens::balance(TRANCHE_CURRENCY, &destination), AMOUNT);
});
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/integration-tests/submodules/liquidity-pools
Submodule liquidity-pools updated 1 files
+1 −1 README.md