Skip to content

Commit

Permalink
added function to vft-gateway to mint tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
LouiseMedova committed Sep 4, 2024
1 parent f6b7c02 commit 38c027b
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 32 deletions.
65 changes: 62 additions & 3 deletions gear-programs/vft-gateway/src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub mod msg_tracker;
mod utils;
mod vft;
use error::Error;
use msg_tracker::{MessageInfo, MessageStatus, MessageTracker};
use msg_tracker::{MessageInfo, MessageStatus, MessageTracker, TxDetails};
mod token_operations;

pub struct VftGateway<ExecContext> {
Expand All @@ -31,24 +31,28 @@ pub struct VftGatewayData {
admin: ActorId,
receiver_contract_address: H160,
vara_to_eth_token_id: HashMap<ActorId, H160>,
eth_client: ActorId,
}

#[derive(Debug, Decode, Encode, TypeInfo)]
pub struct InitConfig {
pub receiver_contract_address: H160,
pub gear_bridge_builtin: ActorId,
pub eth_client: ActorId,
pub config: Config,
}

impl InitConfig {
pub fn new(
receiver_contract_address: H160,
gear_bridge_builtin: ActorId,
eth_client: ActorId,
config: Config,
) -> Self {
Self {
receiver_contract_address,
gear_bridge_builtin,
eth_client,
config,
}
}
Expand All @@ -59,6 +63,7 @@ pub struct Config {
gas_to_burn_tokens: u64,
gas_for_reply_deposit: u64,
gas_to_mint_tokens: u64,
gas_to_process_mint_request: u64,
gas_to_send_request_to_builtin: u64,
reply_timeout: u32,
gas_for_transfer_to_eth_msg: u64,
Expand All @@ -69,6 +74,7 @@ impl Config {
gas_to_burn_tokens: u64,
gas_for_reply_deposit: u64,
gas_to_mint_tokens: u64,
gas_to_process_mint_request: u64,
gas_to_send_request_to_builtin: u64,
reply_timeout: u32,
gas_for_transfer_to_eth_msg: u64,
Expand All @@ -77,6 +83,7 @@ impl Config {
gas_to_burn_tokens,
gas_for_reply_deposit,
gas_to_mint_tokens,
gas_to_process_mint_request,
gas_to_send_request_to_builtin,
reply_timeout,
gas_for_transfer_to_eth_msg,
Expand Down Expand Up @@ -116,6 +123,7 @@ where
&mut self,
gas_to_burn_tokens: Option<u64>,
gas_to_mint_tokens: Option<u64>,
gas_to_process_mint_request: Option<u64>,
gas_for_reply_deposit: Option<u64>,
gas_to_send_request_to_builtin: Option<u64>,
reply_timeout: Option<u32>,
Expand All @@ -132,6 +140,10 @@ where
self.config_mut().gas_to_mint_tokens = gas_to_mint_tokens;
}

if let Some(gas_to_process_mint_request) = gas_to_process_mint_request {
self.config_mut().gas_to_process_mint_request = gas_to_process_mint_request;
}

if let Some(gas_to_send_request_to_builtin) = gas_to_send_request_to_builtin {
self.config_mut().gas_to_send_request_to_builtin = gas_to_send_request_to_builtin;
}
Expand All @@ -149,6 +161,41 @@ where
}
}

pub async fn mint_tokens(
&mut self,
vara_token_id: ActorId,
receiver: ActorId,
amount: U256,
) -> Result<(), Error> {
let data = self.data();
let sender = self.exec_context.actor_id();

assert_eq!(sender, data.eth_client, "Only eth client can mint tokens");

let config = self.config();
if gstd::exec::gas_available()
< config.gas_to_mint_tokens
+ config.gas_to_process_mint_request
+ config.gas_for_reply_deposit
{
panic!("Please attach more gas");
}

let msg_id = gstd::msg::id();
let transaction_details = TxDetails::MintTokens {
vara_token_id,
receiver,
amount,
};
msg_tracker_mut().insert_message_info(
msg_id,
MessageStatus::SendingMessageToMintTokens,
transaction_details,
);
utils::set_critical_hook(msg_id);
token_operations::mint_tokens(vara_token_id, receiver, amount, config, msg_id).await
}

pub async fn transfer_vara_to_eth(
&mut self,
vara_token_id: ActorId,
Expand All @@ -158,7 +205,6 @@ where
let data = self.data();
let sender = self.exec_context.actor_id();
let msg_id = gstd::msg::id();

let eth_token_id = self.get_eth_token_id(&vara_token_id)?;
let config = self.config();

Expand All @@ -170,6 +216,7 @@ where
{
panic!("Please attach more gas");
}

token_operations::burn_tokens(vara_token_id, sender, receiver, amount, config, msg_id)
.await?;
let nonce = match bridge_builtin_operations::send_message_to_bridge_builtin(
Expand Down Expand Up @@ -207,7 +254,18 @@ where
.get_message_info(&msg_id)
.expect("Unexpected: msg status does not exist");

let (sender, amount, receiver, vara_token_id) = msg_info.details.data();
let (sender, amount, receiver, vara_token_id) = if let TxDetails::TransferVaraToEth {
vara_token_id,
sender,
amount,
receiver,
} = msg_info.details
{
(sender, amount, receiver, vara_token_id)
} else {
panic!("Wrong message type")
};

let eth_token_id = data
.vara_to_eth_token_id
.get(&vara_token_id)
Expand Down Expand Up @@ -295,6 +353,7 @@ where
gear_bridge_builtin: config.gear_bridge_builtin,
receiver_contract_address: config.receiver_contract_address,
admin: exec_context.actor_id(),
eth_client: config.eth_client,
..Default::default()
});
CONFIG = Some(config.config);
Expand Down
36 changes: 14 additions & 22 deletions gear-programs/vft-gateway/src/services/msg_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,30 @@ pub struct MessageTracker {
#[derive(Debug, Clone, Encode, Decode, TypeInfo)]
pub struct MessageInfo {
pub status: MessageStatus,
pub details: TransactionDetails,
pub details: TxDetails,
}

#[derive(Debug, Clone, Encode, Decode, TypeInfo)]
pub struct TransactionDetails {
vara_token_id: ActorId,
sender: ActorId,
amount: U256,
receiver: H160,
pub enum TxDetails {
TransferVaraToEth {
vara_token_id: ActorId,
sender: ActorId,
amount: U256,
receiver: H160,
},
MintTokens {
vara_token_id: ActorId,
receiver: ActorId,
amount: U256,
},
}

impl MessageTracker {
pub fn insert_message_info(
&mut self,
msg_id: MessageId,
status: MessageStatus,
details: TransactionDetails,
details: TxDetails,
) {
self.message_info
.insert(msg_id, MessageInfo { status, details });
Expand Down Expand Up @@ -111,18 +118,3 @@ pub enum MessageStatus {

MessageProcessedWithSuccess(U256),
}

impl TransactionDetails {
pub fn new(sender: ActorId, amount: U256, receiver: H160, vara_token_id: ActorId) -> Self {
Self {
vara_token_id,
sender,
amount,
receiver,
}
}

pub fn data(&self) -> (ActorId, U256, H160, ActorId) {
(self.sender, self.amount, self.receiver, self.vara_token_id)
}
}
18 changes: 11 additions & 7 deletions gear-programs/vft-gateway/src/services/token_operations.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::msg_tracker::TransactionDetails;
use super::msg_tracker::TxDetails;
use super::{msg_tracker_mut, utils, vft::vft::io as vft_io, Config, Error, MessageStatus};

use sails_rs::prelude::*;

pub async fn burn_tokens(
token_id: ActorId,
vara_token_id: ActorId,
sender: ActorId,
receiver: H160,
amount: U256,
Expand All @@ -13,7 +13,12 @@ pub async fn burn_tokens(
) -> Result<(), Error> {
let bytes: Vec<u8> = vft_io::Burn::encode_call(sender, amount);

let transaction_details = TransactionDetails::new(sender, amount, receiver, token_id);
let transaction_details = TxDetails::TransferVaraToEth {
vara_token_id,
sender,
amount,
receiver,
};

msg_tracker_mut().insert_message_info(
msg_id,
Expand All @@ -23,7 +28,7 @@ pub async fn burn_tokens(

utils::set_critical_hook(msg_id);
utils::send_message_with_gas_for_reply(
token_id,
vara_token_id,
bytes,
config.gas_to_burn_tokens,
config.gas_for_reply_deposit,
Expand All @@ -36,15 +41,14 @@ pub async fn burn_tokens(

pub async fn mint_tokens(
token_id: ActorId,
sender: ActorId,
receiver: ActorId,
amount: U256,
config: &Config,
msg_id: MessageId,
) -> Result<(), Error> {
msg_tracker_mut().update_message_status(msg_id, MessageStatus::SendingMessageToMintTokens);

let bytes: Vec<u8> = vft_io::Mint::encode_call(sender, amount);

let bytes: Vec<u8> = vft_io::Mint::encode_call(receiver, amount);
utils::send_message_with_gas_for_reply(
token_id,
bytes,
Expand Down

0 comments on commit 38c027b

Please sign in to comment.