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

feat(nft): move db lock, add tx fee and confirmations #1989

Merged
merged 20 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
d0268a6
move db to NftCtx, async_sqlite_connection into ctx, use nft_ctx.get_…
laruh Sep 28, 2023
f19dab7
use `AsyncMutex` for `nft_cache_db` and `async_sqlite_connection`, pr…
laruh Oct 5, 2023
a6bc793
polish doc com
laruh Oct 6, 2023
60ad989
add `async_conn_tests`
laruh Oct 8, 2023
31ff933
move code blocks in wasm storage, remove dead_code
laruh Oct 11, 2023
d97f584
use oneshot from futures crate
laruh Oct 18, 2023
78c8a05
use `get_fee_details` func in `get_moralis_nft_transfers`
laruh Oct 19, 2023
31d70df
transfers confirmations
laruh Oct 23, 2023
d34c3cd
remove package
laruh Oct 31, 2023
47c3970
use `HashMap::with_capacity`, `to_ticker` returns `&'static str`, rem…
laruh Nov 2, 2023
632f7bf
use `BigUint` for `token_id`, import `prelude::*`, use `cfg_wasm32!`,…
laruh Nov 7, 2023
af1b000
match pattern, add serialize_token_id, deserialize_token_id
laruh Nov 7, 2023
8c7aff8
`deserialize_token_id` in `Nft` and `NftTransferHistory`
laruh Nov 7, 2023
7e3ef91
find `current_block` in chains concurrently
laruh Nov 8, 2023
eb67871
add doc comms for sqlite connections
laruh Nov 8, 2023
0dccc83
use try_join_all, deprecated doc coms, display for `LockDBError`, rem…
laruh Nov 9, 2023
2c240fc
define func locally, yse absolute db path, Internal err instead of Ot…
laruh Nov 10, 2023
c9127ff
close async conn in stop rpc
laruh Nov 17, 2023
2560e37
use match when we `canonicalize` the `sqlite_file_path`
laruh Nov 21, 2023
4ba539f
don't update spam transfers with empty meta
laruh Dec 7, 2023
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
3 changes: 3 additions & 0 deletions Cargo.lock

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

