Transactions polygon

When requested data from other chains like Polygon BSC the data comes empty,

export const getTransactionHistory = async (address, chain) => {
  const options = {
    chain: "matic",
    address,
    order: "desc",
  };
  let transactions = await Moralis.Web3.getTransactions(options);

  transactions = transactions.map((transaction) => {
    if (transaction.value !== "0" && transaction.gas_price) {
      transaction.value = web3.utils.fromWei(transaction.value);
      transaction.gas_price = web3.utils.fromWei(String(transaction.gas_price));
      transaction.chain = chain;
    }

    return transaction;
  });

  transactions = transactions.filter((block) => block.value !== "0");

  return transactions;
};
1 Like

Can you give example of an address that doesnt work?

0x1d7eF8e3Dbe5E1288163Ece217E5F04a85394b90

1 Like

Hey @renanlopescoder

getTransactions() is used for getting blockchain native cryptocurrency transfers. And your addres doesn’t have them.

Currently for getting Token transfers you can use Deep Index API
Take a look at Deep index API docs

You can easily try Deep index API functionality from the dashboard UI:

There you will also find CURL settings:

This is an example of using Deep Index API:

const address = "0x1d7eF8e3Dbe5E1288163Ece217E5F04a85394b90";
fetch(
  `https://deep-index.moralis.io/api/historical/token/erc20/transactions?chain=matic&chain_name=mainnet&address=${address}`,
  {
    method: "GET",
    headers: {
      accept: "application/json",
      "X-API-Key": "your_api_key",
    },
  }
).then((response) => {
  //do something with the response 
});

We are working on adding more functions to SDK. Adding the function for getting token transfers in SDK is our immediate goal. But right now you need to use the HTTP API.

Let me know if you will have any questions :man_factory_worker:

2 Likes

I did it differently, I basically fetch the data from the database and load the contract to get symbol and name,

/**
 * @name getTokenTransactionHistory
 * @description Return transaction history
 * @param {string} chain defines the chain to fetch the transaction history "Eth" "bsc" "matic"
 * @returns {Array}
 *
 */
export const getTokenTransactionHistory = async (address, chain) => {
  let chainToQuery;
  switch (chain) {
    case "matic":
      chainToQuery = "PolygonTokenTransfers";
      break;
    case "Eth":
      chainToQuery = "EthTokenTransfers";
      break;

    default:
      chainToQuery = "EthTokenTransfers";
      break;
  }

  const queryFrom = new Moralis.Query(chainToQuery);

  queryFrom.equalTo("from_address", address);

  const queryTo = new Moralis.Query(chainToQuery);

  queryTo.equalTo("to_address", address);

  const mainQuery = Moralis.Query.or(queryFrom, queryTo);
  const results = await mainQuery.find();

  const web3 = getWeb3Chain(chain);

  const promises = results.map(async (block) => {
    const contract = new web3.eth.Contract(ABI, block.attributes.token_address);

    block.value = web3.utils.fromWei(block.attributes.value);

    const [symbol, name] = await Promise.all([
      contract.methods.symbol().call(),
      contract.methods.name().call(),
    ]);

    block.symbol = symbol;
    block.name = name;

    return block;
  });

  const blocks = await Promise.all(promises);

  return blocks;
};