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): nft abi in withdraw_nft RPC, clear_nft_db RPC #2039

Merged
merged 16 commits into from
Feb 12, 2024
Merged

Conversation

laruh
Copy link
Member

@laruh laruh commented Dec 18, 2023

This PR introduces two new functions, erc1155_balance and erc721_owner, to the EthCoin structure.

These enhancements streamline the NFT withdrawal process in the withdraw_nft RPC by directly interacting with smart contracts for validation, eliminating the need to access database information.

Key Features and Benefits:

  • Direct Balance Check for ERC1155: The erc1155_balance function allows us to verify the current balance of ERC1155 tokens owned by a user by querying the smart contract directly.

  • Ownership Validation for ERC721: With erc721_owner, we can now confirm whether a user is the rightful owner of an ERC721 token at the time of the withdrawal request.

  • Prevent Invalid Withdrawal Requests: This update crucially enables the system to reject withdrawal requests if the user attempts to transfer an NFT using an RPC method that is incompatible with the NFT's contract type (such as using withdraw_erc1155 for an ERC721 token, and vice versa).

Technical Notes:

  • The ERC721 standard uniquely features the ownerOf function, unlike ERC1155.
  • Although both ERC721 and ERC1155 standards have a balanceOf function, they require different parameters (ERC721's balanceOf, ERC1155's balanceOf).

New clear_nft_db RPC for NFT data management was added.

This new method allows users to selectively clear NFT data for specified blockchain networks, or to completely wipe it across all supported chains.

Key Features:

  • Selective Clearing: Users can specify which blockchain networks (like Ethereum, BSC) to clear.
  • Global Clearing Option: With the clear_all flag set to true, the RPC clears NFT data from all chains. In this case chains parameter will be ignored.
  • Parameter Validation: When clear_all is false and no specific chains are provided, then the mm error occurs "Nothing to clear was specified".

Copy link
Member

@onur-ozkan onur-ozkan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First review iteration:

mm2src/coins/nft/storage/sql_storage.rs Outdated Show resolved Hide resolved
mm2src/coins/nft/storage/sql_storage.rs Outdated Show resolved Hide resolved
mm2src/coins/eth.rs Outdated Show resolved Hide resolved
mm2src/coins/nft.rs Outdated Show resolved Hide resolved
Copy link
Member

@borngraced borngraced left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great work! few changes from me

Comment on lines 129 to 181
/// `EnumVariantList` is a procedural macro used to generate a method that returns a vector containing all variants of an enum.
/// This macro is intended for use with simple enums (enums without associated data or complex structures).
///
/// ### USAGE:
///
/// ```rust
/// use enum_from::EnumVariantList;
///
/// #[derive(EnumVariantList)]
/// enum Chain {
/// Avalanche,
/// Bsc,
/// Eth,
/// Fantom,
/// Polygon,
/// }
///
///#[test]
///fn test_enum_variant_list() {
/// let all_chains = Chain::variant_list();
/// assert_eq!(all_chains, vec![
/// Chain::Avalanche,
/// Chain::Bsc,
/// Chain::Eth,
/// Chain::Fantom,
/// Chain::Polygon
/// ]);
///}
/// ```
#[proc_macro_derive(EnumVariantList)]
pub fn enum_variant_list(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = input.ident;

let variants = match input.data {
Data::Enum(DataEnum { variants, .. }) => variants,
Data::Struct(_) => return CompileError::expected_enum(ENUM_VARIANT_LIST_IDENT, "struct").into(),
Data::Union(_) => return CompileError::expected_enum(ENUM_VARIANT_LIST_IDENT, "union").into(),
};

let variant_list: Vec<_> = variants.iter().map(|v| &v.ident).collect();

let expanded = quote! {
impl #name {
pub fn variant_list() -> Vec<#name> {
vec![ #( #name::#variant_list ),* ]
}
}
};

TokenStream::from(expanded)
}

Copy link
Member

