Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pallets/hsm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pallet-hsm"
version = "1.7.0"
version = "1.7.1"
edition = "2021"
description = "Hollar stability module"
authors = ["GalacticCouncil"]
Expand Down
5 changes: 3 additions & 2 deletions pallets/hsm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,12 +911,13 @@ pub mod pallet {
.write(Bytes(arb_data))
.build();

let call_result = T::Evm::call(context, data, U256::zero(), T::GasLimit::get());

let receiver_balance_initial = <T as crate::pallet::Config>::Currency::total_balance(
collateral_asset_id,
&T::ArbitrageProfitReceiver::get(),
);

let call_result = T::Evm::call(context, data, U256::zero(), T::GasLimit::get());

if call_result.exit_reason != ExitReason::Succeed(ExitSucceed::Returned) {
log::error!(target: "hsm", "Flash loan Hollar EVM execution failed - {:?}. Reason: {:?}", call_result.exit_reason, call_result.value);
return Err(T::EvmErrorDecoder::convert(call_result));
Expand Down
63 changes: 63 additions & 0 deletions pallets/hsm/src/tests/arb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,66 @@
assert_eq!(opportunity, Some(Arbitrage::HollarOut(78321364099875978581618)));
});
}

#[test]
fn execute_arbitrage_should_emit_event_profit_matching_receiver_balance_gain() {
let pool_id = 100u32;
ExtBuilder::default()
.with_endowed_accounts(vec![(ALICE, DAI, 1_000 * ONE)])
.with_registered_assets(vec![(DAI, 18), (HOLLAR, 18), (pool_id, 18)])
.with_pool(
pool_id,
vec![DAI, HOLLAR],
22,
Permill::from_percent(0),
vec![PegSource::Value((1, 1)), PegSource::Value((1, 1))],
)
.with_initial_pool_liquidity(
100,
vec![
AssetAmount {
asset_id: HOLLAR,
amount: 999_000 * ONE,
},
AssetAmount {
asset_id: DAI,
amount: 1_000_000 * ONE,
},
],
)
.with_collateral_buyback_limit(
DAI,
pool_id,
Permill::from_float(0.),
FixedU128::from_rational(99, 100),
Permill::from_float(0.),
Perbill::from_float(0.0001),
)
.build()
.execute_with(|| {
move_block();
let flash_minter: EvmAddress = hex!["8F3aC7f6482ABc1A5c48a95D97F7A235186dBb68"].into();
assert_ok!(HSM::set_flash_minter(RuntimeOrigin::root(), flash_minter,));

let opportunity = HSM::find_arbitrage_opportunity(DAI);

let receiver_balance_before = Tokens::free_balance(DAI, &HsmArbProfitReceiver::get());
assert_ok!(HSM::execute_arbitrage(RuntimeOrigin::none(), DAI, opportunity));
let receiver_balance_gain = Tokens::free_balance(DAI, &HsmArbProfitReceiver::get()) - receiver_balance_before;
assert_eq!(receiver_balance_gain, 10_875_005_266_593_893);

let emitted_profit = System::events()

Check failure on line 403 in pallets/hsm/src/tests/arb.rs

View workflow job for this annotation

GitHub Actions / build

called `Iterator::last` on a `DoubleEndedIterator`; this will needlessly iterate the entire iterator
.into_iter()
.filter_map(|record| {
if let RuntimeEvent::HSM(crate::Event::ArbitrageExecuted { profit, .. }) = record.event {
Some(profit)
} else {
None
}
})
.last()
.expect("ArbitrageExecuted event must have been emitted");

assert_eq!(emitted_profit, receiver_balance_gain);
});
}
Loading