Skip to content

Commit e3d9a9f

Browse files
committed
Merge branch 'dev' into wc-integration
2 parents fdc18d7 + df1077b commit e3d9a9f

30 files changed

+1864
-89
lines changed

.github/workflows/pr-lint.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ jobs:
2222
types: |
2323
feat
2424
fix
25+
improvement
2526
chore
2627
docs
2728
deps
@@ -38,9 +39,9 @@ jobs:
3839
TITLE: ${{ github.event.pull_request.title }}
3940
run: |
4041
title_length=${#TITLE}
41-
if [ $title_length -gt 72 ]
42+
if [ $title_length -gt 85 ]
4243
then
43-
echo "PR title is too long (greater than 72 characters)"
44+
echo "PR title is too long (greater than 85 characters)"
4445
exit 1
4546
fi
4647

Cargo.lock

+24-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ members = [
4040
"mm2src/proxy_signature",
4141
"mm2src/rpc_task",
4242
"mm2src/trezor",
43+
"mm2src/trading_api",
4344
]
4445

4546
exclude = [

mm2src/coins/eth.rs

+18-23
Original file line numberDiff line numberDiff line change
@@ -527,20 +527,6 @@ pub type Web3RpcFut<T> = Box<dyn Future<Item = T, Error = MmError<Web3RpcError>>
527527
pub type Web3RpcResult<T> = Result<T, MmError<Web3RpcError>>;
528528
type EthPrivKeyPolicy = PrivKeyPolicy<KeyPair>;
529529

530-
#[macro_export]
531-
macro_rules! wei_from_gwei_decimal {
532-
($big_decimal: expr) => {
533-
$crate::eth::wei_from_big_decimal($big_decimal, $crate::eth::ETH_GWEI_DECIMALS)
534-
};
535-
}
536-
537-
#[macro_export]
538-
macro_rules! wei_to_gwei_decimal {
539-
($gwei: expr) => {
540-
$crate::eth::u256_to_big_decimal($gwei, $crate::eth::ETH_GWEI_DECIMALS)
541-
};
542-
}
543-
544530
#[derive(Clone, Debug)]
545531
pub(crate) struct LegacyGasPrice {
546532
pub(crate) gas_price: U256,
@@ -585,11 +571,11 @@ impl TryFrom<PayForGasParams> for PayForGasOption {
585571
fn try_from(param: PayForGasParams) -> Result<Self, Self::Error> {
586572
match param {
587573
PayForGasParams::Legacy(legacy) => Ok(Self::Legacy(LegacyGasPrice {
588-
gas_price: wei_from_gwei_decimal!(&legacy.gas_price)?,
574+
gas_price: wei_from_gwei_decimal(&legacy.gas_price)?,
589575
})),
590576
PayForGasParams::Eip1559(eip1559) => Ok(Self::Eip1559(Eip1559FeePerGas {
591-
max_fee_per_gas: wei_from_gwei_decimal!(&eip1559.max_fee_per_gas)?,
592-
max_priority_fee_per_gas: wei_from_gwei_decimal!(&eip1559.max_priority_fee_per_gas)?,
577+
max_fee_per_gas: wei_from_gwei_decimal(&eip1559.max_fee_per_gas)?,
578+
max_priority_fee_per_gas: wei_from_gwei_decimal(&eip1559.max_priority_fee_per_gas)?,
593579
})),
594580
}
595581
}
@@ -1089,6 +1075,9 @@ impl EthCoinImpl {
10891075
let guard = self.erc20_tokens_infos.lock().unwrap();
10901076
(*guard).clone()
10911077
}
1078+
1079+
#[inline(always)]
1080+
pub fn chain_id(&self) -> u64 { self.chain_id }
10921081
}
10931082

10941083
async fn get_raw_transaction_impl(coin: EthCoin, req: RawTransactionRequest) -> RawTransactionResult {
@@ -1211,8 +1200,8 @@ pub async fn withdraw_erc1155(ctx: MmArc, withdraw_type: WithdrawErc1155) -> Wit
12111200
let fee_details = EthTxFeeDetails::new(gas, pay_for_gas_option, fee_coin)?;
12121201

12131202
Ok(TransactionNftDetails {
1214-
tx_hex: BytesJson::from(signed_bytes.to_vec()),
1215-
tx_hash: format!("{:02x}", signed.tx_hash_as_bytes()),
1203+
tx_hex: BytesJson::from(signed_bytes.to_vec()), // TODO: should we return tx_hex 0x-prefixed (everywhere)?
1204+
tx_hash: format!("{:02x}", signed.tx_hash_as_bytes()), // TODO: add 0x hash (use unified hash format for eth wherever it is returned)
12161205
from: vec![eth_coin.my_address()?],
12171206
to: vec![withdraw_type.to],
12181207
contract_type: ContractType::Erc1155,
@@ -1303,7 +1292,7 @@ pub async fn withdraw_erc721(ctx: MmArc, withdraw_type: WithdrawErc721) -> Withd
13031292

13041293
Ok(TransactionNftDetails {
13051294
tx_hex: BytesJson::from(signed_bytes.to_vec()),
1306-
tx_hash: format!("{:02x}", signed.tx_hash_as_bytes()),
1295+
tx_hash: format!("{:02x}", signed.tx_hash_as_bytes()), // TODO: add 0x hash (use unified hash format for eth wherever it is returned)
13071296
from: vec![eth_coin.my_address()?],
13081297
to: vec![withdraw_type.to],
13091298
contract_type: ContractType::Erc721,
@@ -2468,7 +2457,7 @@ impl MarketCoinOps for EthCoin {
24682457
let fut = async move {
24692458
coin.send_raw_transaction(bytes.into())
24702459
.await
2471-
.map(|res| format!("{:02x}", res))
2460+
.map(|res| format!("{:02x}", res)) // TODO: add 0x hash (use unified hash format for eth wherever it is returned)
24722461
.map_err(|e| ERRL!("{}", e))
24732462
};
24742463

@@ -4837,7 +4826,7 @@ impl EthCoin {
48374826
self.call(request, Some(BlockId::Number(BlockNumber::Latest))).await
48384827
}
48394828

4840-
fn allowance(&self, spender: Address) -> Web3RpcFut<U256> {
4829+
pub fn allowance(&self, spender: Address) -> Web3RpcFut<U256> {
48414830
let coin = self.clone();
48424831
let fut = async move {
48434832
match coin.coin_type {
@@ -4902,7 +4891,7 @@ impl EthCoin {
49024891
Box::new(fut.boxed().compat())
49034892
}
49044893

4905-
fn approve(&self, spender: Address, amount: U256) -> EthTxFut {
4894+
pub fn approve(&self, spender: Address, amount: U256) -> EthTxFut {
49064895
let coin = self.clone();
49074896
let fut = async move {
49084897
let token_addr = match coin.coin_type {
@@ -6268,6 +6257,12 @@ pub fn wei_from_big_decimal(amount: &BigDecimal, decimals: u8) -> NumConversResu
62686257
U256::from_dec_str(&amount).map_to_mm(|e| NumConversError::new(format!("{:?}", e)))
62696258
}
62706259

6260+
pub fn wei_from_gwei_decimal(bigdec: &BigDecimal) -> NumConversResult<U256> {
6261+
wei_from_big_decimal(bigdec, ETH_GWEI_DECIMALS)
6262+
}
6263+
6264+
pub fn wei_to_gwei_decimal(wei: U256) -> NumConversResult<BigDecimal> { u256_to_big_decimal(wei, ETH_GWEI_DECIMALS) }
6265+
62716266
impl Transaction for SignedEthTx {
62726267
fn tx_hex(&self) -> Vec<u8> { rlp::encode(self).to_vec() }
62736268

mm2src/coins/eth/eip1559_gas_fee.rs

+26-26
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Provides estimations of base and priority fee per gas or fetch estimations from a gas api provider
22
33
use super::web3_transport::FeeHistoryResult;
4-
use super::{Web3RpcError, Web3RpcResult};
5-
use crate::{wei_from_gwei_decimal, wei_to_gwei_decimal, EthCoin, NumConversError};
4+
use super::{wei_from_gwei_decimal, wei_to_gwei_decimal, Web3RpcError, Web3RpcResult};
5+
use crate::{EthCoin, NumConversError};
66
use ethereum_types::U256;
77
use mm2_err_handle::mm_error::MmError;
88
use mm2_err_handle::or_mm_error::OrMmError;
@@ -104,24 +104,24 @@ impl TryFrom<InfuraFeePerGas> for FeePerGasEstimated {
104104

105105
fn try_from(infura_fees: InfuraFeePerGas) -> Result<Self, Self::Error> {
106106
Ok(Self {
107-
base_fee: wei_from_gwei_decimal!(&infura_fees.estimated_base_fee)?,
107+
base_fee: wei_from_gwei_decimal(&infura_fees.estimated_base_fee)?,
108108
low: FeePerGasLevel {
109-
max_fee_per_gas: wei_from_gwei_decimal!(&infura_fees.low.suggested_max_fee_per_gas)?,
110-
max_priority_fee_per_gas: wei_from_gwei_decimal!(&infura_fees.low.suggested_max_priority_fee_per_gas)?,
109+
max_fee_per_gas: wei_from_gwei_decimal(&infura_fees.low.suggested_max_fee_per_gas)?,
110+
max_priority_fee_per_gas: wei_from_gwei_decimal(&infura_fees.low.suggested_max_priority_fee_per_gas)?,
111111
min_wait_time: Some(infura_fees.low.min_wait_time_estimate),
112112
max_wait_time: Some(infura_fees.low.max_wait_time_estimate),
113113
},
114114
medium: FeePerGasLevel {
115-
max_fee_per_gas: wei_from_gwei_decimal!(&infura_fees.medium.suggested_max_fee_per_gas)?,
116-
max_priority_fee_per_gas: wei_from_gwei_decimal!(
117-
&infura_fees.medium.suggested_max_priority_fee_per_gas
115+
max_fee_per_gas: wei_from_gwei_decimal(&infura_fees.medium.suggested_max_fee_per_gas)?,
116+
max_priority_fee_per_gas: wei_from_gwei_decimal(
117+
&infura_fees.medium.suggested_max_priority_fee_per_gas,
118118
)?,
119119
min_wait_time: Some(infura_fees.medium.min_wait_time_estimate),
120120
max_wait_time: Some(infura_fees.medium.max_wait_time_estimate),
121121
},
122122
high: FeePerGasLevel {
123-
max_fee_per_gas: wei_from_gwei_decimal!(&infura_fees.high.suggested_max_fee_per_gas)?,
124-
max_priority_fee_per_gas: wei_from_gwei_decimal!(&infura_fees.high.suggested_max_priority_fee_per_gas)?,
123+
max_fee_per_gas: wei_from_gwei_decimal(&infura_fees.high.suggested_max_fee_per_gas)?,
124+
max_priority_fee_per_gas: wei_from_gwei_decimal(&infura_fees.high.suggested_max_priority_fee_per_gas)?,
125125
min_wait_time: Some(infura_fees.high.min_wait_time_estimate),
126126
max_wait_time: Some(infura_fees.high.max_wait_time_estimate),
127127
},
@@ -143,33 +143,33 @@ impl TryFrom<BlocknativeBlockPricesResponse> for FeePerGasEstimated {
143143
return Ok(FeePerGasEstimated::default());
144144
}
145145
Ok(Self {
146-
base_fee: wei_from_gwei_decimal!(&block_prices.block_prices[0].base_fee_per_gas)?,
146+
base_fee: wei_from_gwei_decimal(&block_prices.block_prices[0].base_fee_per_gas)?,
147147
low: FeePerGasLevel {
148-
max_fee_per_gas: wei_from_gwei_decimal!(
149-
&block_prices.block_prices[0].estimated_prices[2].max_fee_per_gas
148+
max_fee_per_gas: wei_from_gwei_decimal(
149+
&block_prices.block_prices[0].estimated_prices[2].max_fee_per_gas,
150150
)?,
151-
max_priority_fee_per_gas: wei_from_gwei_decimal!(
152-
&block_prices.block_prices[0].estimated_prices[2].max_priority_fee_per_gas
151+
max_priority_fee_per_gas: wei_from_gwei_decimal(
152+
&block_prices.block_prices[0].estimated_prices[2].max_priority_fee_per_gas,
153153
)?,
154154
min_wait_time: None,
155155
max_wait_time: None,
156156
},
157157
medium: FeePerGasLevel {
158-
max_fee_per_gas: wei_from_gwei_decimal!(
159-
&block_prices.block_prices[0].estimated_prices[1].max_fee_per_gas
158+
max_fee_per_gas: wei_from_gwei_decimal(
159+
&block_prices.block_prices[0].estimated_prices[1].max_fee_per_gas,
160160
)?,
161-
max_priority_fee_per_gas: wei_from_gwei_decimal!(
162-
&block_prices.block_prices[0].estimated_prices[1].max_priority_fee_per_gas
161+
max_priority_fee_per_gas: wei_from_gwei_decimal(
162+
&block_prices.block_prices[0].estimated_prices[1].max_priority_fee_per_gas,
163163
)?,
164164
min_wait_time: None,
165165
max_wait_time: None,
166166
},
167167
high: FeePerGasLevel {
168-
max_fee_per_gas: wei_from_gwei_decimal!(
169-
&block_prices.block_prices[0].estimated_prices[0].max_fee_per_gas
168+
max_fee_per_gas: wei_from_gwei_decimal(
169+
&block_prices.block_prices[0].estimated_prices[0].max_fee_per_gas,
170170
)?,
171-
max_priority_fee_per_gas: wei_from_gwei_decimal!(
172-
&block_prices.block_prices[0].estimated_prices[0].max_priority_fee_per_gas
171+
max_priority_fee_per_gas: wei_from_gwei_decimal(
172+
&block_prices.block_prices[0].estimated_prices[0].max_priority_fee_per_gas,
173173
)?,
174174
min_wait_time: None,
175175
max_wait_time: None,
@@ -260,7 +260,7 @@ impl FeePerGasSimpleEstimator {
260260
let max_priority_fee_per_gas = Self::percentile_of(&level_rewards, Self::PRIORITY_FEE_PERCENTILES[level_index]);
261261
// Convert the priority fee to BigDecimal gwei, falling back to 0 on error.
262262
let max_priority_fee_per_gas_gwei =
263-
wei_to_gwei_decimal!(max_priority_fee_per_gas).unwrap_or_else(|_| BigDecimal::from(0));
263+
wei_to_gwei_decimal(max_priority_fee_per_gas).unwrap_or_else(|_| BigDecimal::from(0));
264264

265265
// Calculate the max fee per gas by adjusting the base fee and adding the priority fee.
266266
let adjust_max_fee =
@@ -273,7 +273,7 @@ impl FeePerGasSimpleEstimator {
273273

274274
Ok(FeePerGasLevel {
275275
max_priority_fee_per_gas,
276-
max_fee_per_gas: wei_from_gwei_decimal!(&max_fee_per_gas_dec)?,
276+
max_fee_per_gas: wei_from_gwei_decimal(&max_fee_per_gas_dec)?,
277277
// TODO: Consider adding default wait times if applicable (and mark them as uncertain).
278278
min_wait_time: None,
279279
max_wait_time: None,
@@ -290,7 +290,7 @@ impl FeePerGasSimpleEstimator {
290290
.first()
291291
.cloned()
292292
.unwrap_or_else(|| U256::from(0));
293-
let latest_base_fee_dec = wei_to_gwei_decimal!(latest_base_fee).unwrap_or_else(|_| BigDecimal::from(0));
293+
let latest_base_fee_dec = wei_to_gwei_decimal(latest_base_fee).unwrap_or_else(|_| BigDecimal::from(0));
294294

295295
// The predicted base fee is not used for calculating eip1559 values here and is provided for other purposes
296296
// (f.e if the caller would like to do own estimates of max fee and max priority fee)

mm2src/coins/eth/eth_rpc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ impl EthCoin {
241241
.and_then(|t| serde_json::from_value(t).map_err(Into::into))
242242
}
243243

244-
/// Get chain id
245-
pub(crate) async fn chain_id(&self) -> Result<U256, web3::Error> {
244+
/// Get chain id from network
245+
pub(crate) async fn network_chain_id(&self) -> Result<U256, web3::Error> {
246246
self.try_rpc_send("eth_chainId", vec![])
247247
.await
248248
.and_then(|t| serde_json::from_value(t).map_err(Into::into))

mm2src/coins/eth/eth_tests.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1042,3 +1042,10 @@ fn test_gas_limit_conf() {
10421042
&& eth_coin.gas_limit.eth_max_trade_gas == 150_000
10431043
);
10441044
}
1045+
1046+
#[test]
1047+
fn test_h256_to_str() {
1048+
let h = H256::from_str("5136701f11060010841c9708c3eb26f6606a070b8ae43f4b98b6d7b10a545258").unwrap();
1049+
let b: BytesJson = h.0.to_vec().into();
1050+
println!("H256={}", format!("0x{:02x}", b));
1051+
}

mm2src/coins/lp_coins.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,8 @@ pub mod coins_tests;
220220
pub mod eth;
221221
use eth::erc20::get_erc20_ticker_by_contract_address;
222222
use eth::eth_swap_v2::{PaymentStatusErr, PrepareTxDataError, ValidatePaymentV2Err};
223-
use eth::GetValidEthWithdrawAddError;
224223
use eth::{eth_coin_from_conf_and_request, get_eth_address, EthCoin, EthGasDetailsErr, EthTxFeeDetails,
225-
GetEthAddressError, SignedEthTx};
224+
GetEthAddressError, GetValidEthWithdrawAddError, SignedEthTx};
226225

227226
pub mod hd_wallet;
228227
use hd_wallet::{AccountUpdatingError, AddressDerivingError, HDAccountOps, HDAddressId, HDAddressOps, HDCoinAddress,
@@ -661,6 +660,10 @@ impl TransactionErr {
661660
}
662661
}
663662

663+
impl std::fmt::Display for TransactionErr {
664+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "{}", self.get_plain_text_format()) }
665+
}
666+
664667
#[derive(Debug, PartialEq)]
665668
pub enum FoundSwapTxSpend {
666669
Spent(TransactionEnum),

mm2src/coins/rpc_command/get_estimated_fees.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! RPCs to start/stop gas fee estimator and get estimated base and priority fee per gas
22
3-
use crate::eth::{EthCoin, EthCoinType, FeeEstimatorContext, FeeEstimatorState, FeePerGasEstimated};
4-
use crate::{lp_coinfind_or_err, wei_to_gwei_decimal, AsyncMutex, CoinFindError, MmCoinEnum, NumConversError};
3+
use crate::eth::{wei_to_gwei_decimal, EthCoin, EthCoinType, FeeEstimatorContext, FeeEstimatorState, FeePerGasEstimated};
4+
use crate::{lp_coinfind_or_err, AsyncMutex, CoinFindError, MmCoinEnum, NumConversError};
55
use common::executor::{spawn_abortable, Timer};
66
use common::log::debug;
77
use common::{HttpStatusCode, StatusCode};
@@ -66,22 +66,22 @@ impl TryFrom<FeePerGasEstimated> for FeePerGasEstimatedExt {
6666

6767
fn try_from(fees: FeePerGasEstimated) -> Result<Self, Self::Error> {
6868
Ok(Self {
69-
base_fee: wei_to_gwei_decimal!(fees.base_fee)?,
69+
base_fee: wei_to_gwei_decimal(fees.base_fee)?,
7070
low: FeePerGasLevel {
71-
max_fee_per_gas: wei_to_gwei_decimal!(fees.low.max_fee_per_gas)?,
72-
max_priority_fee_per_gas: wei_to_gwei_decimal!(fees.low.max_priority_fee_per_gas)?,
71+
max_fee_per_gas: wei_to_gwei_decimal(fees.low.max_fee_per_gas)?,
72+
max_priority_fee_per_gas: wei_to_gwei_decimal(fees.low.max_priority_fee_per_gas)?,
7373
min_wait_time: fees.low.min_wait_time,
7474
max_wait_time: fees.low.max_wait_time,
7575
},
7676
medium: FeePerGasLevel {
77-
max_fee_per_gas: wei_to_gwei_decimal!(fees.medium.max_fee_per_gas)?,
78-
max_priority_fee_per_gas: wei_to_gwei_decimal!(fees.medium.max_priority_fee_per_gas)?,
77+
max_fee_per_gas: wei_to_gwei_decimal(fees.medium.max_fee_per_gas)?,
78+
max_priority_fee_per_gas: wei_to_gwei_decimal(fees.medium.max_priority_fee_per_gas)?,
7979
min_wait_time: fees.medium.min_wait_time,
8080
max_wait_time: fees.medium.max_wait_time,
8181
},
8282
high: FeePerGasLevel {
83-
max_fee_per_gas: wei_to_gwei_decimal!(fees.high.max_fee_per_gas)?,
84-
max_priority_fee_per_gas: wei_to_gwei_decimal!(fees.high.max_priority_fee_per_gas)?,
83+
max_fee_per_gas: wei_to_gwei_decimal(fees.high.max_fee_per_gas)?,
84+
max_priority_fee_per_gas: wei_to_gwei_decimal(fees.high.max_priority_fee_per_gas)?,
8585
min_wait_time: fees.high.min_wait_time,
8686
max_wait_time: fees.high.max_wait_time,
8787
},

0 commit comments

Comments
 (0)