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

refactor(vft-gateway): Remove Vara address from MintTokens service/message #133

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions gear-programs/vft-gateway/src/services/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ pub enum Error {
TokensRefunded,
NotEthClient,
NotEnoughGas,
NoCorrespondingVaraAddress,
}
33 changes: 25 additions & 8 deletions gear-programs/vft-gateway/src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct VftGatewayData {
admin: ActorId,
receiver_contract_address: H160,
vara_to_eth_token_id: HashMap<ActorId, H160>,
gshep marked this conversation as resolved.
Show resolved Hide resolved
eth_to_vara_token_id: HashMap<H160, ActorId>,
eth_client: ActorId,
}

Expand Down Expand Up @@ -107,16 +108,24 @@ where
if self.data().admin != self.exec_context.actor_id() {
panic!("Not admin")
}

self.data_mut()
.vara_to_eth_token_id
.insert(vara_token_id, eth_token_id);
self.data_mut()
.eth_to_vara_token_id
.insert(eth_token_id, vara_token_id);
}

pub fn remove_vara_to_eth_address(&mut self, vara_token_id: ActorId) {
if self.data().admin != self.exec_context.actor_id() {
panic!("Not admin")
}
self.data_mut().vara_to_eth_token_id.remove(&vara_token_id);

self.data_mut()
.vara_to_eth_token_id
.remove(&vara_token_id)
.map(|eth_token_id| self.data_mut().eth_to_vara_token_id.remove(&eth_token_id));
}

pub fn update_config(&mut self, config: Config) {
Expand All @@ -131,10 +140,11 @@ where

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

Expand Down Expand Up @@ -234,9 +244,8 @@ where
panic!("Wrong message type")
};

let eth_token_id = data
.vara_to_eth_token_id
.get(&vara_token_id)
let eth_token_id = self
.get_eth_token_id(&vara_token_id)
.expect("No corresponding Ethereum address for the specified Vara token address");

match msg_info.status {
Expand All @@ -245,14 +254,14 @@ where
data.gear_bridge_builtin,
data.receiver_contract_address,
receiver,
*eth_token_id,
eth_token_id,
amount,
config,
msg_id,
)
.await
{
Ok(nonce) => Ok((nonce, *eth_token_id)),
Ok(nonce) => Ok((nonce, eth_token_id)),
Err(_) => {
// In case of failure, mint tokens back to the sender
token_operations::mint_tokens(
Expand All @@ -269,7 +278,7 @@ where
}
MessageStatus::BridgeResponseReceived(Some(nonce)) => {
msg_tracker_mut().remove_message_info(&msg_id);
Ok((nonce, *eth_token_id))
Ok((nonce, eth_token_id))
}
MessageStatus::MintTokensStep => {
token_operations::mint_tokens(vara_token_id, sender, amount, config, msg_id)
Expand Down Expand Up @@ -355,6 +364,14 @@ where
.cloned()
.ok_or(Error::NoCorrespondingEthAddress)
}

fn get_vara_token_id(&self, eth_token_id: &H160) -> Result<ActorId, Error> {
self.data()
.eth_to_vara_token_id
.get(eth_token_id)
.cloned()
.ok_or(Error::NoCorrespondingVaraAddress)
}
}
fn msg_tracker() -> &'static MessageTracker {
unsafe {
Expand Down
6 changes: 3 additions & 3 deletions gear-programs/vft-gateway/src/wasm/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ pub trait VftGateway {
fn mint_tokens(
&self,
from: u64,
vara_token_id: ActorId,
eth_token_id: H160,
amount: U256,
receiver: ActorId,
error: bool,
Expand Down Expand Up @@ -245,15 +245,15 @@ impl VftGateway for Program<'_> {
fn mint_tokens(
&self,
from: u64,
vara_token_id: ActorId,
eth_token_id: H160,
amount: U256,
receiver: ActorId,
error: bool,
) {
let payload = [
"VftGateway".encode(),
"MintTokens".encode(),
(vara_token_id, receiver, amount).encode(),
(eth_token_id, receiver, amount).encode(),
]
.concat();
let result = self.send_bytes(from, payload);
Expand Down
14 changes: 9 additions & 5 deletions gear-programs/vft-gateway/src/wasm/tests/vft_gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,15 @@ fn test_mint_tokens_from_eth_client() {
.main_failed());

let vft_gateway = Program::vft_gateway(&system);
let eth_token_id = H160::default();
vft_gateway.map_vara_to_eth_address(ADMIN_ID, vft.id(), eth_token_id);

let receiver: u64 = 10000;
let receiver: u64 = 10_000;
let amount = U256::from(10_000_000_000_u64);

vft.grant_minter_role(ADMIN_ID, vft_gateway.id());

vft_gateway.mint_tokens(ETH_CLIENT_ID, vft.id(), amount, receiver.into(), false);
vft_gateway.mint_tokens(ETH_CLIENT_ID, eth_token_id, amount, receiver.into(), false);

let balance = vft.balance_of(receiver.into());
assert_eq!(balance, amount);
Expand All @@ -256,15 +258,17 @@ fn test_mint_tokens_from_arbitrary_address() {
.main_failed());

let vft_gateway = Program::vft_gateway(&system);
let eth_token_id = H160::default();
vft_gateway.map_vara_to_eth_address(ADMIN_ID, vft.id(), eth_token_id);

let receiver: u64 = 10000;
let receiver: u64 = 10_000;
let amount = U256::from(10_000_000_000_u64);

vft.grant_minter_role(ADMIN_ID, vft_gateway.id());

let wrond_address = 1010;
let wrond_address = 1_010;

vft_gateway.mint_tokens(wrond_address, vft.id(), amount, receiver.into(), true);
vft_gateway.mint_tokens(wrond_address, eth_token_id, amount, receiver.into(), true);
}

#[test]
Expand Down
3 changes: 2 additions & 1 deletion gear-programs/vft-gateway/src/wasm/vft-gateway.idl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Error = enum {
TokensRefunded,
NotEthClient,
NotEnoughGas,
NoCorrespondingVaraAddress,
};

type MessageInfo = struct {
Expand Down Expand Up @@ -71,7 +72,7 @@ constructor {
service VftGateway {
HandleInterruptedTransfer : (msg_id: message_id) -> result (struct { u256, h160 }, Error);
MapVaraToEthAddress : (vara_token_id: actor_id, eth_token_id: h160) -> null;
MintTokens : (vara_token_id: actor_id, receiver: actor_id, amount: u256) -> result (null, Error);
MintTokens : (eth_token_id: h160, receiver: actor_id, amount: u256) -> result (null, Error);
RemoveVaraToEthAddress : (vara_token_id: actor_id) -> null;
TransferVaraToEth : (vara_token_id: actor_id, amount: u256, receiver: h160) -> result (struct { u256, h160 }, Error);
UpdateConfig : (config: Config) -> null;
Expand Down
Loading