Skip to content

Commit

Permalink
solana-ibc: refund user fees when transfer acknowledgement fails (#331)
Browse files Browse the repository at this point in the history
Co-authored-by: dhruvja <dhruv@iamsizzling.com>
  • Loading branch information
mina86 and dhruvja authored May 24, 2024
1 parent c3da323 commit ab538c0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
4 changes: 3 additions & 1 deletion solana/solana-ibc/programs/solana-ibc/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ pub use ibc::apps;
pub use ibc::apps::transfer::types::error::TokenTransferError;
pub use ibc::apps::transfer::types::msgs::transfer::MsgTransfer;
pub use ibc::core::channel::context::SendPacketValidationContext;
pub use ibc::core::channel::types::acknowledgement::Acknowledgement;
pub use ibc::core::channel::types::acknowledgement::{
Acknowledgement, AcknowledgementStatus,
};
pub use ibc::core::channel::types::channel::ChannelEnd;
pub use ibc::core::channel::types::commitment::{
AcknowledgementCommitment, PacketCommitment,
Expand Down
3 changes: 1 addition & 2 deletions solana/solana-ibc/programs/solana-ibc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

extern crate alloc;

use ::ibc::core::client::types::error::ClientError;
use anchor_lang::prelude::*;
use anchor_lang::solana_program;
use anchor_spl::associated_token::AssociatedToken;
Expand Down Expand Up @@ -461,7 +460,7 @@ pub mod solana_ibc {
.map_err(|e| error::Error::ContextError(e.into()))?;
if !status.is_active() {
return Err(error::Error::ContextError(
ClientError::ClientNotActive { status }.into(),
ibc::ClientError::ClientNotActive { status }.into(),
)
.into());
}
Expand Down
17 changes: 16 additions & 1 deletion solana/solana-ibc/programs/solana-ibc/src/transfer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,22 @@ impl ibc::Module for IbcStorage<'_, '_> {
relayer,
);

let status = serde_json::from_slice::<ibc::AcknowledgementStatus>(
acknowledgement.as_bytes(),
);
let success = if let Ok(status) = status {
status.is_successful()
} else {
let description =
ibc::TokenTransferError::AckDeserialization.to_string();
return (
ibc::ModuleExtras::empty(),
Err(ibc::PacketError::AppModule { description }),
);
};

// refund fee if there was an error on the counterparty chain
if result.1.is_err() {
if !success {
let store = self.borrow();
let accounts = &store.accounts;
let receiver = accounts.receiver.clone().unwrap();
Expand All @@ -249,6 +263,7 @@ impl ibc::Module for IbcStorage<'_, '_> {
**receiver.try_borrow_mut_lamports().unwrap() +=
crate::REFUND_FEE_AMOUNT_IN_LAMPORTS;
}

(
result.0,
result.1.map_err(|e| ibc::PacketError::AppModule {
Expand Down

0 comments on commit ab538c0

Please sign in to comment.