Skip to content

Commit

Permalink
Cleanup submit_receipt
Browse files Browse the repository at this point in the history
  • Loading branch information
mertwole committed Dec 16, 2024
1 parent adabc14 commit 23cb4c1
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 109 deletions.
41 changes: 34 additions & 7 deletions gear-programs/vft-manager/app/src/services/submit_receipt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ use super::{error::Error, TokenSupply, VftManager};
pub mod abi;
mod msg_tracker;
mod token_operations;
mod utils;

use msg_tracker::{MessageStatus, TxDetails};
use msg_tracker::{msg_tracker_mut, MessageStatus, TxDetails};

pub use msg_tracker::{msg_tracker_state, MessageInfo as MsgTrackerMessageInfo};

Expand Down Expand Up @@ -111,12 +110,12 @@ pub async fn submit_receipt<T: ExecContext>(
}
transactions.insert((slot, transaction_index));

msg_tracker::msg_tracker_mut().insert_message_info(
msg_tracker_mut().insert_message_info(
msg_id,
MessageStatus::SendingMessageToWithdrawTokens,
transaction_details,
);
utils::set_critical_hook(msg_id);
set_critical_hook(msg_id);

match supply_type {
TokenSupply::Ethereum => {
Expand All @@ -127,6 +126,8 @@ pub async fn submit_receipt<T: ExecContext>(
}
}

msg_tracker_mut().check_withdraw_result(&msg_id)?;

Ok(())
}

Expand All @@ -135,7 +136,7 @@ pub async fn handle_interrupted_transfer<T: ExecContext>(
msg_id: MessageId,
) -> Result<(U256, H160), Error> {
let config = service.config();
let msg_tracker = msg_tracker::msg_tracker_mut();
let msg_tracker = msg_tracker_mut();

let msg_info = msg_tracker
.get_message_info(&msg_id)
Expand All @@ -149,7 +150,11 @@ pub async fn handle_interrupted_transfer<T: ExecContext>(
} = msg_info.details;

match msg_info.status {
MessageStatus::WithdrawTokensStep => {
MessageStatus::SendingMessageToWithdrawTokens | MessageStatus::TokenWithdrawFailed => {
msg_tracker_mut()
.update_message_status(msg_id, MessageStatus::SendingMessageToWithdrawTokens);
set_critical_hook(msg_id);

match token_supply {
TokenSupply::Ethereum => {
token_operations::mint(vara_token_id, receiver, amount, config, msg_id).await?;
Expand All @@ -160,11 +165,33 @@ pub async fn handle_interrupted_transfer<T: ExecContext>(
}
}

msg_tracker_mut().check_withdraw_result(&msg_id)?;

// TODO: Not refunded, just minted.
Err(Error::TokensRefunded)
}
_ => {
MessageStatus::WaitingReplyFromTokenWithdrawMessage
| MessageStatus::TokenWithdrawCompleted => {
panic!("Unexpected status or transaction completed.")
}
}
}

fn set_critical_hook(msg_id: MessageId) {
gstd::critical::set_hook(move || {
let msg_tracker = msg_tracker_mut();
let msg_info = msg_tracker
.get_message_info(&msg_id)
.expect("Unexpected: msg info does not exist");

match msg_info.status {

Check failure on line 187 in gear-programs/vft-manager/app/src/services/submit_receipt/mod.rs

View workflow job for this annotation

GitHub Actions / lints

you seem to be trying to use `match` for an equality check. Consider using `if`
MessageStatus::SendingMessageToWithdrawTokens => {
msg_tracker.update_message_status(
msg_id,
MessageStatus::WaitingReplyFromTokenWithdrawMessage,
);
}
_ => {}
};
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl MessageTracker {
if let Some(info) = self.message_info.get(msg_id) {
match info.status {
MessageStatus::TokenWithdrawCompleted => Ok(()),
MessageStatus::WithdrawTokensStep => Err(Error::MessageFailed),
MessageStatus::TokenWithdrawFailed => Err(Error::MessageFailed),
_ => Err(Error::InvalidMessageStatus),
}
} else {
Expand All @@ -64,9 +64,9 @@ impl MessageTracker {
#[derive(Debug, Clone, PartialEq, Encode, Decode, TypeInfo)]
pub enum MessageStatus {
SendingMessageToWithdrawTokens,
TokenWithdrawCompleted,
WaitingReplyFromTokenWithdrawMessage,
WithdrawTokensStep,
TokenWithdrawCompleted,
TokenWithdrawFailed,
}

pub fn init() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use sails_rs::prelude::*;
use gstd::msg;
use sails_rs::{calls::ActionIo, prelude::*};

use extended_vft_client::vft::io as vft_io;

use super::super::{Config, Error};
use super::msg_tracker::{msg_tracker_mut, MessageStatus};
use super::utils;
use super::{
super::{Config, Error, TokenSupply},
msg_tracker::{msg_tracker_mut, MessageStatus},
};

pub async fn mint(
token_id: ActorId,
Expand All @@ -13,20 +15,16 @@ pub async fn mint(
config: &Config,
msg_id: MessageId,
) -> Result<(), Error> {
msg_tracker_mut().update_message_status(msg_id, MessageStatus::SendingMessageToWithdrawTokens);

let bytes: Vec<u8> = vft_io::Mint::encode_call(receiver, amount);
utils::send_message_with_gas_for_reply(
send_message_with_gas_for_reply(
token_id,
bytes,
config.gas_for_token_ops,
config.gas_for_reply_deposit,
config.reply_timeout,
msg_id,
)
.await?;

msg_tracker_mut().check_withdraw_result(&msg_id)
.await
}

pub async fn unlock(
Expand All @@ -36,20 +34,71 @@ pub async fn unlock(
config: &Config,
msg_id: MessageId,
) -> Result<(), Error> {
msg_tracker_mut().update_message_status(msg_id, MessageStatus::SendingMessageToWithdrawTokens);

let sender = gstd::exec::program_id();
let bytes: Vec<u8> = vft_io::TransferFrom::encode_call(sender, recepient, amount);

utils::send_message_with_gas_for_reply(
send_message_with_gas_for_reply(
vara_token_id,
bytes,
config.gas_for_token_ops,
config.gas_for_reply_deposit,
config.reply_timeout,
msg_id,
)
.await?;
.await
}

pub async fn send_message_with_gas_for_reply(
destination: ActorId,
message: Vec<u8>,
gas_to_send: u64,
gas_deposit: u64,
reply_timeout: u32,
msg_id: MessageId,
) -> Result<(), Error> {
gstd::msg::send_bytes_with_gas_for_reply(destination, message, gas_to_send, 0, gas_deposit)
.map_err(|_| Error::SendFailure)?
.up_to(Some(reply_timeout))
.map_err(|_| Error::ReplyTimeout)?
.handle_reply(move || handle_reply_hook(msg_id))
.map_err(|_| Error::ReplyHook)?
.await
.map_err(|_| Error::ReplyFailure)?;

Ok(())
}

fn handle_reply_hook(msg_id: MessageId) {
let msg_tracker = msg_tracker_mut();

let msg_info = msg_tracker
.get_message_info(&msg_id)
.expect("Unexpected: msg info does not exist");
let reply_bytes = msg::load_bytes().expect("Unable to load bytes");

match msg_info.status {

Check failure on line 79 in gear-programs/vft-manager/app/src/services/submit_receipt/token_operations.rs

View workflow job for this annotation

GitHub Actions / lints

you seem to be trying to use `match` for an equality check. Consider using `if`
MessageStatus::SendingMessageToWithdrawTokens => {
let reply = match msg_info.details.token_supply {
TokenSupply::Ethereum => decode_mint_reply(&reply_bytes),
TokenSupply::Gear => decode_unlock_reply(&reply_bytes),
}
.unwrap_or(false);

if !reply {
msg_tracker.update_message_status(msg_id, MessageStatus::TokenWithdrawFailed);
} else {
msg_tracker.update_message_status(msg_id, MessageStatus::TokenWithdrawCompleted);
}
}

_ => {}
};
}

fn decode_mint_reply(bytes: &[u8]) -> Result<bool, Error> {
vft_io::Mint::decode_reply(bytes).map_err(|_| Error::MintTokensDecode)
}

msg_tracker_mut().check_withdraw_result(&msg_id)
fn decode_unlock_reply(bytes: &[u8]) -> Result<bool, Error> {
vft_io::TransferFrom::decode_reply(bytes).map_err(|_| Error::TransferFromDecode)
}
84 changes: 0 additions & 84 deletions gear-programs/vft-manager/app/src/services/submit_receipt/utils.rs

This file was deleted.

0 comments on commit 23cb4c1

Please sign in to comment.