Skip to content

Commit

Permalink
Merge pull request mapprotocol#140 from PandaRR007/fix-deposit-native
Browse files Browse the repository at this point in the history
deposit native toke to wrap token before deposit out
  • Loading branch information
philllau authored Nov 28, 2022
2 parents c4c4401 + d14f89b commit 3175c62
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
17 changes: 12 additions & 5 deletions mcs/near/map-cross-chain-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,13 +538,13 @@ impl MapCrossChainService {
log!("{}{}", TRANSFER_OUT_TYPE, event);
}

/// Finish deposit out once the mcs token is burned from MCSToken contract.
/// Finish deposit out once the nep141 token is burned from MCSToken contract or native token is transferred.
pub fn finish_deposit_out(
&self,
event: DepositOutEvent,
) {
assert_self();
assert_eq!(PromiseResult::Successful(vec![]), env::promise_result(0), "burn mcs token failed");
assert_eq!(PromiseResult::Successful(vec![]), env::promise_result(0), "burn mcs token or call near_deposit() failed");
log!("deposit out: {}", serde_json::to_string(&event).unwrap());
log!("{}{}", DEPOSIT_OUT_TYPE, event);
}
Expand Down Expand Up @@ -585,7 +585,7 @@ impl MapCrossChainService {
}

#[payable]
pub fn deposit_out_native(&mut self, to: Vec<u8>) {
pub fn deposit_out_native(&mut self, to: Vec<u8>) -> Promise {
self.check_not_paused(PAUSE_DEPOSIT_OUT_NATIVE);
self.check_to_account(to.clone(), self.map_chain_id);

Expand All @@ -607,8 +607,15 @@ impl MapCrossChainService {
amount: amount.into(),
};

log!("deposit out: {}", serde_json::to_string(&event).unwrap());
log!("{}{}", DEPOSIT_OUT_TYPE, event);
ext_wnear_token::ext(self.wrapped_token.parse().unwrap())
.with_static_gas(NEAR_DEPOSIT_GAS)
.with_attached_deposit(env::attached_deposit())
.near_deposit()
.then(
Self::ext(env::current_account_id())
.with_static_gas(FINISH_DEPOSIT_OUT_GAS)
.finish_deposit_out(event)
)
}

#[payable]
Expand Down
40 changes: 26 additions & 14 deletions mcs/near/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3582,11 +3582,15 @@ async fn test_deposit_out_native() -> anyhow::Result<()> {
log!("balance_mcs_0:{}, balance_mcs_1:{}", balance_mcs_0, balance_mcs_1);
log!("{}, {}", balance_from_0- balance_from_1, balance_mcs_1 - balance_mcs_0);
assert!(balance_from_0 - balance_from_1 > amount);
assert!(balance_mcs_1 - balance_mcs_0 > amount);
assert!((balance_from_0 - balance_from_1 - amount) > (balance_mcs_1 - balance_mcs_0 - amount));
log!("{}", balance_from_0 - balance_from_1 - amount);
log!("{}", balance_mcs_1 - balance_mcs_0 - amount);
assert!(balance_mcs_1 - balance_mcs_0 < amount);

let balance = wnear
.call(&worker, "ft_balance_of")
.args_json((mcs.id().to_string(), ))?
.view()
.await?
.json::<U128>()?;
assert_eq!(amount, balance.0, "wnear balance of mcs contract account == transferred out native token amount");
Ok(())
}

Expand Down Expand Up @@ -3667,11 +3671,14 @@ async fn test_deposit_out_native_too_small() -> anyhow::Result<()> {
log!("balance_mcs_2:{}, balance_mcs_1:{}", balance_mcs_2, balance_mcs_1);
log!("{}, {}", balance_from_1- balance_from_2, balance_mcs_2 - balance_mcs_1);
assert!(balance_from_1 - balance_from_2 > amount);
assert!(balance_mcs_2 - balance_mcs_1 > amount);
assert!((balance_from_1 - balance_from_2 - amount) > (balance_mcs_2 - balance_mcs_1 - amount));
log!("{}", balance_from_1 - balance_from_2 - amount);
log!("{}", balance_mcs_2 - balance_mcs_1 - amount);

let balance = wnear
.call(&worker, "ft_balance_of")
.args_json((mcs.id().to_string(), ))?
.view()
.await?
.json::<U128>()?;
assert_eq!(amount, balance.0, "wnear balance of mcs contract account == transferred out native token amount");
Ok(())
}

Expand Down Expand Up @@ -3720,17 +3727,22 @@ async fn test_deposit_out_native_no_deposit() -> anyhow::Result<()> {
.await?;
assert!(res.is_success(), "deposit_out_native should succeed");
println!("logs: {:?}", res.logs());
assert!(res.logs().get(1).unwrap().contains(DEPOSIT_OUT_TYPE), "should be deposit out log");
assert!(res.logs().get(2).unwrap().contains(DEPOSIT_OUT_TYPE), "should be deposit out log");
let balance_from_2 = from.view_account(&worker).await?.balance;
let balance_mcs_2 = mcs.view_account(&worker).await?.balance;
log!("balance_from_2:{}, balance_from_1:{}", balance_from_2, balance_from_1);
log!("balance_mcs_2:{}, balance_mcs_1:{}", balance_mcs_2, balance_mcs_1);
log!("{}, {}", balance_from_1- balance_from_2, balance_mcs_2 - balance_mcs_1);
assert!(balance_from_1 - balance_from_2 > amount);
assert!(balance_mcs_2 - balance_mcs_1 > amount);
assert!((balance_from_1 - balance_from_2 - amount) > (balance_mcs_2 - balance_mcs_1 - amount));
log!("{}", balance_from_1 - balance_from_2 - amount);
log!("{}", balance_mcs_2 - balance_mcs_1 - amount);
assert!(balance_mcs_2 - balance_mcs_1 < amount);

let balance = wnear
.call(&worker, "ft_balance_of")
.args_json((mcs.id().to_string(), ))?
.view()
.await?
.json::<U128>()?;
assert_eq!(amount, balance.0, "wnear balance of mcs contract account == transferred out native token amount");

Ok(())
}
Expand Down Expand Up @@ -4630,7 +4642,7 @@ async fn test_deposit_out_mcs_token_amount_too_large() -> anyhow::Result<()> {
.transact()
.await;
assert!(res.is_err(), "deposit_out_token should failed");
assert!(res.as_ref().err().unwrap().to_string().contains("burn mcs token failed"), "should be burn mcs token failed error");
assert!(res.as_ref().err().unwrap().to_string().contains("burn mcs token or call near_deposit() failed"), "should be burn mcs token failed error");

let balance = from
.call(&worker, &token_account, "ft_balance_of")
Expand Down

0 comments on commit 3175c62

Please sign in to comment.