Skip to content
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

Enhance enable with tokens methods #1762

Merged
merged 14 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
**Enhancements/Fixes:**
- cosmos/iris ethermint account compatibility implemented [#1765](https://github.com/KomodoPlatform/atomicDEX-API/pull/1765)
- p2p stack is improved [#1755](https://github.com/KomodoPlatform/atomicDEX-API/pull/1755)
- - Validate topics if they are mixed or not.
- - Do early return if the message data is not valid (since no point to iterate over and over on the invalid message)
- - Break the loop right after processing any of `SWAP_PREFIX`, `WATCHER_PREFIX`, `TX_HELPER_PREFIX` topic.
- Validate topics if they are mixed or not.
- Do early return if the message data is not valid (since no point to iterate over and over on the invalid message)
- Break the loop right after processing any of `SWAP_PREFIX`, `WATCHER_PREFIX`, `TX_HELPER_PREFIX` topic.
- An issue was fixed where we don't have to wait for all EVM nodes to sync the latest account nonce [#1757](https://github.com/KomodoPlatform/atomicDEX-API/pull/1757)
- optimized dev and release compilation profiles and removed ci [#1759](https://github.com/KomodoPlatform/atomicDEX-API/pull/1759)
- fix receiver trade fee for cosmos swaps [#1767](https://github.com/KomodoPlatform/atomicDEX-API/pull/1767)
- All features were enabled to be checked under x86-64 code lint CI step with `--all-features` option [#1760](https://github.com/KomodoPlatform/atomicDEX-API/pull/1760)
- use OS generated secrets for cryptographically secure randomness in `maker_swap` and `tendermint_coin::get_sender_trade_fee_for_denom` [#1753](https://github.com/KomodoPlatform/atomicDEX-API/pull/1753)
- Some enhancements were done for `enable_bch_with_tokens`,`enable_eth_with_tokens`,`enable_tendermint_with_assets` RPCs in [#1762](https://github.com/KomodoPlatform/atomicDEX-API/pull/1762)
- A new parameter `get_balances` was added to the above methods requests, when this parameter is set to `false`, balances will not be returned in the response. The default value for this parameter is `true` to ensure backward compatibility.
- Token balances requests are now performed concurrently for the above methods.


## v1.0.2-beta - 2023-04-11
Expand Down
22 changes: 11 additions & 11 deletions mm2src/coins/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use ethereum_types::{Address, H160, H256, U256};
use ethkey::{public_to_address, KeyPair, Public, Signature};
use ethkey::{sign, verify_address};
use futures::compat::Future01CompatExt;
use futures::future::{join_all, select_ok, Either, FutureExt, TryFutureExt};
use futures::future::{join_all, select_ok, try_join_all, Either, FutureExt, TryFutureExt};
use futures01::Future;
use http::StatusCode;
use mm2_core::mm_ctx::{MmArc, MmWeak};
Expand Down Expand Up @@ -3430,19 +3430,19 @@ impl EthCoin {
}

pub async fn get_tokens_balance_list(&self) -> Result<HashMap<String, CoinBalance>, MmError<BalanceError>> {
let coin = self.clone();
let mut token_balances = HashMap::new();
for (token_ticker, info) in self.get_erc_tokens_infos().iter() {
let balance_as_u256 = coin.get_token_balance_by_address(info.token_address).await?;
let balance_as_big_decimal = u256_to_big_decimal(balance_as_u256, info.decimals)?;
let balance = CoinBalance {
spendable: balance_as_big_decimal,
unspendable: BigDecimal::from(0),
let coin = || self;
let mut requests = Vec::new();
for (token_ticker, info) in self.get_erc_tokens_infos() {
let fut = async move {
let balance_as_u256 = coin().get_token_balance_by_address(info.token_address).await?;
let balance_as_big_decimal = u256_to_big_decimal(balance_as_u256, info.decimals)?;
let balance = CoinBalance::new(balance_as_big_decimal);
Ok((token_ticker, balance))
};
token_balances.insert(token_ticker.clone(), balance);
requests.push(fut);
}

Ok(token_balances)
try_join_all(requests).await.map(|res| res.into_iter().collect())
}

async fn get_token_balance_by_address(&self, token_address: Address) -> Result<U256, MmError<BalanceError>> {
Expand Down
3 changes: 1 addition & 2 deletions mm2src/coins/solana.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,9 @@ impl SolanaCoin {
Ok((hash, fees))
}

pub async fn my_balance_spl(&self, infos: &SplTokenInfo) -> Result<CoinBalance, MmError<BalanceError>> {
pub async fn my_balance_spl(&self, infos: SplTokenInfo) -> Result<CoinBalance, MmError<BalanceError>> {
let token_accounts = async_blocking({
let coin = self.clone();
rozhkovdmitrii marked this conversation as resolved.
Show resolved Hide resolved
let infos = infos.clone();
move || {
coin.rpc().get_token_accounts_by_owner(
&coin.key_pair.pubkey(),
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/solana/spl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ impl SplToken {
let coin = self.clone();
let fut = async move {
coin.platform_coin
.my_balance_spl(&SplTokenInfo {
.my_balance_spl(SplTokenInfo {
token_contract_address: coin.conf.token_contract_address,
decimals: coin.conf.decimals,
})
Expand Down
24 changes: 16 additions & 8 deletions mm2src/coins/tendermint/tendermint_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use cosmrs::tx::{self, Fee, Msg, Raw, SignDoc, SignerInfo};
use cosmrs::{AccountId, Any, Coin, Denom, ErrorReport};
use crypto::{privkey::key_pair_from_secret, Secp256k1Secret, StandardHDPathToCoin};
use derive_more::Display;
use futures::future::try_join_all;
use futures::lock::Mutex as AsyncMutex;
use futures::{FutureExt, TryFutureExt};
use futures01::Future;
Expand Down Expand Up @@ -422,17 +423,24 @@ impl TendermintCommons for TendermintCoin {
let platform_balance = big_decimal_from_sat_unsigned(platform_balance_denom, self.decimals);
let ibc_assets_info = self.tokens_info.lock().clone();

let mut result = AllBalancesResult {
platform_balance,
tokens_balances: HashMap::new(),
};
let mut requests = Vec::new();
for (ticker, info) in ibc_assets_info {
let balance_denom = self.balance_for_denom(info.denom.to_string()).await?;
let balance_decimal = big_decimal_from_sat_unsigned(balance_denom, info.decimals);
result.tokens_balances.insert(ticker, balance_decimal);
let fut = async move {
let balance_denom = self
.balance_for_denom(info.denom.to_string())
.await
.map_err(|e| e.into_inner())?;
let balance_decimal = big_decimal_from_sat_unsigned(balance_denom, info.decimals);
Ok::<_, TendermintCoinRpcError>((ticker.clone(), balance_decimal))
};
requests.push(fut);
}
let tokens_balances = try_join_all(requests).await?.into_iter().collect();

Ok(result)
Ok(AllBalancesResult {
platform_balance,
tokens_balances,
})
}

#[inline(always)]
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/tendermint/tendermint_tx_history_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@ pub async fn tendermint_history_loop(
coin: TendermintCoin,
storage: impl TxHistoryStorage,
_ctx: MmArc,
_current_balance: BigDecimal,
_current_balance: Option<BigDecimal>,
) {
let balances = match coin.all_balances().await {
Ok(balances) => balances,
Expand Down
37 changes: 28 additions & 9 deletions mm2src/coins/utxo/utxo_tx_history_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,18 +679,37 @@ pub async fn bch_and_slp_history_loop(
coin: BchCoin,
storage: impl TxHistoryStorage,
metrics: MetricsArc,
current_balance: BigDecimal,
current_balance: Option<BigDecimal>,
) {
let my_address = match coin.my_address() {
Ok(my_address) => my_address,
Err(e) => {
error!("{}", e);
return;
let balances = match current_balance {
Some(current_balance) => {
let my_address = match coin.my_address() {
Ok(my_address) => my_address,
Err(e) => {
error!("{}", e);
return;
},
};
HashMap::from([(my_address, current_balance)])
},
None => {
let ticker = coin.ticker().to_string();
match retry_on_err!(async { coin.my_addresses_balances().await })
.until_ready()
.repeat_every_secs(30.)
.inspect_err(move |e| {
error!("Error {e:?} on balance fetching for the coin {}", ticker);
})
.await
{
Ok(addresses_balances) => addresses_balances,
Err(e) => {
error!("{}", e);
return;
},
}
},
};
let mut balances = HashMap::new();
balances.insert(my_address, current_balance);
drop_mutability!(balances);

let ctx = UtxoTxHistoryCtx {
coin,
Expand Down
73 changes: 42 additions & 31 deletions mm2src/coins_activation/src/bch_with_tokens_activation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use coins::utxo::UtxoCommonOps;
use coins::{CoinBalance, CoinProtocol, MarketCoinOps, MmCoin, PrivKeyBuildPolicy, PrivKeyPolicyNotAllowed,
UnexpectedDerivationMethod};
use common::executor::{AbortSettings, SpawnAbortable};
use common::true_f;
use common::Future01CompatExt;
use crypto::CryptoCtxError;
use mm2_core::mm_ctx::MmArc;
Expand Down Expand Up @@ -119,6 +120,8 @@ pub struct BchWithTokensActivationRequest {
#[serde(flatten)]
platform_request: BchActivationRequest,
slp_tokens_requests: Vec<TokenActivationRequest<SlpActivationRequest>>,
#[serde(default = "true_f")]
pub get_balances: bool,
}

impl TxHistory for BchWithTokensActivationRequest {
Expand Down Expand Up @@ -149,11 +152,11 @@ pub struct BchWithTokensActivationResult {
}

impl GetPlatformBalance for BchWithTokensActivationResult {
fn get_platform_balance(&self) -> BigDecimal {
fn get_platform_balance(&self) -> Option<BigDecimal> {
self.bch_addresses_infos
.iter()
.fold(BigDecimal::from(0), |total, (_, addr_info)| {
&total + &addr_info.balances.get_total()
.fold(Some(BigDecimal::from(0)), |total, (_, addr_info)| {
total.and_then(|t| addr_info.balances.as_ref().map(|b| t + b.get_total()))
})
}
}
Expand Down Expand Up @@ -244,52 +247,60 @@ impl PlatformWithTokensActivationOps for BchCoin {

async fn get_activation_result(
&self,
activation_request: &Self::ActivationRequest,
) -> Result<BchWithTokensActivationResult, MmError<BchWithTokensActivationError>> {
let current_block = self.as_ref().rpc_client.get_block_count().compat().await?;

let my_address = self.as_ref().derivation_method.single_addr_or_err()?;
let my_slp_address = self
.get_my_slp_address()
.map_to_mm(BchWithTokensActivationError::Internal)?
.encode()
.map_to_mm(BchWithTokensActivationError::Internal)?;

let current_block = self.as_ref().rpc_client.get_block_count().compat().await?;

let bch_unspents = self.bch_unspents_for_display(my_address).await?;
let bch_balance = bch_unspents.platform_balance(self.decimals());

let mut token_balances = HashMap::new();
for (token_ticker, info) in self.get_slp_tokens_infos().iter() {
let token_balance = bch_unspents.slp_token_balance(&info.token_id, info.decimals);
token_balances.insert(token_ticker.clone(), token_balance);
}

let mut result = BchWithTokensActivationResult {
current_block,
bch_addresses_infos: HashMap::new(),
slp_addresses_infos: HashMap::new(),
let pubkey = self.my_public_key()?.to_string();

let (bch_balance, token_balances) = if activation_request.get_balances {
let bch_unspents = self.bch_unspents_for_display(my_address).await?;
let token_balances = self
.get_slp_tokens_infos()
.iter()
.map(|(token_ticker, info)| {
let token_balance = bch_unspents.slp_token_balance(&info.token_id, info.decimals);
(token_ticker.clone(), token_balance)
})
.collect();
(
Some(bch_unspents.platform_balance(self.decimals())),
Some(token_balances),
)
} else {
(None, None)
};

result
.bch_addresses_infos
.insert(my_address.to_string(), CoinAddressInfo {
derivation_method: DerivationMethod::Iguana,
pubkey: self.my_public_key()?.to_string(),
balances: bch_balance,
});
let bch_addresses_infos = HashMap::from([(my_address.to_string(), CoinAddressInfo {
derivation_method: DerivationMethod::Iguana,
pubkey: pubkey.clone(),
balances: bch_balance,
})]);

result.slp_addresses_infos.insert(my_slp_address, CoinAddressInfo {
let slp_addresses_infos = HashMap::from([(my_slp_address, CoinAddressInfo {
derivation_method: DerivationMethod::Iguana,
pubkey: self.my_public_key()?.to_string(),
pubkey,
balances: token_balances,
});
Ok(result)
})]);

Ok(BchWithTokensActivationResult {
current_block,
bch_addresses_infos,
slp_addresses_infos,
})
}

fn start_history_background_fetching(
&self,
ctx: MmArc,
storage: impl TxHistoryStorage + Send + 'static,
initial_balance: BigDecimal,
initial_balance: Option<BigDecimal>,
) {
let fut = bch_and_slp_history_loop(self.clone(), storage, ctx.metrics.clone(), initial_balance);

Expand Down
Loading