@continuumdao/ctm-mpc-defi 0.2.43 → 0.2.44

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.
@@ -4040,6 +4040,158 @@ async function buildEvmMultisignBodyMorphoMerklDistributorClaim(args) {
4040
4040
  })
4041
4041
  });
4042
4042
  }
4043
+ var MERKL_API_BASE = "https://api.merkl.xyz";
4044
+ var MERKL_DISTRIBUTOR_ADDRESS = "0x3Ef3D8bA38EBe18DB133cEc108f4D14CE00Dd9Ae";
4045
+ var MERKL_DISTRIBUTOR_CLAIM_GAS_FALLBACK = 500000n;
4046
+ var MERKL_REWARDS_NOTES = "Wallet-wide Merkl on this chain (not protocol-filtered). Other venues\u2019 campaigns can appear. Claims the first Merkle root batch; call again if additionalRoots > 0. rEUL is omitted (Euler unlock flow). Merkl updates every 8\u201312 hours.";
4047
+ function normHex32(p) {
4048
+ const s = (p ?? "").trim();
4049
+ if (!s) return null;
4050
+ const x = s.startsWith("0x") ? s : `0x${s}`;
4051
+ if (!/^0x[0-9a-fA-F]{64}$/.test(x)) return null;
4052
+ return x;
4053
+ }
4054
+ function merklUserRewardsUrl(addr, chainId) {
4055
+ return `${MERKL_API_BASE}/v4/users/${encodeURIComponent(addr)}/rewards?chainId=${chainId}`;
4056
+ }
4057
+ async function loadMerklUserRewardsJson(args) {
4058
+ const addr = viem.getAddress(args.user);
4059
+ const url = merklUserRewardsUrl(addr, args.chainId);
4060
+ const res = await fetch(url, { cache: "no-store" });
4061
+ if (!res.ok) {
4062
+ let detail = `Merkl API error ${res.status}`;
4063
+ try {
4064
+ const j = await res.json();
4065
+ if (j?.error) {
4066
+ detail = j.details ? `${j.error}: ${j.details}` : j.error;
4067
+ }
4068
+ } catch {
4069
+ }
4070
+ throw new Error(detail);
4071
+ }
4072
+ return res.json();
4073
+ }
4074
+ function parseMerklUserRewardsToLeaves(raw, args) {
4075
+ const addr = viem.getAddress(args.user);
4076
+ if (!Array.isArray(raw)) {
4077
+ throw new Error("Merkl API returned an unexpected rewards shape.");
4078
+ }
4079
+ const collected = [];
4080
+ for (const block of raw) {
4081
+ for (const rw of block.rewards ?? []) {
4082
+ const root = (rw.root ?? "").trim();
4083
+ const dist = rw.distributionChainId;
4084
+ if (!root || dist !== args.chainId) continue;
4085
+ let recipient;
4086
+ try {
4087
+ recipient = viem.getAddress((rw.recipient ?? "").trim());
4088
+ } catch {
4089
+ continue;
4090
+ }
4091
+ if (recipient !== addr) continue;
4092
+ let amt = 0n;
4093
+ let clm = 0n;
4094
+ try {
4095
+ amt = BigInt((rw.amount ?? "0").toString().trim() || "0");
4096
+ clm = BigInt((rw.claimed ?? "0").toString().trim() || "0");
4097
+ } catch {
4098
+ continue;
4099
+ }
4100
+ if (amt <= clm) continue;
4101
+ let tokenAddr;
4102
+ try {
4103
+ tokenAddr = viem.getAddress((rw.token?.address ?? "").trim());
4104
+ } catch {
4105
+ continue;
4106
+ }
4107
+ const proofsRaw = rw.proofs ?? [];
4108
+ const proofs = [];
4109
+ for (const p of proofsRaw) {
4110
+ const h = normHex32(String(p));
4111
+ if (!h) continue;
4112
+ proofs.push(h);
4113
+ }
4114
+ if (proofsRaw.length > 0 && proofs.length !== proofsRaw.length) continue;
4115
+ const sym = (rw.token?.symbol ?? "").trim() || "\u2014";
4116
+ let dec = 18;
4117
+ try {
4118
+ const d = Number(rw.token?.decimals ?? 18);
4119
+ if (Number.isFinite(d) && d >= 0 && d <= 36) dec = d;
4120
+ } catch {
4121
+ dec = 18;
4122
+ }
4123
+ collected.push({ root, token: tokenAddr, amountWei: amt, proofs, symbol: sym, decimals: dec });
4124
+ }
4125
+ }
4126
+ return collected;
4127
+ }
4128
+ function selectFirstRootMerklLeaves(collected) {
4129
+ if (collected.length === 0) return [];
4130
+ const root0 = collected[0].root;
4131
+ return collected.filter((x) => x.root === root0);
4132
+ }
4133
+ function isMerklRewardEulLeaf(leaf) {
4134
+ const s = leaf.symbol.trim().toLowerCase().replace(/\s+/g, " ").trim();
4135
+ return s === "reul" || s === "reward eul";
4136
+ }
4137
+ function selectWalletWideMerklClaimLeaves(collected) {
4138
+ return selectFirstRootMerklLeaves(collected.filter((l) => !isMerklRewardEulLeaf(l)));
4139
+ }
4140
+ async function fetchAllMerklDistributorClaimLeaves(args) {
4141
+ const raw = await loadMerklUserRewardsJson(args);
4142
+ return parseMerklUserRewardsToLeaves(raw, args);
4143
+ }
4144
+ function encodeMerklDistributorClaimData(args) {
4145
+ if (args.leaves.length === 0) throw new Error("No Merkl claim leaves.");
4146
+ const u = viem.getAddress(args.user);
4147
+ const abi = viem.parseAbi([
4148
+ "function claim(address[] users, address[] tokens, uint256[] amounts, bytes32[][] proofs) external"
4149
+ ]);
4150
+ const users = args.leaves.map(() => u);
4151
+ const tokens = args.leaves.map((l) => l.token);
4152
+ const amounts = args.leaves.map((l) => l.amountWei);
4153
+ const proofs = args.leaves.map((l) => [...l.proofs]);
4154
+ return viem.encodeFunctionData({
4155
+ abi,
4156
+ functionName: "claim",
4157
+ args: [users, tokens, amounts, proofs]
4158
+ });
4159
+ }
4160
+ function merklLeafToRow(leaf) {
4161
+ return {
4162
+ symbol: leaf.symbol,
4163
+ token: leaf.token,
4164
+ amountHuman: viem.formatUnits(leaf.amountWei, leaf.decimals),
4165
+ amountWei: leaf.amountWei.toString(),
4166
+ decimals: leaf.decimals,
4167
+ root: leaf.root
4168
+ };
4169
+ }
4170
+ async function fetchMerklRewardsSummary(args) {
4171
+ const user = viem.getAddress(args.user);
4172
+ const chainId = args.chainId;
4173
+ const all = await fetchAllMerklDistributorClaimLeaves({ chainId, user });
4174
+ const skippedReulCount = all.filter(isMerklRewardEulLeaf).length;
4175
+ const display = all.filter((l) => !isMerklRewardEulLeaf(l));
4176
+ const firstRoot = selectFirstRootMerklLeaves(display);
4177
+ const roots = new Set(display.map((l) => l.root));
4178
+ return {
4179
+ chainId,
4180
+ user,
4181
+ distributor: MERKL_DISTRIBUTOR_ADDRESS,
4182
+ claimable: display.map(merklLeafToRow),
4183
+ claimableCount: display.length,
4184
+ firstRootClaimableCount: firstRoot.length,
4185
+ additionalRoots: Math.max(0, roots.size - (firstRoot.length > 0 ? 1 : 0)),
4186
+ skippedReulCount,
4187
+ notes: MERKL_REWARDS_NOTES
4188
+ };
4189
+ }
4190
+
4191
+ // src/protocols/evm/morpho/merklRewards.ts
4192
+ async function morphoFetchMerklRewardsSummary(args) {
4193
+ return fetchMerklRewardsSummary(args);
4194
+ }
4043
4195
 
