Skip to content

Commit

Permalink
Merge pull request #5158 from kdembler/argo-revert
Browse files Browse the repository at this point in the history
add Argo revert_outbound_transfer extrinsic
  • Loading branch information
kdembler committed Jun 4, 2024
2 parents 9d0f3b7 + 137e386 commit e193904
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
2 changes: 2 additions & 0 deletions runtime-modules/argo-bridge/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use frame_support::decl_event;

use crate::{RemoteAccount, RemoteTransfer, TransferId};
use sp_std::vec::Vec;

use crate::types::*;

Expand All @@ -19,6 +20,7 @@ decl_event!(
{
OutboundTransferRequested(TransferId, AccountId, RemoteAccount, Balance, Balance),
InboundTransferFinalized(RemoteTransfer, AccountId, Balance),
OutboundTransferReverted(TransferId, AccountId, Balance, Vec<u8>),
BridgePaused(AccountId),
BridgeThawnStarted(AccountId, BlockNumber),
BridgeThawnFinished(),
Expand Down
27 changes: 27 additions & 0 deletions runtime-modules/argo-bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,33 @@ decl_module! {
Ok(())
}

// TODO: add weight for revert_outbound_transfer
#[weight = WeightInfoArgo::<T>::finalize_inbound_transfer()]
pub fn revert_outbound_transfer(
origin,
transfer_id: TransferId,
revert_account: T::AccountId,
revert_amount: BalanceOf<T>,
rationale: vec::Vec<u8>,
) -> DispatchResult {
ensure!(Self::operator_account().is_some(), Error::<T>::OperatorAccountNotSet);
let caller = ensure_signed(origin)?;
ensure!(caller == Self::operator_account().unwrap(), Error::<T>::NotOperatorAccount);

ensure!(Self::status() == BridgeStatus::Active, Error::<T>::BridgeNotActive);
ensure!(revert_amount <= Self::mint_allowance(), Error::<T>::InsufficientBridgeMintAllowance);

<MintAllowance<T>>::put(Self::mint_allowance() - revert_amount);
let _ = balances::Pallet::<T>::deposit_creating(
&revert_account,
revert_amount
);

Self::deposit_event(RawEvent::OutboundTransferReverted(transfer_id, revert_account, revert_amount, rationale));

Ok(())
}

#[weight = WeightInfoArgo::<T>::pause_bridge()]
pub fn pause_bridge(origin) -> DispatchResult {
let caller = ensure_signed(origin)?;
Expand Down
103 changes: 103 additions & 0 deletions runtime-modules/argo-bridge/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,109 @@ fn finalize_inbound_transfer_with_insufficient_bridge_mint() {
});
}

#[test]
fn revert_outbound_transfer_success() {
with_test_externalities_custom_mint_allowance(joy!(1000), || {
let remote_chains = BoundedVec::try_from(vec![1u32]).unwrap();
let parameters = BridgeConstraints {
operator_account: Some(account!(1)),
pauser_accounts: None,
bridging_fee: None,
thawn_duration: None,
remote_chains: Some(remote_chains),
};
ArgoBridge::update_bridge_constrains(RuntimeOrigin::root(), parameters).unwrap();

let transfer_id = 1u64;
let revert_amount = joy!(123);
let revert_account = account!(2);
let rationale = "test".as_bytes().to_vec();
let result = ArgoBridge::revert_outbound_transfer(
RuntimeOrigin::signed(account!(1)),
transfer_id,
revert_account,
revert_amount,
rationale.clone(),
);
assert_ok!(result);
assert_eq!(Balances::free_balance(revert_account), revert_amount);
last_event_eq!(RawEvent::OutboundTransferReverted(
transfer_id,
revert_account,
revert_amount,
rationale,
));
});
}

#[test]
fn revert_outbound_transfer_with_no_operator_account() {
with_test_externalities(|| {
let result = ArgoBridge::revert_outbound_transfer(
RuntimeOrigin::signed(account!(1)),
1u64,
account!(2),
joy!(123),
vec![],
);
assert_err!(result, Error::<Test>::OperatorAccountNotSet);
});
}

#[test]
fn revert_outbound_transfer_with_unauthorized_account() {
with_test_externalities(|| {
let remote_chains = BoundedVec::try_from(vec![1u32]).unwrap();
let parameters = BridgeConstraints {
operator_account: Some(account!(1)),
pauser_accounts: None,
bridging_fee: None,
thawn_duration: None,
remote_chains: Some(remote_chains),
};
assert_ok!(ArgoBridge::update_bridge_constrains(
RuntimeOrigin::root(),
parameters
));

let result = ArgoBridge::revert_outbound_transfer(
RuntimeOrigin::signed(account!(2)),
1u64,
account!(2),
joy!(123),
vec![],
);
assert_err!(result, Error::<Test>::NotOperatorAccount);
});
}

#[test]
fn revert_outbound_transfer_with_insufficient_bridge_mint() {
with_test_externalities(|| {
let remote_chains = BoundedVec::try_from(vec![1u32]).unwrap();
let parameters = BridgeConstraints {
operator_account: Some(account!(1)),
pauser_accounts: None,
bridging_fee: None,
thawn_duration: None,
remote_chains: Some(remote_chains),
};
assert_ok!(ArgoBridge::update_bridge_constrains(
RuntimeOrigin::root(),
parameters
));

let result = ArgoBridge::revert_outbound_transfer(
RuntimeOrigin::signed(account!(1)),
1u64,
account!(2),
joy!(100),
vec![],
);
assert_err!(result, Error::<Test>::InsufficientBridgeMintAllowance);
});
}

#[test]
fn pause_bridge_success() {
with_test_externalities(|| {
Expand Down

0 comments on commit e193904

Please sign in to comment.