From ab538c00c68e17e20a75fac28edcdb82d1c8cb89 Mon Sep 17 00:00:00 2001 From: Michal Nazarewicz Date: Fri, 24 May 2024 20:30:10 +0200 Subject: [PATCH] solana-ibc: refund user fees when transfer acknowledgement fails (#331) Co-authored-by: dhruvja --- .../solana-ibc/programs/solana-ibc/src/ibc.rs | 4 +++- .../solana-ibc/programs/solana-ibc/src/lib.rs | 3 +-- .../programs/solana-ibc/src/transfer/mod.rs | 17 ++++++++++++++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/solana/solana-ibc/programs/solana-ibc/src/ibc.rs b/solana/solana-ibc/programs/solana-ibc/src/ibc.rs index 56f41940..dbfec48e 100644 --- a/solana/solana-ibc/programs/solana-ibc/src/ibc.rs +++ b/solana/solana-ibc/programs/solana-ibc/src/ibc.rs @@ -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, diff --git a/solana/solana-ibc/programs/solana-ibc/src/lib.rs b/solana/solana-ibc/programs/solana-ibc/src/lib.rs index 4c73319a..e00f63b1 100644 --- a/solana/solana-ibc/programs/solana-ibc/src/lib.rs +++ b/solana/solana-ibc/programs/solana-ibc/src/lib.rs @@ -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; @@ -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()); } diff --git a/solana/solana-ibc/programs/solana-ibc/src/transfer/mod.rs b/solana/solana-ibc/programs/solana-ibc/src/transfer/mod.rs index b3cbfdc0..15af9dc3 100644 --- a/solana/solana-ibc/programs/solana-ibc/src/transfer/mod.rs +++ b/solana/solana-ibc/programs/solana-ibc/src/transfer/mod.rs @@ -238,8 +238,22 @@ impl ibc::Module for IbcStorage<'_, '_> { relayer, ); + let status = serde_json::from_slice::( + 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(); @@ -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 {