@merkl/api 0.10.153 → 0.10.154

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -90,5 +90,5 @@
90
90
  "access": "public",
91
91
  "registry": "https://registry.npmjs.org/"
92
92
  },
93
- "version": "v0.10.153"
93
+ "version": "v0.10.154"
94
94
  }
@@ -1,2 +0,0 @@
1
- import { type ChainId } from "@sdk";
2
- export declare function getUserRewards(merklChainId: ChainId, merklRoot: string, user: string, mainParameter: string | undefined, rewardToken: string | undefined, proof: boolean): Promise<any>;
@@ -1,154 +0,0 @@
1
- import { engineDbClient } from "../../utils/prisma";
2
- import { DistributorInterface, NETWORK_LABELS, registry } from "@sdk";
3
- import Big from "big.js";
4
- import { BigNumber } from "ethers";
5
- import { batchMulticallCallWithRetry } from "../../utils/generic";
6
- export async function getUserRewards(merklChainId, merklRoot, user, mainParameter, rewardToken, proof) {
7
- if (!merklRoot)
8
- throw new Error(`No merklRoot when calling getUserRewards on ${NETWORK_LABELS[merklChainId]} for ${user}`);
9
- const res = {};
10
- let filters = {
11
- chainId: merklChainId,
12
- recipient: user,
13
- };
14
- if (rewardToken) {
15
- filters = {
16
- ...filters,
17
- rewardToken: rewardToken,
18
- };
19
- }
20
- const [leaves, claims] = await engineDbClient.$transaction([
21
- engineDbClient.leaves.findMany({ where: { ...filters, root: merklRoot } }),
22
- engineDbClient.claims.findMany({ where: { ...filters } }),
23
- ]);
24
- const calls = [];
25
- const earnedTokens = [];
26
- const claimedTokenAmounts = {};
27
- for (const leaf of leaves) {
28
- if (!earnedTokens.includes(leaf.rewardToken)) {
29
- earnedTokens.push(leaf.rewardToken);
30
- }
31
- }
32
- for (const token of earnedTokens) {
33
- calls.push({
34
- target: registry(merklChainId)?.Merkl?.Distributor,
35
- callData: DistributorInterface.encodeFunctionData("claimed", [user, token]),
36
- allowFailure: true,
37
- });
38
- }
39
- const claimedCallResult = await batchMulticallCallWithRetry(merklChainId, { calls });
40
- const fullyClaimedTokens = [];
41
- for (const [index, token] of earnedTokens.entries()) {
42
- const amount = DistributorInterface.decodeFunctionResult("claimed", claimedCallResult[index].returnData)[0];
43
- const root = DistributorInterface.decodeFunctionResult("claimed", claimedCallResult[index].returnData)[2];
44
- claimedTokenAmounts[token] = amount;
45
- if (root === merklRoot) {
46
- fullyClaimedTokens.push(token);
47
- }
48
- }
49
- let campaignFilters = {
50
- campaignId: { in: leaves.map(leaf => leaf.campaignId) },
51
- chainId: merklChainId,
52
- };
53
- if (mainParameter) {
54
- campaignFilters = {
55
- ...campaignFilters,
56
- mainParameter: mainParameter,
57
- };
58
- }
59
- if (rewardToken) {
60
- campaignFilters = {
61
- ...campaignFilters,
62
- rewardToken: rewardToken,
63
- };
64
- }
65
- const campaigns = await engineDbClient.campaigns.findMany({ where: campaignFilters });
66
- const campaignMapping = campaigns.reduce((prev, campaign) => {
67
- prev[`${campaign.chainId}_${campaign.campaignId}`] = campaign;
68
- return prev;
69
- }, {});
70
- if (leaves.length === 0) {
71
- return {};
72
- }
73
- const proofMapping = {};
74
- if (proof) {
75
- const proofs = await engineDbClient.proofs.findMany({
76
- select: {
77
- chainId: true,
78
- proof: true,
79
- rewardToken: true,
80
- },
81
- where: {
82
- chainId: merklChainId,
83
- recipient: user,
84
- root: merklRoot,
85
- },
86
- });
87
- // Create a map of proofs for each token
88
- for (const proof of proofs) {
89
- if (!Object.hasOwn(proofMapping, proof.chainId)) {
90
- proofMapping[proof.chainId] = {};
91
- }
92
- proofMapping[proof.chainId][proof.rewardToken] = proof.proof;
93
- }
94
- }
95
- for (const leaf of leaves) {
96
- if (mainParameter && campaignMapping[`${leaf.chainId}_${leaf.campaignId}`]?.mainParameter !== mainParameter) {
97
- continue;
98
- }
99
- if (!Object.hasOwn(res, leaf.rewardToken)) {
100
- res[leaf.rewardToken] = {
101
- accumulated: leaf.amount,
102
- decimals: campaignMapping?.[`${leaf.chainId}_${leaf.campaignId}`]?.campaignParameters
103
- ?.decimalsRewardToken,
104
- reasons: {},
105
- symbol: campaignMapping?.[`${leaf.chainId}_${leaf.campaignId}`]?.campaignParameters?.symbolRewardToken,
106
- unclaimed: fullyClaimedTokens.includes(leaf.rewardToken) ? "0" : leaf.amount,
107
- };
108
- if (proof) {
109
- res[leaf.rewardToken].proof = proofMapping?.[leaf.chainId]?.[leaf.rewardToken] ?? [];
110
- }
111
- }
112
- else {
113
- res[leaf.rewardToken].accumulated = new Big(leaf.amount)
114
- .add(new Big(res[leaf.rewardToken].accumulated))
115
- .toFixed(0);
116
- res[leaf.rewardToken].unclaimed = fullyClaimedTokens.includes(leaf.rewardToken)
117
- ? "0"
118
- : new Big(leaf.amount).add(new Big(res[leaf.rewardToken].unclaimed)).toFixed(0);
119
- }
120
- if (!Object.hasOwn(res[leaf.rewardToken].reasons, leaf.reason)) {
121
- res[leaf.rewardToken].reasons[leaf.reason] = {
122
- accumulated: leaf.amount,
123
- unclaimed: fullyClaimedTokens.includes(leaf.rewardToken) ? "0" : leaf.amount,
124
- };
125
- }
126
- else {
127
- res[leaf.rewardToken].reasons[leaf.reason].accumulated = new Big(leaf.amount)
128
- .add(new Big(res[leaf.rewardToken].reasons[leaf.reason].accumulated))
129
- .toFixed(0);
130
- res[leaf.rewardToken].reasons[leaf.reason].unclaimed = fullyClaimedTokens.includes(leaf.rewardToken)
131
- ? "0"
132
- : new Big(leaf.amount).add(new Big(res[leaf.rewardToken].reasons[leaf.reason].unclaimed)).toFixed(0);
133
- }
134
- }
135
- for (const claim of claims) {
136
- if (mainParameter && campaignMapping[`${claim.chainId}_${claim.campaignId}`]?.mainParameter !== mainParameter) {
137
- continue;
138
- }
139
- if (!!res?.[claim.rewardToken] && !fullyClaimedTokens.includes(claim.rewardToken)) {
140
- res[claim.rewardToken].unclaimed = new Big(res[claim.rewardToken].unclaimed).sub(claim.claimed).toFixed(0);
141
- if (!!res[claim.rewardToken].reasons?.[claim.reason]) {
142
- res[claim.rewardToken].reasons[claim.reason].unclaimed = new Big(res[claim.rewardToken].reasons[claim.reason].unclaimed)
143
- .sub(claim.claimed)
144
- .toFixed(0);
145
- }
146
- }
147
- }
148
- for (const token of Object.keys(res)) {
149
- if (BigNumber.from(res[token].accumulated).lte(claimedTokenAmounts?.[token] ?? "0") && !mainParameter) {
150
- res[token].unclaimed = "0";
151
- }
152
- }
153
- return res;
154
- }