Skip to content

Commit

Permalink
fix: Fix nft request for wasm target (#1817)
Browse files Browse the repository at this point in the history
* remove body from send_moralis_request for wasm target

* test moralis requests

* use constants for urls

* return target check for test

* CHANGELOG entry

---------

Reviewed-by: ozkanonur <[email protected]>, shamardy <[email protected]>
  • Loading branch information
laruh authored May 11, 2023
1 parent 1c261cd commit fbe9f16
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
- Improved protocol to let only the taker pay the reward
- Add passive parent coin state for keeping tokens active when platform is disabled [#1763](https://github.com/KomodoPlatform/atomicDEX-API/pull/1763)
- Detect a chain reorganization, if it occurs, redownload and revalidate the new best chain headers for SPV [#1728](https://github.com/KomodoPlatform/atomicDEX-API/pull/1728)

- Fix moralis request in wasm target, add moralis tests [#1817](https://github.com/KomodoPlatform/atomicDEX-API/pull/1817)

## v1.0.3-beta - 2023-04-28

Expand Down
3 changes: 1 addition & 2 deletions mm2src/coins/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use mm2_err_handle::prelude::{MmError, MmResult};

pub(crate) mod nft_errors;
pub(crate) mod nft_structs;
#[cfg(any(test, target_arch = "wasm32"))] mod nft_tests;

use crate::WithdrawError;
use nft_errors::GetNftInfoError;
Expand Down Expand Up @@ -243,8 +244,6 @@ async fn send_moralis_request(uri: &str) -> MmResult<Json, GetNftInfoError> {
}

let result = FetchRequest::get(uri)
.cors()
.body_utf8("".to_owned())
.header(ACCEPT.as_str(), APPLICATION_JSON)
.request_str()
.await;
Expand Down
80 changes: 80 additions & 0 deletions mm2src/coins/nft/nft_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const NFT_LIST_URL_TEST: &str = "https://moralis-proxy.komodo.earth/api/v2/0x394d86994f954ed931b86791b62fe64f4c5dac37/nft?chain=POLYGON&format=decimal";
const NFT_HISTORY_URL_TEST: &str = "https://moralis-proxy.komodo.earth/api/v2/0x394d86994f954ed931b86791b62fe64f4c5dac37/nft/transfers?chain=POLYGON&format=decimal&direction=both";
const NFT_METADATA_URL_TEST: &str = "https://moralis-proxy.komodo.earth/api/v2/nft/0xed55e4477b795eaa9bb4bca24df42214e1a05c18/1111777?chain=POLYGON&format=decimal";
const TEST_WALLET_ADDR_EVM: &str = "0x394d86994f954ed931b86791b62fe64f4c5dac37";

#[cfg(all(test, not(target_arch = "wasm32")))]
mod native_tests {
use crate::nft::nft_structs::{NftTransferHistoryWrapper, NftWrapper};
use crate::nft::nft_tests::{NFT_HISTORY_URL_TEST, NFT_LIST_URL_TEST, NFT_METADATA_URL_TEST, TEST_WALLET_ADDR_EVM};
use crate::nft::send_moralis_request;
use common::block_on;

#[test]
fn test_moralis_nft_list() {
let response = block_on(send_moralis_request(NFT_LIST_URL_TEST)).unwrap();
let nfts_list = response["result"].as_array().unwrap();
assert_eq!(2, nfts_list.len());
for nft_json in nfts_list {
let nft_wrapper: NftWrapper = serde_json::from_str(&nft_json.to_string()).unwrap();
assert_eq!(TEST_WALLET_ADDR_EVM, nft_wrapper.owner_of)
}
}

#[test]
fn test_moralis_nft_transfer_history() {
let response = block_on(send_moralis_request(NFT_HISTORY_URL_TEST)).unwrap();
let transfer_list = response["result"].as_array().unwrap();
assert_eq!(2, transfer_list.len());
for transfer in transfer_list {
let transfer_wrapper: NftTransferHistoryWrapper = serde_json::from_str(&transfer.to_string()).unwrap();
assert_eq!(TEST_WALLET_ADDR_EVM, transfer_wrapper.to_address);
}
}

#[test]
fn test_moralis_nft_metadata() {
let response = block_on(send_moralis_request(NFT_METADATA_URL_TEST)).unwrap();
let nft_wrapper: NftWrapper = serde_json::from_str(&response.to_string()).unwrap();
assert_eq!(41237364, *nft_wrapper.block_number_minted)
}
}

#[cfg(target_arch = "wasm32")]
mod wasm_tests {
use crate::nft::nft_structs::{NftTransferHistoryWrapper, NftWrapper};
use crate::nft::nft_tests::{NFT_HISTORY_URL_TEST, NFT_LIST_URL_TEST, NFT_METADATA_URL_TEST, TEST_WALLET_ADDR_EVM};
use crate::nft::send_moralis_request;
use wasm_bindgen_test::*;

wasm_bindgen_test_configure!(run_in_browser);

#[wasm_bindgen_test]
async fn test_moralis_nft_list() {
let response = send_moralis_request(NFT_LIST_URL_TEST).await.unwrap();
let nfts_list = response["result"].as_array().unwrap();
assert_eq!(2, nfts_list.len());
for nft_json in nfts_list {
let nft_wrapper: NftWrapper = serde_json::from_str(&nft_json.to_string()).unwrap();
assert_eq!(TEST_WALLET_ADDR_EVM, nft_wrapper.owner_of)
}
}

#[wasm_bindgen_test]
async fn test_moralis_nft_transfer_history() {
let response = send_moralis_request(NFT_HISTORY_URL_TEST).await.unwrap();
let transfer_list = response["result"].as_array().unwrap();
assert_eq!(2, transfer_list.len());
for transfer in transfer_list {
let transfer_wrapper: NftTransferHistoryWrapper = serde_json::from_str(&transfer.to_string()).unwrap();
assert_eq!(TEST_WALLET_ADDR_EVM, transfer_wrapper.to_address);
}
}

#[wasm_bindgen_test]
async fn test_moralis_nft_metadata() {
let response = send_moralis_request(NFT_METADATA_URL_TEST).await.unwrap();
let nft_wrapper: NftWrapper = serde_json::from_str(&response.to_string()).unwrap();
assert_eq!(41237364, *nft_wrapper.block_number_minted)
}
}

0 comments on commit fbe9f16

Please sign in to comment.