Skip to content

Commit

Permalink
fix: Minor fixes for v1.0.4-beta release (#1827)
Browse files Browse the repository at this point in the history
* lock SCP github action to v0.8.0

* use strict types for requests in nft_structs.rs

* Remove unneeded comment in block_header_utxo_loop

* use try_into with expect instead of type casting in now_sec_u32/now_sec_i64

* fix nft_tests

* use Url methods to construct the moralis requests urls

---------

Reviewed-by: ozkanonur <contact@onurozkan.dev>, laruh
  • Loading branch information
shamardy committed May 17, 2023
1 parent cbf312d commit 0660e75
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 90 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/dev-build.yml
Expand Up @@ -61,7 +61,7 @@ jobs:
env:
AVAILABLE: ${{ secrets.FILE_SERVER_KEY }}
if: ${{ env.AVAILABLE != '' }}
uses: garygrossgarten/github-action-scp@release
uses: garygrossgarten/github-action-scp@v0.8.0
with:
host: ${{ secrets.FILE_SERVER_HOST }}
username: ${{ secrets.FILE_SERVER_USERNAME }}
Expand Down Expand Up @@ -120,7 +120,7 @@ jobs:
env:
AVAILABLE: ${{ secrets.FILE_SERVER_KEY }}
if: ${{ env.AVAILABLE != '' }}
uses: garygrossgarten/github-action-scp@release
uses: garygrossgarten/github-action-scp@v0.8.0
with:
host: ${{ secrets.FILE_SERVER_HOST }}
username: ${{ secrets.FILE_SERVER_USERNAME }}
Expand Down Expand Up @@ -169,7 +169,7 @@ jobs:
env:
AVAILABLE: ${{ secrets.FILE_SERVER_KEY }}
if: ${{ env.AVAILABLE != '' }}
uses: garygrossgarten/github-action-scp@release
uses: garygrossgarten/github-action-scp@v0.8.0
with:
host: ${{ secrets.FILE_SERVER_HOST }}
username: ${{ secrets.FILE_SERVER_USERNAME }}
Expand Down Expand Up @@ -217,7 +217,7 @@ jobs:
env:
AVAILABLE: ${{ secrets.FILE_SERVER_KEY }}
if: ${{ env.AVAILABLE != '' }}
uses: garygrossgarten/github-action-scp@release
uses: garygrossgarten/github-action-scp@v0.8.0
with:
host: ${{ secrets.FILE_SERVER_HOST }}
username: ${{ secrets.FILE_SERVER_USERNAME }}
Expand Down Expand Up @@ -277,7 +277,7 @@ jobs:
env:
AVAILABLE: ${{ secrets.FILE_SERVER_KEY }}
if: ${{ env.AVAILABLE != '' }}
uses: garygrossgarten/github-action-scp@release
uses: garygrossgarten/github-action-scp@v0.8.0
with:
host: ${{ secrets.FILE_SERVER_HOST }}
username: ${{ secrets.FILE_SERVER_USERNAME }}
Expand Down Expand Up @@ -326,7 +326,7 @@ jobs:
env:
AVAILABLE: ${{ secrets.FILE_SERVER_KEY }}
if: ${{ env.AVAILABLE != '' }}
uses: garygrossgarten/github-action-scp@release
uses: garygrossgarten/github-action-scp@v0.8.0
with:
host: ${{ secrets.FILE_SERVER_HOST }}
username: ${{ secrets.FILE_SERVER_USERNAME }}
Expand Down Expand Up @@ -389,7 +389,7 @@ jobs:
env:
AVAILABLE: ${{ secrets.FILE_SERVER_KEY }}
if: ${{ env.AVAILABLE != '' }}
uses: garygrossgarten/github-action-scp@release
uses: garygrossgarten/github-action-scp@v0.8.0
with:
host: ${{ secrets.FILE_SERVER_HOST }}
username: ${{ secrets.FILE_SERVER_USERNAME }}
Expand Down Expand Up @@ -452,7 +452,7 @@ jobs:
env:
AVAILABLE: ${{ secrets.FILE_SERVER_KEY }}
if: ${{ env.AVAILABLE != '' }}
uses: garygrossgarten/github-action-scp@release
uses: garygrossgarten/github-action-scp@v0.8.0
with:
host: ${{ secrets.FILE_SERVER_HOST }}
username: ${{ secrets.FILE_SERVER_USERNAME }}
Expand Down
16 changes: 8 additions & 8 deletions .github/workflows/release-build.yml
Expand Up @@ -55,7 +55,7 @@ jobs:
mv $NAME ./$BRANCH_NAME/
- name: Upload output
uses: garygrossgarten/github-action-scp@release
uses: garygrossgarten/github-action-scp@v0.8.0
with:
host: ${{ secrets.FILE_SERVER_HOST }}
username: ${{ secrets.FILE_SERVER_USERNAME }}
Expand Down Expand Up @@ -106,7 +106,7 @@ jobs:
mv $NAME ./$BRANCH_NAME/
- name: Upload output
uses: garygrossgarten/github-action-scp@release
uses: garygrossgarten/github-action-scp@v0.8.0
with:
host: ${{ secrets.FILE_SERVER_HOST }}
username: ${{ secrets.FILE_SERVER_USERNAME }}
Expand Down Expand Up @@ -149,7 +149,7 @@ jobs:
mv $NAME ./$Env:BRANCH_NAME/
- name: Upload output
uses: garygrossgarten/github-action-scp@release
uses: garygrossgarten/github-action-scp@v0.8.0
with:
host: ${{ secrets.FILE_SERVER_HOST }}
username: ${{ secrets.FILE_SERVER_USERNAME }}
Expand Down Expand Up @@ -191,7 +191,7 @@ jobs:
mv $NAME ./$BRANCH_NAME/
- name: Upload output
uses: garygrossgarten/github-action-scp@release
uses: garygrossgarten/github-action-scp@v0.8.0
with:
host: ${{ secrets.FILE_SERVER_HOST }}
username: ${{ secrets.FILE_SERVER_USERNAME }}
Expand Down Expand Up @@ -245,7 +245,7 @@ jobs:
mv $NAME ./$BRANCH_NAME/
- name: Upload output
uses: garygrossgarten/github-action-scp@release
uses: garygrossgarten/github-action-scp@v0.8.0
with:
host: ${{ secrets.FILE_SERVER_HOST }}
username: ${{ secrets.FILE_SERVER_USERNAME }}
Expand Down Expand Up @@ -288,7 +288,7 @@ jobs:
mv $NAME ./$BRANCH_NAME/
- name: Upload output
uses: garygrossgarten/github-action-scp@release
uses: garygrossgarten/github-action-scp@v0.8.0
with:
host: ${{ secrets.FILE_SERVER_HOST }}
username: ${{ secrets.FILE_SERVER_USERNAME }}
Expand Down Expand Up @@ -345,7 +345,7 @@ jobs:
mv $NAME ./$BRANCH_NAME/
- name: Upload output
uses: garygrossgarten/github-action-scp@release
uses: garygrossgarten/github-action-scp@v0.8.0
with:
host: ${{ secrets.FILE_SERVER_HOST }}
username: ${{ secrets.FILE_SERVER_USERNAME }}
Expand Down Expand Up @@ -402,7 +402,7 @@ jobs:
mv $NAME ./$BRANCH_NAME/
- name: Upload output
uses: garygrossgarten/github-action-scp@release
uses: garygrossgarten/github-action-scp@v0.8.0
with:
host: ${{ secrets.FILE_SERVER_HOST }}
username: ${{ secrets.FILE_SERVER_USERNAME }}
Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions mm2src/coins/Cargo.toml
Expand Up @@ -95,6 +95,7 @@ utxo_signer = { path = "utxo_signer" }
# using the same version as cosmrs
tendermint-rpc = { version = "=0.23.7", default-features = false }
tiny-bip39 = "0.8.0"
url = { version = "2.2.2", features = ["serde"] }
uuid = { version = "1.2.2", features = ["fast-rng", "serde", "v4"] }
# One of web3 dependencies is the old `tokio-uds 0.1.7` which fails cross-compiling to ARM.
# We don't need the default web3 features at all since we added our own web3 transport using shared HYPER instance.
Expand Down
3 changes: 2 additions & 1 deletion mm2src/coins/eth.rs
Expand Up @@ -67,6 +67,7 @@ use std::ops::Deref;
use std::str::FromStr;
use std::sync::atomic::{AtomicU64, Ordering as AtomicOrdering};
use std::sync::{Arc, Mutex};
use url::Url;
use web3::types::{Action as TraceAction, BlockId, BlockNumber, Bytes, CallRequest, FilterBuilder, Log, Trace,
TraceFilterBuilder, Transaction as Web3Transaction, TransactionId, U64};
use web3::{self, Web3};
Expand Down Expand Up @@ -874,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, url: String) -> WithdrawNftResult {
pub async fn withdraw_erc1155(ctx: MmArc, withdraw_type: WithdrawErc1155, url: Url) -> WithdrawNftResult {
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)?;
Expand Down
88 changes: 50 additions & 38 deletions mm2src/coins/nft.rs
Expand Up @@ -7,35 +7,46 @@ pub(crate) mod nft_structs;

use crate::WithdrawError;
use nft_errors::GetNftInfoError;
use nft_structs::{Chain, ConvertChain, Nft, NftList, NftListReq, NftMetadataReq, NftTransferHistory,
use nft_structs::{ConvertChain, Nft, NftList, NftListReq, NftMetadataReq, NftTransferHistory,
NftTransferHistoryWrapper, NftTransfersReq, NftWrapper, NftsTransferHistoryList,
TransactionNftDetails, WithdrawNftReq};

use crate::eth::{get_eth_address, withdraw_erc1155, withdraw_erc721};
use crate::nft::nft_structs::WithdrawNftType;
use common::APPLICATION_JSON;
use http::header::ACCEPT;
use mm2_err_handle::map_to_mm::MapToMmResult;
use mm2_number::BigDecimal;
use serde_json::Value as Json;

const MORALIS_API_ENDPOINT: &str = "/api/v2/";
/// query parameter for moralis request: The format of the token ID
const FORMAT_DECIMAL_MORALIS: &str = "format=decimal";
/// query parameter for moralis request: The transfer direction
const DIRECTION_BOTH_MORALIS: &str = "direction=both";
const MORALIS_API_ENDPOINT: &str = "api/v2";
/// query parameters for moralis request: The format of the token ID
const MORALIS_FORMAT_QUERY_NAME: &str = "format";
const MORALIS_FORMAT_QUERY_VALUE: &str = "decimal";
/// query parameters for moralis request: The transfer direction
const MORALIS_DIRECTION_QUERY_NAME: &str = "direction";
const MORALIS_DIRECTION_QUERY_VALUE: &str = "both";

pub type WithdrawNftResult = Result<TransactionNftDetails, MmError<WithdrawError>>;

/// `get_nft_list` function returns list of NFTs on requested chains owned by user.
pub async fn get_nft_list(ctx: MmArc, req: NftListReq) -> MmResult<NftList, GetNftInfoError> {
let mut res_list = Vec::new();
for chain in req.chains {
let (coin_str, chain_str) = chain.to_ticker_chain();
let my_address = get_eth_address(&ctx, &coin_str).await?;
let req_url = &req.url;
let wallet_address = my_address.wallet_address;
let uri_without_cursor =
format!("{req_url}{MORALIS_API_ENDPOINT}{wallet_address}/nft?chain={chain_str}&{FORMAT_DECIMAL_MORALIS}");
let my_address = get_eth_address(&ctx, &chain.to_ticker()).await?;

let mut uri_without_cursor = req.url.clone();
uri_without_cursor.set_path(MORALIS_API_ENDPOINT);
uri_without_cursor
.path_segments_mut()
.map_to_mm(|_| GetNftInfoError::Internal("Invalid URI".to_string()))?
.push(&my_address.wallet_address)
.push("nft");
uri_without_cursor
.query_pairs_mut()
.append_pair("chain", &chain.to_string())
.append_pair(MORALIS_FORMAT_QUERY_NAME, MORALIS_FORMAT_QUERY_VALUE);
drop_mutability!(uri_without_cursor);

// The cursor returned in the previous response (used for getting the next page).
let mut cursor = String::new();
Expand Down Expand Up @@ -92,19 +103,18 @@ pub async fn get_nft_list(ctx: MmArc, req: NftListReq) -> MmResult<NftList, GetN
/// of the same token. `get_nft_metadata` returns NFTs info with the most recent owner.
/// **Dont** use this function to get specific info about owner address, amount etc, you will get info not related to my_address.
pub async fn get_nft_metadata(_ctx: MmArc, req: NftMetadataReq) -> MmResult<Nft, GetNftInfoError> {
let chain_str = match req.chain {
Chain::Avalanche => "avalanche",
Chain::Bsc => "bsc",
Chain::Eth => "eth",
Chain::Fantom => "fantom",
Chain::Polygon => "polygon",
};
let req_url = &req.url;
let token_address = &req.token_address;
let token_id = &req.token_id;
let uri = format!(
"{req_url}{MORALIS_API_ENDPOINT}nft/{token_address}/{token_id}?chain={chain_str}&{FORMAT_DECIMAL_MORALIS}"
);
let mut uri = req.url;
uri.set_path(MORALIS_API_ENDPOINT);
uri.path_segments_mut()
.map_to_mm(|_| GetNftInfoError::Internal("Invalid URI".to_string()))?
.push("nft")
.push(&format!("{:#02x}", &req.token_address))
.push(&req.token_id.to_string());
uri.query_pairs_mut()
.append_pair("chain", &req.chain.to_string())
.append_pair(MORALIS_FORMAT_QUERY_NAME, MORALIS_FORMAT_QUERY_VALUE);
drop_mutability!(uri);

let response = send_moralis_request(uri.as_str()).await?;
let nft_wrapper: NftWrapper = serde_json::from_str(&response.to_string())?;
let nft_metadata = Nft {
Expand Down Expand Up @@ -135,20 +145,22 @@ pub async fn get_nft_transfers(ctx: MmArc, req: NftTransfersReq) -> MmResult<Nft
let mut res_list = Vec::new();

for chain in req.chains {
let (coin_str, chain_str) = match chain {
Chain::Avalanche => ("AVAX", "avalanche"),
Chain::Bsc => ("BNB", "bsc"),
Chain::Eth => ("ETH", "eth"),
Chain::Fantom => ("FTM", "fantom"),
Chain::Polygon => ("MATIC", "polygon"),
};
let my_address = get_eth_address(&ctx, coin_str).await?;
let req_url = &req.url;
let wallet_address = my_address.wallet_address;
let uri_without_cursor = format!(
"{req_url}{MORALIS_API_ENDPOINT}{wallet_address}/nft/transfers?chain={chain_str}&{FORMAT_DECIMAL_MORALIS}&{DIRECTION_BOTH_MORALIS}",
let my_address = get_eth_address(&ctx, &chain.to_ticker()).await?;

);
let mut uri_without_cursor = req.url.clone();
uri_without_cursor.set_path(MORALIS_API_ENDPOINT);
uri_without_cursor
.path_segments_mut()
.map_to_mm(|_| GetNftInfoError::Internal("Invalid URI".to_string()))?
.push(&my_address.wallet_address)
.push("nft")
.push("transfers");
uri_without_cursor
.query_pairs_mut()
.append_pair("chain", &chain.to_string())
.append_pair(MORALIS_FORMAT_QUERY_NAME, MORALIS_FORMAT_QUERY_VALUE)
.append_pair(MORALIS_DIRECTION_QUERY_NAME, MORALIS_DIRECTION_QUERY_VALUE);
drop_mutability!(uri_without_cursor);

// The cursor returned in the previous response (used for getting the next page).
let mut cursor = String::new();
Expand Down
37 changes: 20 additions & 17 deletions mm2src/coins/nft/nft_structs.rs
@@ -1,21 +1,24 @@
use crate::{TransactionType, TxFeeDetails, WithdrawFee};
use ethereum_types::Address;
use mm2_number::BigDecimal;
use rpc::v1::types::Bytes as BytesJson;
use serde::Deserialize;
use std::fmt;
use std::str::FromStr;
use url::Url;

#[derive(Debug, Deserialize)]
pub struct NftListReq {
pub(crate) chains: Vec<Chain>,
pub(crate) url: String,
pub(crate) url: Url,
}

#[derive(Debug, Deserialize)]
pub struct NftMetadataReq {
pub(crate) token_address: String,
pub(crate) token_address: Address,
pub(crate) token_id: BigDecimal,
pub(crate) chain: Chain,
pub(crate) url: String,
pub(crate) url: Url,
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
Expand All @@ -28,10 +31,20 @@ pub(crate) enum Chain {
Polygon,
}

impl fmt::Display for Chain {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self {
Chain::Avalanche => write!(f, "avalanche"),
Chain::Bsc => write!(f, "bsc"),
Chain::Eth => write!(f, "eth"),
Chain::Fantom => write!(f, "fantom"),
Chain::Polygon => write!(f, "polygon"),
}
}
}

pub(crate) trait ConvertChain {
fn to_ticker(&self) -> String;

fn to_ticker_chain(&self) -> (String, String);
}

impl ConvertChain for Chain {
Expand All @@ -44,16 +57,6 @@ impl ConvertChain for Chain {
Chain::Polygon => "MATIC".to_owned(),
}
}

fn to_ticker_chain(&self) -> (String, String) {
match self {
Chain::Avalanche => ("AVAX".to_owned(), "avalanche".to_owned()),
Chain::Bsc => ("BNB".to_owned(), "bsc".to_owned()),
Chain::Eth => ("ETH".to_owned(), "eth".to_owned()),
Chain::Fantom => ("FTM".to_owned(), "fantom".to_owned()),
Chain::Polygon => ("MATIC".to_owned(), "polygon".to_owned()),
}
}
}

#[derive(Debug, Display)]
Expand Down Expand Up @@ -175,7 +178,7 @@ pub struct WithdrawErc721 {

#[derive(Clone, Deserialize)]
pub struct WithdrawNftReq {
pub(crate) url: String,
pub(crate) url: Url,
pub(crate) withdraw_type: WithdrawNftType,
}

Expand Down Expand Up @@ -217,7 +220,7 @@ pub struct TransactionNftDetails {
#[derive(Debug, Deserialize)]
pub struct NftTransfersReq {
pub(crate) chains: Vec<Chain>,
pub(crate) url: String,
pub(crate) url: Url,
}

#[derive(Debug, Serialize)]
Expand Down

0 comments on commit 0660e75

Please sign in to comment.