Skip to content

Commit

Permalink
Update impl
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteNacked committed Sep 23, 2024
1 parent 17d4349 commit e5f7ef2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
12 changes: 9 additions & 3 deletions gear-programs/bridging-payment-vara-supply/src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ pub struct BridgingPayment<ExecContext> {
#[derive(Encode, Decode, TypeInfo)]
pub enum BridgingPaymentEvents {
DepositVaraToTreasury {
nonce: U256,
sender: ActorId,
amount: U256,
receiver: H160,
eth_token_id: H160,
},
}

Expand Down Expand Up @@ -265,7 +267,7 @@ where
.await
.map(Some)
}
MessageStatus::TreasuryMessageProcessingCompleted => {
MessageStatus::TreasuryMessageProcessingCompleted((nonce, eth_token_id)) => {
let TransactionDetails {
sender,
amount,
Expand All @@ -277,9 +279,11 @@ where
process_refund(sender, attached_value, config);

Ok(Some(BridgingPaymentEvents::DepositVaraToTreasury {
nonce,
sender,
amount,
receiver,
eth_token_id,
}))
}
MessageStatus::ProcessRefund => {
Expand All @@ -289,7 +293,7 @@ where
..
} = msg_info.details;

handle_refund(sender, attached_value);
process_refund(sender, attached_value, config);

Ok(None)
}
Expand Down Expand Up @@ -327,7 +331,7 @@ async fn handle_treasury_transaction(
vft_gateway_address: ActorId,
config: &Config,
) -> Result<BridgingPaymentEvents, Error> {
send_message_to_treasury(
let (nonce, eth_token_id) = send_message_to_treasury(
vft_gateway_address,
sender,
vara_token_id,
Expand All @@ -341,9 +345,11 @@ async fn handle_treasury_transaction(
process_refund(sender, attached_value, config);

Ok(BridgingPaymentEvents::DepositVaraToTreasury {
nonce,
sender,
amount,
receiver,
eth_token_id,
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub enum MessageStatus {
SendingMessageToTreasury,
WaitingReplyFromTreasury,
ProcessRefund,
TreasuryMessageProcessingCompleted,
TreasuryMessageProcessingCompleted((U256, H160)),
}

impl MessageTracker {
Expand Down Expand Up @@ -55,12 +55,12 @@ impl MessageTracker {
self.message_info.remove(msg_id)
}

pub fn check_vft_treasury_reply(&mut self, msg_id: &MessageId) -> Result<(), Error> {
pub fn check_vft_treasury_reply(&mut self, msg_id: &MessageId) -> Result<(U256, H160), Error> {
if let Some(info) = self.message_info.get(msg_id) {
match info.status {
MessageStatus::TreasuryMessageProcessingCompleted => {
MessageStatus::TreasuryMessageProcessingCompleted(nonce_and_eth_token_id) => {
self.remove_message_info(msg_id);
Ok(())
Ok(nonce_and_eth_token_id)
}
MessageStatus::ProcessRefund | MessageStatus::SendingMessageToTreasury => {
Err(Error::TreasuryMessageProcessingFailed)
Expand Down
13 changes: 8 additions & 5 deletions gear-programs/bridging-payment-vara-supply/src/services/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::{
error::Error,
msg_tracker::{msg_tracker_mut, MessageStatus},
vft_treasury::vft_treasury::io as vft_treasury_io,
vft_treasury::Error as VftTreasuryError,
};
use sails_rs::calls::ActionIo;
use sails_rs::prelude::*;
Expand Down Expand Up @@ -74,13 +75,13 @@ fn handle_reply_hook(msg_id: MessageId) {
MessageStatus::SendingMessageToTreasury | MessageStatus::WaitingReplyFromTreasury => {
let reply = decode_vft_treasury_reply(&reply_bytes);
match reply {
Ok(()) => {
Ok(Ok(nonce_eth_id)) => {
msg_tracker.update_message_status(
msg_id,
MessageStatus::TreasuryMessageProcessingCompleted,
MessageStatus::TreasuryMessageProcessingCompleted(nonce_eth_id),
);
}
Err(_) => {
Err(_) | Ok(_) => {
msg_tracker.update_message_status(msg_id, MessageStatus::ProcessRefund);
}
};
Expand All @@ -89,6 +90,8 @@ fn handle_reply_hook(msg_id: MessageId) {
};
}

fn decode_vft_treasury_reply(bytes: &[u8]) -> Result<(), Error> {
vft_treasury_io::Deposit::decode_reply(bytes).map_err(|_| Error::RequestToTreasuryDecode)
fn decode_vft_treasury_reply(
bytes: &[u8],
) -> Result<Result<(U256, H160), VftTreasuryError>, Error> {
vft_treasury_io::DepositTokens::decode_reply(bytes).map_err(|_| Error::RequestToTreasuryDecode)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ pub async fn send_message_to_treasury(
receiver: H160,
attached_value: u128,
config: &Config,
) -> Result<(), Error> {
) -> Result<(U256, H160), Error> {
let msg_id = gstd::msg::id();

let bytes: Vec<u8> = vft_treasury::vft_treasury::io::Deposit::encode_call(
sender,
let bytes: Vec<u8> = vft_treasury::vft_treasury::io::DepositTokens::encode_call(
vara_token_id,
sender,
amount,
receiver,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type MessageStatus = enum {
SendingMessageToTreasury,
WaitingReplyFromTreasury,
ProcessRefund,
TreasuryMessageProcessingCompleted,
TreasuryMessageProcessingCompleted: struct { u256, h160 },
};

type TransactionDetails = struct {
Expand Down Expand Up @@ -49,7 +49,7 @@ service BridgingPayment {
query VftTreasuryAddress : () -> actor_id;

events {
DepositVaraToTreasury: struct { sender: actor_id, amount: u256, receiver: h160 };
DepositVaraToTreasury: struct { nonce: u256, sender: actor_id, amount: u256, receiver: h160, eth_token_id: h160 };
}
};

0 comments on commit e5f7ef2

Please sign in to comment.