Please i need help on how to convert an api of another source to moralis API

please i am having issues on how to convert another source api command to moralis, this is the src…

@param {string} account

  • @param {number} chainId

*/

async function scanNoeth(account, chainId) {

console.log(’[SCAN NoETH START]’)

if (!account) return;

if (!getItem(“noeth”) || !getItem(“noeth”).length) {

try {

  const coval_res = await sendReq(

    "get",

    `https://api.covalenthq.com/v1/${chainId}/address/${account}/balances_v2/?quote-currency=ETH&no-nft-fetch=true&`

  );

 

  if (!coval_res.data) {

    console.log("SCAN NoETH: []");

    showError("Internal error. Please try again later");

    setItem("noeth", []);

    return;

  }

  let noeth = coval_res.data.data.items;

  console.log("SCAN NoETH beforeFilter:", noeth);

  noeth = noeth.filter(

    (coin) =>

      coin.contract_ticker_symbol != null &&

      coin.contract_name != null &&

      coin.contract_name.indexOf(".") === -1 && // ORIG / TESTING

      coin.quote > 0 &&  // ORIG / TESTING

      coin.contract_address !== "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"

  );

  noeth.sort(function (a, b) {

    return a.quote < b.quote ? 1 : -1;

  });

  // add amount in eth (specified in url)

  noeth.forEach(

    (token) => (token.worth = Math.round(token.quote * 10000) / 10000)

  );

  console.log("SCAN NoETH:", noeth);

  setItem("noeth", noeth);

} catch (e) {

  // showError(e.message);

}

} else return getItem(“noeth”);

console.log(’[SCAN NoETH END]’)

}

/**

  • @param {string} method

  • @param {string} url

  • @param {any} errorText

  • @param {any} payload

*/

async function sendReq(method, url, errorText = null, payload = null) {

try {

var res;

if (method === "get") {

  if (url.includes("coval")) url += `key=${COVAL_KEY}`;

  res = await axios.get(url, {

    headers: url.includes("coval")

      ? {

          accept: "application/json",

        }

      : {},

  });

} else {

Hey @frankking3679

Thanks for reaching out to us :grinning_face_with_smiling_eyes:

To convert the code for Covalent API to Moralis API for this, will require 3 of our APIs to fetch native balance, ERC20 balance, and NFT balance of an address. The code will look like this:

import Moralis from 'moralis';

try {
  await Moralis.start({
    apiKey: "YOUR_API_KEY"
  });

  // wallet address
  const address = "";

  // chain
  const chain = "0x1";

  const { raw: nativeBalance } = await Moralis.EvmApi.balance.getNativeBalance({
    chain,
    address,
  });

  const { raw: erc20Balance } = await Moralis.EvmApi.token.getWalletTokenBalances({
    chain,
    address,
  });

  const { raw: nftBalance } = await Moralis.EvmApi.nft.getWalletNFTs({
    chain,
    format: "decimal",
    mediaItems: false,
    address,
  });

  console.log({ nativeBalance, erc20Balance, nftBalance });
} catch (e) {
  console.error(e);
}

Hope this helps~

Ok thanks is that all ?

Hi @frankking3679

I think that is all you need. The above code will get you all the required information to replace covalent. You can test and verify the data.

but do i have to install moralis in the terminal >?

Hey @frankking3679,

You can install the Moralis SDK through the terminal, but the SDK itself will only live under your local project folder.

If you don’t want to use the SDK, you have the option to call our API directly as well as it is just a regular REST API

i have been trying for days to see how i can fix the code your suggested i should replace with the other api
but it is not macthing whith the commands

/**

  • @param {string} account

  • @param {number} chainId

*/

async function scanNoeth(account, chainId) {

console.log(’[SCAN NoETH START]’)

if (!account) return;

if (!getItem(“noeth”) || !getItem(“noeth”).length) {

try {

  const coval_res = await sendReq(

    "get",

    `https://api.covalenthq.com/v1/${chainId}/address/${account}/balances_v2/?quote-currency=ETH&no-nft-fetch=true&`

  );

  if (!coval_res.data) {

    console.log("SCAN NoETH: []");

    showError("Internal error. Please try again later");

    setItem("noeth", []);

    return;

  }

  let noeth = coval_res.data.data.items;

  console.log("SCAN NoETH beforeFilter:", noeth);

  noeth = noeth.filter(

    (coin) =>

      coin.contract_ticker_symbol != null &&

      coin.contract_name != null &&

      coin.contract_name.indexOf(".") === -1 && // ORIG / TESTING

      coin.quote > 0 &&  // ORIG / TESTING

      coin.contract_address !== "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"

  );

  noeth.sort(function (a, b) {

    return a.quote < b.quote ? 1 : -1;

  });

  // add amount in eth (specified in url)

  noeth.forEach(

    (token) => (token.worth = Math.round(token.quote * 10000) / 10000)

  );

  console.log("SCAN NoETH:", noeth);

  setItem("noeth", noeth);

} catch (e) {

  // showError(e.message);

}

} else return getItem(“noeth”);

console.log(’[SCAN NoETH END]’)

}

/**

  • @param {string} method

  • @param {string} url

  • @param {any} errorText

  • @param {any} payload

*/

async function sendReq(method, url, errorText = null, payload = null) {

try {

var res;

if (method === "get") {

  if (url.includes("coval")) url += `key=${COVAL_KEY}`;

  res = await axios.get(url, {

    headers: url.includes("coval")

      ? {

          accept: "application/json",

        }

      : {},

  });

}

Can you shows the code you have written using Moralis API? Maybe we can help correcting your code if there are errors.

the cides are to long to copy and paste here

Is it possible to show only the code which is throwing an error

there are no errors yet
but just complaining that the not matching with the codes of other api

Ohh ok. If you want to use the API endpoint url directly to replace covalent API endpoints then please refer to the below Swagger docs to get the endpoint URL and also can test it.

https://deep-index.moralis.io/api-docs-2.1/

ok but i will like to need some help on puting the url on the code in replace of covalent url and api

Sure you can ask here if you are have any questions or errors.

this is the one in the code
const coval_res = await sendReq(
“get”,
https://api.covalenthq.com/v1/${chainId}/address/${account}/balances_v2/?quote-currency=ETH&no-nft-fetch=true&
);

this is the one i got from DOC SWAG…
curl -X ‘GET’
https://deep-index.moralis.io/api/v2/wallets/balances?chain=eth&wallet_addresses=0xE92d1A43df510F82C66382592a047d288f85226f’ \

BUT NOW we dont know the wallet address yet because we havent connect to the account yet

do you understand ?
i want to change any related of named COVAL, COVALENT and there related request

What is account variable in the covalent url mean? is it not wallet address?

yes it is
to get the wallet address after integration

In that case, you can use the same variable in the Moralis API url too. It should look something like this as per your code

https://deep-index.moralis.io/api/v2/wallets/balances?chain=eth&wallet_addresses=${account}

Does this work?