4044
4196
  // src/protocols/evm/morpho/stockDiscovery.ts
4045
4197
  init_midnightApi();
@@ -4110,7 +4262,8 @@ var morphoProtocolModule = {
4110
4262
  { id: "morpho.blue-borrow", protocolId: MORPHO_PROTOCOL_ID, chainCategory: "evm", description: "Borrow from Morpho Blue market", commonParams: ["keyGen", "purposeText", "useCustomGas"], params: {} },
4111
4263
  { id: "morpho.blue-repay", protocolId: MORPHO_PROTOCOL_ID, chainCategory: "evm", description: "Repay Morpho Blue borrow", commonParams: ["keyGen", "purposeText", "useCustomGas"], params: {} },
4112
4264
  { id: "morpho.blue-collateral-withdraw", protocolId: MORPHO_PROTOCOL_ID, chainCategory: "evm", description: "Withdraw Morpho Blue collateral", commonParams: ["keyGen", "purposeText", "useCustomGas"], params: {} },
4113
- { id: "morpho.merkl-claim", protocolId: MORPHO_PROTOCOL_ID, chainCategory: "evm", description: "Claim Morpho Merkl rewards", commonParams: ["keyGen", "purposeText", "useCustomGas"], params: {} },
4265
+ { id: "morpho.fetch-merkl-rewards", protocolId: MORPHO_PROTOCOL_ID, chainCategory: "evm", description: "List wallet-wide Merkl claimables on a chain", commonParams: [], params: {} },
4266
+ { id: "morpho.merkl-claim", protocolId: MORPHO_PROTOCOL_ID, chainCategory: "evm", description: "Claim wallet-wide Merkl rewards", commonParams: ["keyGen", "purposeText", "useCustomGas"], params: {} },
4114
4267
  { id: "morpho.fetch-blue-markets", protocolId: MORPHO_PROTOCOL_ID, chainCategory: "evm", description: "Search Morpho Blue variable-rate markets", commonParams: [], params: {} },
4115
4268
  { id: "morpho.fetch-stock-markets", protocolId: MORPHO_PROTOCOL_ID, chainCategory: "evm", description: "List Coinbase B20 stock-backed Morpho Blue and Midnight markets on Base", commonParams: [], params: {} },
4116
4269
  { id: "morpho.fetch-positions", protocolId: MORPHO_PROTOCOL_ID, chainCategory: "evm", description: "List Morpho vault and Blue positions for a user", commonParams: [], params: {} },
@@ -4128,6 +4281,10 @@ registerProtocolModule(morphoProtocolModule);
4128
4281
 
4129
4282
  exports.ARC_MAINNET_CHAIN_ID = ARC_MAINNET_CHAIN_ID;
4130
4283
  exports.ARC_TESTNET_CHAIN_ID = ARC_TESTNET_CHAIN_ID;
4284
+ exports.MERKL_API_BASE = MERKL_API_BASE;
4285
+ exports.MERKL_DISTRIBUTOR_ADDRESS = MERKL_DISTRIBUTOR_ADDRESS;
4286
+ exports.MERKL_DISTRIBUTOR_CLAIM_GAS_FALLBACK = MERKL_DISTRIBUTOR_CLAIM_GAS_FALLBACK;
4287
+ exports.MERKL_REWARDS_NOTES = MERKL_REWARDS_NOTES;
4131
4288
  exports.MORPHO_ARC_USDC = MORPHO_ARC_USDC;
4132
4289
  exports.MORPHO_BASE_USDC = MORPHO_BASE_USDC;
4133
4290
  exports.MORPHO_BASE_USDT = MORPHO_BASE_USDT;
@@ -4168,12 +4325,15 @@ exports.buildEvmMultisignBodyMorphoMidnightRepayBatch = buildEvmMultisignBodyMor
4168
4325
  exports.buildEvmMultisignBodyMorphoVaultDepositBatch = buildEvmMultisignBodyMorphoVaultDepositBatch;
4169
4326
  exports.buildEvmMultisignBodyMorphoVaultWithdraw = buildEvmMultisignBodyMorphoVaultWithdraw;
4170
4327
  exports.emptyMorphoEarnVaultDetailFields = emptyMorphoEarnVaultDetailFields;
4328
+ exports.encodeMerklDistributorClaimData = encodeMerklDistributorClaimData;
4171
4329
  exports.encodeMorphoBlueMarketParamsCallbackData = encodeMorphoBlueMarketParamsCallbackData;
4172
4330
  exports.encodeMorphoBlueSupplyCalldata = encodeMorphoBlueSupplyCalldata;
4173
4331
  exports.encodeMorphoBlueWithdrawCalldata = encodeMorphoBlueWithdrawCalldata;
4174
4332
  exports.enrichMorphoEarnOfferingRows = enrichMorphoEarnOfferingRows;
4175
4333
  exports.enrichMorphoEarnOfferingRowsForAgent = enrichMorphoEarnOfferingRowsForAgent;
4176
4334
  exports.ensureMorphoChainAssetCache = ensureMorphoChainAssetCache;
4335
+ exports.fetchAllMerklDistributorClaimLeaves = fetchAllMerklDistributorClaimLeaves;
4336
+ exports.fetchMerklRewardsSummary = fetchMerklRewardsSummary;
4177
4337
  exports.fetchMorphoBorrowMarketsForCollateral = fetchMorphoBorrowMarketsForCollateral;
4178
4338
  exports.fetchMorphoBorrowMarketsForCollaterals = fetchMorphoBorrowMarketsForCollaterals;
4179
4339
  exports.fetchMorphoBorrowMarketsForLoan = fetchMorphoBorrowMarketsForLoan;
@@ -4206,22 +4366,27 @@ exports.formatMorphoMarketApySummary = formatMorphoMarketApySummary;
4206
4366
  exports.formatMorphoMidnightAprPct = formatMorphoMidnightAprPct;
4207
4367
  exports.formatMorphoUsd = formatMorphoUsd;
4208
4368
  exports.formatMorphoUtilizationPct = formatMorphoUtilizationPct;
4369
+ exports.isMerklRewardEulLeaf = isMerklRewardEulLeaf;
4209
4370
  exports.isMorphoArcChainId = isMorphoArcChainId;
4210
4371
  exports.isMorphoB20Collateral = isMorphoB20Collateral;
4211
4372
  exports.isMorphoStockQuery = isMorphoStockQuery;
4212
4373
  exports.isMorphoVaultListed = isMorphoVaultListed;
4213
4374
  exports.isRobinhoodEarnProductFlag = isRobinhoodEarnProductFlag;
4214
4375
  exports.isRobinhoodEarnQuery = isRobinhoodEarnQuery;
4376
+ exports.loadMerklUserRewardsJson = loadMerklUserRewardsJson;
4215
4377
  exports.loadMorphoSupportedChainIds = loadMorphoSupportedChainIds;
4216
4378
  exports.looksLikeMorphoStockTicker = looksLikeMorphoStockTicker;
4217
4379
  exports.mapMorphoIncentiveRewards = mapMorphoIncentiveRewards;
4218
4380
  exports.marketParamsFromApiRow = marketParamsFromApiRow;
4219
4381
  exports.mergeMorphoSupportedChainIds = mergeMorphoSupportedChainIds;
4382
+ exports.merklLeafToRow = merklLeafToRow;
4383
+ exports.merklUserRewardsUrl = merklUserRewardsUrl;
4220
4384
  exports.morphoAssetLabel = morphoAssetLabel;
4221
4385
  exports.morphoB20CollateralAddresses = morphoB20CollateralAddresses;
4222
4386
  exports.morphoEarnOfferingToDiscoveryRow = morphoEarnOfferingToDiscoveryRow;
4223
4387
  exports.morphoFetchBlueMarketsSummary = morphoFetchBlueMarketsSummary;
4224
4388
  exports.morphoFetchEarnVaultsSummary = morphoFetchEarnVaultsSummary;
4389
+ exports.morphoFetchMerklRewardsSummary = morphoFetchMerklRewardsSummary;
4225
4390
  exports.morphoFetchMidnightBooksSummary = morphoFetchMidnightBooksSummary;
4226
4391
  exports.morphoFetchMidnightMakerOffersSummary = morphoFetchMidnightMakerOffersSummary;
4227
4392
  exports.morphoFetchMidnightPositionsSummary = morphoFetchMidnightPositionsSummary;
@@ -4248,6 +4413,7 @@ exports.morphoMidnightTtmSeconds = morphoMidnightTtmSeconds;
4248
4413
  exports.morphoMidnightUnitsFromAssets = morphoMidnightUnitsFromAssets;
4249
4414
  exports.morphoProtocolModule = morphoProtocolModule;
4250
4415
  exports.morphoResolveListedEarnVaultByAddress = morphoResolveListedEarnVaultByAddress;
4416
+ exports.parseMerklUserRewardsToLeaves = parseMerklUserRewardsToLeaves;
4251
4417
  exports.parseMorphoMidnightAprInput = parseMorphoMidnightAprInput;
4252
4418
  exports.parseMorphoMidnightPriceWad = parseMorphoMidnightPriceWad;
4253
4419
  exports.pickBestBlueSupplyMarketForLoan = pickBestBlueSupplyMarketForLoan;
@@ -4255,5 +4421,7 @@ exports.previewMorphoBlueHealthAfterCollateralWithdraw = previewMorphoBlueHealth
4255
4421
  exports.resolveMorphoAssetRef = resolveMorphoAssetRef;
4256
4422
  exports.resolveRobinhoodEarnFetchQuery = resolveRobinhoodEarnFetchQuery;
4257
4423
  exports.searchMorphoListedEarnVaults = searchMorphoListedEarnVaults;
4424
+ exports.selectFirstRootMerklLeaves = selectFirstRootMerklLeaves;
4425
+ exports.selectWalletWideMerklClaimLeaves = selectWalletWideMerklClaimLeaves;
4258
4426
  //# sourceMappingURL=index.cjs.map
4259
4427
  //# sourceMappingURL=index.cjs.map