@merkl/api 0.10.361 → 0.10.362

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.
package/package.json CHANGED
@@ -93,5 +93,5 @@
93
93
  "access": "public",
94
94
  "registry": "https://registry.npmjs.org/"
95
95
  },
96
- "version": "v0.10.361"
96
+ "version": "v0.10.362"
97
97
  }
@@ -1 +0,0 @@
1
- export declare const writeBreakdownsToAPI: (chainId: number, root: string) => Promise<void>;
@@ -1,55 +0,0 @@
1
- import { CampaignService, TokenService } from "../modules/v4";
2
- import { BucketService } from "../modules/v4/bucket/bucket.service";
3
- import { RewardService } from "../modules/v4/reward";
4
- import { log } from "../utils/logger";
5
- import { apiDbClient } from "../utils/prisma";
6
- import moment from "moment";
7
- // @dev Trigger at the end of the rewards job
8
- export const writeBreakdownsToAPI = async (chainId, root) => {
9
- const start = moment().unix();
10
- const environmentName = process.env.ENV === "staging" ? "staging" : "production";
11
- const breakdownBatches = [[]];
12
- const breakdownBatchPromises = [];
13
- let currentBreakdownBatchIndex = 0;
14
- const pushBreakdowns = async (index) => {
15
- log.local(`pushing breakdown batch ${index} (${breakdownBatches[index].length} points) to API DB`);
16
- // Upsert users in case they don't exist yet
17
- const breakdownsToCreate = [];
18
- for (const b of breakdownBatches[index]) {
19
- const campaignId = CampaignService.hashId({ distributionChain: chainId, campaignId: b.campaignId });
20
- const rewardTokenId = TokenService.hashId({ chainId, address: b.token });
21
- const rewardId = RewardService.hashId(root, b.recipient, rewardTokenId);
22
- breakdownsToCreate.push({
23
- rewardId,
24
- protocolId: b.protocolId ? b.protocolId : undefined,
25
- campaignId,
26
- reason: b.reason ? b.reason : "",
27
- amount: b.amount,
28
- claimed: b.claimed,
29
- pending: b.pending,
30
- auxiliaryData1: b.auxiliaryData1 ?? "",
31
- auxiliaryData2: b.auxiliaryData2 ?? "",
32
- });
33
- }
34
- return (await apiDbClient.rewardBreakdown.createMany({
35
- data: breakdownsToCreate,
36
- skipDuplicates: true, // To avoid revert if data already exists
37
- })).count;
38
- };
39
- await BucketService.readStreamFromBucket(`breakdowns/${chainId}-${root}.gz`, `merkl-rewards-lake-${environmentName}`, `merkl-data-${environmentName}`, async (x) => {
40
- const breakdowns = JSON.parse(x);
41
- for (const b of breakdowns) {
42
- breakdownBatches[currentBreakdownBatchIndex].push(b);
43
- if (breakdownBatches[currentBreakdownBatchIndex].length >= 30_000) {
44
- breakdownBatchPromises.push(pushBreakdowns(currentBreakdownBatchIndex));
45
- breakdownBatches.push([]);
46
- currentBreakdownBatchIndex++;
47
- }
48
- }
49
- return;
50
- });
51
- // Final batch
52
- breakdownBatchPromises.push(pushBreakdowns(currentBreakdownBatchIndex));
53
- const breakdownsCreated = (await Promise.all(breakdownBatchPromises)).reduce((acc, x) => acc + x, 0);
54
- log.info(`✅ Successfully created ${breakdownsCreated} breakdowns in ${moment().unix() - start}sec`);
55
- };
@@ -1 +0,0 @@
1
- export declare const writeRewardsToAPI: (chainId: number, root: string) => Promise<void>;
@@ -1,74 +0,0 @@
1
- import { TokenService } from "../modules/v4";
2
- import { BucketService } from "../modules/v4/bucket/bucket.service";
3
- import { RewardService } from "../modules/v4/reward";
4
- import { log } from "../utils/logger";
5
- import { apiDbClient } from "../utils/prisma";
6
- import { HOUR } from "@sdk";
7
- import moment from "moment";
8
- import { writeBreakdownsToAPI } from "./breakdowns";
9
- export const writeRewardsToAPI = async (chainId, root) => {
10
- const start = moment().unix();
11
- const environmentName = process.env.ENV === "staging" ? "staging" : "production";
12
- const now = moment().unix();
13
- // Upsert root
14
- await apiDbClient.merklRoot.upsert({
15
- create: {
16
- root,
17
- chainId,
18
- epoch: Math.floor(now / HOUR),
19
- timestamp: now,
20
- },
21
- update: {},
22
- where: {
23
- root,
24
- },
25
- });
26
- // Read reward stream and populate the table
27
- const rewardBatches = [[]];
28
- const rewardBatchPromises = [];
29
- let currentRewardBatchIndex = 0;
30
- const pushRewards = async (index) => {
31
- log.local(`pushing rewards batch ${index} (${rewardBatches[index].length} points) to API DB`);
32
- // Upsert users in case they don't exist yet
33
- await apiDbClient.user.createMany({
34
- data: rewardBatches[index].map(r => {
35
- return {
36
- address: r.recipient,
37
- };
38
- }),
39
- skipDuplicates: true,
40
- });
41
- const rewardsToCreate = rewardBatches[index].map(reward => {
42
- const rewardTokenId = TokenService.hashId({ chainId, address: reward.rewardToken });
43
- const id = RewardService.hashId(root, reward.recipient, rewardTokenId);
44
- return {
45
- id,
46
- root: reward.root,
47
- amount: reward.amount,
48
- pending: reward.pending,
49
- claimed: reward.claimed,
50
- recipient: reward.recipient,
51
- rewardTokenId,
52
- proofs: reward.proofs,
53
- };
54
- });
55
- return (await apiDbClient.reward.createMany({
56
- data: rewardsToCreate,
57
- skipDuplicates: true, // To avoid revert if data already exists
58
- })).count;
59
- };
60
- await BucketService.readStreamFromBucket(`rewards/${chainId}-${root}.gz`, `merkl-rewards-lake-${environmentName}`, `merkl-data-${environmentName}`, async (x) => {
61
- rewardBatches[currentRewardBatchIndex].push(JSON.parse(x));
62
- if (rewardBatches[currentRewardBatchIndex].length >= 20_000) {
63
- rewardBatchPromises.push(pushRewards(currentRewardBatchIndex));
64
- currentRewardBatchIndex++;
65
- rewardBatches.push([]);
66
- }
67
- return;
68
- });
69
- // Final batch
70
- rewardBatchPromises.push(pushRewards(currentRewardBatchIndex));
71
- const rewardsCreated = (await Promise.all(rewardBatchPromises)).reduce((acc, x) => acc + x, 0);
72
- log.info(`✅ Successfully created ${rewardsCreated} rewards in ${moment().unix() - start}sec`);
73
- await writeBreakdownsToAPI(chainId, root);
74
- };