Skip to content

Commit c6956ea

Browse files
committed
skip add_deposit for refund
1 parent f2a7260 commit c6956ea

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

crates/ibc/src/context/token_transfer.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ where
2727
pub(crate) inner: Rc<RefCell<C>>,
2828
pub(crate) verifiers: Rc<RefCell<BTreeSet<Address>>>,
2929
is_shielded: bool,
30+
is_refund: bool,
3031
}
3132

3233
impl<C> TokenTransferContext<C>
@@ -42,6 +43,7 @@ where
4243
inner,
4344
verifiers,
4445
is_shielded: false,
46+
is_refund: false,
4547
}
4648
}
4749

@@ -55,6 +57,11 @@ where
5557
self.is_shielded = true;
5658
}
5759

60+
/// Set the transfer as refund
61+
pub fn enable_refund_transfer(&mut self) {
62+
self.is_refund = true;
63+
}
64+
5865
fn validate_sent_coin(&self, coin: &PrefixedCoin) -> Result<(), HostError> {
5966
// The base denom should not be an IBC token address because an IBC
6067
// token address has been already encoded and other chains can't extract
@@ -312,7 +319,9 @@ where
312319
) -> Result<(), HostError> {
313320
let (ibc_token, amount) = self.get_token_amount(coin)?;
314321

315-
self.add_deposit(&ibc_token, amount)?;
322+
if !self.is_refund {
323+
self.add_deposit(&ibc_token, amount)?;
324+
}
316325

317326
self.inner
318327
.borrow_mut()
@@ -329,7 +338,9 @@ where
329338
let (ibc_token, amount) = self.get_token_amount(coin)?;
330339

331340
self.update_mint_amount(&ibc_token, amount, true)?;
332-
self.add_deposit(&ibc_token, amount)?;
341+
if !self.is_refund {
342+
self.add_deposit(&ibc_token, amount)?;
343+
}
333344

334345
// A transfer of NUT tokens must be verified by their VP
335346
if ibc_token.is_internal()

crates/ibc/src/context/transfer_mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ where
302302
acknowledgement: &Acknowledgement,
303303
relayer: &Signer,
304304
) -> (ModuleExtras, Result<(), ChannelError>) {
305+
self.ctx.enable_refund_transfer();
305306
let (extras, result) = on_acknowledgement_packet_execute(
306307
&mut self.ctx,
307308
packet,
@@ -325,6 +326,7 @@ where
325326
packet: &Packet,
326327
relayer: &Signer,
327328
) -> (ModuleExtras, Result<(), ChannelError>) {
329+
self.ctx.enable_refund_transfer();
328330
let (extras, result) =
329331
on_timeout_packet_execute(&mut self.ctx, packet, relayer);
330332
(extras, result.map_err(into_channel_error))

crates/tests/src/e2e/ibc_tests.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,7 @@ fn ibc_upgrade_client() -> Result<()> {
10731073
/// - Transfer 1 NAM in an epoch will succeed
10741074
/// - Transfer 1 NAM in the same epoch will fail
10751075
/// - Transfer 1 NAM in the next epoch will succeed
1076+
/// - Refund will succeed after sending 1 NAM
10761077
/// 2. Test the mint limit
10771078
/// - The mint limit is 1
10781079
/// - Receiving 2 samoleans from Gaia will fail
@@ -1233,6 +1234,33 @@ fn ibc_rate_limit() -> Result<()> {
12331234
epoch = get_epoch(&test, &rpc).unwrap();
12341235
}
12351236

1237+
// Send 1 Apfel from Namada to an invalid Gaia address to trigger a refund
1238+
transfer(
1239+
&test,
1240+
ALBERT,
1241+
"invalid_receiver",
1242+
APFEL,
1243+
1,
1244+
Some(ALBERT_KEY),
1245+
&port_id_namada,
1246+
&channel_id_namada,
1247+
None,
1248+
None,
1249+
None,
1250+
None,
1251+
false,
1252+
None,
1253+
None,
1254+
)?;
1255+
// the per-epoch limit has reached but the refund should succeed
1256+
wait_for_packet_relay(
1257+
&hermes_dir,
1258+
&port_id_namada,
1259+
&channel_id_namada,
1260+
&test,
1261+
)?;
1262+
check_balance(&test, ALBERT, APFEL, 1_000_000)?;
1263+
12361264
// Transfer 2 samoleans from Gaia to Namada will succeed, but Namada can't
12371265
// receive due to the mint limit and the packet will be timed out
12381266
let namada_receiver = find_address(&test, ALBERT)?.to_string();

0 commit comments

Comments
 (0)