Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //https://dashboard.quicknode.com/streams/new?network=tron_mainnet&dataset=block_with_receipts
- // Utility functions
- function stripHexPrefix(hex) {
- return hex.startsWith('0x') ? hex.slice(2) : hex;
- }
- function addHexPrefix(hex) {
- return hex.startsWith('0x') ? hex : '0x' + hex;
- }
- function padLeft(string, chars, sign) {
- return new Array(chars - string.length + 1).join(sign ? sign : "0") + string;
- }
- function decodeParameter(type, value) {
- value = stripHexPrefix(value);
- if (type === 'uint256') {
- return BigInt(addHexPrefix(value)).toString();
- } else if (type === 'address') {
- return addHexPrefix(value.slice(-40));
- }
- return value;
- }
- function decodeParameters(types, data) {
- data = stripHexPrefix(data);
- const result = [];
- let offset = 0;
- types.forEach(type => {
- if (type === 'uint256' || type === 'address') {
- result.push(decodeParameter(type, data.slice(offset, offset + 64)));
- offset += 64;
- } else if (type === 'uint256[]') {
- const arrayOffset = parseInt(data.slice(offset, offset + 64), 16) * 2;
- const length = parseInt(data.slice(arrayOffset, arrayOffset + 64), 16);
- const array = [];
- for (let i = 0; i < length; i++) {
- array.push(decodeParameter('uint256', data.slice(arrayOffset + 64 + i * 64, arrayOffset + 128 + i * 64)));
- }
- result.push(array);
- offset += 64;
- }
- });
- return result;
- }
- function main(data) {
- const nativeTransfers = [];
- const trc20Transfers = [];
- const trc721Transfers = [];
- const trc1155Transfers = [];
- const block = data.streamData.block;
- const receipts = data.streamData.receipts;
- for (let i = 0; i < block.transactions.length; i++) {
- const tx = block.transactions[i];
- const receipt = receipts[i];
- // Check for native transfers (TRX or TRC10)
- if (receipt.gasUsed === "0x0" && receipt.logs.length === 0 && receipt.status === "0x1" && tx.input === "0x") {
- nativeTransfers.push({
- type: 'NativeTransfer',
- from: tx.from,
- to: tx.to,
- value: BigInt(tx.value).toString(),
- txHash: tx.hash,
- timestamp: parseInt(block.timestamp)
- });
- }
- // Parse logs for other token transfers
- receipt.logs.forEach(log => {
- if (log.topics && log.topics.length > 0) {
- const topic0 = log.topics[0];
- // TRC-20 Transfer event
- if (topic0 === '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef' && log.topics.length === 3) {
- const from = addHexPrefix(log.topics[1].slice(-40));
- const to = addHexPrefix(log.topics[2].slice(-40));
- const value = decodeParameter('uint256', log.data);
- trc20Transfers.push({
- type: 'TRC20',
- from,
- to,
- value,
- contract: log.address,
- txHash: tx.hash,
- timestamp: parseInt(block.timestamp)
- });
- }
- // TRC-721 Transfer event
- else if (topic0 === '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef' && log.topics.length === 4) {
- const from = addHexPrefix(log.topics[1].slice(-40));
- const to = addHexPrefix(log.topics[2].slice(-40));
- const tokenId = decodeParameter('uint256', log.topics[3]);
- trc721Transfers.push({
- type: 'TRC721',
- from,
- to,
- tokenId,
- contract: log.address,
- txHash: tx.hash,
- timestamp: parseInt(block.timestamp)
- });
- }
- // TRC-1155 TransferSingle event
- else if (topic0 === '0xc3d58168c5ae7397731d063d5bbf3d657854427343f4c083240f7aacaa2d0f62') {
- const operator = addHexPrefix(log.topics[1].slice(-40));
- const from = addHexPrefix(log.topics[2].slice(-40));
- const to = addHexPrefix(log.topics[3].slice(-40));
- const [id, value] = decodeParameters(['uint256', 'uint256'], log.data);
- trc1155Transfers.push({
- type: 'TRC1155_Single',
- operator,
- from,
- to,
- tokenId: id,
- value,
- contract: log.address,
- txHash: tx.hash,
- timestamp: parseInt(block.timestamp)
- });
- }
- // TRC-1155 TransferBatch event
- else if (topic0 === '0x4a39dc06d4c0dbc64b70af90fd698a233a518aa5d07e595d983b8c0526c8f7fb') {
- const operator = addHexPrefix(log.topics[1].slice(-40));
- const from = addHexPrefix(log.topics[2].slice(-40));
- const to = addHexPrefix(log.topics[3].slice(-40));
- const [ids, values] = decodeParameters(['uint256[]', 'uint256[]'], log.data);
- ids.forEach((id, index) => {
- trc1155Transfers.push({
- type: 'TRC1155_Batch',
- operator,
- from,
- to,
- tokenId: id,
- value: values[index],
- contract: log.address,
- txHash: tx.hash,
- timestamp: parseInt(block.timestamp)
- });
- });
- }
- }
- });
- }
- return {
- nativeTransfers,
- trc20: trc20Transfers,
- trc721: trc721Transfers,
- trc1155: trc1155Transfers
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment