Skip to content

Commit

Permalink
Minor fixes for Binance Smart Chain/BEP20 integration #801 (#876)
Browse files Browse the repository at this point in the history
* Temporary comment the usage of estimate_gas in get_fee_to_send_taker_fee.

* Update web3 and always use None block number in estimate_gas.

* Stop excluding mm2.1-bsc from CI.

* Fix CI pipeline.

* Fix CI pipeline.

* Stop always running Test MM2 on Linux to speed up the build.

* Fix max ETH withdraw.

* Put back the comment about possible estimate_gas failure.
  • Loading branch information
artemii235 committed Mar 23, 2021
1 parent e14a556 commit a1e4cbf
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions azure-pipelines-build-stage-job.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ jobs:
WASM_BINDGEN_TEST_TIMEOUT: 120
GECKODRIVER: '/home/azureagent/wasm/geckodriver'
# Explicit --test-threads=16 makes testing process slightly faster on agents that have <16 CPU cores.
# Always run tests on mm2.1 branch and PRs
- bash: |
cargo test --all -- --test-threads=16
displayName: 'Test MM2'
Expand All @@ -55,6 +56,7 @@ jobs:
ALICE_PASSPHRASE: $(${{ parameters.alice_passphrase }})
ALICE_USERPASS: $(${{ parameters.alice_userpass }})
RUST_LOG: debug
condition: or( eq( variables['Build.Reason'], 'PullRequest' ), eq( variables['Build.SourceBranchName'], 'mm2.1' ) )
- bash: |
cargo clippy -- -D warnings
displayName: 'Check Clippy warnings'
Expand Down
2 changes: 1 addition & 1 deletion etomic_build/client/buy_ONE_ANOTHER
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ curl --url "http://127.0.0.1:7783" --data '
"method":"buy",
"base":"'$1'",
"rel":"'$2'",
"volume":"1",
"volume":"0.1",
"price":"2"
}
'
3 changes: 3 additions & 0 deletions etomic_build/client/enable_ADEX
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
source userpass
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"enable\",\"coin\":\"ADEX\",\"urls\":[\"https://bsc-dataseed.binance.org\"],\"swap_contract_address\":\"0xeDc5b89Fe1f0382F9E4316069971D90a0951DB31\"}"
3 changes: 3 additions & 0 deletions etomic_build/client/enable_BNB
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
source userpass
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"enable\",\"coin\":\"BNB\",\"urls\":[\"https://bsc-dataseed.binance.org\"],\"swap_contract_address\":\"0xeDc5b89Fe1f0382F9E4316069971D90a0951DB31\"}"
3 changes: 3 additions & 0 deletions etomic_build/client/enable_BNBT
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
source userpass
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"enable\",\"coin\":\"BNBT\",\"urls\":[\"https://data-seed-prebsc-1-s2.binance.org:8545\"],\"swap_contract_address\":\"0xcCD17C913aD7b772755Ad4F0BDFF7B34C6339150\"}"
22 changes: 12 additions & 10 deletions mm2src/coins/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,9 @@ impl EthCoinImpl {
Box::new(fut)
}

fn estimate_gas(
&self,
req: CallRequest,
block: Option<BlockNumber>,
) -> Box<dyn Future<Item = U256, Error = web3::Error> + Send> {
Box::new(self.web3.eth().estimate_gas(req, block))
fn estimate_gas(&self, req: CallRequest) -> Box<dyn Future<Item = U256, Error = web3::Error> + Send> {
// always using None block number as old Geth version accept only single argument in this RPC
Box::new(self.web3.eth().estimate_gas(req, None))
}

/// Gets `ReceiverSpent` events from etomic swap smart contract since `from_block`
Expand Down Expand Up @@ -425,8 +422,14 @@ async fn withdraw_impl(ctx: MmArc, coin: EthCoin, req: WithdrawRequest) -> Resul
Some(_) => return ERR!("Unsupported input fee type"),
None => {
let gas_price = try_s!(coin.get_gas_price().compat().await);
// covering edge case by deducting the standard transfer fee when we want to max withdraw ETH
let eth_value_for_estimate = if req.max && coin.coin_type == EthCoinType::Eth {
eth_value - gas_price * U256::from(21000)
} else {
eth_value
};
let estimate_gas_req = CallRequest {
value: Some(eth_value),
value: Some(eth_value_for_estimate),
data: Some(data.clone().into()),
from: Some(coin.my_address),
to: call_addr,
Expand All @@ -435,7 +438,7 @@ async fn withdraw_impl(ctx: MmArc, coin: EthCoin, req: WithdrawRequest) -> Resul
// logic on gas price, e.g. TUSD: https://github.com/KomodoPlatform/atomicDEX-API/issues/643
gas_price: Some(gas_price),
};
let gas_fut = coin.estimate_gas(estimate_gas_req, None).compat();
let gas_fut = coin.estimate_gas(estimate_gas_req).compat();
(try_s!(gas_fut.await), gas_price)
},
};
Expand Down Expand Up @@ -2602,10 +2605,9 @@ impl MmCoin for EthCoin {
// Please note if the wallet's balance is insufficient to withdraw, then `estimate_gas` may fail with the `Exception` error.
// Ideally we should determine the case when we have the insufficient balance and return `TradePreimageError::NotSufficientBalance` error.
let gas_limit = try_map!(
coin.estimate_gas(estimate_gas_req, None).compat().await,
coin.estimate_gas(estimate_gas_req).compat().await,
TradePreimageError::Other
);

let total_fee = gas_limit * gas_price;
let amount = try_map!(u256_to_big_decimal(total_fee, 18), TradePreimageError::Other);
Ok(TradeFee {
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/eth/eth_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ fn test_get_fee_to_send_taker_fee() {

EthCoinImpl::get_gas_price.mock_safe(|_| MockResult::Return(Box::new(futures01::future::ok(GAS_PRICE.into()))));
EthCoinImpl::estimate_gas
.mock_safe(|_, _, _| MockResult::Return(Box::new(futures01::future::ok(TRANSFER_GAS_LIMIT.into()))));
.mock_safe(|_, _| MockResult::Return(Box::new(futures01::future::ok(TRANSFER_GAS_LIMIT.into()))));

// fee to send taker fee is `TRANSFER_GAS_LIMIT * gas_price` always.
let amount = u256_to_big_decimal((TRANSFER_GAS_LIMIT * GAS_PRICE).into(), 18).expect("!u256_to_big_decimal");
Expand Down

0 comments on commit a1e4cbf

Please sign in to comment.