Skip to content

Commit 4b67c13

Browse files
Improve process_dispatch_error handler
1 parent 0396702 commit 4b67c13

File tree

2 files changed

+22
-33
lines changed

2 files changed

+22
-33
lines changed

crates/pallet-evm-swap/src/precompile.rs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,15 @@ where
114114

115115
EvmSwapT::NativeToken::can_deposit(&to, estimated_swapped_balance, Provenance::Extant)
116116
.into_result()
117-
.map_err(|err| {
118-
process_dispatch_error(err, "unable to deposit into target native account")
119-
})?;
117+
.map_err(process_dispatch_error)?;
120118

121119
EvmSwapT::EvmToken::transfer(
122120
&from,
123121
&EvmSwapT::BridgePotEvm::get(),
124122
value,
125123
Preservation::Expendable,
126124
)
127-
.map_err(|err| {
128-
process_dispatch_error(
129-
err,
130-
"unable to transfer from source evm to bridge pot evm account",
131-
)
132-
})?;
125+
.map_err(process_dispatch_error)?;
133126

134127
EvmSwapT::NativeToken::transfer(
135128
&EvmSwapT::BridgePotNative::get(),
@@ -138,12 +131,7 @@ where
138131
// Bridge pot native account shouldn't be killed.
139132
Preservation::Preserve,
140133
)
141-
.map_err(|err| {
142-
process_dispatch_error(
143-
err,
144-
"unable to transfer from bridge pot native to target native account",
145-
)
146-
})?;
134+
.map_err(process_dispatch_error)?;
147135

148136
let logs_builder = LogsBuilder::new(handle.context().address);
149137

@@ -161,20 +149,27 @@ where
161149
}
162150

163151
/// A helper function to process dispatch related errors.
164-
fn process_dispatch_error(
165-
error: DispatchError,
166-
other_error_message: &'static str,
167-
) -> PrecompileFailure {
152+
fn process_dispatch_error(error: DispatchError) -> PrecompileFailure {
168153
match error {
169154
DispatchError::Token(frame_support::sp_runtime::TokenError::FundsUnavailable) => {
170155
PrecompileFailure::Error {
171156
exit_status: ExitError::OutOfFund,
172157
}
173158
}
174-
other_error_details => PrecompileFailure::Error {
175-
exit_status: ExitError::Other(
176-
format!("{other_error_message}: {other_error_details:?}").into(),
177-
),
159+
DispatchError::Token(frame_support::sp_runtime::TokenError::BelowMinimum) => {
160+
PrecompileFailure::Error {
161+
exit_status: ExitError::Other(
162+
"resulted balance is less than existential deposit".into(),
163+
),
164+
}
165+
}
166+
DispatchError::Token(frame_support::sp_runtime::TokenError::NotExpendable) => {
167+
PrecompileFailure::Error {
168+
exit_status: ExitError::Other("account would be killed".into()),
169+
}
170+
}
171+
_ => PrecompileFailure::Error {
172+
exit_status: ExitError::Other("unable to execute swap".into()),
178173
},
179174
}
180175
}

crates/pallet-evm-swap/src/tests/evm_to_native.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ fn swap_fail_target_balance_below_ed() {
309309
expected_gas_usage,
310310
transaction,
311311
ExitReason::Error(ExitError::Other(
312-
"unable to deposit into target native account: Token(BelowMinimum)".into(),
312+
"resulted balance is less than existential deposit".into(),
313313
)),
314314
);
315315
});
@@ -348,9 +348,7 @@ fn swap_fail_target_balance_overflow() {
348348
run_failed_test_and_assert(
349349
expected_gas_usage,
350350
transaction,
351-
ExitReason::Error(ExitError::Other(
352-
"unable to deposit into target native account: Arithmetic(Overflow)".into(),
353-
)),
351+
ExitReason::Error(ExitError::Other("unable to execute swap".into())),
354352
);
355353
});
356354
}
@@ -388,9 +386,7 @@ fn swap_fail_bridge_evm_overflow() {
388386
run_failed_test_and_assert(
389387
expected_gas_usage,
390388
transaction,
391-
ExitReason::Error(ExitError::Other(
392-
"unable to transfer from source evm to bridge pot evm account: Arithmetic(Overflow)".into(),
393-
)),
389+
ExitReason::Error(ExitError::Other("unable to execute swap".into())),
394390
);
395391
});
396392
}
@@ -428,9 +424,7 @@ fn swap_fail_bridge_native_killed() {
428424
run_failed_test_and_assert(
429425
expected_gas_usage,
430426
transaction,
431-
ExitReason::Error(ExitError::Other(
432-
"unable to transfer from bridge pot native to target native account: Token(NotExpendable)".into(),
433-
)),
427+
ExitReason::Error(ExitError::Other("account would be killed".into())),
434428
);
435429
});
436430
}

0 commit comments

Comments
 (0)