countTransactionPeriods.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. const getWeekNumber = (date) => {
  2. const year = date.getFullYear();
  3. const oneJan = new Date(year, 0, 1);
  4. const dayIndex = (date.getDay() + 6) % 7;
  5. const daysSinceFirstDay = Math.floor((date.getTime() - oneJan.getTime()) / 86400000);
  6. const weekIndex = Math.floor((daysSinceFirstDay + oneJan.getDay() - dayIndex) / 7);
  7. return `${year}-${weekIndex}`;
  8. };
  9. export const countTransactionPeriods = (
  10. address,
  11. transactions,
  12. protocol,
  13. addresses = [],
  14. ) => {
  15. address;
  16. protocol;
  17. const uniqueDays = new Set();
  18. const uniqueWeeks = new Set();
  19. const uniqueMonths = new Set();
  20. transactions.forEach((transaction) => {
  21. if (
  22. protocol !== 'zksynceraportal' &&
  23. !addresses.includes(transaction.to.toLowerCase()) &&
  24. !addresses.includes(transaction.from.toLowerCase())
  25. )
  26. return;
  27. if (protocol === 'zksynceraportal') {
  28. if (
  29. !transaction.data.startsWith('0x51cff8d9') &&
  30. !(transaction.to.toLowerCase() === address.toLowerCase() && transaction.isL1Originated)
  31. )
  32. return;
  33. }
  34. const timestamp = new Date(transaction.receivedAt);
  35. const year = timestamp.getFullYear();
  36. const month = timestamp.getMonth();
  37. const day = timestamp.getDate();
  38. const week = getWeekNumber(timestamp);
  39. uniqueDays.add(`${year}-${month}-${day}`);
  40. uniqueWeeks.add(`${year}-${week}`);
  41. uniqueMonths.add(`${year}-${month}`);
  42. });
  43. return {
  44. days: uniqueDays.size,
  45. weeks: uniqueWeeks.size,
  46. months: uniqueMonths.size,
  47. };
  48. };