why it's only showing withdraw transaction, show deposit transactions, show both the transactions, check the condition : why it's showing the status: failed

  import axios from 'axios';

  const options = {
  method: 'POST',
  url: 'https://polygon-mainnet.g.alchemy.com/v2/{apikey}',
  headers: {accept: 'application/json', 'content-type': 'application/json'},
  data: {
    id: 1,
    jsonrpc: '2.0',
    method: 'alchemy_getAssetTransfers',
    params: [
      {
        fromBlock: '0x0',
        toBlock: 'latest',
        category: ["external", "internal", "erc20", "erc721", "erc1155"],
        contractAddresses: ['0xc2132d05d31c914a87c6611c10748aeb04b58e8f'],
        withMetadata: true,
        excludeZeroValue: false,
        maxCount: '0x0A',
        fromAddress: '0x1581efd9b6357fe459c7086ecb2ddb7e972a3c42',
      }
    ]
  }
};

axios
  .request(options)
  .then(async function (response) {
    const transfers = response.data.result.transfers;
    // console.log(transfers)
for (const transfer of transfers) {
  const blockNumber = transfer.blockNum;
  const blockInfo = await getBlockInfo(blockNumber);
  transfer.created_at = blockInfo.timestamp;
  transfer.address = transfer.transfer_type == 'OUT' ? 'To address: ' + transfer.to : 'From address: ' + transfer.from; 
  transfer.status = !transfer.successful ? 'FAILED' : "COMPLETED";
  transfer.network = 'polygon-mainnet';
  transfer.transaction_type = transfer.transfer_type == 'OUT' ? 'DEPOSIT' : "WITHDRAW";
  transfer.usd_value = transfer.value;
  delete transfer.value;
  delete transfer.erc721TokenId;
  delete transfer.erc1155Metadata;
  delete transfer.tokenId;
  delete transfer.rawContract;
  delete transfer.blockNum;
  delete transfer.uniqueId; 
  delete transfer.asset;
  delete transfer.category;
  delete transfer.metadata;

  transfer.crypto_symbol = 'USDT';
  
  // transfer.tx_hash = transfer.uniqueId;
  // delete transfer.uniqueId;
}
console.log(transfers);


})
  .catch(function (error) {
    console.error(error.response.data);
  });

async function getBlockInfo(blockNumber) {
  const options = {
    method: 'POST',
    url: 'https://polygon-mainnet.g.alchemy.com/v2/{apikey}',
    headers: {accept: 'application/json', 'content-type': 'application/json'},
    data: {
      id: 1,
      jsonrpc: '2.0',
      method: 'eth_getBlockByNumber',
      params: [blockNumber, true],
    }
  };
  const response = await axios.request(options);
  return response.data.result;
}

ON Output

{
hash: '0x8f65a33e13600a270fdbdbab56473118973987736e2c5450a7a8a671cf15a3df',
from: '0x1581efd9b6357fe459c7086ecb2ddb7e972a3c42',
to: '0xc2132d05d31c914a87c6611c10748aeb04b58e8f',
created_at: '0x644a5af6',
address: 'From address: 0x1581efd9b6357fe459c7086ecb2ddb7e972a3c42',
status: 'FAILED',
network: 'polygon-mainnet',
transaction_type: 'WITHDRAW',
usd_value: 0,
crypto_symbol: 'USDT'
}

Endpoint Used Alchemy_getAssetTransfers. This is the code to retrieve user transaction history using alchemy api on polygon network. But while running the transaction status is showing failed and it's only showing Withdraw transactions.

0

There are 0 best solutions below