@borngraced borngraced Jan 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this macro should be In this mod by the way, the intention and name is quite different from what's expected from the mod just unlike the other macros there . maybe another mod or just create a derives mod in coins and move there

Copy link
Member Author

@laruh laruh Jan 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@borngraced yeah, I felt that this macro probably wont fit the lib name.
What do you think about renaming enum_from to enum_utilities so we can just leave enum_variant_list function in lib.rs and reuse all necessary already implemented types (like error) for EnumVariantList and any other future derive macro for enum.

Copy link
Member Author

@laruh laruh Jan 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this

derives
│
└── enum_utilities
    ├── lib.rs
    ├── from_inner.rs
    ├── from_stringify.rs
    └── from_trait.rs

If we leave enum_from lib name, then we need to add additional lib enum_to which actually could re-utilize implementations or types from enum_from bcz we desire to work with enums. I suggest to keep enum_utilities for different enum macros.

But if you would like to have different names, I will create new lib crate.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, you can rename for sure if it worth it.. what about enum_derives

cc. @shamardy

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

personally, I would lean towards enum_derives as it explicitly suggests that the crate is focused on deriving traits or functionalities for enums. also gives a clear indication of the crate's purpose.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, enum_derives is definitely a better name, changed it, thanks!

@@ -148,8 +203,8 @@ impl fmt::Display for MacroAttr {
struct CompileError(String);

impl CompileError {
Copy link
Member

@borngraced borngraced Jan 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can make CompileError public if you like to use externally.

mm2src/coins/eth.rs Show resolved Hide resolved
mm2src/coins/eth.rs Show resolved Hide resolved
@laruh laruh added in progress Changes will be made from the author and removed under review labels Jan 15, 2024
Comment on lines +106 to +109
pub fn new(table_name: &str) -> SqlResult<Self> {
validate_table_name(table_name)?;
Ok(SafeTableName(table_name.to_owned()))
}
Copy link
Member

@onur-ozkan onur-ozkan Jan 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently we can create SafeTableName without this function and read it with 'instance.0' . Maybe remove "pub" from the inner value to enforce using this function, then create another function for reading it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! made inner value private and impl get_name function SafeTableName

Comment on lines 397 to 400
/// Variants:
/// - `DbError`: Represents errors related to database operations.
/// - `Internal`: Indicates internal errors not directly associated with database operations.
/// - `InvalidRequest`: Used for various types of invalid requests, such as missing or contradictory parameters.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a good approach for documenting rust code. You should add the field-related docs on the fields directly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks for the note! I changed as many doc comments as I've noticed. If I see more, I will fix them.

Comment on lines 389 to 398
/// Parameters for withdrawing an ERC-1155 token.
///
/// Fields:
/// - `chain`: The blockchain network to perform the withdrawal on.
/// - `to`: The address to send the NFT to.
/// - `token_address`: The address of the ERC-1155 token contract.
/// - `token_id`: The unique identifier of the NFT to withdraw.
/// - `amount`: Optional amount of the token to withdraw. Defaults to 1 if not specified.
/// - `max`: If set to `true`, withdraws the maximum amount available. Overrides the `amount` field.
/// - `fee`: Optional details for the withdrawal fee.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here (there are other spots too but I don't want to ping them all)

@laruh laruh added under review and removed in progress Changes will be made from the author labels Jan 16, 2024
Copy link
Member

@onur-ozkan onur-ozkan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last review iteration

mm2src/db_common/src/sqlite.rs Outdated Show resolved Hide resolved
mm2src/coins/nft.rs Outdated Show resolved Hide resolved
mm2src/coins/nft/nft_errors.rs Show resolved Hide resolved
mm2src/coins/eth.rs Outdated Show resolved Hide resolved
shamardy
shamardy previously approved these changes Feb 2, 2024
Copy link
Collaborator

@shamardy shamardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Some minor non-blockers!

mm2src/coins/eth.rs Outdated Show resolved Hide resolved
mm2src/coins/eth.rs Outdated Show resolved Hide resolved
mm2src/coins/nft.rs Outdated Show resolved Hide resolved
@shamardy
Copy link
Collaborator

shamardy commented Feb 12, 2024

@KomodoPlatform/qa Since there are a lot of open PRs, this can be tested in dev. @laruh In addition to this #2039 (comment) you can add example request/responses for the QA team here or in the related issues #900, #1902
P.S. I want to start merging as many PRs as possible since a lot of conflicts are starting to appear, so I will bypass the waiting for QA and sec team approvals on PRs for now and instead the review/approvals can be done on release PR.

@shamardy shamardy merged commit f025652 into dev Feb 12, 2024
27 of 34 checks passed
@shamardy shamardy deleted the nft-enhancements branch February 12, 2024 15:39
dimxy added a commit to dimxy/komodo-defi-framework that referenced this pull request Feb 18, 2024
* evm-hd-wallet: (27 commits)
  Fix todo comments
  Fix HDAddressOps::Address trait bounds
  fix(indexeddb): fix IDB cursor.continue_() call after drop (KomodoPlatform#2028)
  security bump for `h2` (KomodoPlatform#2062)
  fix(makerbot): allow more than one prices url in makerbot (KomodoPlatform#2027)
  fix(wasm worker env): refactor direct usage of `window` (KomodoPlatform#1953)
  feat(nft): nft abi in withdraw_nft RPC, clear_nft_db RPC (KomodoPlatform#2039)
  refactor(utxo): refactor utxo output script creation (KomodoPlatform#1960)
  feat(ETH): balance event streaming for ETH (KomodoPlatform#2041)
  chore(release): bump mm2 version to 2.1.0-beta (KomodoPlatform#2044)
  feat(trezor): add segwit support for withdraw with trezor (KomodoPlatform#1984)
  chore(config): remove vscode launchjson (KomodoPlatform#2040)
  feat(trading-proto-upgrade): wasm DB, kickstart, refund states, v2 RPCs (KomodoPlatform#2015)
  feat(UTXO): balance event streaming for Electrum clients (KomodoPlatform#2013)
  feat(tx): add new sign_raw_transaction rpc for UTXO and EVM coins (KomodoPlatform#1930)
  fix(p2p): handle encode_and_sign errors (KomodoPlatform#2038)
  chore(release): add changelog entries for v2.0.0-beta (KomodoPlatform#2037)
  chore(network): write network information to stdout (KomodoPlatform#2034)
  fix(price_endpoints): add cached url (KomodoPlatform#2032)
  deps(network): sync with upstream yamux (KomodoPlatform#2030)
  ...
dimxy added a commit to dimxy/komodo-defi-framework that referenced this pull request Feb 25, 2024
* dev:
  feat(zcoin): ARRR WASM implementation (KomodoPlatform#1957)
  feat(trading-proto-upgrade): locked amounts, kmd burn and other impl (KomodoPlatform#2046)
  fix(indexeddb): set stop on success cursor condition (KomodoPlatform#2067)
  feat(config): add `max_concurrent_connections` to mm2 config (KomodoPlatform#2063)
  feat(stats_swaps): add gui/mm_version in stats db (KomodoPlatform#2061)
  fix(indexeddb): fix IDB cursor.continue_() call after drop (KomodoPlatform#2028)
  security bump for `h2` (KomodoPlatform#2062)
  fix(makerbot): allow more than one prices url in makerbot (KomodoPlatform#2027)
  fix(wasm worker env): refactor direct usage of `window` (KomodoPlatform#1953)
  feat(nft): nft abi in withdraw_nft RPC, clear_nft_db RPC (KomodoPlatform#2039)
  refactor(utxo): refactor utxo output script creation (KomodoPlatform#1960)
  feat(ETH): balance event streaming for ETH (KomodoPlatform#2041)
  chore(release): bump mm2 version to 2.1.0-beta (KomodoPlatform#2044)
  feat(trezor): add segwit support for withdraw with trezor (KomodoPlatform#1984)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants