Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vft-gateway): Add function to mint tokens #119

Merged
merged 4 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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 @@
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 @@
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 @@
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 All @@ -84,7 +91,7 @@
}
}

#[service]

Check failure on line 94 in gear-programs/vft-gateway/src/services/mod.rs

View workflow job for this annotation

GitHub Actions / lints

this function has too many arguments (8/7)
impl<T> VftGateway<T>
where
T: ExecContext,
Expand Down Expand Up @@ -112,10 +119,11 @@
self.data_mut().vara_to_eth_token_id.remove(&vara_token_id);
}

pub fn update_config(

Check failure on line 122 in gear-programs/vft-gateway/src/services/mod.rs

View workflow job for this annotation

GitHub Actions / lints

this function has too many arguments (8/7)
&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 @@
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 @@
}
}

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 @@
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 @@
{
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 @@
.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")
};
LouiseMedova marked this conversation as resolved.
Show resolved Hide resolved

let eth_token_id = data
.vara_to_eth_token_id
.get(&vara_token_id)
Expand Down Expand Up @@ -295,6 +353,7 @@
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,
LouiseMedova marked this conversation as resolved.
Show resolved Hide resolved
},
}

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
26 changes: 25 additions & 1 deletion gear-programs/vft-gateway/src/wasm/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use vft_gateway_app::services::{error::Error, msg_tracker::MessageInfo, Config,
pub const ADMIN_ID: u64 = 1000;
pub const TOKEN_ID: u64 = 200;
pub const BRIDGE_BUILTIN_ID: u64 = 300;
pub const ETH_CLIENT_ID: u64 = 500;

// Mocks for programs
macro_rules! create_mock {
Expand Down Expand Up @@ -134,6 +135,7 @@ pub trait VftGateway {
from: u64,
msg_id: MessageId,
) -> Result<(U256, H160), Error>;
fn mint_tokens(&self, from: u64, vara_token_id: ActorId, amount: U256, receiver: ActorId);
fn get_msg_tracker_state(&self) -> Vec<(MessageId, MessageInfo)>;
}

Expand All @@ -143,8 +145,10 @@ impl VftGateway for Program<'_> {
let init_config = InitConfig::new(
[1; 20].into(),
BRIDGE_BUILTIN_ID.into(),
ETH_CLIENT_ID.into(),
Config::new(
2_000_000_000,
15_000_000_000,
15_000_000_000,
15_000_000_000,
15_000_000_000,
15_000_000_000,
Expand Down Expand Up @@ -231,6 +235,26 @@ impl VftGateway for Program<'_> {
reply.2
}

fn mint_tokens(&self, from: u64, vara_token_id: ActorId, amount: U256, receiver: ActorId) {
let payload = [
"VftGateway".encode(),
"MintTokens".encode(),
(vara_token_id, receiver, amount).encode(),
]
.concat();
let result = self.send_bytes(from, payload);
let log_entry = result
.log()
.iter()
.find(|log_entry| log_entry.destination() == from.into())
.expect("Unable to get reply");

let reply = <(String, String, Result<(), Error>)>::decode(&mut log_entry.payload())
.expect("Unable to decode reply");
println!("{:?}", reply.2);
LouiseMedova marked this conversation as resolved.
Show resolved Hide resolved
assert!(reply.2.is_ok());
}

fn get_msg_tracker_state(&self) -> Vec<(MessageId, MessageInfo)> {
let payload = ["VftGateway".encode(), "MsgTrackerState".encode()].concat();

Expand Down
Loading
Loading