8 changes: 4 additions & 4 deletions mm2src/coins/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ pub struct EthCoinImpl {
swap_contract_address: Address,
fallback_swap_contract: Option<Address>,
contract_supports_watchers: bool,
web3: Web3<Web3Transport>,
pub(crate) web3: Web3<Web3Transport>,
/// The separate web3 instances kept to get nonce, will replace the web3 completely soon
web3_instances: Vec<Web3Instance>,
decimals: u8,
Expand Down Expand Up @@ -875,7 +875,7 @@ async fn withdraw_impl(coin: EthCoin, req: WithdrawRequest) -> WithdrawResult {
/// `withdraw_erc1155` function returns details of `ERC-1155` transaction including tx hex,
/// which should be sent to`send_raw_transaction` RPC to broadcast the transaction.
pub async fn withdraw_erc1155(ctx: MmArc, withdraw_type: WithdrawErc1155) -> WithdrawNftResult {
let coin = lp_coinfind_or_err(&ctx, &withdraw_type.chain.to_ticker()).await?;
let coin = lp_coinfind_or_err(&ctx, withdraw_type.chain.to_ticker()).await?;
let (to_addr, token_addr, eth_coin) =
get_valid_nft_add_to_withdraw(coin, &withdraw_type.to, &withdraw_type.token_address)?;
let my_address = eth_coin.my_address()?;
Expand Down Expand Up @@ -977,7 +977,7 @@ pub async fn withdraw_erc1155(ctx: MmArc, withdraw_type: WithdrawErc1155) -> Wit
/// `withdraw_erc721` function returns details of `ERC-721` transaction including tx hex,
/// which should be sent to`send_raw_transaction` RPC to broadcast the transaction.
pub async fn withdraw_erc721(ctx: MmArc, withdraw_type: WithdrawErc721) -> WithdrawNftResult {
let coin = lp_coinfind_or_err(&ctx, &withdraw_type.chain.to_ticker()).await?;
let coin = lp_coinfind_or_err(&ctx, withdraw_type.chain.to_ticker()).await?;
let (to_addr, token_addr, eth_coin) =
get_valid_nft_add_to_withdraw(coin, &withdraw_type.to, &withdraw_type.token_address)?;
let my_address = eth_coin.my_address()?;
Expand Down Expand Up @@ -4713,7 +4713,7 @@ pub struct EthTxFeeDetails {
}

impl EthTxFeeDetails {
fn new(gas: U256, gas_price: U256, coin: &str) -> NumConversResult<EthTxFeeDetails> {
pub(crate) fn new(gas: U256, gas_price: U256, coin: &str) -> NumConversResult<EthTxFeeDetails> {
let total_fee = gas * gas_price;
// Fees are always paid in ETH, can use 18 decimals by default
let total_fee = u256_to_big_decimal(total_fee, ETH_DECIMALS)?;
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/my_tx_history_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ where
_ => {},
};

let confirmations = if details.block_height == 0 || details.block_height > current_block {
let confirmations = if details.block_height > current_block {
0
} else {
current_block + 1 - details.block_height
Expand Down
174 changes: 126 additions & 48 deletions mm2src/coins/nft.rs

Large diffs are not rendered by default.

96 changes: 76 additions & 20 deletions mm2src/coins/nft/nft_errors.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use crate::eth::GetEthAddressError;
use crate::nft::storage::{CreateNftStorageError, NftStorageError};
use crate::{GetMyAddressError, WithdrawError};
#[cfg(target_arch = "wasm32")]
use crate::nft::storage::wasm::WasmNftCacheError;
use crate::nft::storage::NftStorageError;
use crate::{CoinFindError, GetMyAddressError, WithdrawError};
use common::{HttpStatusCode, ParseRfc3339Err};
#[cfg(not(target_arch = "wasm32"))]
use db_common::sqlite::rusqlite::Error as SqlError;
use derive_more::Display;
use enum_from::EnumFromStringify;
use http::StatusCode;
Expand Down Expand Up @@ -38,6 +42,7 @@ pub enum GetNftInfoError {
#[display(fmt = "The contract type is required and should not be null.")]
ContractTypeIsNull,
ProtectFromSpamError(ProtectFromSpamError),
TransferConfirmationsError(TransferConfirmationsError),
}

impl From<GetNftInfoError> for WithdrawError {
Expand Down Expand Up @@ -73,14 +78,6 @@ impl From<GetEthAddressError> for GetNftInfoError {
fn from(e: GetEthAddressError) -> Self { GetNftInfoError::GetEthAddressError(e) }
}

impl From<CreateNftStorageError> for GetNftInfoError {
fn from(e: CreateNftStorageError) -> Self {
match e {
CreateNftStorageError::Internal(err) => GetNftInfoError::Internal(err),
}
}
}

impl<T: NftStorageError> From<T> for GetNftInfoError {
fn from(err: T) -> Self { GetNftInfoError::DbError(format!("{:?}", err)) }
}
Expand All @@ -104,6 +101,14 @@ impl From<ProtectFromSpamError> for GetNftInfoError {
fn from(e: ProtectFromSpamError) -> Self { GetNftInfoError::ProtectFromSpamError(e) }
}

impl From<LockDBError> for GetNftInfoError {
fn from(e: LockDBError) -> Self { GetNftInfoError::DbError(e.to_string()) }
}

impl From<TransferConfirmationsError> for GetNftInfoError {
fn from(e: TransferConfirmationsError) -> Self { GetNftInfoError::TransferConfirmationsError(e) }
}

impl HttpStatusCode for GetNftInfoError {
fn status_code(&self) -> StatusCode {
match self {
Expand All @@ -115,7 +120,8 @@ impl HttpStatusCode for GetNftInfoError {
| GetNftInfoError::GetEthAddressError(_)
| GetNftInfoError::TokenNotFoundInWallet { .. }
| GetNftInfoError::DbError(_)
| GetNftInfoError::ProtectFromSpamError(_) => StatusCode::INTERNAL_SERVER_ERROR,
| GetNftInfoError::ProtectFromSpamError(_)
| GetNftInfoError::TransferConfirmationsError(_) => StatusCode::INTERNAL_SERVER_ERROR,
}
}
}
Expand Down Expand Up @@ -184,14 +190,14 @@ pub enum UpdateNftError {
#[from_stringify("serde_json::Error")]
SerdeError(String),
ProtectFromSpamError(ProtectFromSpamError),
}

impl From<CreateNftStorageError> for UpdateNftError {
fn from(e: CreateNftStorageError) -> Self {
match e {
CreateNftStorageError::Internal(err) => UpdateNftError::Internal(err),
}
}
#[display(fmt = "No such coin {}", coin)]
NoSuchCoin {
coin: String,
},
#[display(fmt = "{} coin doesn't support NFT", coin)]
CoinDoesntSupportNft {
coin: String,
},
}

impl From<GetNftInfoError> for UpdateNftError {
Expand All @@ -218,6 +224,18 @@ impl From<ProtectFromSpamError> for UpdateNftError {
fn from(e: ProtectFromSpamError) -> Self { UpdateNftError::ProtectFromSpamError(e) }
}

impl From<LockDBError> for UpdateNftError {
fn from(e: LockDBError) -> Self { UpdateNftError::DbError(e.to_string()) }
}

impl From<CoinFindError> for UpdateNftError {
fn from(e: CoinFindError) -> Self {
match e {
CoinFindError::NoSuchCoin { coin } => UpdateNftError::NoSuchCoin { coin },
}
}
}

impl HttpStatusCode for UpdateNftError {
fn status_code(&self) -> StatusCode {
match self {
Expand All @@ -234,7 +252,9 @@ impl HttpStatusCode for UpdateNftError {
| UpdateNftError::UpdateSpamPhishingError(_)
| UpdateNftError::GetInfoFromUriError(_)
| UpdateNftError::SerdeError(_)
| UpdateNftError::ProtectFromSpamError(_) => StatusCode::INTERNAL_SERVER_ERROR,
| UpdateNftError::ProtectFromSpamError(_)
| UpdateNftError::NoSuchCoin { .. }
| UpdateNftError::CoinDoesntSupportNft { .. } => StatusCode::INTERNAL_SERVER_ERROR,
}
}
}
Expand Down Expand Up @@ -310,3 +330,39 @@ pub(crate) enum MetaFromUrlError {
impl From<GetInfoFromUriError> for MetaFromUrlError {
fn from(e: GetInfoFromUriError) -> Self { MetaFromUrlError::GetInfoFromUriError(e) }
}

#[derive(Debug, Display)]
pub enum LockDBError {
#[cfg(target_arch = "wasm32")]
WasmNftCacheError(WasmNftCacheError),
#[cfg(not(target_arch = "wasm32"))]
SqlError(SqlError),
}

#[cfg(not(target_arch = "wasm32"))]
impl From<SqlError> for LockDBError {
fn from(e: SqlError) -> Self { LockDBError::SqlError(e) }
}

#[cfg(target_arch = "wasm32")]
impl From<WasmNftCacheError> for LockDBError {
fn from(e: WasmNftCacheError) -> Self { LockDBError::WasmNftCacheError(e) }
}

#[derive(Clone, Debug, Deserialize, Display, PartialEq, Serialize)]
pub enum TransferConfirmationsError {
#[display(fmt = "No such coin {}", coin)]
NoSuchCoin { coin: String },
#[display(fmt = "{} coin doesn't support NFT", coin)]
CoinDoesntSupportNft { coin: String },
#[display(fmt = "Get current block error: {}", _0)]
GetCurrentBlockErr(String),
}

impl From<CoinFindError> for TransferConfirmationsError {
fn from(e: CoinFindError) -> Self {
match e {
CoinFindError::NoSuchCoin { coin } => TransferConfirmationsError::NoSuchCoin { coin },
}
}
}
Loading
Loading