Skip to content

Commit

Permalink
refactor(vft-gateway): Remove Vara address from MintTokens service/me…
Browse files Browse the repository at this point in the history
…ssage (#133)
  • Loading branch information
gshep authored Sep 23, 2024
1 parent 95660c2 commit f628119
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
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>,
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 : (sender: actor_id, vara_token_id: actor_id, amount: u256, receiver: h160) -> result (struct { u256, h160 }, Error);
UpdateConfig : (config: Config) -> null;
Expand Down

0 comments on commit f628119

Please sign in to comment.