Guest User

Untitled

a guest
Sep 13th, 2024
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 6.22 KB | Source Code | 0 0
  1. //https://dashboard.quicknode.com/streams/new?network=tron_mainnet&dataset=block_with_receipts
  2.  
  3. // Utility functions
  4. function stripHexPrefix(hex) {
  5.     return hex.startsWith('0x') ? hex.slice(2) : hex;
  6. }
  7.  
  8. function addHexPrefix(hex) {
  9.     return hex.startsWith('0x') ? hex : '0x' + hex;
  10. }
  11.  
  12. function padLeft(string, chars, sign) {
  13.     return new Array(chars - string.length + 1).join(sign ? sign : "0") + string;
  14. }
  15.  
  16. function decodeParameter(type, value) {
  17.     value = stripHexPrefix(value);
  18.     if (type === 'uint256') {
  19.         return BigInt(addHexPrefix(value)).toString();
  20.     } else if (type === 'address') {
  21.         return addHexPrefix(value.slice(-40));
  22.     }
  23.     return value;
  24. }
  25.  
  26. function decodeParameters(types, data) {
  27.     data = stripHexPrefix(data);
  28.     const result = [];
  29.     let offset = 0;
  30.     types.forEach(type => {
  31.         if (type === 'uint256' || type === 'address') {
  32.             result.push(decodeParameter(type, data.slice(offset, offset + 64)));
  33.             offset += 64;
  34.         } else if (type === 'uint256[]') {
  35.             const arrayOffset = parseInt(data.slice(offset, offset + 64), 16) * 2;
  36.             const length = parseInt(data.slice(arrayOffset, arrayOffset + 64), 16);
  37.             const array = [];
  38.             for (let i = 0; i < length; i++) {
  39.                 array.push(decodeParameter('uint256', data.slice(arrayOffset + 64 + i * 64, arrayOffset + 128 + i * 64)));
  40.             }
  41.             result.push(array);
  42.             offset += 64;
  43.         }
  44.     });
  45.     return result;
  46. }
  47.  
  48. function main(data) {
  49.     const nativeTransfers = [];
  50.     const trc20Transfers = [];
  51.     const trc721Transfers = [];
  52.     const trc1155Transfers = [];
  53.  
  54.     const block = data.streamData.block;
  55.     const receipts = data.streamData.receipts;
  56.  
  57.     for (let i = 0; i < block.transactions.length; i++) {
  58.         const tx = block.transactions[i];
  59.         const receipt = receipts[i];
  60.  
  61.         // Check for native transfers (TRX or TRC10)
  62.         if (receipt.gasUsed === "0x0" && receipt.logs.length === 0 && receipt.status === "0x1" && tx.input === "0x") {
  63.             nativeTransfers.push({
  64.                 type: 'NativeTransfer',
  65.                 from: tx.from,
  66.                 to: tx.to,
  67.                 value: BigInt(tx.value).toString(),
  68.                 txHash: tx.hash,
  69.                 timestamp: parseInt(block.timestamp)
  70.             });
  71.         }
  72.  
  73.         // Parse logs for other token transfers
  74.         receipt.logs.forEach(log => {
  75.             if (log.topics && log.topics.length > 0) {
  76.                 const topic0 = log.topics[0];
  77.  
  78.                 // TRC-20 Transfer event
  79.                 if (topic0 === '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef' && log.topics.length === 3) {
  80.                     const from = addHexPrefix(log.topics[1].slice(-40));
  81.                     const to = addHexPrefix(log.topics[2].slice(-40));
  82.                     const value = decodeParameter('uint256', log.data);
  83.                     trc20Transfers.push({
  84.                         type: 'TRC20',
  85.                         from,
  86.                         to,
  87.                         value,
  88.                         contract: log.address,
  89.                         txHash: tx.hash,
  90.                         timestamp: parseInt(block.timestamp)
  91.                     });
  92.                 }
  93.                 // TRC-721 Transfer event
  94.                 else if (topic0 === '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef' && log.topics.length === 4) {
  95.                     const from = addHexPrefix(log.topics[1].slice(-40));
  96.                     const to = addHexPrefix(log.topics[2].slice(-40));
  97.                     const tokenId = decodeParameter('uint256', log.topics[3]);
  98.                     trc721Transfers.push({
  99.                         type: 'TRC721',
  100.                         from,
  101.                         to,
  102.                         tokenId,
  103.                         contract: log.address,
  104.                         txHash: tx.hash,
  105.                         timestamp: parseInt(block.timestamp)
  106.                     });
  107.                 }
  108.                 // TRC-1155 TransferSingle event
  109.                 else if (topic0 === '0xc3d58168c5ae7397731d063d5bbf3d657854427343f4c083240f7aacaa2d0f62') {
  110.                     const operator = addHexPrefix(log.topics[1].slice(-40));
  111.                     const from = addHexPrefix(log.topics[2].slice(-40));
  112.                     const to = addHexPrefix(log.topics[3].slice(-40));
  113.                     const [id, value] = decodeParameters(['uint256', 'uint256'], log.data);
  114.                     trc1155Transfers.push({
  115.                         type: 'TRC1155_Single',
  116.                         operator,
  117.                         from,
  118.                         to,
  119.                         tokenId: id,
  120.                         value,
  121.                         contract: log.address,
  122.                         txHash: tx.hash,
  123.                         timestamp: parseInt(block.timestamp)
  124.                     });
  125.                 }
  126.                 // TRC-1155 TransferBatch event
  127.                 else if (topic0 === '0x4a39dc06d4c0dbc64b70af90fd698a233a518aa5d07e595d983b8c0526c8f7fb') {
  128.                     const operator = addHexPrefix(log.topics[1].slice(-40));
  129.                     const from = addHexPrefix(log.topics[2].slice(-40));
  130.                     const to = addHexPrefix(log.topics[3].slice(-40));
  131.                     const [ids, values] = decodeParameters(['uint256[]', 'uint256[]'], log.data);
  132.                     ids.forEach((id, index) => {
  133.                         trc1155Transfers.push({
  134.                             type: 'TRC1155_Batch',
  135.                             operator,
  136.                             from,
  137.                             to,
  138.                             tokenId: id,
  139.                             value: values[index],
  140.                             contract: log.address,
  141.                             txHash: tx.hash,
  142.                             timestamp: parseInt(block.timestamp)
  143.                         });
  144.                     });
  145.                 }
  146.             }
  147.         });
  148.     }
  149.  
  150.     return {
  151.         nativeTransfers,
  152.         trc20: trc20Transfers,
  153.         trc721: trc721Transfers,
  154.         trc1155: trc1155Transfers
  155.     };
  156. }
Tags: streams
Advertisement
Add Comment
Please, Sign In to add comment