diff --git a/gear-programs/vft-manager/app/src/services/request_bridging/mod.rs b/gear-programs/vft-manager/app/src/services/request_bridging/mod.rs index 235c713a..4e770816 100644 --- a/gear-programs/vft-manager/app/src/services/request_bridging/mod.rs +++ b/gear-programs/vft-manager/app/src/services/request_bridging/mod.rs @@ -101,8 +101,6 @@ pub async fn handle_interrupted_transfer( service: &mut VftManager, msg_id: MessageId, ) -> Result<(U256, H160), Error> { - let state = service.state(); - let config = service.config(); let msg_tracker = msg_tracker::msg_tracker_mut(); @@ -114,61 +112,36 @@ pub async fn handle_interrupted_transfer( vara_token_id, sender, amount, - receiver, + receiver: _, token_supply, } = msg_info.details; - let eth_token_id = service - .state() - .token_map - .get_eth_token_id(&vara_token_id) - .expect("Failed to get ethereum token id"); - match msg_info.status { - MessageStatus::TokenDepositCompleted(true) => { - let payload = Payload { - receiver, - token_id: eth_token_id, - amount, - }; - - match bridge_builtin_operations::send_message_to_bridge_builtin( - state.gear_bridge_builtin, - state.erc20_manager_address, - payload, - config, - msg_id, - ) - .await - { - Ok(nonce) => Ok((nonce, eth_token_id)), - Err(_) => { - match token_supply { - TokenSupply::Ethereum => { - token_operations::mint(vara_token_id, sender, amount, config, msg_id) - .await?; - } - TokenSupply::Gear => { - token_operations::unlock(vara_token_id, sender, amount, config, msg_id) - .await?; - } - } - - // In case of failure, mint tokens back to the sender - Err(Error::TokensRefunded) + MessageStatus::TokenDepositCompleted(true) + | MessageStatus::SendingMessageToBridgeBuiltin + | MessageStatus::SendingMessageToReturnTokens + | MessageStatus::TokensReturnComplete(false) + | MessageStatus::BridgeResponseReceived(None) => { + set_critical_hook(msg_id); + + match token_supply { + TokenSupply::Ethereum => { + token_operations::mint(vara_token_id, sender, amount, config, msg_id).await?; + } + TokenSupply::Gear => { + token_operations::unlock(vara_token_id, sender, amount, config, msg_id).await?; } } - } - MessageStatus::BridgeResponseReceived(Some(nonce)) => { - msg_tracker::msg_tracker_mut().remove_message_info(&msg_id); - Ok((nonce, eth_token_id)) - } - MessageStatus::SendingMessageToReturnTokens | MessageStatus::TokenReturnFailed => { - // TODO: Or unlock. - token_operations::mint(vara_token_id, sender, amount, config, msg_id).await?; + Err(Error::TokensRefunded) } - _ => { + MessageStatus::TokenDepositCompleted(false) + | MessageStatus::SendingMessageToDepositTokens + | MessageStatus::WaitingReplyFromTokenDepositMessage + | MessageStatus::WaitingReplyFromBuiltin + | MessageStatus::WaitingReplyFromTokenReturnMessage + | MessageStatus::BridgeResponseReceived(Some(_)) + | MessageStatus::TokensReturnComplete(true) => { panic!("Unexpected status or transaction completed.") } } @@ -183,31 +156,20 @@ fn set_critical_hook(msg_id: MessageId) { match msg_info.status { MessageStatus::SendingMessageToDepositTokens => { - // If still sending, transition to `WaitingReplyFromBurn`. msg_tracker.update_message_status( msg_id, MessageStatus::WaitingReplyFromTokenDepositMessage, ); } - // MessageStatus::TokenDepositCompleted(false) => { - // // If the token burn fails, cancel the transaction. - // msg_tracker.remove_message_info(&msg_id); - // } MessageStatus::SendingMessageToBridgeBuiltin => { - // If still sending, transition to `WaitingReplyFromBuiltin`. msg_tracker.update_message_status(msg_id, MessageStatus::WaitingReplyFromBuiltin); } - // MessageStatus::BridgeResponseReceived(None) => { - // // If error occurs during builtin message, go to mint step - // msg_tracker.update_message_status(msg_id, MessageStatus::ReturnTokensStep) - // } MessageStatus::SendingMessageToReturnTokens => { msg_tracker.update_message_status( msg_id, MessageStatus::WaitingReplyFromTokenReturnMessage, ); } - _ => {} }; }); diff --git a/gear-programs/vft-manager/app/src/services/request_bridging/msg_tracker.rs b/gear-programs/vft-manager/app/src/services/request_bridging/msg_tracker.rs index d8fd443c..803401b1 100644 --- a/gear-programs/vft-manager/app/src/services/request_bridging/msg_tracker.rs +++ b/gear-programs/vft-manager/app/src/services/request_bridging/msg_tracker.rs @@ -84,8 +84,8 @@ impl MessageTracker { pub fn check_withdraw_result(&mut self, msg_id: &MessageId) -> Result<(), Error> { if let Some(info) = self.message_info.get(msg_id) { match info.status { - MessageStatus::TokensReturned => Ok(()), - MessageStatus::TokenReturnFailed => Err(Error::MessageFailed), + MessageStatus::TokensReturnComplete(true) => Ok(()), + MessageStatus::TokensReturnComplete(false) => Err(Error::MessageFailed), _ => Err(Error::InvalidMessageStatus), } } else { @@ -109,10 +109,7 @@ pub enum MessageStatus { // Return tokens statuses. SendingMessageToReturnTokens, WaitingReplyFromTokenReturnMessage, - TokensReturned, - TokenReturnFailed, - - MessageProcessedWithSuccess(U256), + TokensReturnComplete(bool), } pub fn init() { diff --git a/gear-programs/vft-manager/app/src/services/request_bridging/token_operations.rs b/gear-programs/vft-manager/app/src/services/request_bridging/token_operations.rs index 6feba125..078d3b8d 100644 --- a/gear-programs/vft-manager/app/src/services/request_bridging/token_operations.rs +++ b/gear-programs/vft-manager/app/src/services/request_bridging/token_operations.rs @@ -57,7 +57,7 @@ pub async fn mint( config: &Config, msg_id: MessageId, ) -> Result<(), Error> { - msg_tracker_mut().update_message_status(msg_id, MessageStatus::SendingMessageToReturnTokens); + msg_tracker_mut().update_message_status(msg_id, MessageStatus::SendingMessageToWithdrawTokens); let bytes: Vec = vft_io::Mint::encode_call(receiver, amount); utils::send_message_with_gas_for_reply( @@ -80,7 +80,7 @@ pub async fn unlock( config: &Config, msg_id: MessageId, ) -> Result<(), Error> { - msg_tracker_mut().update_message_status(msg_id, MessageStatus::SendingMessageToReturnTokens); + msg_tracker_mut().update_message_status(msg_id, MessageStatus::SendingMessageToWithdrawTokens); let sender = gstd::exec::program_id(); let bytes: Vec = vft_io::TransferFrom::encode_call(sender, recepient, amount); diff --git a/gear-programs/vft-manager/app/src/services/request_bridging/utils.rs b/gear-programs/vft-manager/app/src/services/request_bridging/utils.rs index 1f005557..91a92cfa 100644 --- a/gear-programs/vft-manager/app/src/services/request_bridging/utils.rs +++ b/gear-programs/vft-manager/app/src/services/request_bridging/utils.rs @@ -41,26 +41,14 @@ fn handle_reply_hook(msg_id: MessageId) { let reply = match msg_info.details.token_supply { TokenSupply::Ethereum => decode_burn_reply(&reply_bytes), TokenSupply::Gear => decode_lock_reply(&reply_bytes), - }; + } + .unwrap_or(false); - match reply { - Ok(reply) => { - msg_tracker - .update_message_status(msg_id, MessageStatus::TokenDepositCompleted(reply)); - } - Err(_) => { - msg_tracker.remove_message_info(&msg_id); - } - }; + msg_tracker.update_message_status(msg_id, MessageStatus::TokenDepositCompleted(reply)); } MessageStatus::SendingMessageToBridgeBuiltin | MessageStatus::WaitingReplyFromBuiltin => { - let reply = decode_bridge_reply(&reply_bytes); - let result = match reply { - Ok(Some(nonce)) => Some(nonce), - _ => None, - }; - msg_tracker - .update_message_status(msg_id, MessageStatus::BridgeResponseReceived(result)); + let reply = decode_bridge_reply(&reply_bytes).ok().flatten(); + msg_tracker.update_message_status(msg_id, MessageStatus::BridgeResponseReceived(reply)); } MessageStatus::WaitingReplyFromTokenReturnMessage | MessageStatus::SendingMessageToReturnTokens => { @@ -70,13 +58,8 @@ fn handle_reply_hook(msg_id: MessageId) { } .unwrap_or(false); - if !reply { - msg_tracker.update_message_status(msg_id, MessageStatus::TokenReturnFailed); - } else { - msg_tracker.update_message_status(msg_id, MessageStatus::TokensReturned); - } + msg_tracker.update_message_status(msg_id, MessageStatus::TokensReturnComplete(reply)); } - _ => {} }; }