@merkl/api 0.15.4 → 0.15.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,6 @@
1
1
  // ─── Reward Breakdowns ETL ───────────────────────────────────────────────────
2
2
  if (!process.env.ENV || !process.env.CHAIN_ID || !process.env.ROOT)
3
3
  throw new Error("[ENV]: missing variable");
4
- import { CampaignService } from "../../modules/v4/campaign";
5
4
  import { log } from "../../utils/logger";
6
5
  import { apiDbClient } from "../../utils/prisma";
7
6
  import { withRetry } from "@sdk";
@@ -60,56 +59,63 @@ const extract = async () => {
60
59
  };
61
60
  // ─── Transform ───────────────────────────────────────────────────────────────
62
61
  const transform = async (rewardBreakdowns) => {
63
- const missingCampaigns = [];
64
- const foundCampaigns = [];
65
- const campaigns = rewardBreakdowns.reduce((acc, x) => {
66
- const campaignUnique = {
67
- campaignId: x.campaignId,
68
- distributionChain: Number.parseInt(process.env.CHAIN_ID),
69
- };
70
- if (!Object.keys(acc).includes(CampaignService.hashId(campaignUnique)))
71
- acc[CampaignService.hashId(campaignUnique)] = {
72
- campaignId: x.campaignId,
73
- distributionChain: Number.parseInt(process.env.CHAIN_ID),
74
- };
75
- return acc;
76
- }, {});
77
- for (const campaign of Object.values(campaigns)) {
78
- const campaignExists = await CampaignService.checkIfExist(campaign);
79
- if (!campaignExists) {
80
- const { success, fail } = await CampaignService.fill([campaign]);
81
- if (fail === 1 || success !== 1) {
82
- missingCampaigns.push(CampaignService.hashId(campaign));
83
- log.warn(`createManyBreakdown - Missing campaign: ${campaign.campaignId}`);
84
- continue;
85
- }
86
- }
87
- foundCampaigns.push(CampaignService.hashId(campaign));
88
- }
89
62
  const transformedRewardBreakdowns = [];
90
63
  for (const rewardBreakdown of rewardBreakdowns) {
91
- const campaignId = Bun.hash(`${process.env.CHAIN_ID}${rewardBreakdown.campaignId}`).toString();
92
64
  const rewardTokenId = Bun.hash(`${process.env.CHAIN_ID}${rewardBreakdown.token}`).toString();
93
65
  const rewardId = Bun.hash(`${process.env.ROOT}${rewardBreakdown.recipient}${rewardTokenId}`).toString();
94
66
  transformedRewardBreakdowns.push({
95
67
  rewardId,
96
68
  protocolId: rewardBreakdown.protocolId ? rewardBreakdown.protocolId : undefined,
97
- campaignId,
69
+ campaignId: rewardBreakdown.campaignId,
98
70
  reason: rewardBreakdown.reason ? rewardBreakdown.reason : "",
99
71
  amount: rewardBreakdown.amount,
100
72
  claimed: rewardBreakdown.claimed,
101
73
  pending: rewardBreakdown.pending,
102
74
  });
103
75
  }
104
- if (missingCampaigns.length !== 0) {
105
- log.warn(`createManyBreakdown - Missing ${missingCampaigns.length} campaigns: ${missingCampaigns.join(", ")}`);
106
- }
107
- return transformedRewardBreakdowns.filter(x => !missingCampaigns.includes(x.campaignId));
76
+ return transformedRewardBreakdowns;
108
77
  };
109
78
  // ─── Load ────────────────────────────────────────────────────────────────────
110
79
  const load = async (rewardBreakdowns) => {
80
+ log.info(`pushing ${rewardBreakdowns.length} rewardBreakdowns`);
81
+ // const missingCampaigns: string[] = [];
82
+ // const foundCampaigns: string[] = [];
83
+ // const campaigns = rewardBreakdowns.reduce(
84
+ // (acc, x) => {
85
+ // const campaignUnique = {
86
+ // campaignId: x.campaignId,
87
+ // distributionChain: Number.parseInt(process.env.CHAIN_ID!),
88
+ // };
89
+ // if (!Object.keys(acc).includes(CampaignService.hashId(campaignUnique)))
90
+ // acc[CampaignService.hashId(campaignUnique)] = {
91
+ // campaignId: x.campaignId,
92
+ // distributionChain: Number.parseInt(process.env.CHAIN_ID!),
93
+ // };
94
+ // return acc;
95
+ // },
96
+ // {} as Record<string, CampaignUnique>
97
+ // );
98
+ // for (const campaign of Object.values(campaigns)) {
99
+ // const campaignExists = await CampaignService.checkIfExist(campaign);
100
+ // if (!campaignExists) {
101
+ // const { success, fail } = await CampaignService.fill([campaign]);
102
+ // if (fail === 1 || success !== 1) {
103
+ // missingCampaigns.push(CampaignService.hashId(campaign));
104
+ // log.warn(`createManyBreakdown - Missing campaign: ${campaign.campaignId}`);
105
+ // continue;
106
+ // }
107
+ // }
108
+ // foundCampaigns.push(CampaignService.hashId(campaign));
109
+ // }
110
+ // if (missingCampaigns.length !== 0) {
111
+ // log.warn(`createManyBreakdown - Missing ${missingCampaigns.length} campaigns: ${missingCampaigns.join(", ")}`);
112
+ // }
111
113
  return (await apiDbClient.rewardBreakdown.createMany({
112
- data: rewardBreakdowns,
114
+ data: rewardBreakdowns.map(x => {
115
+ const campaignId = Bun.hash(`${process.env.CHAIN_ID}${x.campaignId}`).toString();
116
+ return { ...x, campaignId };
117
+ }),
118
+ // .filter(x => !missingCampaigns.includes(x.campaignId)),
113
119
  skipDuplicates: true,
114
120
  })).count;
115
121
  };