-
Notifications
You must be signed in to change notification settings - Fork 118
feat(trading-proto-upgrade): locked amounts, kmd burn and other impl #2046
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
Changes from 58 commits
f5eb0fc
2e3f79b
8b1676b
ec6d5e2
15aca49
1f2de9b
eaf0ace
e8b99a3
0464eec
fd60519
d9ff38e
c361507
94dee63
fbf9650
6c9ab43
e1d4908
98493fb
fd53c39
a5fffe7
d684fa7
1f109f1
7bd7755
a8969b3
be9d8d9
5c7e43c
8b9fb7a
d745e73
46a91b7
222813a
a1aeb96
ee098d3
55ed473
d719246
186064c
d8dbb18
afcc59b
4cb48af
88d279b
b500c81
6b7e5ba
2efddbd
e44fc68
6bb2c27
7a0756d
1e76676
b924154
3578c17
4a5c7b2
b746028
e04ff1a
9071b1a
8369d90
79768b4
e02ed59
57352d2
c6ea8fa
52aaa16
442e759
e1b42aa
3d76e08
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -111,16 +111,17 @@ use crate::nft::{find_wallet_nft_amount, WithdrawNftResult}; | |
| use v2_activation::{build_address_and_priv_key_policy, EthActivationV2Error}; | ||
|
|
||
| mod nonce; | ||
| use crate::coin_errors::ValidatePaymentResult; | ||
| use crate::{PrivKeyPolicy, TransactionResult, WithdrawFrom}; | ||
| use nonce::ParityNonce; | ||
|
|
||
| /// https://github.com/artemii235/etomic-swap/blob/master/contracts/EtomicSwap.sol | ||
| /// Dev chain (195.201.137.5:8565) contract address: 0x83965C539899cC0F918552e5A26915de40ee8852 | ||
| /// Ropsten: https://ropsten.etherscan.io/address/0x7bc1bbdd6a0a722fc9bffc49c921b685ecb84b94 | ||
| /// ETH mainnet: https://etherscan.io/address/0x8500AFc0bc5214728082163326C2FF0C73f4a871 | ||
| const SWAP_CONTRACT_ABI: &str = include_str!("eth/swap_contract_abi.json"); | ||
| pub const SWAP_CONTRACT_ABI: &str = include_str!("eth/swap_contract_abi.json"); | ||
| /// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md | ||
| const ERC20_ABI: &str = include_str!("eth/erc20_abi.json"); | ||
| pub const ERC20_ABI: &str = include_str!("eth/erc20_abi.json"); | ||
| /// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md | ||
| const ERC721_ABI: &str = include_str!("eth/erc721_abi.json"); | ||
| /// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1155.md | ||
|
|
@@ -417,7 +418,7 @@ pub struct EthCoinImpl { | |
| ticker: String, | ||
| pub coin_type: EthCoinType, | ||
| priv_key_policy: EthPrivKeyPolicy, | ||
| my_address: Address, | ||
| pub my_address: Address, | ||
| sign_message_prefix: Option<String>, | ||
| swap_contract_address: Address, | ||
| fallback_swap_contract: Option<Address>, | ||
|
|
@@ -1128,13 +1129,13 @@ impl SwapOps for EthCoin { | |
| } | ||
|
|
||
| #[inline] | ||
| fn validate_maker_payment(&self, input: ValidatePaymentInput) -> ValidatePaymentFut<()> { | ||
| self.validate_payment(input) | ||
| async fn validate_maker_payment(&self, input: ValidatePaymentInput) -> ValidatePaymentResult<()> { | ||
| self.validate_payment(input).compat().await | ||
| } | ||
|
|
||
| #[inline] | ||
| fn validate_taker_payment(&self, input: ValidatePaymentInput) -> ValidatePaymentFut<()> { | ||
| self.validate_payment(input) | ||
| async fn validate_taker_payment(&self, input: ValidatePaymentInput) -> ValidatePaymentResult<()> { | ||
| self.validate_payment(input).compat().await | ||
| } | ||
|
|
||
| fn check_if_my_payment_sent( | ||
|
|
@@ -1481,7 +1482,7 @@ impl WatcherOps for EthCoin { | |
| .watcher_reward | ||
| .clone() | ||
| .ok_or_else(|| ValidatePaymentError::WatcherRewardError("Watcher reward not found".to_string()))); | ||
| let expected_reward_amount = try_f!(wei_from_big_decimal(&watcher_reward.amount, self.decimals)); | ||
| let expected_reward_amount = try_f!(wei_from_big_decimal(&watcher_reward.amount, ETH_DECIMALS)); | ||
|
|
||
| let expected_swap_contract_address = try_f!(input | ||
| .swap_contract_address | ||
|
|
@@ -1659,10 +1660,12 @@ impl WatcherOps for EthCoin { | |
| .map_to_mm(ValidatePaymentError::TxDeserializationError)?; | ||
| let total_amount = match input.spend_type { | ||
| WatcherSpendType::MakerPaymentSpend => { | ||
| if let RewardTarget::None = watcher_reward.reward_target { | ||
| trade_amount | ||
| } else { | ||
| if !matches!(watcher_reward.reward_target, RewardTarget::None) | ||
| || watcher_reward.send_contract_reward_on_spend | ||
| { | ||
| trade_amount + expected_reward_amount | ||
| } else { | ||
| trade_amount | ||
| } | ||
| }, | ||
| WatcherSpendType::TakerPaymentRefund => trade_amount + expected_reward_amount, | ||
|
|
@@ -1737,7 +1740,6 @@ impl WatcherOps for EthCoin { | |
| }; | ||
| let expected_swap_contract_address = self.swap_contract_address; | ||
| let fallback_swap_contract = self.fallback_swap_contract; | ||
| let decimals = self.decimals; | ||
|
|
||
| let fut = async move { | ||
| let tx_from_rpc = selfi.web3.eth().transaction(TransactionId::Hash(tx.hash)).await?; | ||
|
|
@@ -1781,7 +1783,7 @@ impl WatcherOps for EthCoin { | |
| .get_taker_watcher_reward(&input.maker_coin, None, None, None, input.wait_until) | ||
| .await | ||
| .map_err(|err| ValidatePaymentError::WatcherRewardError(err.into_inner().to_string()))?; | ||
| let expected_reward_amount = wei_from_big_decimal(&watcher_reward.amount, decimals)?; | ||
| let expected_reward_amount = wei_from_big_decimal(&watcher_reward.amount, ETH_DECIMALS)?; | ||
|
|
||
| match &selfi.coin_type { | ||
| EthCoinType::Eth => { | ||
|
|
@@ -1992,7 +1994,6 @@ impl WatcherOps for EthCoin { | |
| RewardTarget::PaymentSender | ||
| }; | ||
|
|
||
| let is_exact_amount = reward_amount.is_some(); | ||
| let amount = match reward_amount { | ||
| Some(amount) => amount, | ||
| None => self.get_watcher_reward_amount(wait_until).await?, | ||
|
|
@@ -2002,7 +2003,7 @@ impl WatcherOps for EthCoin { | |
|
|
||
| Ok(WatcherReward { | ||
| amount, | ||
| is_exact_amount, | ||
| is_exact_amount: false, | ||
| reward_target, | ||
| send_contract_reward_on_spend, | ||
| }) | ||
|
|
@@ -3365,7 +3366,7 @@ impl EthCoin { | |
| let data = match &args.watcher_reward { | ||
| Some(reward) => { | ||
| let reward_amount = try_tx_fus!(wei_from_big_decimal(&reward.amount, self.decimals)); | ||
| if !matches!(reward.reward_target, RewardTarget::None) { | ||
| if !matches!(reward.reward_target, RewardTarget::None) || reward.send_contract_reward_on_spend { | ||
| value += reward_amount; | ||
| } | ||
|
|
||
|
|
@@ -3403,14 +3404,33 @@ impl EthCoin { | |
| let mut value = U256::from(0); | ||
| let mut amount = trade_amount; | ||
|
|
||
| debug!("Using watcher reward {:?} for swap payment", args.watcher_reward); | ||
|
|
||
| let data = match args.watcher_reward { | ||
| Some(reward) => { | ||
| let reward_amount = try_tx_fus!(wei_from_big_decimal(&reward.amount, self.decimals)); | ||
|
|
||
| match reward.reward_target { | ||
| RewardTarget::Contract | RewardTarget::PaymentSender => value += reward_amount, | ||
| RewardTarget::PaymentSpender => amount += reward_amount, | ||
| _ => (), | ||
| let reward_amount = match reward.reward_target { | ||
| RewardTarget::Contract | RewardTarget::PaymentSender => { | ||
| let eth_reward_amount = try_tx_fus!(wei_from_big_decimal(&reward.amount, ETH_DECIMALS)); | ||
| value += eth_reward_amount; | ||
| eth_reward_amount | ||
| }, | ||
| RewardTarget::PaymentSpender => { | ||
| let token_reward_amount = | ||
| try_tx_fus!(wei_from_big_decimal(&reward.amount, self.decimals)); | ||
| amount += token_reward_amount; | ||
| token_reward_amount | ||
| }, | ||
| _ => { | ||
| // TODO tests passed without this change, need to research on how it worked | ||
| if reward.send_contract_reward_on_spend { | ||
| let eth_reward_amount = | ||
| try_tx_fus!(wei_from_big_decimal(&reward.amount, ETH_DECIMALS)); | ||
| value += eth_reward_amount; | ||
| eth_reward_amount | ||
| } else { | ||
| 0.into() | ||
| } | ||
|
Comment on lines
+3430
to
+3438
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please specify which tests pass? As I can see when
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIRC, all watcher tests were passing with old testnet environment but started to fail after switching to dockerized Geth. And when it's true, the contract attempts to send additional ETH, which isn't available on its balance. With this change, the reward ETH is also transferred to a contract during payment, making ETH/ERC20 watcher tests pass.
I agree with this. My goal was to make tests green and learn ETH watchers code/understand it better. It seems that
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah, missed this since I was looking for cases with
Sure. Thanks a lot for offering your help on this :) |
||
| }, | ||
| }; | ||
|
|
||
| try_tx_fus!(function.encode_input(&[ | ||
|
|
@@ -4319,7 +4339,11 @@ impl EthCoin { | |
| )?; | ||
|
|
||
| match watcher_reward.reward_target { | ||
| RewardTarget::None | RewardTarget::PaymentReceiver => (), | ||
| RewardTarget::None | RewardTarget::PaymentReceiver => { | ||
| if watcher_reward.send_contract_reward_on_spend { | ||
| expected_value += actual_reward_amount | ||
| } | ||
| }, | ||
| RewardTarget::PaymentSender | RewardTarget::PaymentSpender | RewardTarget::Contract => { | ||
| expected_value += actual_reward_amount | ||
| }, | ||
|
|
@@ -4407,7 +4431,23 @@ impl EthCoin { | |
| ))); | ||
| } | ||
|
|
||
| let expected_reward_amount = wei_from_big_decimal(&watcher_reward.amount, decimals)?; | ||
| let expected_reward_amount = match watcher_reward.reward_target { | ||
| RewardTarget::Contract | RewardTarget::PaymentSender => { | ||
| wei_from_big_decimal(&watcher_reward.amount, ETH_DECIMALS)? | ||
| }, | ||
| RewardTarget::PaymentSpender => { | ||
| wei_from_big_decimal(&watcher_reward.amount, selfi.decimals)? | ||
| }, | ||
| _ => { | ||
| // TODO tests passed without this change, need to research on how it worked | ||
| if watcher_reward.send_contract_reward_on_spend { | ||
| wei_from_big_decimal(&watcher_reward.amount, ETH_DECIMALS)? | ||
| } else { | ||
| 0.into() | ||
| } | ||
| }, | ||
| }; | ||
|
|
||
| let actual_reward_amount = get_function_input_data(&decoded, function, 8) | ||
| .map_to_mm(ValidatePaymentError::TxDeserializationError)? | ||
| .into_uint() | ||
|
|
@@ -4428,7 +4468,11 @@ impl EthCoin { | |
| expected_value += actual_reward_amount | ||
| }, | ||
| RewardTarget::PaymentSpender => expected_amount += actual_reward_amount, | ||
| _ => (), | ||
| _ => { | ||
| if watcher_reward.send_contract_reward_on_spend { | ||
| expected_value += actual_reward_amount | ||
| } | ||
| }, | ||
| }; | ||
|
|
||
| if decoded[1] != Token::Uint(expected_amount) { | ||
|
|
@@ -4442,7 +4486,7 @@ impl EthCoin { | |
| if tx_from_rpc.value != expected_value { | ||
| return MmError::err(ValidatePaymentError::WrongPaymentTx(format!( | ||
| "Payment tx value arg {:?} is invalid, expected {:?}", | ||
| tx_from_rpc.value, trade_amount | ||
| tx_from_rpc.value, expected_value | ||
| ))); | ||
| } | ||
| }, | ||
|
|
@@ -4579,8 +4623,8 @@ impl EthCoin { | |
| .map_err(|_| WatcherRewardError::RPCError("Error getting the gas price".to_string()))?; | ||
|
|
||
| let gas_cost_wei = U256::from(REWARD_GAS_AMOUNT) * gas_price; | ||
| let gas_cost_eth = | ||
| u256_to_big_decimal(gas_cost_wei, 18).map_err(|e| WatcherRewardError::InternalError(e.to_string()))?; | ||
| let gas_cost_eth = u256_to_big_decimal(gas_cost_wei, ETH_DECIMALS) | ||
| .map_err(|e| WatcherRewardError::InternalError(e.to_string()))?; | ||
| Ok(gas_cost_eth) | ||
| } | ||
|
|
||
|
|
@@ -5598,7 +5642,7 @@ pub async fn eth_coin_from_conf_and_request( | |
|
|
||
| /// Displays the address in mixed-case checksum form | ||
| /// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md | ||
| fn checksum_address(addr: &str) -> String { | ||
| pub fn checksum_address(addr: &str) -> String { | ||
| let mut addr = addr.to_lowercase(); | ||
| if addr.starts_with("0x") { | ||
| addr.replace_range(..2, ""); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.