@glacislabs/stablegenie-zerodelta 3.0.0 → 3.0.2
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/dist/{chunk-VL5MWEY2.js → chunk-AZPTL3KD.js} +92 -22
- package/dist/{chunk-6FPSGLRR.js → chunk-TR2XMP7X.js} +310 -56
- package/dist/core/index.cjs +92 -22
- package/dist/core/index.d.cts +11 -9
- package/dist/core/index.d.ts +11 -9
- package/dist/core/index.js +1 -1
- package/dist/index.cjs +401 -77
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/{mount-yTJi4VdA.d.ts → mount-8nchcNbL.d.ts} +1 -1
- package/dist/{mount-yqXrDBLc.d.cts → mount-BC5gY1X6.d.cts} +1 -1
- package/dist/mount.cjs +401 -77
- package/dist/mount.d.cts +2 -2
- package/dist/mount.d.ts +2 -2
- package/dist/mount.js +2 -2
- package/dist/standalone.global.js +12 -8
- package/dist/{verify-CaO0OD4Z.d.cts → verify-DFTuQ2g3.d.cts} +9 -1
- package/dist/{verify-CaO0OD4Z.d.ts → verify-DFTuQ2g3.d.ts} +9 -1
- package/package.json +1 -1
|
@@ -856,6 +856,16 @@ function createApiClient(cfg) {
|
|
|
856
856
|
}
|
|
857
857
|
|
|
858
858
|
// src/core/plan.ts
|
|
859
|
+
function optionBalanceKey(option) {
|
|
860
|
+
return `${Number(option.chainId)}:${String(option.address || "").toLowerCase()}`;
|
|
861
|
+
}
|
|
862
|
+
function hasPositiveBalance(option, balances) {
|
|
863
|
+
try {
|
|
864
|
+
return BigInt(balances[optionBalanceKey(option)]?.raw ?? "0") > 0n;
|
|
865
|
+
} catch {
|
|
866
|
+
return false;
|
|
867
|
+
}
|
|
868
|
+
}
|
|
859
869
|
function validateDestination(destination) {
|
|
860
870
|
assertAddress(destination.address, "Destination address");
|
|
861
871
|
const chainId = Number(destination.chainId);
|
|
@@ -871,7 +881,7 @@ function planErrorSummary(errors) {
|
|
|
871
881
|
const unique = [...new Set(errors.map((error) => error?.message || String(error)).filter(Boolean))].slice(0, 3);
|
|
872
882
|
return unique.length ? unique.join(" | ") : "No ZeroDelta route is currently available for this payment.";
|
|
873
883
|
}
|
|
874
|
-
async function fetchPaymentPlan(api, request, payer, wallet = {}) {
|
|
884
|
+
async function fetchPaymentPlan(api, request, payer, wallet = {}, readBalances) {
|
|
875
885
|
validateDestination(request);
|
|
876
886
|
assertAddress(payer, "Payer wallet");
|
|
877
887
|
const [chains, tokens, routes] = await Promise.all([api.fetchChains(), api.fetchTokens(), api.fetchRoutes()]);
|
|
@@ -882,6 +892,7 @@ async function fetchPaymentPlan(api, request, payer, wallet = {}) {
|
|
|
882
892
|
};
|
|
883
893
|
const destinationToken = findToken(tokens, destination.chainId, destination.asset);
|
|
884
894
|
if (!destinationToken) throw new Error(`${destination.asset} is not enabled on ${chainNameOf(destination.chainId, chains)}.`);
|
|
895
|
+
const allowedFunding = wallet.fundingAssets?.length ? new Set(wallet.fundingAssets.map(symbolKey)) : null;
|
|
885
896
|
const options = buildPayOptions({
|
|
886
897
|
invoiceAsset: destination.asset,
|
|
887
898
|
invoiceChainId: destination.chainId,
|
|
@@ -889,14 +900,25 @@ async function fetchPaymentPlan(api, request, payer, wallet = {}) {
|
|
|
889
900
|
routes,
|
|
890
901
|
chains,
|
|
891
902
|
walletChainId: wallet?.chainId
|
|
892
|
-
});
|
|
903
|
+
}).filter((option) => option.direct || !allowedFunding || allowedFunding.has(option.symbolKey));
|
|
893
904
|
if (!options.length) throw new Error(`No enabled ZeroDelta funding assets can settle ${destination.asset} on ${chainNameOf(destination.chainId, chains)}.`);
|
|
905
|
+
const balanceTargets = [];
|
|
906
|
+
const seenTargets = /* @__PURE__ */ new Set();
|
|
907
|
+
for (const option of options) {
|
|
908
|
+
const key = optionBalanceKey(option);
|
|
909
|
+
if (seenTargets.has(key)) continue;
|
|
910
|
+
seenTargets.add(key);
|
|
911
|
+
balanceTargets.push({ chainId: option.chainId, tokenAddress: option.address, decimals: option.decimals });
|
|
912
|
+
}
|
|
913
|
+
const walletBalances = readBalances ? await readBalances(payer, balanceTargets).catch(() => ({})) : {};
|
|
894
914
|
const candidates = options.filter((option) => option.direct);
|
|
915
|
+
const heldOptions = options.filter((option) => !option.direct && hasPositiveBalance(option, walletBalances));
|
|
916
|
+
const heldKeys = new Set(heldOptions.map(optionBalanceKey));
|
|
895
917
|
const seenChains = /* @__PURE__ */ new Set();
|
|
896
918
|
const perChainBest = [];
|
|
897
919
|
const leftovers = [];
|
|
898
920
|
for (const option of options) {
|
|
899
|
-
if (option.direct) continue;
|
|
921
|
+
if (option.direct || heldKeys.has(optionBalanceKey(option))) continue;
|
|
900
922
|
if (seenChains.has(option.chainId)) {
|
|
901
923
|
leftovers.push(option);
|
|
902
924
|
} else {
|
|
@@ -904,7 +926,7 @@ async function fetchPaymentPlan(api, request, payer, wallet = {}) {
|
|
|
904
926
|
perChainBest.push(option);
|
|
905
927
|
}
|
|
906
928
|
}
|
|
907
|
-
for (const option of [...perChainBest, ...leftovers].slice(0, MAX_QUOTE_CANDIDATES)) {
|
|
929
|
+
for (const option of [...heldOptions, ...perChainBest, ...leftovers].slice(0, MAX_QUOTE_CANDIDATES)) {
|
|
908
930
|
candidates.push(option);
|
|
909
931
|
}
|
|
910
932
|
const routeErrors = [];
|
|
@@ -939,6 +961,8 @@ async function fetchPaymentPlan(api, request, payer, wallet = {}) {
|
|
|
939
961
|
expiresAt: new Date(Date.now() + 2 * 60 * 1e3).toISOString(),
|
|
940
962
|
apiBase: api.apiBase,
|
|
941
963
|
routes: routesBuilt,
|
|
964
|
+
fundingBalances: walletBalances,
|
|
965
|
+
fundingAccount: payer.toLowerCase(),
|
|
942
966
|
destination: {
|
|
943
967
|
asset: destinationToken.symbol,
|
|
944
968
|
chainId: destination.chainId,
|
|
@@ -1219,6 +1243,23 @@ function createVerifier(api, fetchImpl, rpcOverrides = {}) {
|
|
|
1219
1243
|
const json = await response.json();
|
|
1220
1244
|
return json?.result ?? null;
|
|
1221
1245
|
}
|
|
1246
|
+
async function rpcBatchCall(rpcUrl, calls) {
|
|
1247
|
+
const response = await fetchImpl(rpcUrl, {
|
|
1248
|
+
method: "POST",
|
|
1249
|
+
headers: { "Content-Type": "application/json" },
|
|
1250
|
+
body: JSON.stringify(calls.map((call, index) => ({ jsonrpc: "2.0", id: index + 1, ...call }))),
|
|
1251
|
+
signal: AbortSignal.timeout(TX_VERIFY_FETCH_MS)
|
|
1252
|
+
});
|
|
1253
|
+
if (!response.ok) throw new Error(`RPC ${response.status}`);
|
|
1254
|
+
const json = await response.json();
|
|
1255
|
+
if (!Array.isArray(json)) throw new Error("RPC batch unsupported");
|
|
1256
|
+
const results = /* @__PURE__ */ new Map();
|
|
1257
|
+
for (const item of json) {
|
|
1258
|
+
const id = Number(item?.id);
|
|
1259
|
+
if (Number.isInteger(id) && Object.hasOwn(item || {}, "result")) results.set(id, item.result);
|
|
1260
|
+
}
|
|
1261
|
+
return results;
|
|
1262
|
+
}
|
|
1222
1263
|
async function payerNonceBaseline(chainId, account) {
|
|
1223
1264
|
if (!isAddress(account)) return null;
|
|
1224
1265
|
const rpcUrl = await chainRpcUrl(chainId);
|
|
@@ -1285,25 +1326,54 @@ function createVerifier(api, fetchImpl, rpcOverrides = {}) {
|
|
|
1285
1326
|
const out = {};
|
|
1286
1327
|
if (!isAddress(account) || !Array.isArray(targets) || !targets.length) return out;
|
|
1287
1328
|
const owner = normalizeAddress(account);
|
|
1288
|
-
const
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1329
|
+
const groups = /* @__PURE__ */ new Map();
|
|
1330
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1331
|
+
for (const target of targets) {
|
|
1332
|
+
if (!target || !isAddress(target.tokenAddress)) continue;
|
|
1333
|
+
const key = `${Number(target.chainId)}:${String(target.tokenAddress).toLowerCase()}`;
|
|
1334
|
+
if (seen.has(key)) continue;
|
|
1335
|
+
seen.add(key);
|
|
1336
|
+
const chainId = Number(target.chainId);
|
|
1337
|
+
const group = groups.get(chainId) || [];
|
|
1338
|
+
group.push(target);
|
|
1339
|
+
groups.set(chainId, group);
|
|
1340
|
+
}
|
|
1341
|
+
function record(target, hex) {
|
|
1342
|
+
if (typeof hex !== "string" || !/^0x[0-9a-fA-F]*$/.test(hex)) return false;
|
|
1343
|
+
const raw = BigInt(hex || "0x0").toString();
|
|
1344
|
+
const key = `${Number(target.chainId)}:${String(target.tokenAddress).toLowerCase()}`;
|
|
1345
|
+
out[key] = { raw, formatted: formatUnits(raw, Number(target.decimals)) };
|
|
1346
|
+
return true;
|
|
1347
|
+
}
|
|
1348
|
+
async function readOne(rpcUrl, target) {
|
|
1349
|
+
try {
|
|
1350
|
+
const hex = await rpcCall(rpcUrl, "eth_call", [{ to: normalizeAddress(target.tokenAddress), data: encodeBalanceOf(owner) }, "latest"]);
|
|
1351
|
+
record(target, hex);
|
|
1352
|
+
} catch {
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1355
|
+
async function readGroup(chainId, group) {
|
|
1356
|
+
const rpcUrl = await balanceRpcUrl(chainId).catch(() => "");
|
|
1357
|
+
if (!rpcUrl) return;
|
|
1358
|
+
if (group.length === 1) return readOne(rpcUrl, group[0]);
|
|
1359
|
+
try {
|
|
1360
|
+
const results = await rpcBatchCall(
|
|
1361
|
+
rpcUrl,
|
|
1362
|
+
group.map((target) => ({
|
|
1363
|
+
method: "eth_call",
|
|
1364
|
+
params: [{ to: normalizeAddress(target.tokenAddress), data: encodeBalanceOf(owner) }, "latest"]
|
|
1365
|
+
}))
|
|
1366
|
+
);
|
|
1367
|
+
const missing = [];
|
|
1368
|
+
group.forEach((target, index) => {
|
|
1369
|
+
if (!record(target, results.get(index + 1))) missing.push(target);
|
|
1370
|
+
});
|
|
1371
|
+
await Promise.all(missing.map((target) => readOne(rpcUrl, target)));
|
|
1372
|
+
} catch {
|
|
1373
|
+
await Promise.all(group.map((target) => readOne(rpcUrl, target)));
|
|
1304
1374
|
}
|
|
1305
1375
|
}
|
|
1306
|
-
await Promise.all(
|
|
1376
|
+
await Promise.all([...groups.entries()].map(([chainId, group]) => readGroup(chainId, group)));
|
|
1307
1377
|
return out;
|
|
1308
1378
|
}
|
|
1309
1379
|
async function readNativeBalance(chainId, account) {
|
|
@@ -1405,7 +1475,7 @@ function createEngine(config = {}) {
|
|
|
1405
1475
|
fetchTokens: api.fetchTokens,
|
|
1406
1476
|
fetchRoutes: api.fetchRoutes,
|
|
1407
1477
|
fetchQuote: api.fetchQuote,
|
|
1408
|
-
fetchPaymentPlan: (request, payer, wallet) => fetchPaymentPlan(api, request, payer, wallet),
|
|
1478
|
+
fetchPaymentPlan: (request, payer, wallet) => fetchPaymentPlan(api, request, payer, wallet, verifier.readBalances),
|
|
1409
1479
|
quoteSwapPreview: (swap, owner) => quoteSwapPreview(api, swap, owner),
|
|
1410
1480
|
buildSwapPlan: (swap, payer, wallet) => buildSwapPlan(api, swap, payer, wallet),
|
|
1411
1481
|
payerNonceBaseline: verifier.payerNonceBaseline,
|