@zkp2p/sdk 0.10.0-rc.6 → 0.10.0

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.
@@ -0,0 +1,5 @@
1
+ export { enrichPvDepositView, enrichPvIntentView, parseDepositView, parseIntentView } from './chunk-JRLUSPR3.mjs';
2
+ import './chunk-ZFBH4HD7.mjs';
3
+ import './chunk-J5LGTIGS.mjs';
4
+ //# sourceMappingURL=protocolViewerParsers-QMHI74AA.mjs.map
5
+ //# sourceMappingURL=protocolViewerParsers-QMHI74AA.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":[],"names":[],"mappings":"","file":"protocolViewerParsers-HMOHJWSP.mjs"}
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"protocolViewerParsers-QMHI74AA.mjs"}
package/dist/react.cjs CHANGED
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var react = require('react');
4
+ var viem = require('viem');
4
5
 
5
6
  // src/react/hooks/useCreateDeposit.ts
6
7
  function useCreateDeposit({ client, onSuccess, onError }) {
@@ -105,74 +106,34 @@ function useSignalIntent({ client, onSuccess, onError }) {
105
106
  [signalIntent, prepareSignalIntent, isLoading, error, txHash, prepared]
106
107
  );
107
108
  }
108
- function useGetTakerStake({
109
- client,
110
- owner,
111
- chainId,
112
- autoFetch = true,
113
- onSuccess,
114
- onError
115
- }) {
116
- const [isLoading, setIsLoading] = react.useState(false);
117
- const [error, setError] = react.useState(null);
118
- const [takerStake, setTakerStake] = react.useState(null);
119
- const onSuccessRef = react.useRef(onSuccess);
120
- const onErrorRef = react.useRef(onError);
121
- react.useEffect(() => {
122
- onSuccessRef.current = onSuccess;
123
- onErrorRef.current = onError;
124
- });
125
- const requestSeqRef = react.useRef(0);
126
- const getTakerStake = react.useCallback(
127
- async (params) => {
128
- if (!client) {
129
- const err = new Error("Zkp2pClient is not initialized");
130
- setError(err);
131
- onErrorRef.current?.(err);
132
- return null;
133
- }
134
- const request = params ?? (owner && chainId ? { owner, chainId } : null);
135
- if (!request) {
136
- const err = new Error("Owner address and chainId are required");
137
- setError(err);
138
- onErrorRef.current?.(err);
139
- return null;
140
- }
141
- const requestId = ++requestSeqRef.current;
142
- setIsLoading(true);
143
- setError(null);
144
- setTakerStake(null);
145
- try {
146
- const response = await client.getTakerStake(request);
147
- if (requestId !== requestSeqRef.current) return null;
148
- if (!response.success || !response.responseObject) {
149
- throw new Error(response.message || "Taker stake request failed");
150
- }
151
- setTakerStake(response.responseObject);
152
- onSuccessRef.current?.(response.responseObject);
153
- return response;
154
- } catch (err) {
155
- if (requestId !== requestSeqRef.current) return null;
156
- const e = err instanceof Error ? err : new Error(String(err));
157
- setError(e);
158
- onErrorRef.current?.(e);
159
- return null;
160
- } finally {
161
- if (requestId === requestSeqRef.current) setIsLoading(false);
162
- }
163
- },
164
- [client, owner, chainId]
165
- );
166
- react.useEffect(() => {
167
- if (!autoFetch || !owner || !chainId) return;
168
- void getTakerStake({ owner, chainId });
169
- }, [autoFetch, owner, chainId, getTakerStake]);
170
- return react.useMemo(
171
- () => ({ getTakerStake, takerStake, isLoading, error }),
172
- [getTakerStake, takerStake, isLoading, error]
173
- );
109
+
110
+ // src/takerTiers.ts
111
+ var TAKER_TIER_SCHEDULE = [
112
+ { tier: "PLATINUM", perIntentCapUsd: 5e3, minVolumeUsd: 1e5, maxVolumeUsd: Infinity },
113
+ { tier: "PRO", perIntentCapUsd: 2500, minVolumeUsd: 5e4, maxVolumeUsd: 99999 },
114
+ { tier: "PLUS", perIntentCapUsd: 1e3, minVolumeUsd: 1e4, maxVolumeUsd: 49999 },
115
+ { tier: "PEER", perIntentCapUsd: 250, minVolumeUsd: 1e3, maxVolumeUsd: 9999 },
116
+ { tier: "PEASANT", perIntentCapUsd: 100, minVolumeUsd: 0, maxVolumeUsd: 999 }
117
+ ];
118
+ var TAKER_TIER_ORDER = [
119
+ "PEASANT",
120
+ "PEER",
121
+ "PLUS",
122
+ "PRO",
123
+ "PLATINUM"
124
+ ];
125
+ var TAKER_TIER_CAPS = Object.fromEntries(
126
+ TAKER_TIER_SCHEDULE.map((entry) => [entry.tier, entry.perIntentCapUsd])
127
+ );
128
+ function getNextTakerTier(currentTier) {
129
+ if (!currentTier) return null;
130
+ const index = TAKER_TIER_ORDER.indexOf(currentTier);
131
+ if (index === -1 || index >= TAKER_TIER_ORDER.length - 1) return null;
132
+ return TAKER_TIER_ORDER[index + 1];
174
133
  }
175
- function useGetTakerCapacity({
134
+
135
+ // src/react/hooks/useGetTakerTier.ts
136
+ function useGetTakerTier({
176
137
  client,
177
138
  owner,
178
139
  chainId,
@@ -182,63 +143,98 @@ function useGetTakerCapacity({
182
143
  }) {
183
144
  const [isLoading, setIsLoading] = react.useState(false);
184
145
  const [error, setError] = react.useState(null);
185
- const [takerCapacity, setTakerCapacity] = react.useState(null);
186
- const onSuccessRef = react.useRef(onSuccess);
187
- const onErrorRef = react.useRef(onError);
188
- react.useEffect(() => {
189
- onSuccessRef.current = onSuccess;
190
- onErrorRef.current = onError;
191
- });
192
- const requestSeqRef = react.useRef(0);
193
- const getTakerCapacity = react.useCallback(
146
+ const [takerTier, setTakerTier] = react.useState(null);
147
+ const getTakerTier = react.useCallback(
194
148
  async (params) => {
195
149
  if (!client) {
196
150
  const err = new Error("Zkp2pClient is not initialized");
197
151
  setError(err);
198
- onErrorRef.current?.(err);
152
+ onError?.(err);
199
153
  return null;
200
154
  }
201
155
  const request = params ?? (owner && chainId ? { owner, chainId } : null);
202
156
  if (!request) {
203
157
  const err = new Error("Owner address and chainId are required");
204
158
  setError(err);
205
- onErrorRef.current?.(err);
159
+ onError?.(err);
206
160
  return null;
207
161
  }
208
- const requestId = ++requestSeqRef.current;
209
162
  setIsLoading(true);
210
163
  setError(null);
211
- setTakerCapacity(null);
164
+ setTakerTier(null);
212
165
  try {
213
- const response = await client.getTakerCapacity(request);
214
- if (requestId !== requestSeqRef.current) return null;
215
- if (!response.success || !response.responseObject) {
216
- throw new Error(response.message || "Taker capacity request failed");
217
- }
218
- setTakerCapacity(response.responseObject);
219
- onSuccessRef.current?.(response.responseObject);
166
+ const response = await client.getTakerTier(request);
167
+ setTakerTier(response.responseObject);
168
+ onSuccess?.(response.responseObject);
220
169
  return response;
221
170
  } catch (err) {
222
- if (requestId !== requestSeqRef.current) return null;
223
171
  const e = err instanceof Error ? err : new Error(String(err));
224
172
  setError(e);
225
- onErrorRef.current?.(e);
173
+ onError?.(e);
226
174
  return null;
227
175
  } finally {
228
- if (requestId === requestSeqRef.current) setIsLoading(false);
176
+ setIsLoading(false);
229
177
  }
230
178
  },
231
- [client, owner, chainId]
179
+ [client, owner, chainId, onSuccess, onError]
232
180
  );
233
181
  react.useEffect(() => {
234
182
  if (!autoFetch || !owner || !chainId) return;
235
- void getTakerCapacity({ owner, chainId });
236
- }, [autoFetch, owner, chainId, getTakerCapacity]);
183
+ void getTakerTier({ owner, chainId });
184
+ }, [autoFetch, owner, chainId, getTakerTier]);
237
185
  return react.useMemo(
238
- () => ({ getTakerCapacity, takerCapacity, isLoading, error }),
239
- [getTakerCapacity, takerCapacity, isLoading, error]
186
+ () => ({ getTakerTier, takerTier, isLoading, error }),
187
+ [getTakerTier, takerTier, isLoading, error]
240
188
  );
241
189
  }
190
+ function getNextTierCap(currentTier) {
191
+ const nextTier = getNextTakerTier(currentTier);
192
+ if (!nextTier) return null;
193
+ const nextCap = TAKER_TIER_CAPS[nextTier];
194
+ return nextCap != null ? `$${nextCap.toLocaleString()}` : null;
195
+ }
196
+ function formatCapFromBaseUnits(baseUnits) {
197
+ if (!baseUnits || baseUnits === "") return "$0";
198
+ const value = Number(viem.formatUnits(BigInt(baseUnits), 6));
199
+ return `$${value.toLocaleString()}`;
200
+ }
201
+ function getCapForDisplay(tier) {
202
+ const platformLimits = tier.platformLimits ?? [];
203
+ if (platformLimits.length === 0) {
204
+ return tier.perIntentCapBaseUnits ?? tier.maxOrderSize;
205
+ }
206
+ const bestCap = platformLimits.filter((limit) => !limit.isLocked && limit.effectiveCap && limit.effectiveCap !== "0").reduce((current, limit) => {
207
+ if (!current) return limit.effectiveCap;
208
+ return BigInt(limit.effectiveCap) > BigInt(current) ? limit.effectiveCap : current;
209
+ }, void 0);
210
+ return bestCap ?? "0";
211
+ }
212
+ function getTierDisplayInfo(tier) {
213
+ if (!tier) {
214
+ return {
215
+ tierLabel: "Loading...",
216
+ capDisplay: "...",
217
+ isNewUser: false,
218
+ showScore: false
219
+ };
220
+ }
221
+ const isNewUser = tier.source === "fallback";
222
+ const isZeroFulfills = tier.stats?.lifetimeFulfilledCount === 0;
223
+ const tierLabels = {
224
+ PEASANT: "Peer Peasant",
225
+ PEER: "Peer",
226
+ PLUS: "Peer Plus",
227
+ PRO: "Peer Pro",
228
+ PLATINUM: "Peer Platinum"
229
+ };
230
+ const capDisplay = formatCapFromBaseUnits(getCapForDisplay(tier));
231
+ return {
232
+ tierLabel: tierLabels[tier.tier] || tier.tier,
233
+ capDisplay,
234
+ isNewUser: isNewUser || isZeroFulfills,
235
+ showScore: !isNewUser && !!tier.stats && tier.stats.lockScoreDiluted !== "0"
236
+ };
237
+ }
242
238
  function useFulfillIntent({ client, onSuccess, onError }) {
243
239
  const [isLoading, setIsLoading] = react.useState(false);
244
240
  const [error, setError] = react.useState(null);
@@ -1206,6 +1202,148 @@ function useSetVaultFee({ client, onSuccess, onError }) {
1206
1202
  [setVaultFee, prepareSetVaultFee, isLoading, error, txHash, prepared]
1207
1203
  );
1208
1204
  }
1205
+
1206
+ // src/client/IntentGuardianOperations.ts
1207
+ var GUARDIAN_ERROR_CODES = {
1208
+ ExtensionsDisabled: "EXTENSIONS_DISABLED",
1209
+ ExtensionCostExceedsMax: "COST_EXCEEDS_MAX",
1210
+ ExtensionFeeExceedsIntentAmount: "FEE_EXCEEDS_INTENT_AMOUNT",
1211
+ IntentAlreadyExpired: "INTENT_ALREADY_EXPIRED",
1212
+ EscrowNotWhitelisted: "ESCROW_NOT_WHITELISTED",
1213
+ ZeroAddress: "ZERO_ADDRESS"
1214
+ };
1215
+ function classifyIntentGuardianError(error) {
1216
+ const haystack = error instanceof Error ? `${error.name} ${error.message}` : typeof error === "string" ? error : "";
1217
+ for (const [name, code] of Object.entries(GUARDIAN_ERROR_CODES)) {
1218
+ if (haystack.includes(name)) return code;
1219
+ }
1220
+ return "UNKNOWN";
1221
+ }
1222
+
1223
+ // src/react/hooks/useExtendIntentLifetime.ts
1224
+ function useExtendIntentLifetime({
1225
+ client,
1226
+ onSuccess,
1227
+ onError
1228
+ }) {
1229
+ const [isLoading, setIsLoading] = react.useState(false);
1230
+ const [error, setError] = react.useState(null);
1231
+ const [errorCode, setErrorCode] = react.useState(null);
1232
+ const [txHash, setTxHash] = react.useState(null);
1233
+ const [prepared, setPrepared] = react.useState(null);
1234
+ const [policy, setPolicy] = react.useState(null);
1235
+ const [quote, setQuote] = react.useState(null);
1236
+ const fail = react.useCallback(
1237
+ (err) => {
1238
+ const e = err instanceof Error ? err : new Error(String(err));
1239
+ const code = classifyIntentGuardianError(e);
1240
+ setError(e);
1241
+ setErrorCode(code);
1242
+ onError?.(e, code);
1243
+ return null;
1244
+ },
1245
+ [onError]
1246
+ );
1247
+ const requireClient = react.useCallback(() => {
1248
+ if (!client) {
1249
+ fail(new Error("Zkp2pClient is not initialized"));
1250
+ return null;
1251
+ }
1252
+ return client;
1253
+ }, [client, fail]);
1254
+ const refreshPolicy = react.useCallback(async () => {
1255
+ const c = requireClient();
1256
+ if (!c) return null;
1257
+ setIsLoading(true);
1258
+ setError(null);
1259
+ setErrorCode(null);
1260
+ try {
1261
+ const next = await c.getIntentGuardianPolicy();
1262
+ setPolicy(next);
1263
+ return next;
1264
+ } catch (err) {
1265
+ return fail(err);
1266
+ } finally {
1267
+ setIsLoading(false);
1268
+ }
1269
+ }, [requireClient, fail]);
1270
+ const refreshQuote = react.useCallback(
1271
+ async (params) => {
1272
+ setQuote(null);
1273
+ const c = requireClient();
1274
+ if (!c) return null;
1275
+ setIsLoading(true);
1276
+ setError(null);
1277
+ setErrorCode(null);
1278
+ try {
1279
+ const next = await c.quoteIntentExtension(params);
1280
+ setQuote(next);
1281
+ return next;
1282
+ } catch (err) {
1283
+ return fail(err);
1284
+ } finally {
1285
+ setIsLoading(false);
1286
+ }
1287
+ },
1288
+ [requireClient, fail]
1289
+ );
1290
+ const extendIntentLifetime = react.useCallback(
1291
+ async (params) => {
1292
+ const c = requireClient();
1293
+ if (!c) return null;
1294
+ setIsLoading(true);
1295
+ setError(null);
1296
+ setErrorCode(null);
1297
+ setTxHash(null);
1298
+ try {
1299
+ const hash = await c.extendIntentLifetime(params);
1300
+ setTxHash(hash);
1301
+ onSuccess?.(hash);
1302
+ return hash;
1303
+ } catch (err) {
1304
+ return fail(err);
1305
+ } finally {
1306
+ setIsLoading(false);
1307
+ }
1308
+ },
1309
+ [requireClient, fail, onSuccess]
1310
+ );
1311
+ const prepareExtendIntentLifetime = react.useCallback(
1312
+ async (params) => {
1313
+ const c = requireClient();
1314
+ if (!c) return null;
1315
+ setIsLoading(true);
1316
+ setError(null);
1317
+ setErrorCode(null);
1318
+ setPrepared(null);
1319
+ try {
1320
+ const tx = await c.extendIntentLifetime.prepare(params);
1321
+ setPrepared(tx);
1322
+ return tx;
1323
+ } catch (err) {
1324
+ return fail(err);
1325
+ } finally {
1326
+ setIsLoading(false);
1327
+ }
1328
+ },
1329
+ [requireClient, fail]
1330
+ );
1331
+ return {
1332
+ extendIntentLifetime,
1333
+ prepareExtendIntentLifetime,
1334
+ refreshPolicy,
1335
+ refreshQuote,
1336
+ policy,
1337
+ quote,
1338
+ isLoading,
1339
+ error,
1340
+ errorCode,
1341
+ txHash,
1342
+ prepared,
1343
+ /** False where no guardian is deployed; hide the extension action entirely. */
1344
+ isAvailable: client?.hasIntentGuardian() ?? false
1345
+ };
1346
+ }
1209
1347
  function useSetVaultMinRate({ client, onSuccess, onError }) {
1210
1348
  const [isLoading, setIsLoading] = react.useState(false);
1211
1349
  const [error, setError] = react.useState(null);
@@ -1584,6 +1722,8 @@ exports.asErrorMessage = asErrorMessage;
1584
1722
  exports.assertDelegationMethodSupport = assertDelegationMethodSupport;
1585
1723
  exports.classifyDelegationState = classifyDelegationState;
1586
1724
  exports.getDelegationRoute = getDelegationRoute;
1725
+ exports.getNextTierCap = getNextTierCap;
1726
+ exports.getTierDisplayInfo = getTierDisplayInfo;
1587
1727
  exports.isZeroRateManagerId = isZeroRateManagerId;
1588
1728
  exports.normalizeRateManagerId = normalizeRateManagerId;
1589
1729
  exports.normalizeRegistry = normalizeRegistry;
@@ -1593,9 +1733,9 @@ exports.useAddPaymentMethods = useAddPaymentMethods;
1593
1733
  exports.useCreateDeposit = useCreateDeposit;
1594
1734
  exports.useCreateVault = useCreateVault;
1595
1735
  exports.useDeactivateCurrency = useDeactivateCurrency;
1736
+ exports.useExtendIntentLifetime = useExtendIntentLifetime;
1596
1737
  exports.useFulfillIntent = useFulfillIntent;
1597
- exports.useGetTakerCapacity = useGetTakerCapacity;
1598
- exports.useGetTakerStake = useGetTakerStake;
1738
+ exports.useGetTakerTier = useGetTakerTier;
1599
1739
  exports.usePruneExpiredIntents = usePruneExpiredIntents;
1600
1740
  exports.useReleaseFundsToPayer = useReleaseFundsToPayer;
1601
1741
  exports.useRemoveCurrency = useRemoveCurrency;